Merge branch 'fsm' into 'master'

Fsm

See merge request !1
This commit is contained in:
dukat 2017-02-19 21:52:29 +01:00
commit 8304f8f9c6
7 changed files with 561 additions and 190 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
.clang_complete .clang_complete
.gcc-flags.json .gcc-flags.json
.DS_Store .DS_Store
esp-wemos-schild/data/homie/config.json

87
Readme.md Normal file
View File

@ -0,0 +1,87 @@
Homie Schild Firmware
=====================
## About
## Features
### LED Strip
|Topic |Descriptions |settable |Values |
|---------|--------------|:---------:|---------|
|`device_id`/strip/pixel/|Range property from 0 - (number of pixels - 1)|yes|Color as uint32_t see [Color](#color)|
|`device_id`/strip/color|Range property to set the effects colors 1 & 2 see: [effect colors](#effect-colors)|yes|Color as uint32_t see [Color](#color)|
|`device_id`/strip/brightness|Sets the brightness of the pixel strip|yes|possible values: 0 - 255|
|`device_id`/strip/effect|Set effect|yes|see: [effects](#effects)|
|`device_id`/strip/clear|Clears the pixels strip|yes|any value is possible|
|`device_id`/strip/length|Set the strip length|yes|Possible values: 0 - length|
### Motion sensor
|Topic |Descriptions |settable |Values |
|---------|--------------|:---------:|---------|
|`device_id`/sensor/motion|Activated on motion|no|true or false|
## Color
To convert RGB value use the following bash code:
```bash
function rgbToColor {
echo $(( $(($1<<16)) + $(($2<<8)) + $(($3)) ));
}
function colorToRGB {
echo "Red: $(($1>>16&0xff))"
echo "Green: $(($1>>8&0xff))"
echo "Blue: $(($1&0xff))"
}
```
#### Example
RGB Value to color uint32_t
```bash
bash$ rgbToColor 155 230 32
10216992
bash$
```
uint32_t to RGB values
```bash
bash$ colorToRGB 10216992
Red: 155
Green: 230
Blue: 32
bash$
```
## Effects
* scanner
Shows the moving larson scanner eye known form *Battlestar Galactica* and *Knight Rider*. The used effect color can be specified by *color_0*
* randomscanner
This is the same scanner then the scanner above but uses an alternating color pattern
* rainbowcycle
Shows a cycling rainbown on the LED strip
* theaterchase
Shows an color chasing LED strip.
You can specify the color by set *color_0* and *color_1*
* fade
Fades from effect color_0 to effect_color_1
* randomfade
Fades thru an alternating color pattern
* none
Stop all effects
## Effect colors
You can set to different effect colors
* *color_0* (default R: 255, G: 0 B: 0)
* *color_1* (default R: 0, G: 0 B: 255)
The effect color has to be set after the effect.
###### Example:
1. `homie/device_id/strip/effect/set -m "fade"`
2. `homie/device_id/strip/color_0/set -m "255"`
3. `homie/device_id/strip/color_1/set -m "10216992"`
##### color_0
This color will be used for the following effects:
* scanner
* theaterchase
* fade
##### color_1
This color will be used for the following effects:
* theaterchase
* fade

View File

@ -0,0 +1,293 @@
#include "NeoPatterns.h"
NeoPatterns::NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)()) :
Adafruit_NeoPixel(pixels, pin, type)
{
OnComplete = callback;
}
void NeoPatterns::Update(){
if((millis() - lastUpdate) > Interval) // time to update
{
lastUpdate = millis();
switch(ActivePattern)
{
case RAINBOW_CYCLE:
RainbowCycleUpdate();
break;
case THEATER_CHASE:
TheaterChaseUpdate();
break;
case COLOR_WIPE:
ColorWipeUpdate();
break;
case SCANNER:
ScannerUpdate();
break;
case FADE:
FadeUpdate();
break;
case RANDOM_FADE:
RandomFadeUpdate();
break;
case NONE:
break;
default:
break;
}
}
}
void NeoPatterns::Increment()
{
if (Direction == FORWARD)
{
Index++;
if (Index >= TotalSteps)
{
Index = 0;
if (OnComplete != NULL)
{
OnComplete(); // call the comlpetion callback
}
}
}
else // Direction == REVERSE
{
--Index;
if (Index <= 0)
{
Index = TotalSteps-1;
if (OnComplete != NULL)
{
OnComplete(); // call the comlpetion callback
}
}
}
}
void NeoPatterns::Reverse(){
if (Direction == FORWARD)
{
Direction = REVERSE;
Index = TotalSteps-1;
}
else
{
Direction = FORWARD;
Index = 0;
}
}
void NeoPatterns::None(){
if(ActivePattern != NONE) {
clear();
show();
}
ActivePattern = NONE;
}
void NeoPatterns::RainbowCycle(uint8_t interval, direction dir){
ActivePattern = RAINBOW_CYCLE;
Interval = interval;
TotalSteps = 255;
Index = 0;
Direction = dir;
}
void NeoPatterns::RainbowCycleUpdate()
{
for(int i=0; i< numPixels(); i++)
{
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
}
show();
Increment();
}
void NeoPatterns::TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir){
ActivePattern = THEATER_CHASE;
Interval = interval;
TotalSteps = numPixels();
Color1 = color1;
Color2 = color2;
Index = 0;
Direction = dir;
}
void NeoPatterns::TheaterChaseUpdate(){
for(int i=0; i< numPixels(); i++)
{
if ((i + Index) % 3 == 0)
{
setPixelColor(i, Color1);
}
else
{
setPixelColor(i, Color2);
}
}
show();
Increment();
}
void NeoPatterns::ColorWipe(uint32_t color, uint8_t interval, direction dir)
{
ActivePattern = COLOR_WIPE;
Interval = interval;
TotalSteps = numPixels();
Color1 = color;
Index = 0;
Direction = dir;
}
// Update the Color Wipe Pattern
void NeoPatterns::ColorWipeUpdate()
{
setPixelColor(Index, Color1);
show();
Increment();
}
// Initialize for a SCANNNER
void NeoPatterns::Scanner(uint32_t color1, uint8_t interval, bool colorful)
{
ActivePattern = SCANNER;
Interval = interval;
TotalSteps = (numPixels() - 1) * 2;
Color1 = color1;
Index = 0;
wPos = 0;
this->colorful = colorful;
}
// Update the Scanner Pattern
void NeoPatterns::ScannerUpdate()
{
if(colorful) {
Color1 = Wheel(wPos);
if(wPos >= 255) {
wPos =0;
}
else {
wPos++;
}
}
for (int i = 0; i < numPixels(); i++)
{
if (i == Index) // Scan Pixel to the right
{
setPixelColor(i, Color1);
}
else if (i == TotalSteps - Index) // Scan Pixel to the left
{
setPixelColor(i, Color1);
}
else // Fading tail
{
setPixelColor(i, DimColor(getPixelColor(i)));
}
}
show();
Increment();
}
void NeoPatterns::Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir)
{
ActivePattern = FADE;
Interval = interval;
TotalSteps = steps;
Color1 = color1;
Color2 = color2;
Index = 0;
Direction = dir;
}
// Update the Fade Pattern
void NeoPatterns::FadeUpdate()
{
// Calculate linear interpolation between Color1 and Color2
// Optimise order of operations to minimize truncation error
uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
ColorSet(Color(red, green, blue));
show();
Increment();
}
void NeoPatterns::RandomFade(uint8_t interval ){
ActivePattern = RANDOM_FADE;
Interval = interval;
TotalSteps = 255;
Index = 0;
}
void NeoPatterns::RandomFadeUpdate(){
ColorSet(Wheel(Index));
Increment();
}
void NeoPatterns::SetColor1(uint32_t color){
Color1 = color;
}
void NeoPatterns::SetColor2(uint32_t color){
Color2 = color;
}
// Calculate 50% dimmed version of a color (used by ScannerUpdate)
uint32_t NeoPatterns::DimColor(uint32_t color)
{
// Shift R, G and B components one bit to the right
uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
return dimColor;
}
// Set all pixels to a color (synchronously)
void NeoPatterns::ColorSet(uint32_t color)
{
for (int i = 0; i < numPixels(); i++)
{
setPixelColor(i, color);
}
show();
}
// Returns the Red component of a 32-bit color
uint8_t NeoPatterns::Red(uint32_t color)
{
return (color >> 16) & 0xFF;
}
// Returns the Green component of a 32-bit color
uint8_t NeoPatterns::Green(uint32_t color)
{
return (color >> 8) & 0xFF;
}
// Returns the Blue component of a 32-bit color
uint8_t NeoPatterns::Blue(uint32_t color)
{
return color & 0xFF;
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t NeoPatterns::Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
if(WheelPos < 85)
{
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else if(WheelPos < 170)
{
WheelPos -= 85;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
else
{
WheelPos -= 170;
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}

View File

@ -0,0 +1,59 @@
#include <Adafruit_NeoPixel.h>
// Pattern types supported:
enum pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE, RANDOM_FADE };
// Patern directions supported:
enum direction { FORWARD, REVERSE };
class NeoPatterns : public Adafruit_NeoPixel
{
public:
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)());
void Update();
void Reverse();
void None();
void RainbowCycle(uint8_t interval, direction dir = FORWARD);
void RainbowCycleUpdate();
void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD);
void TheaterChaseUpdate();
void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD);
void ColorWipeUpdate();
void Scanner(uint32_t color1, uint8_t interval = 40,bool colorful = false);
void ScannerUpdate();
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD);
void FadeUpdate();
void RandomFade(uint8_t interval = 100);
void RandomFadeUpdate();
void SetColor1(uint32_t color);
void SetColor2(uint32_t color);
//Utilities
void ColorSet(uint32_t color);
uint8_t Red(uint32_t color);
uint8_t Green(uint32_t color);
uint8_t Blue(uint32_t color);
uint32_t Wheel(byte WheelPos);
private:
// Member Variables:
pattern ActivePattern; // which pattern is running
direction Direction; // direction to run the pattern
unsigned long Interval; // milliseconds between updates
unsigned long lastUpdate; // last update of position
uint32_t Color1, Color2; // What colors are in use
uint16_t TotalSteps; // total number of steps in the pattern
uint16_t Index; // current step within the pattern
byte wPos;
bool colorful;
uint32_t DimColor(uint32_t color);
void Increment();
void (*OnComplete)(); // Callback on completion of pattern
};

