summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-06-30 03:14:18 (GMT)
committerChristian Pointner <equinox@spreadspace.org>2015-06-30 03:14:18 (GMT)
commite33c8dda1de044bd58d22a954aea7f10f554119d (patch)
treed015d68f832749f0218fe13de607ea999866a8b1
parent5141478f9fbf09e142daaf0aabe79a29866b39c7 (diff)
syncing led blink if frequency matches
-rw-r--r--dolmetsch-ctl/keypad.c40
1 files changed, 34 insertions, 6 deletions
diff --git a/dolmetsch-ctl/keypad.c b/dolmetsch-ctl/keypad.c
index 973ce9b..1291a6e 100644
--- a/dolmetsch-ctl/keypad.c
+++ b/dolmetsch-ctl/keypad.c
@@ -33,9 +33,10 @@ static struct {
} keypad_state[KEYPAD_NUM_KEYS];
static struct {
- uint16_t delay;
+ uint16_t timebase;
uint16_t cnt;
} led_blink_state[KEYPAD_NUM_KEYS];
+#define KEYPAD_BLINK_TIMEBASE_MULT 128
void keypad_init(void)
{
@@ -47,7 +48,7 @@ void keypad_init(void)
keypad_state[i].last_sent = 0;
keypad_state[i].lp_cnt = 0;
- led_blink_state[i].delay = 0;
+ led_blink_state[i].timebase = 0;
led_blink_state[i].cnt = 0;
}
@@ -92,15 +93,42 @@ void keypad_led_toggle(uint8_t led)
}
}
+static uint8_t keypad_led_get_state(uint8_t led)
+{
+ switch(led) {
+ case 0:
+ case 1: return (PORTD & (1 << led)) ? 0 : 1;
+ case 2:
+ case 3: return (PORTD & (1 << led)) ? 1 : 0;
+ default: return 0;
+ }
+}
+
+static void keypad_led_blink_sync(uint8_t led)
+{
+ uint8_t i;
+ for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
+ if(i != led && led_blink_state[i].timebase == led_blink_state[led].timebase) {
+ led_blink_state[led].cnt = led_blink_state[i].cnt;
+ if(keypad_led_get_state(led) != keypad_led_get_state(i))
+ keypad_led_toggle(led);
+ break;
+ }
+ }
+}
+
void keypad_led_blink(uint8_t led, uint8_t value)
{
if(led < KEYPAD_NUM_KEYS) {
- led_blink_state[led].delay = value * 128;
+ led_blink_state[led].timebase = value * KEYPAD_BLINK_TIMEBASE_MULT;
led_blink_state[led].cnt = 0;
+ if(value)
+ keypad_led_blink_sync(led);
+
} else {
uint8_t i;
for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
- led_blink_state[i].delay = value * 128;
+ led_blink_state[i].timebase = value * KEYPAD_BLINK_TIMEBASE_MULT;
led_blink_state[i].cnt = 0;
}
}
@@ -123,8 +151,8 @@ static inline void keypad_key_lowpass(uint8_t key_idx, uint8_t current_state)
static inline void keypad_led_blinking(uint8_t key_idx)
{
- if(led_blink_state[key_idx].delay > 1) {
- if(++led_blink_state[key_idx].cnt >= led_blink_state[key_idx].delay) {
+ if(led_blink_state[key_idx].timebase > 1) {
+ if(++led_blink_state[key_idx].cnt >= led_blink_state[key_idx].timebase) {
keypad_led_toggle(key_idx);
led_blink_state[key_idx].cnt = 0;
}