optimisations

This commit is contained in:
Jay Robson 2024-08-31 16:31:19 +10:00
parent 3d7584a951
commit b10b72567b
5 changed files with 138 additions and 116 deletions

View File

@ -2,25 +2,29 @@
#include <Arduino.h> #include <Arduino.h>
#include "indicator.hpp" #include "indicator.hpp"
#include "tones.hpp" #include "tones.hpp"
#include "util.hpp"
constexpr int PIN = 9; constexpr int PINS[] = {9, 10};
void indicator::init() { void indicator::init() {
pinMode(PIN+0, OUTPUT); for(int pin : PINS) {
pinMode(PIN+1, OUTPUT); pinMode(pin, 1);
}
} }
void indicator::reset() { void indicator::reset() {
analogWrite(PIN+0, 0); for(int pin : PINS) {
analogWrite(PIN+1, 0); analogWrite(pin, 0);
}
} }
void indicator::update() { void indicator::update() {
unsigned v[2] = {2}; int v[size(PINS)] = {0};
for(int i = 0; i < tones::active; i++) { for(int i = 0; i < tones::active; i++) {
v[i % 2] += tones::all[i].amplitude; v[i % size(PINS)] += tones::all[i].amplitude;
}
for(int i = 0; i < size(PINS); i++) {
analogWrite(PINS[i], v[i]);
} }
analogWrite(PIN+0, min(v[0], 255));
analogWrite(PIN+1, min(v[1], 255));
} }

View File