View File

@ -2,11 +2,11 @@
"name": "Homie CTDO Schild", "name": "Homie CTDO Schild",
"device_id": "schild", "device_id": "schild",
"wifi": { "wifi": {
"ssid": "ctdo", "ssid": "Nudel",
"password": "ctdo2342" "password": "Unser WLAN ist sicher!"
}, },
"mqtt": { "mqtt": {
"host": "raum.ctdo.de", "host": "prometheus.local",
"port": 1883, "port": 1883,
"auth": false "auth": false
}, },

View File

@ -3,229 +3,159 @@
#include <Homie.h> #include <Homie.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#include "NeoPatterns.h"
#define PIN D1 #define PIN D1
#define NUMPIXELS 30 #define SENSOR D3
#define NUMPIXELS 144
uint16_t effectI=0,effectJ=0,wait = 50;
unsigned long lastCall = 0;
byte wPos = 0;
uint8_t state = 0;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
bool lastSensorValue = false;
void StripComplete(){
return;
}
NeoPatterns pixels = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800,&StripComplete);
HomieNode stripNode("strip", "strip"); HomieNode stripNode("strip", "strip");
HomieNode sensorNode("sensor", "sensor");
Bounce debouncer = Bounce();
String effect = "none";
// Input a value 0 to 255 to get a color value. bool onSetColor(const HomieRange& range, const String& value){
// The colours are a transition r - g - b - back to r. if (!range.isRange || range.index < 0 || range.index > 1) {
uint32_t wheel(byte wheelPos) { return false;
wheelPos = 255 - wheelPos; }
if(wheelPos < 85) { switch(range.index) {
return pixels.Color(255 - wheelPos * 3, 0, wheelPos * 3); case 0:
} pixels.SetColor1(value.toInt());
if(wheelPos < 170) { break;
wheelPos -= 85; case 1:
return pixels.Color(0, wheelPos * 3, 255 - wheelPos * 3); pixels.SetColor2(value.toInt());
} break;
wheelPos -= 170; }
return pixels.Color(wheelPos * 3, 255 - wheelPos * 3, 0); stripNode.setProperty("color_" + String(range.index)).send(value);
} }
void getRGBValues(uint32_t *rgbBuffer,uint32_t const color){
rgbBuffer[0] = color >> 16 & 255;
rgbBuffer[1] = color >> 8 & 255;
rgbBuffer[2] = color & 255;
return ;
}
bool onSetPixel(const HomieRange& range, const String& value){ bool onSetPixel(const HomieRange& range, const String& value){
if(!range.isRange){ if(!range.isRange) {
effect = "none"; pixels.None();
pixels.clear(); pixels.ColorSet(value.toInt());
for(int i=0;i<pixels.numPixels();i++){ stripNode.setProperty("pixel").send(value);
pixels.setPixelColor(i, value.toInt()); return true;
} }
pixels.show(); if (range.index < 0 || range.index > pixels.numPixels()-1) {
stripNode.setProperty("pixel").send(value); return false;
return true; }
} pixels.None();
if (range.index < 0 || range.index > pixels.numPixels()-1) { pixels.setPixelColor(range.index, value.toInt());
return false; pixels.show();
} stripNode.setProperty("pixel_" + String(range.index)).send(value);
effect = "none";
pixels.setPixelColor(range.index, value.toInt());
pixels.show();
stripNode.setProperty("pixel_" + String(range.index)).send(value);
} }
bool onSetBrightness(const HomieRange& range, const String& value){ bool onSetBrightness(const HomieRange& range, const String& value){
long brightness= value.toInt(); long brightness= value.toInt();
if (brightness < 0 || brightness > 255) { if (brightness < 0 || brightness > 255) {
return false; return false;
} }
pixels.setBrightness(brightness); pixels.setBrightness(brightness);
pixels.show(); pixels.show();
stripNode.setProperty("brightness").send(value); stripNode.setProperty("brightness").send(value);
} }
bool onSetEffect(const HomieRange& range, const String& value){ bool onSetEffect(const HomieRange& range, const String& value){
effect = value; String effect = value;
effect.toLowerCase(); effect.toLowerCase();
effectI=0,effectJ=0,wait=50; if(effect == "scanner") {
stripNode.setProperty("effect").send(value); pixels.Scanner(pixels.Color(255, 0, 0));
}
else if(effect == "randomscanner") {
pixels.Scanner(pixels.Color(255, 0, 0), 40, true);
}
else if(effect == "rainbowcycle") {
pixels.RainbowCycle(50);
}
else if(effect == "theaterchase") {
pixels.TheaterChase(pixels.Color(255, 0, 0), pixels.Color(0,0,255), 100);
}
else if(effect == "fade") {
pixels.Fade(pixels.Color(255, 0, 0), pixels.Color(0,0,255), 200, 100);
}
else if(effect == "randomfade") {
pixels.RandomFade();
}
else {
pixels.None();
}
stripNode.setProperty("effect").send(value);
} }
bool onSetClear(const HomieRange& range, const String& value){ bool onSetClear(const HomieRange& range, const String& value){
effect = "none"; pixels.None();
pixels.clear(); pixels.clear();
pixels.show(); pixels.show();
stripNode.setProperty("clear").send(value); stripNode.setProperty("clear").send(value);
} }
bool onSetLength(const HomieRange& range, const String& value){ bool onSetLength(const HomieRange& range, const String& value){
effect = "none"; pixels.None();
pixels.clear(); pixels.clear();
pixels.show(); pixels.show();
int newLength = value.toInt(); int newLength = value.toInt();
if(newLength > 0){ if(newLength > 0) {
pixels.updateLength(newLength); pixels.updateLength(newLength);
} }
stripNode.setProperty("length").send(value); stripNode.setProperty("length").send(value);
} }
void loopHandler() { void loopHandler() {
if (effect == "none"){ pixels.Update();
return;
}
else if (effect == "larson") {
int SpeedDelay = 20;
int EyeSize = 5;
uint32_t rgb[3] = {0};
switch(state){ bool sensorValue = debouncer.read();
case 0: if (Homie.isConfigured() && Homie.isConnected() && sensorValue != lastSensorValue) {
if(lastCall + wait < millis()){ sensorNode.setProperty("motion").send(sensorValue ? "true" : "false");
state++; lastSensorValue = sensorValue;
} }
break;
case 1:
for(int i = 0; i < pixels.numPixels()-EyeSize-2; i++) {
pixels.clear();
getRGBValues(rgb,wheel(wPos));
pixels.setPixelColor(i, rgb[0]/10, rgb[1]/10, rgb[2]/10);
for(int j = 1; j <= EyeSize; j++) {
pixels.setPixelColor(i+j, wheel(wPos++));
}
getRGBValues(rgb,wheel(wPos));
pixels.setPixelColor(i+EyeSize+1, rgb[0]/10, rgb[1]/10, rgb[2]/10);
pixels.show();
delay(SpeedDelay);
}
lastCall = millis();
state++;
break;
case 2:
if(lastCall + wait < millis()){
state++;
}
break;
case 3:
for(int i = pixels.numPixels()-EyeSize-2; i > 0; i--) {
pixels.clear();
getRGBValues(rgb,wheel(wPos));
pixels.setPixelColor(i, rgb[0]/10, rgb[1]/10, rgb[2]/10);
for(int j = 1; j <= EyeSize; j++) {
pixels.setPixelColor(i+j, wheel(wPos++));
}
pixels.setPixelColor(i+EyeSize+1, rgb[0]/10, rgb[1]/10, rgb[2]/10);
pixels.show();
delay(SpeedDelay);
}
lastCall = millis();
state++;
break;
default:
state = 0;
}
}
else if (effect == "randomfade") {
if(lastCall + wait > millis()){
return;
}
lastCall = millis();
for(int i=0;i<pixels.numPixels();i++){
pixels.setPixelColor(i,wheel(wPos));
}
pixels.show();
wPos++;
}
else if (effect == "rainbow") {
if(wait + lastCall >= millis()){
return;
}
if(effectJ<256) {
if(effectI<pixels.numPixels()) {
pixels.setPixelColor(effectI, wheel((effectI+effectJ) & 255));
effectI++;
}
else {
effectI=0;
effectJ++;
}
pixels.show();
lastCall = millis();
}
else {
effectJ=0;
}
}
else if (effect == "rainbowcycle") {
if(effectJ<256*5) {
if(effectI<pixels.numPixels()) {
pixels.setPixelColor(effectI, wheel(((effectI * 256 / pixels.numPixels()) + effectJ) & 255));
effectI++;
}
else {
effectI=0;
effectJ++;
}
pixels.show();
lastCall = millis();
}
else {
effectJ=0;
}
}
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Homie_setFirmware("schild", "1.0.0");
Homie.setLoopFunction(loopHandler);
stripNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel); debouncer.attach(SENSOR,INPUT);
stripNode.advertise("brightness").settable(onSetBrightness); debouncer.interval(50);
stripNode.advertise("effect").settable(onSetEffect);
stripNode.advertise("clear").settable(onSetClear);
stripNode.advertise("length").settable(onSetLength);
pixels.begin(); Homie_setFirmware("schild", "1.0.0");
pixels.clear(); Homie.setLoopFunction(loopHandler);
pixels.setBrightness(64);
pixels.show();
Homie.setup(); stripNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel);
stripNode.advertiseRange("color", 0, 1).settable(onSetColor);
stripNode.advertise("brightness").settable(onSetBrightness);
stripNode.advertise("effect").settable(onSetEffect);
stripNode.advertise("clear").settable(onSetClear);
stripNode.advertise("length").settable(onSetLength);
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId); sensorNode.advertise("motion");
ArduinoOTA.setPassword((const char *)"ctdo2342");
ArduinoOTA.begin(); pixels.begin();
pixels.clear();
pixels.setBrightness(64);
pixels.show();
Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.setPassword((const char *)"ctdo2342");
ArduinoOTA.begin();
} }
void loop() { void loop() {
Homie.loop(); Homie.loop();
ArduinoOTA.handle(); debouncer.update();
ArduinoOTA.handle();
} }

View File

@ -15,8 +15,9 @@ framework=arduino
;upload_port = /dev/tty.wchusbserial410 ;upload_port = /dev/tty.wchusbserial410
;upload_speed = 921600 ;upload_speed = 921600
;upload_port = schild.local
upload_port = 5ccf7f1db369.local ;upload_port = 5ccf7f1db369.local
upload_port = schild.local
upload_flags = --auth=ctdo2342 upload_flags = --auth=ctdo2342