Switch movement detection function added

This commit is contained in:
bsongis
2012-05-06 17:39:58 +00:00
parent 9a4f9b373a
commit 1b2eda66d3
2 changed files with 33 additions and 0 deletions
+32
View File
@@ -578,6 +578,38 @@ bool getSwitch(int8_t swtch, bool nc)
return __getSwitch(swtch);
}
int8_t getMovedSwitch()
{
static uint8_t switches_states = 0;
int8_t result = 0;
for (uint8_t i=MAX_PSWITCH; i>0; i--) {
bool prev;
uint8_t mask = 0;
if (i <= 3) {
mask = (1<<(i-1));
prev = (switches_states & mask);
}
else if (i <= 6) {
prev = ((switches_states & 0b00011000) == ((i-3) << 3));
}
else {
mask = (1<<(i-2));
prev = (switches_states & mask);
}
bool next = __getSwitch(i);
if (prev != next) {
result = i;
if (mask)
switches_states ^= mask;
else
switches_states = (switches_states & 0b11100111) | ((i-3) << 3);
}
}
return result;
}
#ifdef FLIGHT_PHASES
uint8_t getFlightPhase()
{