SlatusLed & reverting to Ethernet2 wiznet lib

This commit is contained in:
2020-07-04 22:04:57 +03:00
parent 76fb07a7b2
commit 3c38e151e1
7 changed files with 357 additions and 11 deletions

View File

@@ -5,3 +5,4 @@
-DMODBUS_SERIAL_BAUD=9600
-DOTA
-DSYSLOG_ENABLE
-DWiz5500

View File

@@ -643,6 +643,259 @@ short Item::cmd2changeActivity(int lastActivity, short defaultCmd)
}
*/
int Ctrl(itemCmd cmd, int suffixCode, char* subItem)
{
/*
char stringBuffer[16];
bool operation = isNotRetainingStatus() ;
if ((!subItem || !strlen(subItem)) && strlen(defaultSubItem))
subItem = defaultSubItem; /// possible problem here with truncated default
if (!suffixCode && subItem && strlen(subItem))
suffixCode = retrieveCode(&subItem);
if (!suffixCode && defaultSuffixCode)
suffixCode = defaultSuffixCode;
debugSerial<<F("RAM=")<<freeRam()<<F(" Item=")<<itemArr->name<<F(" Sub=")<<subItem<<F(" Suff=")<<suffixCode<<F(" Cmd=")<<cmd.toCmd()<<F(" Par=")<<cmd.toString(stringBuffer, sizeof(stringBuffer));
if (!itemArr) return -1;
if (itemType != CH_GROUP )
{
//Check if subitem is some sort of command
int subitemCmd = subitem2cmd(subItem);
if (subitemCmd && subitemCmd != getCmd())
{
debugSerial<<F("Ignored, channel cmd=")<<getCmd()<<endl;
return -1;
}
}
else
// Group channel
if (! operation) return -1;
int iaddr = getArg();
bool chActive =(isActive()>0);
switch (cmd.toCmd()) {
int t;
case CMD_TOGGLE:
if (chActive) cmd.Cmd(CMD_OFF);
else cmd.Cmd(CMD_ON);
break;
case CMD_RESTORE:
if (itemType != CH_GROUP) //individual threating of channels. Ignore restore command for groups
switch (t = getCmd()) {
case CMD_HALT: //previous command was HALT ?
debugSerial << F("Restored from:") << t << endl;
if (itemType == CH_THERMO) cmd.Cmd(CMD_AUTO);
else cmd.Cmd(CMD_ON); //turning on
break;
default:
return -3;
}
break;
case CMD_XOFF:
if (itemType != CH_GROUP) //individual threating of channels. Ignore restore command for groups
switch (t = getCmd()) {
case CMD_XON: //previous command was CMD_XON ?
debugSerial << F("Turned off from:") << t << endl;
cmd.Cmd(CMD_OFF); //turning Off
break;
default:
debugSerial<<F("XOFF skipped. Prev cmd:")<<t<<endl;
return -3;
}
break;
case CMD_DN:
case CMD_UP:
{
if (itemType == CH_GROUP) break; ////bug here
if (!n || !Par[0]) Par[0] = DEFAULT_INC_STEP;
if (cmd == CMD_DN) Par[0]=-Par[0];
st.aslong = getVal();
int cType=getChanType();
debugSerial<<"from: h="<<st.h<<" s="<<st.s <<" v="<<st.v<<endl;
switch (suffixCode)
{
case S_NOTFOUND:
case S_SET:
Par[0] += st.v;
if (Par[0]>100) Par[0]=100;
if (Par[0]<0) Par[0]=0;
n=1;
cmd=CMD_NUM;
debugSerial<<" to v="<<Par[0]<<endl;
break;
case S_HUE:
case S_SAT:
if ( cType != CH_RGB && cType != CH_RGBW && cType != CH_GROUP) return 0; //HUE and SAT only applicable for RGBx channels
}
if ( cType == CH_RGB || cType == CH_RGBW)
{
bool modified = false;
switch (suffixCode)
{
case S_HSV:
Par[0] += st.h;
Par[1] += st.s;
Par[2] += st.v;
modified = true;
break;
case S_HUE:
Par[0] += st.h;
Par[1] = st.s;
Par[2] = st.v;
modified = true;
break;
case S_SAT:
Par[1] = st.s + Par[0];
Par[0] = st.h;
Par[2] = st.v;
modified = true;
break;
} //switch suffix
if (modified)
{
if (Par[0]>365 ) Par[0]=0;
if (Par[0]<0) Par[0]=365;
if (Par[1]>100) Par[1]=100;
if (Par[1]<0) Par[1]=0;
if (Par[2]>100) Par[2]=100;
if (Par[2]<0) Par[2]=0;
n=3;
cmd=CMD_NUM;
suffixCode=S_SET;
debugSerial<<"to: h="<<Par[0]<<" s="<<Par[1] <<" v="<<Par[2]<<endl;
} // if modified
} //RGBx channel
}
break;
case CMD_NUM:
//if (itemType == CH_GROUP || n!=1) break;
if (n!=1) break;
int cType=getChanType();
if ( cType == CH_RGB || cType == CH_RGBW || cType == CH_GROUP )
{
st.aslong = getVal();
st.hsv_flag=1;
switch (suffixCode)
{
case S_SAT:
st.s = Par[0];
Par[0] = st.h;
Par[1] = st.s;
Par[2] = st.v;
n=3;
setVal(st.aslong);
break;
case S_HUE:
st.h = Par[0];
Par[1] = st.s;
Par[2] = st.v;
n=3;
setVal(st.aslong);
}
//if (itemType == CH_GROUP) break;
}
else // Non-color channel
if (suffixCode == S_SAT || suffixCode == S_HUE) return -3;
}
=========
int chActive = item->isActive();
bool toExecute = (chActive>0);
long st;
if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it
switch(suffixCode)
{
case S_NOTFOUND:
// turn on and set
toExecute = true;
debugSerial<<F("Forced execution");
case S_SET:
if (!Parameters || n==0) return 0;
item->setVal(st=Parameters[0]); //Store
if (!suffixCode)
{
if (chActive>0 && !st) item->setCmd(CMD_OFF);
if (chActive==0 && st) item->setCmd(CMD_ON);
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS | SEND_DEFFERED);
if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
}
else item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
return 1;
//break;
case S_CMD:
item->setCmd(cmd);
switch (cmd)
{
case CMD_ON:
//retrive stored values
st = item->getVal();
if (st && (st<MIN_VOLUME) ) st=INIT_VOLUME; // & send
item->setVal(st);
if (st) //Stored smthng
{
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS);
debugSerial<<F("Restored: ")<<st<<endl;
}
else
{
debugSerial<<st<<F(": No stored values - default\n");
// Store
st=100;
item->setVal(st);
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS );
}
if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
return 1;
case CMD_OFF:
item->SendStatus(SEND_COMMAND);
if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
return 1;
} //switch cmd
break;
} //switch suffix
debugSerial<<F("Unknown cmd")<<endl;
return 0;
*/
}
int Item::Ctrl(short cmd, short n, int *Parameters, int suffixCode, char *subItem) {
bool send = isNotRetainingStatus() ;
if ((!subItem || !strlen(subItem)) && strlen(defaultSubItem))

View File

@@ -200,6 +200,7 @@ public:
itemCmd Int(uint32_t i);
itemCmd Cmd(uint8_t i);
char * toString(char * Buffer, int bufLen);
short toCmd();
} ;
#pragma pack(pop)
@@ -219,6 +220,7 @@ class Item
boolean Setup();
void Stop();
int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL);
int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL);
int Ctrl(char * payload, char * subItem=NULL);
int getArg(short n=0);

