flipdot/flipcontrol_esp32/src/main.cpp

315 lines
9.0 KiB
C++
Raw Normal View History

#include <Arduino.h>
#include <Homie.h>
HomieNode flipdotNode("display", "Display", "display");
2023-11-23 21:47:45 +00:00
HomieNode settingsNode("settings", "Settings", "settings");
HomieNode resultNode("result", "Result", "result");
2023-01-31 21:50:25 +00:00
#include "flipdot.h"
#include "image.h"
Image flip;
2023-01-31 20:28:49 +00:00
unsigned long loopmillis=0;
unsigned long last_update=0;
2023-01-24 22:46:52 +00:00
2023-11-23 21:47:45 +00:00
bool clearFirst=true;
bool optimizeClear=false;
bool optimizeSet=false;
uint8_t stringToBool(String s);
bool presetHandler(const HomieRange& range, const String& value);
2023-11-23 21:47:45 +00:00
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;
}*/
2023-11-23 21:47:45 +00:00
String error; //used to buffer error messages to be send via mqtt
void setup() {
flip.init();
Serial.begin(115200);
flip.setBuffer_solid(0);
//Setup Homie
Homie_setFirmware("flipdot", "0.1.0");
flipdotNode.advertise("preset").settable(presetHandler);
2023-11-23 21:47:45 +00:00
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();
2023-11-23 21:47:45 +00:00
error="";
}
void loop() {
Homie.loop();
loopmillis = millis();
2023-01-31 20:28:49 +00:00
static unsigned long last_change=0;
//static bool color=0;
/*
static uint8_t image=0;
if (loopmillis-last_change >= 1000*10)
{
//Serial.print("Change to Solid color ="); Serial.println(color);
//flip.setBuffer_solid(color);
//color=1-color;
2023-11-22 18:32:33 +00:00
switch (image){
case 0:
flip.setBuffer_Image1();
break;
case 1:
flip.setBuffer_Image2();
break;
case 2:
flip.setBuffer_Image3();
break;
case 3:
flip.setBuffer_Image4();
break;
2023-11-22 18:32:33 +00:00
case 4:
flip.setBuffer_Image5();
break;
case 5:
flip.setBuffer_Image6();
break;
case 6:
flip.setBuffer_Image7();
break;
case 7:
flip.setBuffer_Image8();
break;
}
image++;
2023-11-22 18:32:33 +00:00
//image+=1+random(8-2);
image%=8;
2023-02-08 20:29:10 +00:00
//uint8_t _randomvalue=random(128);
//Serial.print("set buffer random. ");
//Serial.println(_randomvalue);
//flip.setBuffer_random(_randomvalue);
//for (uint8_t _x=0;_x<COLUMNS;_x++) {
// uint8_t _y=(sin( loopmillis/100000.0 + _x/5.0 )/2+0.5)*15+0.5;
// uint16_t _row=pow(2, _y);
// flip.setBufferColumn(_x,_row);
//}
2023-11-22 18:32:33 +00:00
2023-02-13 18:58:45 +00:00
last_change=loopmillis;
}
*/
2023-11-22 18:32:33 +00:00
2023-11-23 21:47:45 +00:00
UpdateReturn result=flip.updateByColumn(clearFirst,optimizeClear,optimizeSet); //0=not finished, 1=finished
2023-11-22 18:32:33 +00:00
//UpdateReturn result=flip.updateByColumn(true,false,false); //0=not finished, 1=finished <- most simple
2023-11-23 21:47:45 +00:00
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;
2023-02-11 16:31:49 +00:00
if (result == finished) //just finished
2023-02-08 20:29:10 +00:00
{
unsigned long duration=millis()-last_change;
Serial.print("Last Change took "); Serial.print(duration); Serial.println(" ms");
Serial.print("Update max took "); Serial.print(flip.updateDuration); Serial.println(" ms");
resultNode.setProperty("duration").send((String)duration);
resultNode.setProperty("updateduration").send((String)flip.updateDuration);
2023-02-08 20:29:10 +00:00
flip.updateDuration=0; //reset
2023-02-11 17:51:33 +00:00
2023-02-08 20:29:10 +00:00
}
2023-11-23 21:47:45 +00:00
if (error.length()>0) { //is there an error to send?
resultNode.setProperty("error").send(error);
error=""; //clear
}
}
2023-11-23 21:47:45 +00:00
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) {
2023-11-23 21:47:45 +00:00
Serial.print("Preset payload="); Serial.println(value);
if (value == "white"){
flip.setBuffer_solid(1);
Homie.getLogger() << "Preset is White" << endl;
}else if(value == "black"){
flip.setBuffer_solid(0);
Homie.getLogger() << "Preset is Black" << endl;
}else if(value == "image1"){
flip.setBuffer_Image1();
Homie.getLogger() << "Preset is Image1" << endl;
}else if(value == "image2"){
flip.setBuffer_Image2();
Homie.getLogger() << "Preset is Image2" << endl;
}else if(value == "image3"){
flip.setBuffer_Image3();
Homie.getLogger() << "Preset is Image3" << endl;
}else if(value == "image4"){
flip.setBuffer_Image4();
Homie.getLogger() << "Preset is Image4" << endl;
}else if(value == "image5"){
flip.setBuffer_Image5();
Homie.getLogger() << "Preset is Image5" << endl;
}else if(value == "image6"){
flip.setBuffer_Image6();
Homie.getLogger() << "Preset is Image6" << endl;
}else if(value == "image7"){
flip.setBuffer_Image7();
Homie.getLogger() << "Preset is Image7" << endl;
}else if(value == "image8"){
flip.setBuffer_Image8();
Homie.getLogger() << "Preset is Image8" << endl;
2023-11-23 21:47:45 +00:00
}else if(value == "random"){
flip.setBuffer_random(50);
}else{
2023-11-23 21:47:45 +00:00
error="preset \""+value+"\" not found";
return false;
}
flipdotNode.setProperty("preset").send(value);
return true;
2023-11-23 21:47:45 +00:00
}
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;
}
}