@ -2,93 +2,28 @@
#include "soft_twi.hpp" #include "soft_twi.hpp"
#include <Arduino.h> #include <Arduino.h>
SoftTWI::SoftTWI(unsigned pin_sda, unsigned pin_scl, unsigned delay) { //void SoftTWI::sleep(uint8_t t) {
_pin_scl = pin_scl; // unsigned d = _delay * t;
_pin_sda = pin_sda; // if(d) delayMicroseconds(d);
_delay = delay; //}
void SoftTWI::set_pin(uint8_t pin, bool state) {
uint8_t bitmask = digitalPinToBitMask(pin);
volatile uint8_t& reg = *portModeRegister(digitalPinToPort(pin));
if(state) {
reg &= ~bitmask;
} else {
reg |= bitmask;
}
} }
void SoftTWI::begin() { void SoftTWI::begin() {
digitalWrite(_pin_sda, 0); digitalWrite(_sda, 0);
digitalWrite(_pin_scl, 0); digitalWrite(_scl, 0);
} }
void SoftTWI::sleep(unsigned t) { bool SoftTWI::read_pin(uint8_t pin) {
if(_delay) { return digitalRead(pin);
delayMicroseconds(_delay * t);
}
}
void SoftTWI::set_sda(bool s) {
pinMode(_pin_sda, s ? INPUT : OUTPUT);
}
void SoftTWI::set_scl(bool s) {
pinMode(_pin_scl, s ? INPUT : OUTPUT);
}
bool SoftTWI::read_sda() {
return digitalRead(_pin_sda);
}
bool SoftTWI::read_scl() {
return digitalRead(_pin_scl);
}
void SoftTWI::wait_scl() {
while(!read_scl()) {}
}
bool SoftTWI::read_bit() {
set_sda(1);
sleep();
set_scl(1);
sleep(2);
wait_scl();
bool s = read_sda();
set_scl(0);
sleep();
return s;
}
void SoftTWI::write_bit(bool s) {
set_sda(s);
sleep();
set_scl(1);
sleep(2);
wait_scl();
set_scl(0);
sleep();
}
void SoftTWI::start() {
set_sda(1);
sleep();
set_scl(1);
sleep();
set_sda(0);
sleep();
set_scl(0);
sleep();
}
void SoftTWI::end() {
set_sda(0);
sleep();
set_scl(1);
sleep();
set_sda(1);
sleep(2);
}
bool SoftTWI::start_read(uint8_t addr) {
start();
return write((addr << 1) | 1);
}
bool SoftTWI::start_write(uint8_t addr) {
start();
return write(addr << 1);
} }
uint8_t SoftTWI::read(bool ack) { uint8_t SoftTWI::read(bool ack) {
@ -109,6 +44,12 @@ bool SoftTWI::write(uint8_t v) {
return !read_bit(); // get ACK return !read_bit(); // get ACK
} }
void SoftTWI::read(uint8_t* data, unsigned data_len, bool nack_at_end) {
for(unsigned i = 0; i < data_len; i++) {
data[i] = read(nack_at_end ? (i < data_len - 1) : true);
}
}
unsigned SoftTWI::write(const uint8_t* data, unsigned data_len) { unsigned SoftTWI::write(const uint8_t* data, unsigned data_len) {
for(unsigned i = 0; i < data_len; i++) { for(unsigned i = 0; i < data_len; i++) {
if(!write(data[i])) { if(!write(data[i])) {
@ -118,9 +59,3 @@ unsigned SoftTWI::write(const uint8_t* data, unsigned data_len) {
return data_len; return data_len;
} }
void SoftTWI::read(uint8_t* data, unsigned data_len, bool nack_at_end) {
for(unsigned i = 0; i < data_len; i++) {
data[i] = read(nack_at_end ? (i < data_len - 1) : true);
}
}

View File

@ -5,33 +5,115 @@
struct SoftTWI { struct SoftTWI {
SoftTWI(unsigned pin_sda, unsigned pin_scl, unsigned delay = 0); constexpr SoftTWI(unsigned sda, unsigned scl, unsigned delay = 0);
void begin(); void begin();
void sleep(unsigned=1); inline void sleep(uint8_t=1) {}
void set_sda(bool); inline void wait_scl();
void set_scl(bool);
bool read_sda();
bool read_scl();
void wait_scl();
void write_bit(bool); void set_pin(uint8_t, bool);
inline void set_sda(bool);
inline void set_scl(bool);
bool read_pin(uint8_t);
inline bool read_sda();
inline bool read_scl();
inline void write_bit(bool);
bool write(uint8_t); bool write(uint8_t);
unsigned write(const uint8_t*, unsigned); unsigned write(const uint8_t*, unsigned);
bool read_bit(); inline bool read_bit();
uint8_t read(bool=true); uint8_t read(bool=true);
void read(uint8_t*, unsigned, bool=false); void read(uint8_t*, unsigned, bool=false);
bool start_read(uint8_t); inline bool start_read(uint8_t);
bool start_write(uint8_t); inline bool start_write(uint8_t);
void start(); inline void start();
void end(); inline void end();
private: private:
unsigned _pin_scl; unsigned _sda;
unsigned _pin_sda; unsigned _scl;
unsigned _delay; unsigned _delay;
}; };
constexpr SoftTWI::SoftTWI(unsigned sda, unsigned scl, unsigned delay)
: _sda(sda)
, _scl(scl)
, _delay(delay) {
}
inline void SoftTWI::set_sda(bool s) {
set_pin(_sda, s);
}
inline void SoftTWI::set_scl(bool s) {
set_pin(_scl, s);
}
inline bool SoftTWI::read_sda() {
return read_pin(_sda);
}
inline bool SoftTWI::read_scl() {
return read_pin(_scl);
}
inline void SoftTWI::wait_scl() {
while(!read_pin(_scl)) {}
}
inline bool SoftTWI::read_bit() {
set_sda(1);
sleep();
set_scl(1);
sleep(2);
wait_scl();
bool s = read_sda();
set_scl(0);
sleep();
return s;
}
inline void SoftTWI::write_bit(bool s) {
set_sda(s);
sleep();
set_scl(1);
sleep(2);
wait_scl();
set_scl(0);
sleep();
}
inline void SoftTWI::start() {
set_sda(1);
sleep();
set_scl(1);
sleep();
set_sda(0);
sleep();
set_scl(0);
sleep();
}
inline void SoftTWI::end() {
set_sda(0);
sleep();
set_scl(1);
sleep();
set_sda(1);
sleep(2);
}
inline bool SoftTWI::start_read(uint8_t addr) {
start();
return write((addr << 1) | 1);
}
inline bool SoftTWI::start_write(uint8_t addr) {
start();
return write(addr << 1);
}

View File

@ -6,9 +6,6 @@
using tones::all; using tones::all;
using tones::active; using tones::active;
static uint8_t lookup[size(all)];
static uint8_t rlookup[size(all)];
void tones::init() { void tones::init() {
for(uint8_t i = 0; i < size(all); i++) { for(uint8_t i = 0; i < size(all); i++) {
lookup[i] = i; lookup[i] = i;

View File

@ -2,10 +2,14 @@
#pragma once #pragma once
#include "tone.hpp" #include "tone.hpp"
#include "util.hpp"
namespace tones { namespace tones {
inline Tone all[32]; inline Tone all[32];
inline uint8_t lookup[size(all)];
inline uint8_t rlookup[size(all)];
inline int active; inline int active;
void init(); void init();