Skip to content
Snippets Groups Projects
Commit b193c2c2 authored by Dave Murray-Rust's avatar Dave Murray-Rust
Browse files

Added LEDBehaviours, tweaking general operation

parent 473b0151
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,8 @@ public:
virtual boolean is_running() {return _running;};
//What's the name of this behaviour
virtual String name() {return _name; };
//What arguments does the behaviour take? Override this to document your behaviour
virtual char* args() {return "(no args)"; };
//Start the behaviour, with arguments (don't know why this can't be virtual?)
virtual String start(String args) {Serial.println("Base start called <"+args+">");};
//Update the behaviour periodically
......@@ -69,6 +71,14 @@ public:
}
return nullptr;
}
Behaviour* get_by_num(int n) {
return behaviours[n];
}
int get_num_behaviours() {
return num;
}
};
......
#ifndef LED_BEHAVIOUR_h
#define LED_BEHAVIOUR_h
#include "Arduino.h"
#include "Behaviours.h"
#include <Adafruit_NeoPixel.h>
class NumLEDs : public Behaviour {
Adafruit_NeoPixel* _strip;
uint32_t _color;
public:
NumLEDs(Adafruit_NeoPixel* strip, uint32_t color, String name = "num_leds") :
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 "";
}
};
class BrightnessLEDs : public Behaviour {
Adafruit_NeoPixel* _strip;
uint32_t _hue;
uint32_t _sat;
public:
BrightnessLEDs(Adafruit_NeoPixel* strip, uint32_t hue, uint32_t sat=255, String name = "num_leds") :
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 "";
}
};
class BreathingLEDs : public Behaviour {
Adafruit_NeoPixel* _strip;
uint _hue;
uint _sat;
int32_t _current = 0;
//Allows us to have slightly slower behaviours on the go...
int _factor = 4;
int _rate = 0;
int _direction = 1;
public:
BreathingLEDs(Adafruit_NeoPixel* strip, uint32_t hue, uint32_t sat=255, String name = "breathe_leds") :
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();
}
};
#endif
......@@ -9,9 +9,10 @@ class ServoGoto : public Behaviour {
Servo _servo;
public:
ServoGoto(Servo servo, String name = "goto") : Behaviour(name), _servo(servo) {}
ServoGoto(Servo servo, String name = "goto") : Behaviour(name), _servo(servo){ }
//ServoMove(Servo servo, String name) : Behaviour(name), _servo(servo) {}
char* args() {return "<int angle>"; };
String start(String args) {
Serial.println("Goto: '"+args+"'");
int val = args.toInt();
......@@ -33,6 +34,7 @@ class ServoWiggle : public Behaviour {
public:
ServoWiggle(Servo servo, String name, int slowness=3) : Behaviour(name), _servo(servo),_wiggle_factor(slowness) {}
char* args() {return "<int wiggle_angle>"; };
String start(String args) {
_wiggle_angle = args.toInt();
_wiggles = 0;
......
......@@ -3,6 +3,7 @@
#include "Arduino.h"
#include "Behaviours.h"
#include "ServoBehaviours.h"
#include "LEDBehaviours.h"
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
......@@ -24,7 +25,10 @@ class WallVis {
int _loop_time = 5;
Adafruit_MQTT_Client* _mqtt;
Adafruit_MQTT_Subscribe* _device_subscription;
Adafruit_MQTT_Publish* _announce;
Adafruit_MQTT_Publish* _my_announce;
WiFiClient* _client;
String _my_announce_channel;
public:
WallVis(char* id, char* ssid="WallVisNet", char* wifi_pass="wallvisAP",
......@@ -68,6 +72,9 @@ public:
_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);
......@@ -75,7 +82,7 @@ public:
//_device_subscription->setCallback(test_callback);
MQTT_connect();
announce_capabilities();
Serial.println("Init finished");
}
......@@ -97,9 +104,9 @@ public:
void vis_loop() {
int loop_start_time = millis();
serial_command();
//mqtt_command();
mqtt_command();
if( _active ) {
Serial.println("Updating "+_active->name());
//Serial.println("Updating "+_active->name());
_active -> update();
if( ! _active->is_running() ) _active = nullptr;
}
......@@ -201,9 +208,26 @@ public:
Serial.println("MQTT Connected!");
}
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");
}
}
};
/* Left here temporarily - can't use a member function as a callback
So this was the start of a way to create a static function that would work
void* current_wallvis;
......
......@@ -14,11 +14,25 @@ WallVis node(
// Set up the servo we will use
Servo s1 = Servo();
// Set up LEDs as well
#define LED_COUNT 12
#define LED_PIN D2
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
// Attach the servo to the right pin on the board
s1.attach(D3);
// Start the LED library
strip.begin();
strip.fill(strip.Color(30,255,80));
strip.show();
delay(100);
strip.fill(0);
strip.show();
//Get the serial port ready
Serial.begin(115200);
......@@ -29,10 +43,18 @@ void setup()
node.add(new ServoWiggle(s1, "wiggle") );
node.add(new ServoWiggle(s1, "slow_wiggle", 10) );
node.add(new ServoWiggle(s1, "fast_wiggle", 1) );
//Add in a behaviour that just goes to a certain angle, with the default name 'goto'
node.add(new ServoGoto(s1) );
//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" ));
//Initialise the whole infrastructure
node.init();
}
......
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