diff options
author | Christian Pointner <equinox@spreadspace.org> | 2013-02-07 02:37:44 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2013-02-07 02:37:44 +0000 |
commit | a4a7ca818a23fcbceceed202aa51d254552ae33e (patch) | |
tree | beef85a582f89d911b9e6dd9b7128dc919f2f819 /usb-i2c-sl018/limits.c | |
parent | lowpass filterd manual switch (diff) |
fixed manual pin polarity
limit switches now low pass filtered
git-svn-id: https://svn.spreadspace.org/avr/trunk@168 aa12f405-d877-488e-9caf-2d797e2a1cc7
Diffstat (limited to 'usb-i2c-sl018/limits.c')
-rw-r--r-- | usb-i2c-sl018/limits.c | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/usb-i2c-sl018/limits.c b/usb-i2c-sl018/limits.c index b65fe28..5807151 100644 --- a/usb-i2c-sl018/limits.c +++ b/usb-i2c-sl018/limits.c @@ -30,22 +30,62 @@ #define LIMITS_OPEN 6 #define LIMITS_CLOSE 7 +#define LIMITS_LP_MAX 255 + void init_limits(void) { LIMITS_DDR = LIMITS_DDR & ~(1<<LIMITS_OPEN | 1<<LIMITS_CLOSE); LIMITS_PORT |= (1<<LIMITS_OPEN | 1<<LIMITS_CLOSE); } +uint8_t limits_get_close(uint8_t pin) +{ + static uint8_t last_state = 0; + static uint8_t lp_cnt = 0; + + uint8_t state = pin & (1<<LIMITS_CLOSE); + if(state != last_state) + lp_cnt++; + else + lp_cnt += lp_cnt ? -1 : 0; + + if(lp_cnt >= LIMITS_LP_MAX) { + last_state = state; + lp_cnt = 0; + } + + return last_state; +} + +uint8_t limits_get_open(uint8_t pin) +{ + static uint8_t last_state = 0; + static uint8_t lp_cnt = 0; + + uint8_t state = pin & (1<<LIMITS_OPEN); + if(state != last_state) + lp_cnt++; + else + lp_cnt += lp_cnt ? -1 : 0; + + if(lp_cnt >= LIMITS_LP_MAX) { + last_state = state; + lp_cnt = 0; + } + + return last_state; +} + limits_t limits_get(void) { uint8_t tmp = LIMITS_PIN & (1<<LIMITS_OPEN | 1<<LIMITS_CLOSE); - if(!(tmp & 1<<LIMITS_OPEN)) { - if(tmp & 1<<LIMITS_CLOSE) + if(!limits_get_open(tmp)) { + if(limits_get_close(tmp)) return open; else return both; } - else if(!(tmp & 1<<LIMITS_CLOSE)) + else if(!limits_get_close(tmp)) return close; return moving; |