diff --git a/LEDBehaviours.h b/LEDBehaviours.h index 0294d2b977e3da4d3f370988001c24a5abd4b345..c20cc2a05515499fe498cf3ec43730178f7b22c3 100644 --- a/LEDBehaviours.h +++ b/LEDBehaviours.h @@ -10,7 +10,7 @@ class NumLEDs : public Behaviour { uint32_t _color; public: - NumLEDs(Adafruit_NeoPixel* strip, uint32_t color, String name = "num_leds") : + NumLEDs(Adafruit_NeoPixel* strip, String name = "num_leds", uint32_t color=0xFFFFFFFF) : Behaviour(name), _strip(strip), _color(color){ } char* args() {return "<int num_leds>"; }; @@ -33,7 +33,7 @@ class BrightnessLEDs : public Behaviour { uint32_t _sat; public: - BrightnessLEDs(Adafruit_NeoPixel* strip, uint32_t hue, uint32_t sat=255, String name = "num_leds") : + BrightnessLEDs(Adafruit_NeoPixel* strip, String name = "num_leds", uint32_t hue=0, uint32_t sat=0) : Behaviour(name), _strip(strip), _hue(hue), _sat(sat){ } char* args() {return "<int brightness>"; }; @@ -58,7 +58,7 @@ class BreathingLEDs : public Behaviour { int _direction = 1; public: - BreathingLEDs(Adafruit_NeoPixel* strip, uint32_t hue, uint32_t sat=255, String name = "breathe_leds") : + BreathingLEDs(Adafruit_NeoPixel* strip, String name = "breathe_leds", uint32_t hue=0, uint32_t sat=0) : Behaviour(name), _strip(strip), _hue(hue * 255), _sat(sat) { } char* args() {return "<int rate (1-255ish)>"; }; diff --git a/ServoBehaviours.h b/ServoBehaviours.h index 951a3a55dd853fda31bd5c348b04f8b9d89001cc..7fbeb200701f3317e50b6b12641ed8b49017ef6b 100644 --- a/ServoBehaviours.h +++ b/ServoBehaviours.h @@ -60,4 +60,45 @@ public: }; +class RotateReturn : public Behaviour { + Servo _servo; + int _start_angle = 0; + int _end_angle = 180; + int _delay = 30; + int _num_rotations = 1; + int _rotations = 0; + int _pause = 500; + +public: + RotateReturn(Servo servo, String name="rotate", int delay=30, int pause=500, int start_angle = 2, int end_angle=178 ) : + Behaviour(name), _servo(servo),_delay(delay), _pause(pause), _start_angle(start_angle), _end_angle(end_angle) {} + char* args() {return "<int number_of_cycles>"; }; + String start(String args) { + _num_rotations = args.toInt(); + _rotations = 0; + _running = true; + } + + void update() { + _servo.write(_start_angle); + delay(_pause); + for(int i = 0; i < _end_angle; i++) { + _servo.write(i); + delay(_delay); + } + _servo.write(_end_angle); + delay(_pause); + for(int i = 180; i >= _start_angle; i--) { + _servo.write(i); + delay(_delay/2); + } + _servo.write(_start_angle); + _rotations++; + if( _rotations >= _num_rotations ) { + _running = false; + } + } + +}; + #endif diff --git a/WallVis.h b/WallVis.h index b7d3b35be2758682ba98b3e4fe006038449cecaf..78b4257baf66a0be3fd42c0d96dd26c11ce25382 100644 --- a/WallVis.h +++ b/WallVis.h @@ -29,6 +29,7 @@ class WallVis { Adafruit_MQTT_Publish* _my_announce; WiFiClient* _client; String _my_announce_channel; + boolean _wifi; public: WallVis(char* id, char* ssid="WallVisNet", char* wifi_pass="wallvisAP", @@ -39,6 +40,7 @@ public: Serial.println(data); } + void set_wifi(boolean v ) { _wifi = v; } /* @@ -50,38 +52,42 @@ public: Serial.println(F("WallVis Node starting up")); Serial.println("Initialising " + String(_id)); - WiFi.mode(WIFI_STA); + if( _wifi ) { - // Connect to WiFi access point. - Serial.println(); - Serial.print("Connecting to "); - Serial.println(_ssid); - - WiFi.begin(_ssid, _wifi_pass); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(); - - Serial.println("WiFi connected"); - Serial.println("IP address: "); Serial.println(WiFi.localIP()); - // Done Wifi + WiFi.mode(WIFI_STA); - // Setup MQTT - _client = new WiFiClient(); - _mqtt = new Adafruit_MQTT_Client(_client, _server, _port, "" /* mqttt username */, "" /* mqtt pass*/); - _device_subscription = new Adafruit_MQTT_Subscribe(_mqtt, _id); - _announce = new Adafruit_MQTT_Publish(_mqtt, "announce"); - _my_announce_channel = String("announce/") + String(_id); - _my_announce = new Adafruit_MQTT_Publish(_mqtt, _my_announce_channel.c_str()); + // Connect to WiFi access point. + Serial.println(); + Serial.print("Connecting to "); + Serial.println(_ssid); - // Setup MQTT subscription for this device - _mqtt->subscribe(_device_subscription); - // This *would* setup a callback, but we're not doing this right now... - //_device_subscription->setCallback(test_callback); + WiFi.begin(_ssid, _wifi_pass); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(); + + Serial.println("WiFi connected"); + Serial.println("IP address: "); Serial.println(WiFi.localIP()); + // Done Wifi + + // Setup MQTT + _client = new WiFiClient(); + _mqtt = new Adafruit_MQTT_Client(_client, _server, _port, "" /* mqttt username */, "" /* mqtt pass*/); + _device_subscription = new Adafruit_MQTT_Subscribe(_mqtt, _id); + _announce = new Adafruit_MQTT_Publish(_mqtt, "announce"); + _my_announce_channel = String("announce/") + String(_id); + _my_announce = new Adafruit_MQTT_Publish(_mqtt, _my_announce_channel.c_str()); + + // Setup MQTT subscription for this device + _mqtt->subscribe(_device_subscription); + // This *would* setup a callback, but we're not doing this right now... + //_device_subscription->setCallback(test_callback); + + MQTT_connect(); + } - MQTT_connect(); announce_capabilities(); Serial.println("Init finished"); @@ -104,7 +110,7 @@ public: void vis_loop() { int loop_start_time = millis(); serial_command(); - mqtt_command(); + if( _wifi ) { mqtt_command(); } if( _active ) { //Serial.println("Updating "+_active->name()); _active -> update(); @@ -211,16 +217,15 @@ public: void announce_capabilities() { Serial.print("Node "); Serial.println(_id); - if( _announce->publish(_id) ) { - for( int i = 0; i < _behaviours.get_num_behaviours(); i++ ) { - Behaviour* b = _behaviours.get_by_num(i); - String doc = b->name() + ": " + b->args(); - _my_announce->publish(doc.c_str()); - Serial.print("-->"); - Serial.println(doc); - } - } else { - Serial.println("Couldn't make announcement"); + if( _wifi ) { + if( ! _announce->publish(_id) ) { Serial.println("Couldn't make announcement"); } + } + for( int i = 0; i < _behaviours.get_num_behaviours(); i++ ) { + Behaviour* b = _behaviours.get_by_num(i); + String doc = b->name() + ": " + b->args(); + if( _wifi ) { _my_announce->publish(doc.c_str()); } + Serial.print("-->"); + Serial.println(doc); } } diff --git a/examples/ExampleNode/ExampleNode.ino b/examples/ExampleNode/ExampleNode.ino index c28cdcdddaa29ca613c31e3509a8a9d069d35b69..f3715bea31c5b155427a20b0a0ba925e28e4adbb 100644 --- a/examples/ExampleNode/ExampleNode.ino +++ b/examples/ExampleNode/ExampleNode.ino @@ -47,15 +47,20 @@ void setup() //Add in a behaviour that just goes to a certain angle, with the default name 'goto' node.add(new ServoGoto(s1) ); + //Add in a behaviour that rotates from 0 to 180 and back again (e.g. dropping a ball trustball style) + node.add(new RotateReturn(s1,"drop",1,500, 3, 175) ); + //A few useful LED behaviours - node.add(new NumLEDs(&strip, strip.Color(10, 255, 15), "green_leds" ) ); - node.add(new NumLEDs(&strip, strip.Color(255, 30, 40), "red_leds" )); - node.add(new BrightnessLEDs(&strip, 0, 255, "red" )); - node.add(new BrightnessLEDs(&strip, 120, 255, "green" )); - node.add(new BreathingLEDs(&strip, 120, 0, "breathe" )); - node.add(new BreathingLEDs(&strip, 170, 255, "breathe_blue" )); + node.add(new NumLEDs(&strip, "green_leds", strip.Color(10, 255, 15) ) ); + node.add(new NumLEDs(&strip, "red_leds", strip.Color(255, 30, 40) )); + node.add(new NumLEDs(&strip, "leds")); + node.add(new BrightnessLEDs(&strip, "red", 0, 255 )); + node.add(new BrightnessLEDs(&strip, "green", 120, 255 )); + node.add(new BreathingLEDs(&strip, "breathe", 120, 0 )); + node.add(new BreathingLEDs(&strip, "breathe_blue", 170, 255 )); //Initialise the whole infrastructure + node.set_wifi(false); node.init(); }