summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-02-07 02:37:44 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-02-07 02:37:44 +0000
commita4a7ca818a23fcbceceed202aa51d254552ae33e (patch)
treebeef85a582f89d911b9e6dd9b7128dc919f2f819
parentlowpass 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
-rw-r--r--usb-i2c-sl018/limits.c46
-rw-r--r--usb-i2c-sl018/manual.c2
2 files changed, 44 insertions, 4 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;
diff --git a/usb-i2c-sl018/manual.c b/usb-i2c-sl018/manual.c
index 36f1a6c..0aab9b8 100644
--- a/usb-i2c-sl018/manual.c
+++ b/usb-i2c-sl018/manual.c
@@ -50,7 +50,7 @@ void manual_task(void)
lp_cnt += lp_cnt ? -1 : 0;
if(lp_cnt >= MANUAL_LP_MAX) {
- if(state)
+ if(!state)
eventqueue_push(btn_toggle);
last_state = state;
lp_cnt = 0;