Skip to content
Snippets Groups Projects
Commit 22f7da7b authored by Joe Revans's avatar Joe Revans
Browse files

cleaned up pot and enc libs; started on auto naming

parent 591abbba
No related branches found
No related tags found
No related merge requests found
#ifndef NAMEDICTIONARY_h
#define NAMEDICTIONARY_h
typedef struct {
String key;
String value;
} keyValuePair;
class NameDictionary {
keyValuePair data[2] = {
{"1a6b16", "joe"},
{"2b7c27", "cat"}
};
int _arraySize;
public:
NameDictionary() {
_arraySize = sizeof(data) / sizeof(keyValuePair);
};
String get(String key) {
for (int i = 0; i < _arraySize; i++) {
if (key == data[i].key) {
return data[i].value;
}
}
return "null";
};
void list() {
for (int i = 0; i < _arraySize; i++) {
Serial.println(data[i].key + " : " + data[i].value);
}
};
void values() {
for (int i = 0; i < _arraySize; i++) {
Serial.println(data[i].value);
}
};
void keys() {
for (int i = 0; i < _arraySize; i++) {
Serial.println(data[i].key);
}
};
int length() {
return _arraySize;
}
};
#endif
......@@ -4,36 +4,47 @@
class Potentiometer {
int EMA_S = 0; //initialization of EMA S
float EMA_a = 0.6;
int _pin;
int _id;
int _unstableValue;
int _stableValue;
int _value;
int _previousReading;
bool _readingChange;
int _previousValue;
bool _inputFlag = false;
bool _changeFlag = false;
unsigned long _previousTimer;
int _delay = 200;
int _interval = 200;
void (*_cb)(Potentiometer*, uint8_t, uint8_t); // Callback function
int _read() {
int sensorValue = analogRead(_pin); //read the sensor value using ADC
EMA_S = (EMA_a*sensorValue) + ((1-EMA_a)*EMA_S); //run the EMA
int mappedValue = map(EMA_S, 5, 1023, 0, 100);
return mappedValue;
}
void _setValue(int x) {
_value = x;
}
public:
static const uint8_t kEventStableUpdate = 0;
static const uint8_t kEventUnstableUpdate = 1;
static const uint8_t kEventStableUpdate = 0;
static const uint8_t kEventUnstableUpdate = 1;
Potentiometer(int pin, int id) : _pin(pin), _id(id) {
Potentiometer(int pin, int id = 99) : _pin(pin), _id(id) {
pinMode(pin, INPUT);
EMA_S = analogRead(_pin); //set EMA S for t=1
_stableValue = EMA_S;
_value = EMA_S;
_previousReading = EMA_S;
_previousTimer = millis();
_readingChange = true;
_inputFlag = true;
};
void setEventHandler(void(*function)(Potentiometer*, uint8_t, uint8_t)) {
......@@ -41,7 +52,7 @@ class Potentiometer {
}
int getValue() {
return _stableValue;
return _value;
}
int getId() {
......@@ -49,51 +60,29 @@ class Potentiometer {
}
void check() {
unsigned long _timer = millis();
int reading = _read();
//int deltaReading = abs(reading - _previousReading);
int deltaStable = abs(reading - getValue());
int deltaUnstable = abs(reading - _getUnstableValue());
if (reading != _previousReading) {
// When potentiometer reading has a new value:
_readingChange = true; // raise _readingChange flag
_previousTimer = _timer; // reset the timer
} else {
if (_timer - _previousTimer > _delay && _readingChange && deltaStable>1) {
// Once new reading stabilises:
_readingChange = false; // lower _readingChange flag
_stableValue = reading; // update publicly accessible sensor value
_cb(this, kEventStableUpdate, getValue());
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
} else if (_readingChange && deltaUnstable>1) {
_unstableValue = reading;
_cb(this, kEventUnstableUpdate, _getUnstableValue());
int reading = _read();
int deltaValue = abs(reading - _value);
}
if (reading != _value) {
_inputFlag = true;
}
_previousReading = reading;
}
int _getUnstableValue() {
return _unstableValue;
}
int _read() {
float EMA_a = 0.6; //initialization of EMA alpha
int sensorValue = 0; //initialization of sensor variable, equivalent to EMA Y
int mappedValue = 0;
sensorValue = analogRead(_pin); //read the sensor value using ADC
EMA_S = (EMA_a*sensorValue) + ((1-EMA_a)*EMA_S); //run the EMA
mappedValue = map(EMA_S, 5, 1023, 0, 100);
if (_inputFlag == true && deltaValue > 1) {
_inputFlag = false;
_changeFlag = true;
_previousTimer = timer;
_setValue(reading);
_cb(this, kEventUnstableUpdate, getValue());
}
return mappedValue;
if (_changeFlag == true && deltaTime > _interval) {
_changeFlag = false;
_cb(this, kEventStableUpdate, getValue());
}
}
};
#endif
......@@ -7,12 +7,18 @@ class RotaryEncoder {
int _pinA;
int _pinB;
int _id;
int _state[2];
int _position;
volatile int _state[2];
volatile int _position;
unsigned long _timer;
int _debounceInterval = 1;
volatile bool _inputFlag = false;
bool _changeFlag = false;
unsigned long _previousTimer;
int _interval = 200;
void (*_cb)(RotaryEncoder*, uint8_t, int); // Callback function
// {newPin2, newPin1, oldPin2, oldPin1}
int movements[5][4][4] = {
......@@ -55,7 +61,7 @@ class RotaryEncoder {
_position = _position + delta;
}
int _findChange(int state1[2], int state2[2]) {
int _findChange(int state1[2], volatile int state2[2]) {
int stateAppend[] = {state1[1], state1[0], state2[1], state2[0]};
for (int i = 0; i < 3; i++) {
......@@ -114,13 +120,18 @@ class RotaryEncoder {
public:
// Public members:
static const uint8_t kEventStableUpdate = 0;
static const uint8_t kEventUnstableUpdate = 1;
//Public Functions:
RotaryEncoder(int pinA, int pinB) : _pinA(pinA), _pinB(pinB) {
RotaryEncoder(int pinA, int pinB, int id = 99) : _pinA(pinA), _pinB(pinB), _id(id) {
pinMode(_pinA, INPUT_PULLUP);
pinMode(_pinB, INPUT_PULLUP);
_timer = millis();
_previousTimer = millis();
_setState(digitalRead(_pinA), digitalRead(_pinB));
setPosition(0);
}
......@@ -130,22 +141,52 @@ class RotaryEncoder {
attachInterrupt(_pinB, function, CHANGE);
}
void setEventHandler(void(*function)(RotaryEncoder*, uint8_t, int)) {
_cb = function;
}
int getPostition() {
return _position;
}
int getId() {
return _id;
}
void setPosition(int value) {
_position = value;
}
void tick() {
int tempState[] = {digitalRead(_pinA), digitalRead(_pinB)};
int change = _findChange(tempState, _state);
void check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
if (change != 0) {
_incrementPosition(change);
Serial.println(_position);
if (_inputFlag == true) {
_inputFlag = false;
_changeFlag = true;
_previousTimer = timer;
_cb(this, kEventUnstableUpdate, getPostition());
}
if (_changeFlag == true && deltaTime > _interval) {
_changeFlag = false;
_cb(this, kEventStableUpdate, getPostition());
}
}
void ICACHE_RAM_ATTR tick() {
int tempState[] = {digitalRead(_pinA), digitalRead(_pinB)};
int delta = _findChange(tempState, _state);
if (delta != 0) {
_incrementPosition(delta);
_inputFlag = true;
}
_setState(tempState[0], tempState[1]);
}
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment