]> www.average.org Git - pulsecounter.git/blob - msp430/Pulsecounter-Prog.c
cherrypick blink function from the jittercount branch
[pulsecounter.git] / msp430 / Pulsecounter-Prog.c
1 #include "Pulsecounter.h"
2 #include "Hal.h"
3
4 static void gpioHandler(uint8_t id);
5 static void tickHandler(void);
6 static int32_t cold = 0;
7 static int32_t hot  = 0;
8 static bool connected = false;
9
10 void main() {
11     Hal_init();
12     Hal_gpioEnable(gpioHandler);
13     Pulsecounter_setDeviceName("PULS-CNTR");
14     Pulsecounter_start();
15     Hal_idleLoop();
16 }
17
18 static void blink(uint8_t which, uint8_t count) {
19     uint8_t i;
20     for (i = 0; i < count; i++) {
21         if (i) Hal_delay(50);
22         if (which & 1) Hal_greenLedOn();
23         if (which & 2) Hal_redLedOn();
24         Hal_delay(50);
25         if (which & 1) Hal_greenLedOff();
26         if (which & 2) Hal_redLedOff();
27     }
28 }
29
30 static void gpioHandler(uint8_t id) {
31     uint8_t i;
32
33     switch (id) {
34     case 0:
35         /* Pulsecounter_accept(true); */
36         if (connected) {
37             Pulsecounter_coldTick_indicate();
38             Hal_delay(100);
39             Pulsecounter_hotTick_indicate();
40         }
41         blink(3, 2);
42         Hal_tickStart(15000, tickHandler);
43         break;
44     case 1:
45         cold++;
46         if (connected)
47             Pulsecounter_coldTick_indicate();
48         blink(1, 2);
49         break;
50     case 2:
51         hot++;
52         if (connected)
53             Pulsecounter_hotTick_indicate();
54         blink(2, 2);
55         break;
56     default:
57         blink(3, 15);
58     }
59 }
60
61 static void tickHandler(void) {
62     uint8_t i;
63
64     Hal_tickStop();
65     if (connected)
66         return;
67     for (i = 0; i < 3; i++) {
68         Hal_greenLedOn();
69         Hal_delay(50);
70         Hal_redLedOn();
71         Hal_delay(50);
72         Hal_redLedOff();
73         Hal_delay(50);
74         Hal_greenLedOff();
75     }
76     /* Pulsecounter_accept(false); */
77 }
78
79 /* -------- SCHEMA CALLBACKS -------- */
80
81 void Pulsecounter_connectHandler(void) {
82     connected = true;
83     Hal_tickStop();
84     blink(1, 5);
85 }
86
87 void Pulsecounter_disconnectHandler(void) {
88     connected = false;
89     /* Hal_tickStart(15000, tickHandler); */
90     Hal_disconnected();
91     blink(2, 5);
92 }
93
94 void Pulsecounter_coldTick_fetch(Pulsecounter_coldTick_t* const output) {
95     *output = cold;
96 }
97
98 void Pulsecounter_hotTick_fetch(Pulsecounter_hotTick_t* const output) {
99     *output = hot;
100 }