Initial commit
[kk_librfid.git] / firmware / src / os / .svn / text-base / blinkcode.c.svn-base
blob22d4b25c37654c95ede3dca042216087c4ac7411
1 /* SAM7DFU blink code support 
2  * (C) 2006 by Harald Welte <laforge@gnumonks.org>
3  *
4  */
6 #include <os/pit.h>
7 #include <os/dbgu.h>
8 #include <os/led.h>
9 #include <os/blinkcode.h>
11 #define NUM_LEDS 2
13 enum blinkcode_state {
14         BLINKCODE_STATE_NONE,
15         BLINKCODE_STATE_SILENT,         /* period of silence at start */
16         BLINKCODE_STATE_INIT,           /* initial long light */
17         BLINKCODE_STATE_BLINK_OFF,      /* blinking out, currently off */
18         BLINKCODE_STATE_BLINK_ON,       /* blinking out, currently on */
19         BLINKCODE_STATE_DONE,
22 #define TIME_SILENT     (1*HZ)
23 #define TIME_INIT       (1*HZ)
24 #define TIME_BLINK      (HZ/4)
26 struct blinker {
27         struct timer_list timer;
28         enum blinkcode_state state;
29         int num;
30         int cur;
31         u_int8_t led;
34 static struct blinker blink_state[NUM_LEDS];
36 static void blinkcode_cb(void *data)
38         /* we got called back by the timer */
39         struct blinker *bl = data;
40         
41         DEBUGPCRF("(jiffies=%lu, data=%p, state=%u)",
42                   jiffies, data, bl->state);
43         switch (bl->state) {
44         case BLINKCODE_STATE_NONE:
45                 led_switch(bl->led, 0);
46                 bl->state = BLINKCODE_STATE_SILENT;
47                 bl->timer.expires = jiffies + TIME_SILENT;
48                 bl->cur = bl->num;
49                 break;
50         case BLINKCODE_STATE_SILENT:
51                 /* we've finished the period of silence, turn led on */
52                 led_switch(bl->led, 1);
53                 bl->state = BLINKCODE_STATE_INIT;
54                 bl->timer.expires = jiffies + TIME_INIT;
55                 break;
56         case BLINKCODE_STATE_INIT:
57                 /* we've finished the period of init */
58                 led_switch(bl->led, 0);
59                 bl->state = BLINKCODE_STATE_BLINK_OFF;
60                 bl->timer.expires = jiffies + TIME_INIT;
61                 break;
62         case BLINKCODE_STATE_BLINK_OFF:
63                 /* we've been off, turn on */
64                 led_switch(bl->led, 1);
65                 bl->state = BLINKCODE_STATE_BLINK_ON;
66                 bl->cur--;
67                 bl->timer.expires = jiffies + TIME_BLINK;
68                 if (bl->cur <= 0)
69                         bl->state = BLINKCODE_STATE_DONE;
70                 break;
71         case BLINKCODE_STATE_BLINK_ON:
72                 /* we've been on, turn off */
73                 led_switch(bl->led, 0);
74                 bl->state = BLINKCODE_STATE_BLINK_OFF;
75                 bl->timer.expires = jiffies + TIME_BLINK;
76                 break;
77         case BLINKCODE_STATE_DONE:
78                 /* we've been on, turn off */
79                 led_switch(bl->led, 0);
80                 return;
81                 break;
82         }
83         /* default case: re-add the timer */
84         timer_add(&bl->timer);
87 void blinkcode_set(int led, enum blinkcode_num num)
89         DEBUGPCRF("(jiffies=%lu, led=%u, num=%u)", jiffies, led, num);
91         if (--led > NUM_LEDS)
92                 return;
94         timer_del(&blink_state[led].timer);
96         blink_state[led].num = num;
97         blink_state[led].state = BLINKCODE_STATE_NONE;
98         blink_state[led].timer.expires = jiffies;
100         if (num != BLINKCODE_NONE)
101                 timer_add(&blink_state[led].timer);
104 void blinkcode_init(void)
106         int i;
108         for (i = 0; i < NUM_LEDS; i++) {
109                 blink_state[i].num = 0;
110                 blink_state[i].state = BLINKCODE_STATE_NONE;
111                 blink_state[i].led = i+1;
112                 blink_state[i].timer.data = &blink_state[i];
113                 blink_state[i].timer.function = &blinkcode_cb;
114         }