mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
input pulling on HW Timer (DUE)
This commit is contained in:
@@ -526,16 +526,22 @@ if (newState == IS_REQSTATE)
|
|||||||
{
|
{
|
||||||
// Requested delayed change State and safe moment
|
// Requested delayed change State and safe moment
|
||||||
newState=store->reqState; //Retrieve requested state
|
newState=store->reqState; //Retrieve requested state
|
||||||
debugSerial<<F("Pended state retrieved:")<<newState;
|
debugSerial<<F("Pended: #")<<pin<<F(" ")<<store->state<<F("->") <<newState<<endl;
|
||||||
|
if (store->state == newState)
|
||||||
|
{
|
||||||
|
store->delayedState = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else return true; // No pended State
|
else return true; // No pended State
|
||||||
else if (store->delayedState)
|
else if (store->delayedState)
|
||||||
return false; //State changing is postponed already (( giving up
|
return false; //State changing is postponed already (( giving up
|
||||||
|
|
||||||
aJsonObject *cmd = NULL;
|
aJsonObject *cmd = NULL;
|
||||||
|
itemCmd defCmd;
|
||||||
|
|
||||||
int8_t toggle=0;
|
int8_t toggle=0;
|
||||||
if (newState!=store->state) debugSerial<<F("#")<<pin<<F(" ")<<store->state<<F("->") <<newState<<endl;
|
if (newState!=store->state && cause!=CHECK_INTERRUPT) debugSerial<<F("#")<<pin<<F(" ")<<store->state<<F("->") <<newState<<endl;
|
||||||
switch (newState)
|
switch (newState)
|
||||||
{
|
{
|
||||||
case IS_IDLE:
|
case IS_IDLE:
|
||||||
@@ -564,6 +570,7 @@ if (newState!=store->state) debugSerial<<F("#")<<pin<<F(" ")<<store->state<<F("-
|
|||||||
cmd = aJson.getObjectItem(inputObj, "scmd");
|
cmd = aJson.getObjectItem(inputObj, "scmd");
|
||||||
toggle=store->toggle1;
|
toggle=store->toggle1;
|
||||||
store->toggle1 = !store->toggle1;
|
store->toggle1 = !store->toggle1;
|
||||||
|
if (!cmd) defCmd.Cmd(CMD_ON);
|
||||||
break;
|
break;
|
||||||
case IS_PRESSED2: //scmd2
|
case IS_PRESSED2: //scmd2
|
||||||
cmd = aJson.getObjectItem(inputObj, "scmd2");
|
cmd = aJson.getObjectItem(inputObj, "scmd2");
|
||||||
@@ -580,6 +587,7 @@ if (newState!=store->state) debugSerial<<F("#")<<pin<<F(" ")<<store->state<<F("-
|
|||||||
case IS_WAITPRESS:
|
case IS_WAITPRESS:
|
||||||
case IS_RELEASED2:
|
case IS_RELEASED2:
|
||||||
cmd = aJson.getObjectItem(inputObj, "rcmd");
|
cmd = aJson.getObjectItem(inputObj, "rcmd");
|
||||||
|
if (!cmd) defCmd.Cmd(CMD_OFF);
|
||||||
// toggle=state->toggle1;
|
// toggle=state->toggle1;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -609,23 +617,31 @@ if (newState!=store->state) debugSerial<<F("#")<<pin<<F(" ")<<store->state<<F("-
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
if (!cmd)
|
|
||||||
|
aJsonObject *defaultItem = aJson.getObjectItem(inputObj, "item");
|
||||||
|
aJsonObject *defaultEmit = aJson.getObjectItem(inputObj, "emit");
|
||||||
|
|
||||||
|
if (!defaultEmit && !defaultItem) defCmd.Cmd(CMD_VOID);
|
||||||
|
|
||||||
|
if (!cmd && !defCmd.isCommand())
|
||||||
{
|
{
|
||||||
store->state=newState;
|
store->state=newState;
|
||||||
|
store->delayedState=false;
|
||||||
return true; //nothing to do
|
return true; //nothing to do
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (cause != CHECK_INTERRUPT)
|
if (cause != CHECK_INTERRUPT)
|
||||||
{
|
{
|
||||||
store->state=newState;
|
store->state=newState;
|
||||||
executeCommand(cmd,toggle);
|
|
||||||
//Executed
|
|
||||||
store->delayedState=false;
|
store->delayedState=false;
|
||||||
|
executeCommand(cmd,toggle,defCmd,defaultItem,defaultEmit);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//Postpone actual execution
|
//Postpone actual execution
|
||||||
store->reqState=store->state;
|
store->reqState=newState;
|
||||||
store->delayedState=true;
|
store->delayedState=true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -658,7 +674,8 @@ else
|
|||||||
else currentInputState = false;
|
else currentInputState = false;
|
||||||
}
|
}
|
||||||
else currentInputState = (digitalRead(pin) == inputOnLevel);
|
else currentInputState = (digitalRead(pin) == inputOnLevel);
|
||||||
switch (store->state) //Timer based transitions
|
|
||||||
|
if (cause != CHECK_INTERRUPT) switch (store->state) //Timer based transitions
|
||||||
{
|
{
|
||||||
case IS_PRESSED:
|
case IS_PRESSED:
|
||||||
if (isTimeOver(store->timestamp16,millis() & 0xFFFF,T_LONG,0xFFFF))
|
if (isTimeOver(store->timestamp16,millis() & 0xFFFF,T_LONG,0xFFFF))
|
||||||
@@ -750,18 +767,20 @@ switch (store->state) //Timer based transitions
|
|||||||
if (store->bounce) store->bounce = store->bounce - 1;
|
if (store->bounce) store->bounce = store->bounce - 1;
|
||||||
else //confirmed change
|
else //confirmed change
|
||||||
{
|
{
|
||||||
|
//if (cause == CHECK_INTERRUPT) return;
|
||||||
store->timestamp16 = millis() & 0xFFFF; //Saving timestamp of changing
|
store->timestamp16 = millis() & 0xFFFF; //Saving timestamp of changing
|
||||||
|
|
||||||
|
/*
|
||||||
if (inType & IN_PUSH_TOGGLE) { //To refactore
|
if (inType & IN_PUSH_TOGGLE) { //To refactore
|
||||||
if (currentInputState) { //react on leading edge only (change from 0 to 1)
|
if (currentInputState) { //react on leading edge only (change from 0 to 1)
|
||||||
//store->logicState = !store->logicState;
|
//store->logicState = !store->logicState;
|
||||||
store->lastValue = currentInputState;
|
store->lastValue = currentInputState;
|
||||||
onContactChanged(store->toggle1);
|
onContactChanged(store->toggle1);
|
||||||
}
|
}
|
||||||
} else
|
} else */
|
||||||
|
|
||||||
{
|
{
|
||||||
onContactChanged(currentInputState); //Legacy input - to remove later
|
// onContactChanged(currentInputState); //Legacy input - to remove later
|
||||||
|
|
||||||
bool res = true;
|
bool res = true;
|
||||||
if (currentInputState) //Button pressed state transitions
|
if (currentInputState) //Button pressed state transitions
|
||||||
@@ -775,7 +794,14 @@ switch (store->state) //Timer based transitions
|
|||||||
|
|
||||||
case IS_RELEASED:
|
case IS_RELEASED:
|
||||||
case IS_WAITPRESS:
|
case IS_WAITPRESS:
|
||||||
res = changeState(IS_PRESSED2, cause);
|
if ( //No future
|
||||||
|
!aJson.getObjectItem(inputObj, "scmd2") &&
|
||||||
|
!aJson.getObjectItem(inputObj, "lcmd2") &&
|
||||||
|
!aJson.getObjectItem(inputObj, "rpcmd2")
|
||||||
|
)
|
||||||
|
res = changeState(IS_PRESSED, cause);
|
||||||
|
|
||||||
|
else res = changeState(IS_PRESSED2, cause);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -790,6 +816,7 @@ switch (store->state) //Timer based transitions
|
|||||||
case IS_PRESSED:
|
case IS_PRESSED:
|
||||||
|
|
||||||
res = changeState(IS_RELEASED, cause);
|
res = changeState(IS_RELEASED, cause);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IS_LONG:
|
case IS_LONG:
|
||||||
@@ -799,7 +826,12 @@ switch (store->state) //Timer based transitions
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case IS_PRESSED2:
|
case IS_PRESSED2:
|
||||||
res = changeState(IS_RELEASED2, cause);
|
if ( //No future
|
||||||
|
!aJson.getObjectItem(inputObj, "scmd2") &&
|
||||||
|
!aJson.getObjectItem(inputObj, "lcmd2") &&
|
||||||
|
!aJson.getObjectItem(inputObj, "rpcmd2")
|
||||||
|
) res = changeState(IS_IDLE, cause);
|
||||||
|
else res = changeState(IS_RELEASED2, cause);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IS_LONG2:
|
case IS_LONG2:
|
||||||
@@ -826,6 +858,7 @@ switch (store->state) //Timer based transitions
|
|||||||
void Input::analogPoll(short cause) {
|
void Input::analogPoll(short cause) {
|
||||||
int16_t inputVal;
|
int16_t inputVal;
|
||||||
int32_t mappedInputVal; // 10x inputVal
|
int32_t mappedInputVal; // 10x inputVal
|
||||||
|
if (cause == CHECK_INTERRUPT) return;
|
||||||
aJsonObject *inputMap = aJson.getObjectItem(inputObj, "map");
|
aJsonObject *inputMap = aJson.getObjectItem(inputObj, "map");
|
||||||
int16_t Noize = ANALOG_NOIZE;
|
int16_t Noize = ANALOG_NOIZE;
|
||||||
short simple = 0;
|
short simple = 0;
|
||||||
@@ -879,7 +912,7 @@ void Input::analogPoll(short cause) {
|
|||||||
if (simple) {
|
if (simple) {
|
||||||
if (mappedInputVal != store->currentValue)
|
if (mappedInputVal != store->currentValue)
|
||||||
{
|
{
|
||||||
onContactChanged(mappedInputVal);
|
onContactChanged(mappedInputVal);
|
||||||
store->currentValue = mappedInputVal;
|
store->currentValue = mappedInputVal;
|
||||||
}}
|
}}
|
||||||
else
|
else
|
||||||
@@ -892,7 +925,7 @@ void Input::analogPoll(short cause) {
|
|||||||
|
|
||||||
if ((store->bounce<ANALOG_STATE_ATTEMPTS-1 || mappedInputVal == min || mappedInputVal ==max )&& (inputVal != store->currentValue))//confirmed change
|
if ((store->bounce<ANALOG_STATE_ATTEMPTS-1 || mappedInputVal == min || mappedInputVal ==max )&& (inputVal != store->currentValue))//confirmed change
|
||||||
{
|
{
|
||||||
onAnalogChanged(itemCmd().Tens(mappedInputVal));
|
onAnalogChanged(itemCmd().Tens(mappedInputVal));
|
||||||
// store->currentValue = mappedInputVal;
|
// store->currentValue = mappedInputVal;
|
||||||
store->currentValue = inputVal;
|
store->currentValue = inputVal;
|
||||||
}
|
}
|
||||||
@@ -954,6 +987,7 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Input::onAnalogChanged(itemCmd newValue) {
|
void Input::onAnalogChanged(itemCmd newValue) {
|
||||||
debugSerial << F("IN:") << (pin) << F("="); newValue.debugOut();
|
debugSerial << F("IN:") << (pin) << F("="); newValue.debugOut();
|
||||||
|
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ while (configLocked && !isTimeOver(stamp,millis(),10000))
|
|||||||
#endif
|
#endif
|
||||||
if (isNotRetainingStatus()) pollingLoop();
|
if (isNotRetainingStatus()) pollingLoop();
|
||||||
thermoLoop();
|
thermoLoop();
|
||||||
inputLoop();
|
inputLoop(CHECK_INPUT);
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -623,17 +623,18 @@ lan_status lanLoop() {
|
|||||||
lanStatus = DO_REINIT;
|
lanStatus = DO_REINIT;
|
||||||
break;
|
break;
|
||||||
case DHCP_CHECK_RENEW_FAIL:
|
case DHCP_CHECK_RENEW_FAIL:
|
||||||
errorSerial<<F("Error: renewed fail");
|
errorSerial<<F("Error: renewed fail")<<endl;
|
||||||
lanStatus = DO_REINIT;
|
lanStatus = DO_REINIT;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_CHECK_RENEW_OK:
|
case DHCP_CHECK_RENEW_OK:
|
||||||
infoSerial<<F("Renewed success. IP address:");
|
infoSerial<<F("Renewed success. IP address:");
|
||||||
printIPAddress(Ethernet.localIP());
|
printIPAddress(Ethernet.localIP());
|
||||||
|
infoSerial<<endl;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_CHECK_REBIND_FAIL:
|
case DHCP_CHECK_REBIND_FAIL:
|
||||||
errorSerial<<F("Error: rebind fail");
|
errorSerial<<F("Error: rebind fail")<<endl;
|
||||||
if (mqttClient.connected()) mqttClient.disconnect();
|
if (mqttClient.connected()) mqttClient.disconnect();
|
||||||
//timerLanCheckTime = millis();// + 1000;
|
//timerLanCheckTime = millis();// + 1000;
|
||||||
lanStatus = DO_REINIT;
|
lanStatus = DO_REINIT;
|
||||||
@@ -642,12 +643,14 @@ lan_status lanLoop() {
|
|||||||
case DHCP_CHECK_REBIND_OK:
|
case DHCP_CHECK_REBIND_OK:
|
||||||
infoSerial<<F("Rebind success. IP address:");
|
infoSerial<<F("Rebind success. IP address:");
|
||||||
printIPAddress(Ethernet.localIP());
|
printIPAddress(Ethernet.localIP());
|
||||||
|
infoSerial<<endl;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
wdt_en();
|
wdt_en();
|
||||||
#endif
|
#endif
|
||||||
@@ -1975,7 +1978,7 @@ void loop_main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
yield();
|
yield();
|
||||||
inputLoop();
|
inputLoop(CHECK_INPUT);
|
||||||
|
|
||||||
#if defined (_espdmx)
|
#if defined (_espdmx)
|
||||||
yield();
|
yield();
|
||||||
@@ -2009,7 +2012,7 @@ void ethernetIdle(void){
|
|||||||
ethernetIdleCount++;
|
ethernetIdleCount++;
|
||||||
wdt_res();
|
wdt_res();
|
||||||
yield();
|
yield();
|
||||||
inputLoop();
|
inputLoop(CHECK_INPUT);
|
||||||
ethernetIdleCount--;
|
ethernetIdleCount--;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2025,7 +2028,7 @@ void modbusIdle(void) {
|
|||||||
if (artnet) artnet->read();
|
if (artnet) artnet->read();
|
||||||
#endif
|
#endif
|
||||||
yield();
|
yield();
|
||||||
inputLoop();
|
inputLoop(CHECK_INPUT);
|
||||||
}
|
}
|
||||||
#ifdef MDNS_ENABLE
|
#ifdef MDNS_ENABLE
|
||||||
mdns.run();
|
mdns.run();
|
||||||
@@ -2040,7 +2043,7 @@ void modbusIdle(void) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void inputLoop(void) {
|
void inputLoop(short cause) {
|
||||||
if (!inputs) return;
|
if (!inputs) return;
|
||||||
|
|
||||||
configLocked++;
|
configLocked++;
|
||||||
@@ -2060,9 +2063,9 @@ configLocked++;
|
|||||||
while(inputObj)
|
while(inputObj)
|
||||||
{
|
{
|
||||||
Input in(inputObj,input);
|
Input in(inputObj,input);
|
||||||
in.Poll(CHECK_INPUT);
|
in.Poll(cause);
|
||||||
|
|
||||||
yield();
|
//yield();
|
||||||
inputObj = inputObj->next;
|
inputObj = inputObj->next;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2070,18 +2073,18 @@ configLocked++;
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Input in(input);
|
Input in(input);
|
||||||
in.Poll(CHECK_INPUT);
|
in.Poll(cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
yield();
|
//yield();
|
||||||
input = input->next;
|
input = input->next;
|
||||||
}
|
}
|
||||||
timerInputCheck = millis();// + INTERVAL_CHECK_INPUT;
|
if (cause != CHECK_INTERRUPT) timerInputCheck = millis();// + INTERVAL_CHECK_INPUT;
|
||||||
inCache.invalidateInputCache();
|
inCache.invalidateInputCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
//if (millis() > timerSensorCheck)
|
//if (millis() > timerSensorCheck)
|
||||||
if (isTimeOver(timerSensorCheck,millis(),INTERVAL_CHECK_SENSOR))
|
if (cause != CHECK_INTERRUPT && isTimeOver(timerSensorCheck,millis(),INTERVAL_CHECK_SENSOR))
|
||||||
{
|
{
|
||||||
aJsonObject *input = inputs->child;
|
aJsonObject *input = inputs->child;
|
||||||
while (input) {
|
while (input) {
|
||||||
@@ -2096,6 +2099,29 @@ configLocked++;
|
|||||||
}
|
}
|
||||||
configLocked--;
|
configLocked--;
|
||||||
}
|
}
|
||||||
|
volatile unsigned long timerCount=0;
|
||||||
|
|
||||||
|
void TimerHandler(void)
|
||||||
|
{
|
||||||
|
timerCount=millis();
|
||||||
|
inputLoop(CHECK_INTERRUPT);
|
||||||
|
timerCount=millis()-timerCount;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TIMER_INTERVAL_MS 200 // 0.1s = 100ms
|
||||||
|
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
int16_t attachTimer(double microseconds, timerCallback callback, const char* TimerName)
|
||||||
|
{
|
||||||
|
int16_t timerNumber=-1;
|
||||||
|
DueTimerInterrupt dueTimerInterrupt = DueTimer.getAvailable();
|
||||||
|
dueTimerInterrupt.attachInterruptInterval(microseconds, callback);
|
||||||
|
timerNumber = dueTimerInterrupt.getTimerNumber();
|
||||||
|
debugSerial<<TimerName<<F(" attached to Timer(")<<timerNumber<<F(")")<<endl;
|
||||||
|
return timerNumber;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void inputSetup(void) {
|
void inputSetup(void) {
|
||||||
if (!inputs) return;
|
if (!inputs) return;
|
||||||
@@ -2109,6 +2135,10 @@ configLocked++;
|
|||||||
yield();
|
yield();
|
||||||
input = input->next;
|
input = input->next;
|
||||||
}
|
}
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
// Interval in microsecs
|
||||||
|
attachTimer(TIMER_INTERVAL_MS * 1000, TimerHandler, "ITimer");
|
||||||
|
#endif
|
||||||
configLocked--;
|
configLocked--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2230,8 +2260,9 @@ void thermoLoop(void) {
|
|||||||
publishStat();
|
publishStat();
|
||||||
#ifndef DISABLE_FREERAM_PRINT
|
#ifndef DISABLE_FREERAM_PRINT
|
||||||
(thermostatCheckPrinted) ? debugSerial<<F("\nRAM=")<<freeRam()<<F(" Locks:")<<configLocked: debugSerial<<F(" ")<<freeRam()<<F(" Locks:")<<configLocked;
|
(thermostatCheckPrinted) ? debugSerial<<F("\nRAM=")<<freeRam()<<F(" Locks:")<<configLocked: debugSerial<<F(" ")<<freeRam()<<F(" Locks:")<<configLocked;
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
debugSerial<<F("Timer:")<<timerCount<<endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
//#include <DueFlashStorage.h>
|
//#include <DueFlashStorage.h>
|
||||||
#include <watchdog.h>
|
#include <watchdog.h>
|
||||||
#include <ArduinoHttpClient.h>
|
#include <ArduinoHttpClient.h>
|
||||||
|
#include "TimerInterrupt_Generic.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_AVR)
|
#if defined(ARDUINO_ARCH_AVR)
|
||||||
@@ -285,7 +286,7 @@ void owIdle(void);
|
|||||||
|
|
||||||
void modbusIdle(void);
|
void modbusIdle(void);
|
||||||
|
|
||||||
void inputLoop(void);
|
void inputLoop(short);
|
||||||
|
|
||||||
void inputSetup(void);
|
void inputSetup(void);
|
||||||
|
|
||||||
|
|||||||
@@ -417,7 +417,7 @@ void printIPAddress(IPAddress ipAddress) {
|
|||||||
#ifdef WITH_PRINTEX_LIB
|
#ifdef WITH_PRINTEX_LIB
|
||||||
(i < 3) ? debugSerial << (ipAddress[i]) << F(".") : debugSerial << (ipAddress[i])<<F(", ");
|
(i < 3) ? debugSerial << (ipAddress[i]) << F(".") : debugSerial << (ipAddress[i])<<F(", ");
|
||||||
#else
|
#else
|
||||||
(i < 3) ? debugSerial << _DEC(ipAddress[i]) << F(".") : debugSerial << _DEC(ipAddress[i]) << F(", ");
|
(i < 3) ? debugSerial << _DEC(ipAddress[i]) << F(".") : debugSerial << _DEC(ipAddress[i]) << F(" ");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,106 +565,123 @@ bool executeCommand(aJsonObject* cmd, int8_t toggle)
|
|||||||
return executeCommand(cmd,toggle,itemCmd());
|
return executeCommand(cmd,toggle,itemCmd());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd)
|
bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd, aJsonObject* defaultItem, aJsonObject* defaultEmit)
|
||||||
//bool executeCommand(aJsonObject* cmd, int8_t toggle, char* defCmd)
|
//bool executeCommand(aJsonObject* cmd, int8_t toggle, char* defCmd)
|
||||||
{
|
{
|
||||||
if (!cmd) return 0;
|
//char * legacyString =NULL;
|
||||||
switch (cmd->type)
|
aJsonObject *item = NULL;
|
||||||
|
aJsonObject *emit = NULL;
|
||||||
|
aJsonObject *icmd = NULL;
|
||||||
|
aJsonObject *ecmd = NULL;
|
||||||
|
char cmdType = 0;
|
||||||
|
|
||||||
|
if (cmd) cmdType = cmd->type;
|
||||||
|
|
||||||
|
switch (cmdType)
|
||||||
{
|
{
|
||||||
case aJson_String: //legacy - no action
|
|
||||||
break;
|
|
||||||
case aJson_Array: //array - recursive iterate
|
case aJson_Array: //array - recursive iterate
|
||||||
{
|
{
|
||||||
configLocked++;
|
configLocked++;
|
||||||
aJsonObject * command = cmd->child;
|
aJsonObject * command = cmd->child;
|
||||||
while (command)
|
while (command)
|
||||||
{
|
{
|
||||||
executeCommand(command,toggle,_itemCmd);
|
executeCommand(command,toggle,_itemCmd,defaultItem,defaultEmit);
|
||||||
command = command->next;
|
command = command->next;
|
||||||
}
|
}
|
||||||
configLocked--;
|
configLocked--;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case aJson_Object:
|
|
||||||
{
|
|
||||||
aJsonObject *act = aJson.getObjectItem(cmd, "act");
|
|
||||||
if (act) return executeCommand(act,toggle,_itemCmd);
|
|
||||||
|
|
||||||
aJsonObject *item = aJson.getObjectItem(cmd, "item");
|
case aJson_Object: //Modern way
|
||||||
aJsonObject *emit = aJson.getObjectItem(cmd, "emit");
|
{
|
||||||
aJsonObject *icmd = NULL;
|
aJsonObject *act = aJson.getObjectItem(cmd, "act");
|
||||||
aJsonObject *ecmd = NULL;
|
if (act) return executeCommand(act,toggle,_itemCmd);
|
||||||
|
|
||||||
switch (toggle)
|
item = aJson.getObjectItem(cmd, "item");
|
||||||
{
|
emit = aJson.getObjectItem(cmd, "emit");
|
||||||
case 0:
|
|
||||||
icmd = aJson.getObjectItem(cmd, "icmd");
|
|
||||||
ecmd = aJson.getObjectItem(cmd, "ecmd");
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
icmd = aJson.getObjectItem(cmd, "irev");
|
|
||||||
ecmd = aJson.getObjectItem(cmd, "erev");
|
|
||||||
//no *rev parameters - fallback
|
|
||||||
if (!icmd) icmd = aJson.getObjectItem(cmd, "icmd");
|
|
||||||
if (!ecmd) ecmd = aJson.getObjectItem(cmd, "ecmd");
|
|
||||||
}
|
|
||||||
|
|
||||||
char * itemCommand = NULL;
|
|
||||||
char Buffer[16];
|
|
||||||
if(icmd && icmd->type == aJson_String) itemCommand = icmd->valuestring;
|
|
||||||
//else itemCommand = _itemCmd.toString(Buffer,sizeof(Buffer));
|
|
||||||
|
|
||||||
char * emitCommand;
|
|
||||||
if(ecmd && ecmd->type == aJson_String) emitCommand = ecmd->valuestring;
|
|
||||||
else emitCommand = _itemCmd.toString(Buffer,sizeof(Buffer));
|
|
||||||
|
|
||||||
//debugSerial << F("IN:") << (pin) << F(" : ") <<endl;
|
|
||||||
if (item) {
|
|
||||||
if (itemCommand)
|
|
||||||
debugSerial << item->valuestring<< F(" -> ")<<itemCommand<<endl;
|
|
||||||
else debugSerial << item->valuestring<< F(" -> ");_itemCmd.debugOut();
|
|
||||||
}
|
|
||||||
if (emit) debugSerial << emit->valuestring<< F(" -> ")<<emitCommand<<endl;
|
|
||||||
|
|
||||||
|
|
||||||
|
switch (toggle)
|
||||||
|
{
|
||||||
if (emit && emitCommand && emit->type == aJson_String) {
|
case 0:
|
||||||
/*
|
icmd = aJson.getObjectItem(cmd, "icmd");
|
||||||
TODO implement
|
ecmd = aJson.getObjectItem(cmd, "ecmd");
|
||||||
#ifdef WITH_DOMOTICZ
|
break;
|
||||||
if (getIdxField())
|
case 1:
|
||||||
{ (newValue) ? publishDataToDomoticz(0, emit, "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"On\"}",
|
icmd = aJson.getObjectItem(cmd, "irev");
|
||||||
: publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField()); getIdxField())
|
ecmd = aJson.getObjectItem(cmd, "erev");
|
||||||
: publishDataToDomoticz(0, emit,
|
//no *rev parameters - fallback
|
||||||
"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",
|
if (!icmd) icmd = aJson.getObjectItem(cmd, "icmd");
|
||||||
getIdxField());
|
if (!ecmd) ecmd = aJson.getObjectItem(cmd, "ecmd");
|
||||||
} else
|
}
|
||||||
#endif
|
}
|
||||||
*/
|
//Continue
|
||||||
|
case aJson_String: //legacy
|
||||||
|
if (!icmd) icmd=cmd;
|
||||||
char addrstr[MQTT_TOPIC_LENGTH];
|
if (!ecmd) ecmd=cmd;
|
||||||
strncpy(addrstr,emit->valuestring,sizeof(addrstr));
|
//Continue
|
||||||
if (mqttClient.connected() && !ethernetIdleCount)
|
case 0: // NULL command object
|
||||||
{
|
{
|
||||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
if (!item) item = defaultItem;
|
||||||
mqttClient.publish(addrstr, emitCommand , true);
|
if (!emit) emit = defaultEmit;
|
||||||
}
|
|
||||||
|
char * itemCommand = NULL;
|
||||||
} // emit
|
char Buffer[16];
|
||||||
|
if(icmd && icmd->type == aJson_String) itemCommand = icmd->valuestring;
|
||||||
if (item && item->type == aJson_String) {
|
//else itemCommand = _itemCmd.toString(Buffer,sizeof(Buffer));
|
||||||
//debugSerial <<F("Controlled item:")<< item->valuestring <<endl;
|
|
||||||
Item it(item->valuestring);
|
char * emitCommand;
|
||||||
if (it.isValid())
|
if(ecmd && ecmd->type == aJson_String) emitCommand = ecmd->valuestring;
|
||||||
{
|
else emitCommand = _itemCmd.toString(Buffer,sizeof(Buffer));
|
||||||
if (itemCommand) it.Ctrl(itemCommand);
|
|
||||||
else it.Ctrl(_itemCmd);
|
//debugSerial << F("IN:") << (pin) << F(" : ") <<endl;
|
||||||
}
|
if (item) {
|
||||||
|
if (itemCommand)
|
||||||
|
debugSerial << F("Item: ")<< item->valuestring<< F(" -> ")<<itemCommand<<endl;
|
||||||
|
else debugSerial << F("ItemCmd: ")<<item->valuestring<< F(" -> ");_itemCmd.debugOut();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (emit && emitCommand && emit->type == aJson_String) {
|
||||||
|
debugSerial << F("Emit: ")<<emit->valuestring<< F(" -> ")<<emitCommand<<endl;
|
||||||
|
/*
|
||||||
|
TODO implement
|
||||||
|
#ifdef WITH_DOMOTICZ
|
||||||
|
if (getIdxField())
|
||||||
|
{ (newValue) ? publishDataToDomoticz(0, emit, "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"On\"}",
|
||||||
|
: publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField()); getIdxField())
|
||||||
|
: publishDataToDomoticz(0, emit,
|
||||||
|
"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",
|
||||||
|
getIdxField());
|
||||||
|
} else
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
char addrstr[MQTT_TOPIC_LENGTH];
|
||||||
|
strncpy(addrstr,emit->valuestring,sizeof(addrstr));
|
||||||
|
if (mqttClient.connected() && !ethernetIdleCount)
|
||||||
|
{
|
||||||
|
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||||
|
mqttClient.publish(addrstr, emitCommand , true);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // emit
|
||||||
|
|
||||||
|
if (item && item->type == aJson_String) {
|
||||||
|
//debugSerial <<F("Controlled item:")<< item->valuestring <<endl;
|
||||||
|
Item it(item->valuestring);
|
||||||
|
if (it.isValid())
|
||||||
|
{
|
||||||
|
if (itemCommand) it.Ctrl(itemCommand);
|
||||||
|
else it.Ctrl(_itemCmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
} //switch type
|
} //switch type
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ void softRebootFunc();
|
|||||||
bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo = 0xFFFFFFFF);
|
bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo = 0xFFFFFFFF);
|
||||||
//bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
|
//bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
|
||||||
bool executeCommand(aJsonObject* cmd, int8_t toggle = -1);
|
bool executeCommand(aJsonObject* cmd, int8_t toggle = -1);
|
||||||
bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd);
|
bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd, aJsonObject* defaultItem=NULL, aJsonObject* defaultEmit=NULL);
|
||||||
itemCmd mapInt(int32_t arg, aJsonObject* map);
|
itemCmd mapInt(int32_t arg, aJsonObject* map);
|
||||||
unsigned long millisNZ(uint8_t shift=0);
|
unsigned long millisNZ(uint8_t shift=0);
|
||||||
serialParamType str2SerialParam(char * str);
|
serialParamType str2SerialParam(char * str);
|
||||||
|
|||||||
@@ -117,7 +117,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
https://github.com/arcao/Syslog.git
|
https://github.com/arcao/Syslog.git
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
;ArduinoMDNS
|
TimerInterrupt_Generic
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:m5stack]
|
[env:m5stack]
|
||||||
@@ -175,6 +176,8 @@ lib_deps =
|
|||||||
https://github.com/arcao/Syslog.git
|
https://github.com/arcao/Syslog.git
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:esp32-wifi]
|
[env:esp32-wifi]
|
||||||
@@ -237,6 +240,7 @@ lib_deps =
|
|||||||
https://github.com/arcao/Syslog.git
|
https://github.com/arcao/Syslog.git
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
@@ -290,6 +294,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:due]
|
[env:due]
|
||||||
@@ -350,6 +356,7 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:mega2560slim-5100]
|
[env:mega2560slim-5100]
|
||||||
@@ -398,6 +405,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
|
|
||||||
@@ -463,6 +472,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
|
|
||||||
@@ -512,6 +523,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:esp8266-wifi]
|
[env:esp8266-wifi]
|
||||||
@@ -550,6 +563,8 @@ lib_ignore =
|
|||||||
UIPEthernet
|
UIPEthernet
|
||||||
EEPROM
|
EEPROM
|
||||||
M5Stack
|
M5Stack
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
|
https://github.com/anklimov/Arduino-Temperature-Control-Library.git
|
||||||
;COMMENT/UNCOMMENT next line for software 1-wire driver on/off
|
;COMMENT/UNCOMMENT next line for software 1-wire driver on/off
|
||||||
@@ -627,6 +642,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:due-5500]
|
[env:due-5500]
|
||||||
@@ -680,6 +697,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:lighthub21]
|
[env:lighthub21]
|
||||||
@@ -737,6 +756,8 @@ lib_deps =
|
|||||||
Adafruit BusIO
|
Adafruit BusIO
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:controllino]
|
[env:controllino]
|
||||||
@@ -786,6 +807,8 @@ lib_deps =
|
|||||||
https://github.com/arcao/Syslog.git
|
https://github.com/arcao/Syslog.git
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:stm32-enc2860]
|
[env:stm32-enc2860]
|
||||||
@@ -841,6 +864,8 @@ lib_deps =
|
|||||||
SPI
|
SPI
|
||||||
br3ttb/PID@^1.2.1
|
br3ttb/PID@^1.2.1
|
||||||
ArduinoMDNS
|
ArduinoMDNS
|
||||||
|
https://github.com/khoih-prog/TimerInterrupt_Generic.git
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
||||||
; Run the following command to upload with this environment
|
; Run the following command to upload with this environment
|
||||||
|
|||||||
Reference in New Issue
Block a user