View File

@@ -129,7 +129,7 @@ Streamlog errorSerial(&debugSerialPort,LOG_ERROR);
Streamlog infoSerial (&debugSerialPort,LOG_INFO);
#endif
statusLED LED(ledRED);
lan_status lanStatus = INITIAL_STATE;
@@ -172,6 +172,7 @@ aJsonObject *pollingItem = NULL;
bool owReady = false;
bool configOk = false;
bool configLoaded = false;
int8_t ethernetIdleCount =0;
int8_t configLocked = 0;
@@ -258,6 +259,7 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
debugSerial<<F("OOM!");
return;
}
LED.flash(ledBLUE);
for (int i = 0; i < length; i++)
debugSerial<<((char) payload[i]);
debugSerial<<endl;
@@ -351,12 +353,13 @@ lan_status lanLoop() {
switch (lanStatus) {
case INITIAL_STATE:
LED.set(ledRED|((configLoaded)?ledBLINK:0));
if (millis() > nextLanCheckTime)
onInitialStateInitLAN();
break;
case HAVE_IP_ADDRESS:
LED.set(ledRED|ledGREEN|((configLoaded)?ledBLINK:0));
if (configLocked) return HAVE_IP_ADDRESS;
if (!configOk)
lanStatus = loadConfigFromHttp(0, NULL);
@@ -365,9 +368,7 @@ lan_status lanLoop() {
case IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER:
wdt_res();
LED.set(ledRED|ledGREEN|((configLoaded)?ledBLINK:0));
ip_ready_config_loaded_connecting_to_broker();
break;
@@ -387,10 +388,12 @@ lan_status lanLoop() {
}
case OPERATION:
LED.set(ledGREEN|((configLoaded)?ledBLINK:0));
if (!mqttClient.connected()) lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;//2;
break;
case AWAITING_ADDRESS:
LED.set(ledRED|((configLoaded)?ledBLINK:0));
if (millis() > nextLanCheckTime)
lanStatus = INITIAL_STATE;//0;
break;
@@ -1061,7 +1064,7 @@ configLocked++;
udpSyslogArr = aJson.getObjectItem(root, "syslog");
#endif
printConfigSummary();
configLoaded=true;
configLocked--;
}
@@ -1786,7 +1789,7 @@ void setupCmdArduino() {
}
void loop_main() {
LED.poll();
#if defined(M5STACK)
// Initialize the M5Stack object

View File

@@ -630,7 +630,64 @@ itemCmd mapInt(int32_t arg, aJsonObject* map)
return _itemCmd.Int(arg);
}
statusLED::statusLED(uint8_t pattern)
{
pinMode(pinRED, OUTPUT);
pinMode(pinGREEN, OUTPUT);
pinMode(pinBLUE, OUTPUT);
set(pattern);
timestamp=0;
}
void statusLED::show (uint8_t pattern)
{
digitalWrite(pinRED,(pattern & ledRED)?HIGH:LOW );
digitalWrite(pinGREEN,(pattern & ledGREEN)?HIGH:LOW);
digitalWrite(pinBLUE,(pattern & ledBLUE)?HIGH:LOW);
}
void statusLED::set (uint8_t pattern)
{
short newStat = pattern & ledParams;
if (newStat!=(curStat & ledParams))
{
//if (!(curStat & ledHidden))
show(pattern);
curStat=newStat | (curStat & ~ledParams);
}
}
void statusLED::flash(uint8_t pattern)
{
show(pattern);
curStat|=ledFlash;
}
void statusLED::poll()
{
if (curStat & ledFlash)
{
curStat&=~ledFlash;
show(curStat);
}
if (millis()>timestamp)
{
if (curStat & ledFASTBLINK) timestamp=millis()+ledFastDelayms;
else timestamp=millis()+ledDelayms;
if (( curStat & ledBLINK) || (curStat & ledFASTBLINK))
{
curStat^=ledHidden;
if (curStat & ledHidden)
show(0);
else show(curStat);
}
}
}
#pragma message(VAR_NAME_VALUE(debugSerial))

View File

@@ -65,3 +65,32 @@ bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t m
bool executeCommand(aJsonObject* cmd, int8_t toggle = -1);
bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd);
itemCmd mapInt(int32_t arg, aJsonObject* map);
#define ledRED 1
#define ledGREEN 2
#define ledBLUE 4
#define ledBLINK 8
#define ledFASTBLINK 16
#define ledParams (ledRED | ledGREEN | ledBLUE | ledBLINK | ledFASTBLINK)
#define ledFlash 32
#define ledHidden 64
#define pinRED 50
#define pinGREEN 51
#define pinBLUE 52
#define ledDelayms 1000UL
#define ledFastDelayms 300UL
class statusLED {
public:
statusLED(uint8_t pattern = 0);
void set (uint8_t pattern);
void show (uint8_t pattern);
void poll();
void flash(uint8_t pattern);
private:
uint8_t curStat;
uint32_t timestamp;
};

View File

@@ -23,13 +23,13 @@ default_envs =
; mega2560-5500
; LightHub controller HW revision 2.1 and above (Wiznet 5500 CS on pin 53)
; lighthub21
lighthub21
; Arduino DUE + Ethernet shield Wiznet 5100
; due-5100
; Generic DUE
due
; due
; Arduino DUE + Ethernet shield Wiznet 5500
; due-5500
@@ -551,6 +551,7 @@ platform = atmelsam
framework = arduino
board = due
build_flags = !python get_build_flags.py lighthub21
;upload_command = arduinoOTA -address 192.168.88.59 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE
;upload_command = arduinoOTA -address 192.168.88.34 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE
;upload_protocol = custom
lib_ignore =
@@ -562,7 +563,7 @@ lib_ignore =
WifiManager
DmxSimple
httpClient
Ethernet2
Ethernet
Ethernet3
NRFFlashStorage
WebServer
@@ -580,7 +581,7 @@ lib_deps =
https://github.com/anklimov/aJson
https://github.com/anklimov/CmdArduino
https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/Ethernet
https://github.com/anklimov/Ethernet2
https://github.com/knolleary/pubsubclient.git
https://github.com/anklimov/Artnet.git
FastLED@3.3.2