More switches code moved to switches.cpp
This commit is contained in:
@@ -42,6 +42,7 @@
|
||||
#define GETSWITCH_RECURSIVE_TYPE uint16_t
|
||||
#endif
|
||||
|
||||
#define CS_LAST_VALUE_INIT -32768
|
||||
volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_used = 0;
|
||||
volatile GETSWITCH_RECURSIVE_TYPE s_last_switch_value = 0;
|
||||
|
||||
@@ -595,3 +596,122 @@ void checkSwitches()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
void evalLogicalSwitchTimers() {
|
||||
for (uint8_t i=0; i<NUM_LOGICAL_SWITCH; i++) {
|
||||
LogicalSwitchData * cs = cswAddress(i);
|
||||
if (cs->func == LS_FUNC_TIMER) {
|
||||
int16_t *lastValue = &csLastValue[i];
|
||||
if (*lastValue == 0 || *lastValue == CS_LAST_VALUE_INIT) {
|
||||
*lastValue = -cswTimerValue(cs->v1);
|
||||
}
|
||||
else if (*lastValue < 0) {
|
||||
if (++(*lastValue) == 0)
|
||||
*lastValue = cswTimerValue(cs->v2);
|
||||
}
|
||||
else { // if (*lastValue > 0)
|
||||
*lastValue -= 1;
|
||||
}
|
||||
}
|
||||
else if (cs->func == LS_FUNC_STICKY) {
|
||||
PACK(typedef struct {
|
||||
uint8_t state;
|
||||
uint8_t last;
|
||||
}) cs_sticky_struct;
|
||||
cs_sticky_struct & lastValue = (cs_sticky_struct &)csLastValue[i];
|
||||
bool before = lastValue.last & 0x01;
|
||||
if (lastValue.state) {
|
||||
bool now = getSwitch(cs->v2);
|
||||
if (now != before) {
|
||||
lastValue.last ^= 1;
|
||||
if (!before) {
|
||||
lastValue.state = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
bool now = getSwitch(cs->v1);
|
||||
if (before != now) {
|
||||
lastValue.last ^= 1;
|
||||
if (!before) {
|
||||
lastValue.state = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//#if defined(CPUARM)
|
||||
else if (cs->func == LS_FUNC_STAY) {
|
||||
PACK(typedef struct {
|
||||
uint16_t state:1;
|
||||
uint16_t duration:15;
|
||||
}) cs_stay_struct;
|
||||
|
||||
cs_stay_struct & lastValue = (cs_stay_struct &)csLastValue[i];
|
||||
lastValue.state = false;
|
||||
bool state = getSwitch(cs->v1);
|
||||
if (state) {
|
||||
if (cs->v3 == 0 && lastValue.duration == cswTimerValue(cs->v2))
|
||||
lastValue.state = true;
|
||||
if (lastValue.duration < 1000)
|
||||
lastValue.duration++;
|
||||
}
|
||||
else {
|
||||
if (lastValue.duration > cswTimerValue(cs->v2) && lastValue.duration <= cswTimerValue(cs->v2+cs->v3))
|
||||
lastValue.state = true;
|
||||
lastValue.duration = 0;
|
||||
}
|
||||
}
|
||||
//#endif
|
||||
}
|
||||
}
|
||||
|
||||
LogicalSwitchData *cswAddress(uint8_t idx)
|
||||
{
|
||||
return &g_model.customSw[idx];
|
||||
}
|
||||
|
||||
uint8_t cswFamily(uint8_t func)
|
||||
{
|
||||
if (func <= LS_FUNC_ANEG)
|
||||
return LS_FAMILY_OFS;
|
||||
else if (func <= LS_FUNC_XOR)
|
||||
return LS_FAMILY_BOOL;
|
||||
#if defined(CPUARM)
|
||||
else if (func == LS_FUNC_STAY)
|
||||
return LS_FAMILY_STAY;
|
||||
#endif
|
||||
else if (func <= LS_FUNC_LESS)
|
||||
return LS_FAMILY_COMP;
|
||||
else if (func <= LS_FUNC_ADIFFEGREATER)
|
||||
return LS_FAMILY_DIFF;
|
||||
else
|
||||
return LS_FAMILY_TIMER+func-LS_FUNC_TIMER;
|
||||
}
|
||||
|
||||
int16_t cswTimerValue(delayval_t val)
|
||||
{
|
||||
return (val < -109 ? 129+val : (val < 7 ? (113+val)*5 : (53+val)*10));
|
||||
}
|
||||
|
||||
void logicalSwitchesReset() {
|
||||
for (uint8_t i=0; i<NUM_LOGICAL_SWITCH; i++) {
|
||||
csLastValue[i] = CS_LAST_VALUE_INIT;
|
||||
}
|
||||
}
|
||||
|
||||
getvalue_t convertCswTelemValue(LogicalSwitchData * cs)
|
||||
{
|
||||
getvalue_t val;
|
||||
#if defined(CPUARM)
|
||||
val = convert16bitsTelemValue(cs->v1 - MIXSRC_FIRST_TELEM + 1, cs->v2);
|
||||
#else
|
||||
if (cswFamily(cs->func)==LS_FAMILY_OFS)
|
||||
val = convert8bitsTelemValue(cs->v1 - MIXSRC_FIRST_TELEM + 1, 128+cs->v2);
|
||||
else
|
||||
val = convert8bitsTelemValue(cs->v1 - MIXSRC_FIRST_TELEM + 1, 128+cs->v2) - convert8bitsTelemValue(cs->v1 - MIXSRC_FIRST_TELEM + 1, 128);
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user