input pulling on HW Timer (DUE)

This commit is contained in:
2021-07-30 02:21:54 +03:00
parent 256ca471fc
commit 8f88df5739
6 changed files with 226 additions and 118 deletions

View File

@@ -526,16 +526,22 @@ if (newState == IS_REQSTATE)
{
// Requested delayed change State and safe moment
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 if (store->delayedState)
return false; //State changing is postponed already (( giving up
aJsonObject *cmd = NULL;
itemCmd defCmd;
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)
{
case IS_IDLE:
@@ -564,6 +570,7 @@ if (newState!=store->state) debugSerial<<F("#")<<pin<<F(" ")<<store->state<<F("-
cmd = aJson.getObjectItem(inputObj, "scmd");
toggle=store->toggle1;
store->toggle1 = !store->toggle1;
if (!cmd) defCmd.Cmd(CMD_ON);
break;
case IS_PRESSED2: //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_RELEASED2:
cmd = aJson.getObjectItem(inputObj, "rcmd");
if (!cmd) defCmd.Cmd(CMD_OFF);
// toggle=state->toggle1;
break;
@@ -609,23 +617,31 @@ if (newState!=store->state) debugSerial<<F("#")<<pin<<F(" ")<<store->state<<F("-
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->delayedState=false;
return true; //nothing to do
}
if (cause != CHECK_INTERRUPT)
{
store->state=newState;
executeCommand(cmd,toggle);
//Executed
store->delayedState=false;
executeCommand(cmd,toggle,defCmd,defaultItem,defaultEmit);
return true;
}
else
{
//Postpone actual execution
store->reqState=store->state;
store->reqState=newState;
store->delayedState=true;
return true;
}
@@ -658,7 +674,8 @@ else
else currentInputState = false;
}
else currentInputState = (digitalRead(pin) == inputOnLevel);
switch (store->state) //Timer based transitions
if (cause != CHECK_INTERRUPT) switch (store->state) //Timer based transitions
{
case IS_PRESSED:
if (isTimeOver(store->timestamp16,millis() & 0xFFFF,T_LONG,0xFFFF))
@@ -749,19 +766,21 @@ switch (store->state) //Timer based transitions
{
if (store->bounce) store->bounce = store->bounce - 1;
else //confirmed change
{
{
//if (cause == CHECK_INTERRUPT) return;
store->timestamp16 = millis() & 0xFFFF; //Saving timestamp of changing
/*
if (inType & IN_PUSH_TOGGLE) { //To refactore
if (currentInputState) { //react on leading edge only (change from 0 to 1)
//store->logicState = !store->logicState;
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;
if (currentInputState) //Button pressed state transitions
@@ -775,7 +794,14 @@ switch (store->state) //Timer based transitions
case IS_RELEASED:
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;
@@ -790,6 +816,7 @@ switch (store->state) //Timer based transitions
case IS_PRESSED:
res = changeState(IS_RELEASED, cause);
break;
case IS_LONG:
@@ -799,7 +826,12 @@ switch (store->state) //Timer based transitions
break;
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;
case IS_LONG2:
@@ -826,6 +858,7 @@ switch (store->state) //Timer based transitions
void Input::analogPoll(short cause) {
int16_t inputVal;
int32_t mappedInputVal; // 10x inputVal
if (cause == CHECK_INTERRUPT) return;
aJsonObject *inputMap = aJson.getObjectItem(inputObj, "map");
int16_t Noize = ANALOG_NOIZE;
short simple = 0;
@@ -879,7 +912,7 @@ void Input::analogPoll(short cause) {
if (simple) {
if (mappedInputVal != store->currentValue)
{
onContactChanged(mappedInputVal);
onContactChanged(mappedInputVal);
store->currentValue = mappedInputVal;
}}
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
{
onAnalogChanged(itemCmd().Tens(mappedInputVal));
onAnalogChanged(itemCmd().Tens(mappedInputVal));
// store->currentValue = mappedInputVal;
store->currentValue = inputVal;
}
@@ -954,6 +987,7 @@ if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestri
}
}
void Input::onAnalogChanged(itemCmd newValue) {
debugSerial << F("IN:") << (pin) << F("="); newValue.debugOut();