Skip to content
Snippets Groups Projects
Commit da4da765 authored by Matthew's avatar Matthew
Browse files

remove definitions from .hpp files

parent c014cd8b
No related branches found
No related tags found
3 merge requests!4Joe merge,!3Doxygen Documentation,!2Docs
Showing with 334 additions and 622 deletions
BehaviourTable::BehaviourTable()
{}
void BehaviourTable::add(Behaviour *b) {
behaviours[num] = b;
num++;
......@@ -14,6 +16,10 @@ int BehaviourTable::get_num_behaviours() {
Behaviour * BehaviourTable::get_by_num(int n) {
return behaviours[n];
};
Behaviour::Behaviour(String name) : _name(name) {};
Behaviour::~Behaviour() {};
int Behaviour::is_temp() {
return _temp;
};
......@@ -43,6 +49,8 @@ void Behaviour::stop() {
char * Behaviour::args() {
return "null";
};
TestBehaviour::TestBehaviour()
{};
int TestBehaviour::start(String args) {
return "Test behaviour " + _name + " with (" + args + ")";
};
......@@ -11,8 +11,8 @@ protected:
String _name = "name";
public:
Behaviour(String name) : _name(name) {};
~Behaviour() {};
Behaviour(String name);
~Behaviour();
//Can this behaviour be interruped
virtual boolean is_interruptable();
......
Button::Button(int pin, int id = 99) : _pin(pin), _id(id) {
pinMode(_pin, INPUT_PULLUP);
_state = _read();
_previousTimer = millis();
}
void Button::check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
_state = _read();
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
_state = _read();
if (_inputFlag == true) {
_inputFlag = false;
if (_inputFlag == true) {
_inputFlag = false;
// Button pressed
if (_state == LOW
&& _pressedFlag == false) {
_pressedFlag = true;
_previousTimer = timer;
_cb(this, kEventPressed, _state);
return;
// Button pressed
if (_state == LOW
&& _pressedFlag == false) {
_pressedFlag = true;
_previousTimer = timer;
_cb(this, kEventPressed, _state);
return;
// Button clicked
} else if (_state == HIGH
&& deltaTime < _clickInterval
&& _holdFlag == false) {
_pressedFlag = false;
_previousTimer = timer;
_cb(this, kEventClicked, _state);
return;
// Button clicked
} else if (_state == HIGH
&& deltaTime < _clickInterval
&& _holdFlag == false) {
_pressedFlag = false;
_previousTimer = timer;
_cb(this, kEventClicked, _state);
return;
// Button released
} else if (_state == HIGH) {
_pressedFlag = false;
_holdFlag = false;
_previousTimer = timer;
_cb(this, kEventReleased, _state);
return;
// Button released
} else if (_state == HIGH) {
_pressedFlag = false;
_holdFlag = false;
_previousTimer = timer;
_cb(this, kEventReleased, _state);
return;
}
}
}
// Button held
if (_state == LOW
&& deltaTime > _holdInterval
&& _holdFlag == false) {
_holdFlag = true;
_previousTimer = timer;
_cb(this, kEventHeld, _state);
return;
// Button held
if (_state == LOW
&& deltaTime > _holdInterval
&& _holdFlag == false) {
_holdFlag = true;
_previousTimer = timer;
_cb(this, kEventHeld, _state);
return;
// Button tick
} else if (_state == LOW
&& deltaTime > _repeatInterval
&& _holdFlag == true) {
_previousTimer = timer;
_cb(this, kEventTick, _state);
return;
}
};
// Button tick
} else if (_state == LOW
&& deltaTime > _repeatInterval
&& _holdFlag == true) {
_previousTimer = timer;
_cb(this, kEventTick, _state);
return;
}
};
int Button::getHoldInterval() {
return _holdInterval;
};
return _holdInterval;
};
void Button::_setHoldInterval(int x) {
_holdInterval = x;
};
_holdInterval = x;
};
int Button::getId() {
return _id;
};
return _id;
};
int Button::getRepeatInterval() {
return _repeatInterval;
};
void Button::setEventHandler(void(*function)(Button*, uint8_t, bool)) {
_cb = function;
};
return _repeatInterval;
};
void Button::setEventHandler(void (*function)(Button*, uint8_t, bool)) {
_cb = function;
};
void Button::_setRepeatInterval(int x) {
_repeatInterval = x;
};
_repeatInterval = x;
};
bool Button::getState() {
return _state;
};
return _state;
};
bool Button::_read() {
return digitalRead(_pin);
};
return digitalRead(_pin);
};
int Button::getClickInterval() {
return _clickInterval;
};
return _clickInterval;
};
void Button::_setClickInterval(int x) {
_clickInterval = x;
};
void Button::initInterrupts(void(*function)()) {
attachInterrupt(_pin, function, CHANGE);
};
_clickInterval = x;
};
void Button::initInterrupts(void (*function)()) {
attachInterrupt(_pin, function, CHANGE);
};
......@@ -4,7 +4,7 @@
class Button {
private:
int _pin;
int _id;
......@@ -24,21 +24,13 @@ class Button {
void (*_cb)(Button*, uint8_t, bool); // Callback function
bool _read() {
return digitalRead(_pin);
}
bool _read();
void _setClickInterval(int x) {
_clickInterval = x;
}
void _setClickInterval(int x);
void _setHoldInterval(int x) {
_holdInterval = x;
}
void _setHoldInterval(int x);
void _setRepeatInterval(int x) {
_repeatInterval = x;
}
void _setRepeatInterval(int x);
public:
......@@ -52,97 +44,16 @@ class Button {
// Public functions:
Button(int pin, int id = 99) : _pin(pin), _id(id) {
pinMode(_pin, INPUT_PULLUP);
_state = _read();
_previousTimer = millis();
}
void initInterrupts(void(*function)()) {
attachInterrupt(_pin, function, CHANGE);
}
void setEventHandler(void(*function)(Button*, uint8_t, bool)) {
_cb = function;
}
bool getState() {
return _state;
}
int getId() {
return _id;
}
int getClickInterval() {
return _clickInterval;
}
int getHoldInterval() {
return _holdInterval;
}
int getRepeatInterval() {
return _repeatInterval;
}
void check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
_state = _read();
if (_inputFlag == true) {
_inputFlag = false;
// Button pressed
if (_state == LOW
&& _pressedFlag == false) {
_pressedFlag = true;
_previousTimer = timer;
_cb(this, kEventPressed, _state);
return;
// Button clicked
} else if (_state == HIGH
&& deltaTime < _clickInterval
&& _holdFlag == false) {
_pressedFlag = false;
_previousTimer = timer;
_cb(this, kEventClicked, _state);
return;
// Button released
} else if (_state == HIGH) {
_pressedFlag = false;
_holdFlag = false;
_previousTimer = timer;
_cb(this, kEventReleased, _state);
return;
}
}
// Button held
if (_state == LOW
&& deltaTime > _holdInterval
&& _holdFlag == false) {
_holdFlag = true;
_previousTimer = timer;
_cb(this, kEventHeld, _state);
return;
// Button tick
} else if (_state == LOW
&& deltaTime > _repeatInterval
&& _holdFlag == true) {
_previousTimer = timer;
_cb(this, kEventTick, _state);
return;
}
}
void ICACHE_RAM_ATTR tick() {
_inputFlag = true;
}
Button(int pin, int id = 99);
void initInterrupts(void(*function)());
void setEventHandler(void(*function)(Button*, uint8_t, bool));
bool getState();
int getId();
int getClickInterval();
int getHoldInterval();
int getRepeatInterval();
void check();
void ICACHE_RAM_ATTR tick();
};
......
ButtonHeld::ButtonHeld : Behaviour(name), _node(node){ }
int ButtonHeld::start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonHeld behaviour " + _name + " with (" + args + ")";
};
char * ButtonHeld::args() {return "<String buttonId>"; };
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonHeld behaviour " + _name + " with (" + args + ")";
};
char * ButtonHeld::args() {
return "<String buttonId>";
};
ButtonReleased::ButtonReleased(VizBlocks* node, String name = "ButtonReleased") :
Behaviour(name), _node(node){
}
int ButtonReleased::start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonReleased behaviour " + _name + " with (" + args + ")";
};
char * ButtonReleased::args() {return "<String buttonId>"; };
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonReleased behaviour " + _name + " with (" + args + ")";
};
char * ButtonReleased::args() {
return "<String buttonId>";
};
ButtonClicked::ButtonClicked(VizBlocks* node, String name = "ButtonClicked") :
Behaviour(name), _node(node){ }
int ButtonClicked::start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonClicked behaviour " + _name + " with (" + args + ")";
};
char * ButtonClicked::args() {return "<String buttonId>"; };
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonClicked behaviour " + _name + " with (" + args + ")";
};
char * ButtonClicked::args() {
return "<String buttonId>";
};
ButtonPressed::ButtonPressed(VizBlocks* node, String name = "ButtonPressed") :
Behaviour(name), _node(node){
}
int ButtonPressed::start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonPressed behaviour " + _name + " with (" + args + ")";
};
char * ButtonPressed::args() {return "<String buttonId>"; };
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonPressed behaviour " + _name + " with (" + args + ")";
};
char * ButtonPressed::args() {
return "<String buttonId>";
};
int ButtonTick::start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonTick behaviour " + _name + " with (" + args + ")";
};
char * ButtonTick::args() {return "<String buttonId>"; };
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonTick behaviour " + _name + " with (" + args + ")";
};
char * ButtonTick::args() {
return "<String buttonId>";
};
......@@ -13,18 +13,10 @@ class ButtonPressed : public Behaviour {
VizBlocks* _node;
public:
ButtonPressed(VizBlocks* node, String name = "ButtonPressed") :
Behaviour(name), _node(node){ }
char* args() {return "<String buttonId>"; };
String start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonPressed behaviour " + _name + " with (" + args + ")";
}
};
ButtonPressed(VizBlocks* node, String name = "ButtonPressed")
char* args();
String start(String args);
}
class ButtonReleased : public Behaviour {
/*
......@@ -35,20 +27,13 @@ class ButtonReleased : public Behaviour {
VizBlocks* _node;
public:
ButtonReleased(VizBlocks* node, String name = "ButtonReleased") :
Behaviour(name), _node(node){ }
char* args() {return "<String buttonId>"; };
String start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonReleased behaviour " + _name + " with (" + args + ")";
}
ButtonReleased(VizBlocks* node, String name = "ButtonReleased");
char* args()
String start(String args)
};
class ButtonClicked : public Behaviour {
class ButtonClicked : public Behaviour
{
/*
* Class that defines a behaviour that publishes a
* ButtonClicked message to the input topic of the
......@@ -57,20 +42,13 @@ class ButtonClicked : public Behaviour {
VizBlocks* _node;
public:
ButtonClicked(VizBlocks* node, String name = "ButtonClicked") :
Behaviour(name), _node(node){ }
char* args() {return "<String buttonId>"; };
String start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonClicked behaviour " + _name + " with (" + args + ")";
}
ButtonClicked(VizBlocks* node, String name = "ButtonClicked");
char* args()
String start(String args);
};
class ButtonHeld : public Behaviour {
class ButtonHeld : public Behaviour
{
/*
* Class that defines a behaviour that publishes a
* ButtonLongPressed message to the input topic of the
......@@ -79,8 +57,7 @@ class ButtonHeld : public Behaviour {
VizBlocks* _node;
public:
ButtonHeld(VizBlocks* node, String name = "ButtonHeld") :
Behaviour(name), _node(node){ }
ButtonHeld(VizBlocks* node, String name = "ButtonHeld")
char* args() {return "<String buttonId>"; };
......@@ -103,15 +80,8 @@ class ButtonTick : public Behaviour {
public:
ButtonTick(VizBlocks* node, String name = "ButtonTick") :
Behaviour(name), _node(node){ }
char* args() {return "<String buttonId>"; };
String start(String args) {
//This is where you do your stuff for a simple behaviour
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"button\":\"" + args + "\"}}";
_node->announce(str);
return "ButtonTick behaviour " + _name + " with (" + args + ")";
}
String start(String args);
};
#endif
int SendCapabilities::start(String args) {
//This is where you do your stuff for a simple behaviour
_node->announce_capabilities();
return "SendCapabilities behaviour " + _name;
};
//This is where you do your stuff for a simple behaviour
_node->announce_capabilities();
return "SendCapabilities behaviour " + _name;
};
PingServer::PingServer(VizBlocks* node, String name = "PingServer") : Behaviour(name), _node(node) { _background = true; }
void PingServer::update() {
if (millis() > (_t+_interval)) {
_t = millis();
_node->announce(str);
}
};
if (millis() > (_t+_interval)) {
_t = millis();
_node->announce(str);
}
};
int PingServer::start(String args) {
_background = true;
_running = true;
_t = millis();
str = "{\"id\":\"" + String(_node->getId()) + "\",\"PingServer\":{}}";
_node->announce(str);
return "Pinging server";
};
_background = true;
_running = true;
_t = millis();
str = "{\"id\":\"" + String(_node->getId()) + "\",\"PingServer\":{}}";
_node->announce(str);
return "Pinging server";
};
void Link::update() {
if (millis() > (_t+_timeoutInterval)) {
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Unlink\":{\"peerId\":\"" + _peerId + "\"}}";
_node->announce(str);
_peerId = "";
_running = false;
}
};
if (millis() > (_t+_timeoutInterval)) {
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Unlink\":{\"peerId\":\"" + _peerId + "\"}}";
_node->announce(str);
_peerId = "";
_running = false;
}
};
int Link::start(String args) {
_running = true;
if (args == name() || args.indexOf(" ")>0) {
return "Invalid args (" + args + ") in behaviour " + name();
}
_running = true;
if (args == name() || args.indexOf(" ")>0) {
return "Invalid args (" + args + ") in behaviour " + name();
}
_t = millis();
_t = millis();
if (args == _peerId) {
return "Link ping from (" + _peerId + ")";
}
if (args == _peerId) {
return "Link ping from (" + _peerId + ")";
}
_peerId = args;
_peerId = args;
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Link\":{\"peerId\":\"" + _peerId + "\"}}";
_node->announce(str);
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Link\":{\"peerId\":\"" + _peerId + "\"}}";
_node->announce(str);
return "New link with (" + _peerId + ")";
};
char * Link::args() {return "<String peerId>"; };
return "New link with (" + _peerId + ")";
};
char * Link::args() {
return "<String peerId>";
};
......@@ -86,23 +86,9 @@ class PingServer : public Behaviour {
unsigned long _t = 0;
public:
PingServer(VizBlocks* node, String name = "PingServer") : Behaviour(name), _node(node) { _background = true; }
String start(String args) {
_background = true;
_running = true;
_t = millis();
str = "{\"id\":\"" + String(_node->getId()) + "\",\"PingServer\":{}}";
_node->announce(str);
return "Pinging server";
}
void update() {
if (millis() > (_t+_interval)) {
_t = millis();
_node->announce(str);
}
}
PingServer(VizBlocks* node, String name = "PingServer");
String start(String args);
void update();
};
......
NumLEDs::NumLEDs(Adafruit_NeoPixel* strip, String name = "NumLEDs", uint32_t color=0xFFFFFFFF) :
Behaviour(name), _strip(strip), _color(color){ }
int NumLEDs::start(String args) {
int val = args.toInt();
//Always clear the strip first
......@@ -9,6 +12,8 @@ int NumLEDs::start(String args) {
return "";
};
char * NumLEDs::args() {return "<int num_leds>"; };
BrightnessLEDs::BrightnessLEDs(Adafruit_NeoPixel* strip, String name = "BrightnessLEDs", uint32_t hue=0, uint32_t sat=0) :
Behaviour(name), _strip(strip), _hue(hue), _sat(sat){ }
int BrightnessLEDs::start(String args) {
int val = args.toInt();
_strip->clear();
......@@ -16,6 +21,9 @@ int BrightnessLEDs::start(String args) {
_strip->show();
return "";
};
BreathingLEDs::BreathingLEDs(Adafruit_NeoPixel* strip, String name = "BreathingLEDs", uint32_t hue=0, uint32_t sat=0) :
Behaviour(name), _strip(strip), _hue(hue * 255), _sat(sat) { }
char * BrightnessLEDs::args() {return "<int brightness>"; };
void BreathingLEDs::update() {
if( _rate <= 0 ) {
......
......@@ -10,20 +10,10 @@ class NumLEDs : public Behaviour {
uint32_t _color;
public:
NumLEDs(Adafruit_NeoPixel* strip, String name = "NumLEDs", uint32_t color=0xFFFFFFFF) :
Behaviour(name), _strip(strip), _color(color){ }
char* args() {return "<int num_leds>"; };
String start(String args) {
int val = args.toInt();
//Always clear the strip first
_strip->clear();
if( val > 0 ) {
_strip->fill(_color, 0, val);
}
_strip->show();
return "";
}
NumLEDs(Adafruit_NeoPixel* strip, String name = "NumLEDs", uint32_t color=0xFFFFFFFF);
char* args();
String start(String args);
};
......@@ -33,21 +23,15 @@ class BrightnessLEDs : public Behaviour {
uint32_t _sat;
public:
BrightnessLEDs(Adafruit_NeoPixel* strip, String name = "BrightnessLEDs", uint32_t hue=0, uint32_t sat=0) :
Behaviour(name), _strip(strip), _hue(hue), _sat(sat){ }
char* args() {return "<int brightness>"; };
String start(String args) {
int val = args.toInt();
_strip->clear();
_strip->fill(_strip->ColorHSV(_hue,_sat,val));
_strip->show();
return "";
}
BrightnessLEDs(Adafruit_NeoPixel* strip, String name = "BrightnessLEDs", uint32_t hue=0, uint32_t sat=0);
char* args();
String start(String args)
};
class BreathingLEDs : public Behaviour {
class BreathingLEDs : public Behaviour
{
Adafruit_NeoPixel* _strip;
uint _hue;
uint _sat;
......@@ -60,36 +44,11 @@ class BreathingLEDs : public Behaviour {
public:
BreathingLEDs(Adafruit_NeoPixel* strip, String name = "BreathingLEDs", uint32_t hue=0, uint32_t sat=0) :
Behaviour(name), _strip(strip), _hue(hue * 255), _sat(sat) { }
char* args() {return "<int rate (1-255ish)>"; };
String start(String args) {
_current = 0;
_direction = 1;
_running = true;
int val = args.toInt();
_rate = val;
return "";
}
void update() {
if( _rate <= 0 ) {
_strip->fill(0);
_strip->show();
return;
}
_current = _current + (_rate * _direction);
if( _current < 0 ) {
_current = 0;
_direction = 1;
}
if( _current > 255 * _factor ) {
_current = 255 * _factor;
_direction = -1;
}
_strip->fill(_strip->ColorHSV(_hue,_sat,_current / _factor));
_strip->show();
}
char* args();
String start(String args);
void update();
};
......
NameDictionary::NameDictionary()
{
_arraySize = sizeof(data) / sizeof(keyValuePair);
};
int NameDictionary::get(String key) {
for (int i = 0; i < _arraySize; i++) {
if (key == data[i].key) {
......
......@@ -2,79 +2,58 @@
#define NAMEDICTIONARY_h
typedef struct {
String key;
String value;
String key;
String value;
} keyValuePair;
class NameDictionary {
keyValuePair data[50] = {
{"c2c373", "abe"},
{"c31d9d", "aja"},
{"c2b603", "ace"},
{"da58f5", "ali"},
{"da6195", "alf"},
{"da50d8", "amy"},
{"da5649", "ann"},
{"c2b2d6", "art"},
{"da516d", "aya"},
{"c2a597", "bea"},
{"da5331", "bev"},
{"8e07b", "bob"},
{"c318a1", "bud"},
{"da5a56", "deb"},
{"da4a70", "cal"},
{"c2c5c2", "cam"},
{"c2a5e6", "che"},
{"c2a23f", "dot"},
{"c2c415", "dan"},
{"c2bf2a", "dax"},
{"30d6ed", "fin"},
{"30d00b", "jim"},
{"1a6b16", "joe"},
{"8810e9", "kat"},
{"31054b", "kev"},
{"30ce48", "sam"},
{"30db8b", "tim"},
};
keyValuePair data[50] = {
{"c2c373", "abe"},
{"c31d9d", "aja"},
{"c2b603", "ace"},
{"da58f5", "ali"},
{"da6195", "alf"},
{"da50d8", "amy"},
{"da5649", "ann"},
{"c2b2d6", "art"},
{"da516d", "aya"},
{"c2a597", "bea"},
{"da5331", "bev"},
{"8e07b", "bob"},
{"c318a1", "bud"},
{"da5a56", "deb"},
{"da4a70", "cal"},
{"c2c5c2", "cam"},
{"c2a5e6", "che"},
{"c2a23f", "dot"},
{"c2c415", "dan"},
{"c2bf2a", "dax"},
{"30d6ed", "fin"},
{"30d00b", "jim"},
{"1a6b16", "joe"},
{"8810e9", "kat"},
{"31054b", "kev"},
{"30ce48", "sam"},
{"30db8b", "tim"},
};
int _arraySize;
int _arraySize;
public:
NameDictionary() {
_arraySize = sizeof(data) / sizeof(keyValuePair);
};
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 key;
};
String get(String key);
void list() {
for (int i = 0; i < _arraySize; i++) {
Serial.println(data[i].key + " : " + data[i].value);
}
};
void list();
void values() {
for (int i = 0; i < _arraySize; i++) {
Serial.println(data[i].value);
}
};
void values();
void keys() {
for (int i = 0; i < _arraySize; i++) {
Serial.println(data[i].key);
}
};
void keys();
int length() {
return _arraySize;
}
int length();
};
#endif
Potentiometer::Potentiometer(int pin, int id = 99) : _pin(pin), _id(id) {
pinMode(pin, INPUT);
EMA_S = analogRead(_pin); //set EMA S for t=1
_value = EMA_S;
_previousReading = EMA_S;
_previousTimer = millis();
_inputFlag = true;
};
void Potentiometer::check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
......
......@@ -20,69 +20,24 @@ class Potentiometer {
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);
int _read();
return mappedValue;
}
void _setValue(int x) {
_value = x;
}
void _setValue(int x);
public:
static const uint8_t kEventStableUpdate = 0;
static const uint8_t kEventUnstableUpdate = 1;
Potentiometer(int pin, int id = 99) : _pin(pin), _id(id) {
pinMode(pin, INPUT);
EMA_S = analogRead(_pin); //set EMA S for t=1
_value = EMA_S;
_previousReading = EMA_S;
_previousTimer = millis();
_inputFlag = true;
};
void setEventHandler(void(*function)(Potentiometer*, uint8_t, uint8_t)) {
_cb = function;
}
int getValue() {
return _value;
}
int getId() {
return _id;
}
void check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
int reading = _read();
int deltaValue = abs(reading - _value);
if (reading != _value) {
_inputFlag = true;
}
if (_inputFlag == true && deltaValue > 1) {
_inputFlag = false;
_changeFlag = true;
_previousTimer = timer;
_setValue(reading);
_cb(this, kEventUnstableUpdate, getValue());
}
if (_changeFlag == true && deltaTime > _interval) {
_changeFlag = false;
_cb(this, kEventStableUpdate, getValue());
}
}
Potentiometer(int pin, int id = 99) : _pin(pin), _id(id);
void setEventHandler(void(*function)(Potentiometer*, uint8_t, uint8_t));
int getValue();
int getId();
void check();
};
#endif
PotentiometerUpdated::PotentiometerUpdated(VizBlocks* node, String name = "PotentiometerUpdated") :
Behaviour(name), _node(node){ }
int PotentiometerUpdated::start(String args) {
//This is where you do your stuff for a simple behaviour
int index = args.indexOf(" ");
......
......@@ -16,24 +16,9 @@ public:
PotentiometerUpdated(VizBlocks* node, String name = "PotentiometerUpdated") :
Behaviour(name), _node(node){ }
char* args() {return "<String potentiometerId> <int value>"; };
char* args();
String start(String args) {
//This is where you do your stuff for a simple behaviour
int index = args.indexOf(" ");
String pot = "";
String value = "";
if ( index > 0 ) {
pot = args.substring(0, index);
value = args.substring(index+1);
} else {
return "PotentiometerUpdated behaviour args error!";
}
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"pot\":\"" + pot + "\",\"value\":\"" + value + "\"}}";
_node->announce(str);
return "PotentiometerUpdated behaviour " + _name + " with (" + args + ")";
}
String start(String args);
};
#endif
RotaryEncoder::RotaryEncoder(int pinA, int pinB, int id = 99) : _pinA(pinA), _pinB(pinB), _id(id) {
pinMode(_pinA, INPUT_PULLUP);
pinMode(_pinB, INPUT_PULLUP);
_previousTimer = millis();
_setState(digitalRead(_pinA), digitalRead(_pinB));
setPosition(0);
}
void RotaryEncoder::check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
......
......@@ -52,71 +52,13 @@ class RotaryEncoder {
// Private Functions:
void _setState(int a, int b) {
_state[0] = a;
_state[1] = b;
}
void _setState(int a, int b);
void _incrementPosition(int delta) {
_position = _position + delta;
}
void _incrementPosition(int delta);
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++) {
for (int j = 0; j < 4; j++) {
if (_compareArrays(stateAppend, movements[i][j])) {
if (i == 0) {
return 0;
}
else if (i == 1) {
return 1;
}
else if (i == 2) {
return -1;
}
else if (i == 3) {
return 2;
}
else if (i == 4) {
return -2;
}
}
}
}
for (int i = 3; i < 5; i++) {
for (int j = 0; j < 2; j++) {
if (_compareArrays(stateAppend, movements[i][j])) {
if (i == 3) {
return 2;
}
else if (i == 4) {
return -2;
}
}
}
}
Serial.println("INVALID DATA");
return 0;
}
int _findChange(int state1[2], volatile int state2[2]);
boolean _compareArrays(int a[4], int b[4]) {
if (a[0] != b[0]) {
return false;
}
if (a[1] != b[1]) {
return false;
}
if (a[2] != b[2]) {
return false;
}
if (a[3] != b[3]) {
return false;
}
return true;
}
boolean _compareArrays(int a[4], int b[4]);
public:
......@@ -136,56 +78,19 @@ class RotaryEncoder {
setPosition(0);
}
void initInterrupts(void(*function)()) {
attachInterrupt(_pinA, function, CHANGE);
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 check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
void initInterrupts(void(*function)());
if (_inputFlag == true) {
_inputFlag = false;
void setEventHandler(void(*function)(RotaryEncoder*, uint8_t, int));
_changeFlag = true;
_previousTimer = timer;
int getPostition();
_cb(this, kEventUnstableUpdate, getPostition());
}
int getId();
if (_changeFlag == true && deltaTime > _interval) {
_changeFlag = false;
void setPosition(int value);
_cb(this, kEventStableUpdate, getPostition());
}
}
void check();
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]);
}
void ICACHE_RAM_ATTR tick();
};
......
RotaryEncoderUpdated::RotaryEncoderUpdated(VizBlocks* node, String name = "RotaryEncoderUpdated") :
Behaviour(name), _node(node){ }
int RotaryEncoderUpdated::start(String args) {
//This is where you do your stuff for a simple behaviour
int index = args.indexOf(" ");
......
......@@ -13,28 +13,11 @@ class RotaryEncoderUpdated : public Behaviour {
VizBlocks* _node;
public:
RotaryEncoderUpdated(VizBlocks* node, String name = "RotaryEncoderUpdated") :
Behaviour(name), _node(node){ }
RotaryEncoderUpdated(VizBlocks* node, String name = "RotaryEncoderUpdated")
char* args() {return "<String rotaryEncoderId> <int position>"; };
char* args();
String start(String args) {
//This is where you do your stuff for a simple behaviour
int index = args.indexOf(" ");
String encoder = "";
String position = "";
if ( index > 0 ) {
encoder = args.substring(0, index);
position = args.substring(index+1);
} else {
return "RotaryEncoderUpdated behaviour args error!";
}
String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Input\":{\"type\":\"" + String(name()) + "\",\"encoder\":\"" + encoder + "\",\"position\":\"" + position + "\"}}";
_node->announce(str);
return "RotaryEncoderUpdated behaviour " + _name + " with (" + args + ")";
}
String start(String args);
};
#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