add more mqtt handlers

This commit is contained in:
interfisch 2023-11-23 22:47:45 +01:00
parent fb5862e1f3
commit bea5c8691c
3 changed files with 208 additions and 15 deletions

View File

@ -4,6 +4,7 @@
#include <Arduino.h>
#include "flipdot.h"
#include <string.h>
#define COLUMNS 75
@ -57,7 +58,9 @@ public:
void setBuffer_solid(bool set);
void setBufferColumn(uint8_t _colnum, uint16_t _rowdata);
void setBuffer_random(uint8_t randomness);
void setBuffer_random(uint8_t brightness);
void setBuffer_byString(String data,String& error);
void setBuffer_byInt(String data,String& error);
void setBuffer_Image1();
void setBuffer_Image2();

View File

@ -728,12 +728,12 @@ void Image::setBufferColumn(uint8_t _colnum, uint16_t _rowdata)
}
}
void Image::setBuffer_random(uint8_t randomness)
void Image::setBuffer_random(uint8_t brightness)
{
for (uint8_t x=0;x<getW();x++) {
uint16_t randomnumber=0;
for (uint8_t y=0;y<getH();y++) {
if (random(256)<randomness) { //the higher the parameter the more white
if (random(256)<brightness) { //the higher the parameter the more white
randomnumber+=pow(2, y);
}
}
@ -745,6 +745,60 @@ void Image::setBuffer_random(uint8_t randomness)
}
void Image::setBuffer_byString(String data,String& error)
{
//first check size
if (data.length()!=getW()*getH()) { //if characters matches pixels then its likely binary
error="setBuffer_byString: size mismatch";
}
for (uint8_t x=0;x<getW();x++) {
uint16_t columnnumber=0;
for (uint8_t y=0;y<getH();y++) {
char currentChar = data.charAt(x*getH()+y);
if (currentChar!='0' && currentChar!=' ' && currentChar!='_') { //0, Space or "_" are black pixels.
columnnumber+=pow(2, y); //if not one of the above, then make white
}
}
backBuffer[x]=columnnumber;
}
flag_updating=true; //make update run
}
void Image::setBuffer_byInt(String data,String& error)
{
int str_len = data.length() + 1;
char char_array[str_len];
data.toCharArray(char_array, str_len);
char *token = strtok(char_array, ",");
uint8_t x=0;
while (token != NULL && x<getW()) {
String tokenString(token);
uint16_t tokenInt=tokenString.toInt();
backBuffer[x]=tokenInt;
token = strtok(NULL, ","); //get next token
x++;
}
if (x!=getW()) { //x reached exaclty the end
Serial.println("Warning: Not Enough Data!");
error="setBuffer_byInt: not enough data. x="+x;
}
if (token!= NULL) { //exited loop but there was another valid token
Serial.println("Warning: too much data");
error="setBuffer_byInt: too much data";
}
flag_updating=true; //make update run
}
void Image::resetOrder(bool ascending) {
for (uint8_t i=0;i<getW();i++) { //fill with ascending numbers
if (ascending) {
@ -775,6 +829,11 @@ void Image::shuffleOrder(uint8_t iterations) {
UpdateReturn Image::updateByColumn(bool clearFirst, bool optimizeClear, bool optimizeSet)
{
/*
clearFirst : if True, go through all columns and clear them (by set order). Then go through same order and set dots.
optimizeClear : if True, do not clear columns where dots need to be set or no change is happening. if False, clear every column
optimizeSet : if True, do not set already set dots again.
*/
if (!flag_updating) {
return nochange; //finished

View File

@ -3,6 +3,7 @@
#include <Homie.h>
HomieNode flipdotNode("display", "Display", "display");
HomieNode settingsNode("settings", "Settings", "settings");
HomieNode resultNode("result", "Result", "result");
#include "flipdot.h"
@ -16,15 +17,26 @@ Image flip;
unsigned long loopmillis=0;
unsigned long last_update=0;
bool clearFirst=true;
bool optimizeClear=false;
bool optimizeSet=false;
uint8_t stringToBool(String s);
bool presetHandler(const HomieRange& range, const String& value);
bool orderHandler(const HomieRange& range, const String& value);
bool dataHandler(const HomieRange& range, const String& value);
bool clearFirstHandler(const HomieRange& range, const String& value);
bool optimizeClearHandler(const HomieRange& range, const String& value);
bool optimizeSetHandler(const HomieRange& range, const String& value);
/*bool globalInputHandler(const HomieNode& node, const HomieRange& range, const String& property, const String& value) {
Homie.getLogger() << "Global input handler. Received on node " << node.getId() << ": " << property << " = " << value << endl;
return false;
}*/
String error; //used to buffer error messages to be send via mqtt
void setup() {
flip.init();
@ -35,9 +47,19 @@ void setup() {
//Setup Homie
Homie_setFirmware("flipdot", "0.1.0");
flipdotNode.advertise("preset").settable(presetHandler);
flipdotNode.advertise("data").settable(dataHandler);
flipdotNode.advertise("order").settable(orderHandler);
settingsNode.advertise("clearfirst").settable(clearFirstHandler);
settingsNode.advertise("optimizeclear").settable(optimizeClearHandler);
settingsNode.advertise("optimizeset").settable(optimizeSetHandler);
//Homie.setGlobalInputHandler(globalInputHandler);
Homie.setup();
error="";
}
@ -48,12 +70,6 @@ void loop() {
static unsigned long last_change=0;
//static bool color=0;
if (loopmillis-last_change >= 1000*10)
{
flipdotNode.setProperty("preset").send("asdf");
last_change=loopmillis;
}
/*
static uint8_t image=0;
if (loopmillis-last_change >= 1000*10)
@ -125,8 +141,14 @@ void loop() {
*/
UpdateReturn result=flip.updateByColumn(false,true,true); //0=not finished, 1=finished
UpdateReturn result=flip.updateByColumn(clearFirst,optimizeClear,optimizeSet); //0=not finished, 1=finished
//UpdateReturn result=flip.updateByColumn(true,false,false); //0=not finished, 1=finished <- most simple
static UpdateReturn last_result=finished;
if (last_result==nochange && result!=last_result) { //was finished but has just started updating again
last_change=loopmillis; //display started changing dots. set starting time.
Serial.print("Reset Time to "); Serial.println(last_change);
}
last_result=result;
if (result == finished) //just finished
{
unsigned long duration=millis()-last_change;
@ -136,14 +158,35 @@ void loop() {
resultNode.setProperty("updateduration").send((String)flip.updateDuration);
flip.updateDuration=0; //reset
flip.shuffleOrder(10);
}
if (error.length()>0) { //is there an error to send?
resultNode.setProperty("error").send(error);
error=""; //clear
}
}
bool orderHandler(const HomieRange& range, const String& value) {
Serial.print("Order payload="); Serial.println(value);
if (value=="ascending") {
flip.resetOrder(true);
}else if (value=="descending") {
flip.resetOrder(false);
}else if (value=="shuffle") {
flip.shuffleOrder(8);
}else if (value=="random") {
flip.shuffleOrder(flip.getW());
}else{
return false;
}
flipdotNode.setProperty("order").send(value);
return true;
}
bool presetHandler(const HomieRange& range, const String& value) {
Serial.print("Received: "); Serial.println(value);
Serial.print("Preset payload="); Serial.println(value);
if (value == "white"){
flip.setBuffer_solid(1);
Homie.getLogger() << "Preset is White" << endl;
@ -174,11 +217,99 @@ bool presetHandler(const HomieRange& range, const String& value) {
}else if(value == "image8"){
flip.setBuffer_Image8();
Homie.getLogger() << "Preset is Image8" << endl;
}else if(value == "random"){
flip.setBuffer_random(50);
}else{
error="preset \""+value+"\" not found";
return false;
}
flipdotNode.setProperty("preset").send(value);
return true;
}
}
bool dataHandler(const HomieRange& range, const String& value) {
Serial.print("Data Received: "); Serial.println(value);
Serial.print("Length: "); Serial.println(value.length());
/*payload can be either be Binary/String:
"10101101011010.."
"1 111 11 11 1 1..."
"a_aa_aa_aa_aa__a_a_a.."
Or Int (unsigned 16 bit. comma separated for each column)
"837491,12347,592,920,114,0,12,45125,..."
*/
if (value.length()==flip.getW()*flip.getH()) { //if characters matches pixels then its likely binary
Serial.println("Using byString");
flip.setBuffer_byString(value,error);
}else{
Serial.println("Using byInt");
flip.setBuffer_byInt(value,error);
}
return true;
}
bool clearFirstHandler(const HomieRange& range, const String& value) {
Serial.print("clearFirstHandler payload="); Serial.println(value);
uint8_t res=stringToBool(value);
if (res==0){
clearFirst=false;
}else if(res==1){
clearFirst=true;
}else{
error="no valid input";
return false;
}
settingsNode.setProperty("clearfirst").send((String)res);
return true;
}
bool optimizeClearHandler(const HomieRange& range, const String& value) {
Serial.print("OptimizeClearHandler payload="); Serial.println(value);
uint8_t res=stringToBool(value);
if (res==0){
optimizeClear=false;
}else if(res==1){
optimizeClear=true;
}else{
error="no valid input";
return false;
}
settingsNode.setProperty("optimizeclear").send((String)res);
return true;
}
bool optimizeSetHandler(const HomieRange& range, const String& value) {
Serial.print("OptimizeSetHandler payload="); Serial.println(value);
uint8_t res=stringToBool(value);
if (res==0){
optimizeSet=false;
}else if(res==1){
optimizeSet=true;
}else{
error="no valid input";
return false;
}
settingsNode.setProperty("optimizeset").send((String)res);
return true;
}
uint8_t stringToBool(String s) {
String param=s;
param.toLowerCase();
if (param == "true" || param == "1" || param == "on" || param == "yes" || param == "y"){
return 1;
}else if(param == "false" || param == "0" || param == "off" || param== "no" || param == "n"){
return 0;
}else{
return 2;
}
}