pass handler id to userspace
[pulsecounter.git] / Pulsecounter-Prog.c
1 #include "Pulsecounter.h"
2 #include "Hal.h"
3
4 static void buttonHandler(uint8_t id);
5 static void tickHandler(void);
6 static bool connected = false;
7 static int32_t base4 = 0;
8 static int32_t base5 = 1000;
9 static int32_t event4 = 0;
10 static int32_t event5 = 0;
11
12 static bool cold = true;
13
14 void main() {
15     Hal_init();
16     Hal_buttonEnable(buttonHandler);
17     Pulsecounter_setDeviceName("PULS-CNTR");
18     Pulsecounter_start();
19     Hal_idleLoop();
20 }
21
22 static void buttonHandler(uint8_t id) {
23     uint8_t i;
24
25     cold = !cold;
26     if (cold)
27         event4++;
28     else
29         event5++;
30     if (connected) {
31         if (cold)
32             Pulsecounter_event4_indicate();
33         else
34             Pulsecounter_event5_indicate();
35     }
36     else
37         Pulsecounter_accept(true);
38     for (i = 0; i < 3; i++) {
39         Hal_greenLedOn();
40         Hal_redLedOn();
41         Hal_delay(10);
42         Hal_greenLedOff();
43         Hal_redLedOff();
44     }
45     Hal_tickStart(5000, tickHandler);
46 }
47
48 static void tickHandler(void) {
49     uint8_t i;
50
51     Hal_tickStop();
52     if (connected)
53         return;
54     for (i = 0; i < 3; i++) {
55         Hal_greenLedOn();
56         Hal_delay(50);
57         Hal_redLedOn();
58         Hal_delay(50);
59         Hal_redLedOff();
60         Hal_delay(50);
61         Hal_greenLedOff();
62     }
63     Pulsecounter_accept(false);
64 }
65
66 /* -------- SCHEMA CALLBACKS -------- */
67
68 void Pulsecounter_connectHandler(void) {
69     connected = true;
70     Hal_connected();
71     Hal_redLedOn();
72     Hal_delay(100);
73     Hal_redLedOff();
74     Hal_greenLedOn();
75     Hal_delay(100);
76     Hal_greenLedOff();
77 }
78
79 void Pulsecounter_disconnectHandler(void) {
80     connected = false;
81     Hal_greenLedOn();
82     Hal_delay(100);
83     Hal_greenLedOff();
84     Hal_redLedOn();
85     Hal_delay(100);
86     Hal_redLedOff();
87     Hal_tickStart(5000, tickHandler);
88     Hal_disconnected();
89 }
90
91 void Pulsecounter_event4_fetch(Pulsecounter_event4_t* const output) {
92     *output = base4 + event4;
93 }
94
95 void Pulsecounter_event5_fetch(Pulsecounter_event5_t* const output) {
96     *output = base5 + event5;
97 }
98
99 void Pulsecounter_base4_fetch(Pulsecounter_base4_t* const output) {
100     *output = base4;
101 }
102
103 void Pulsecounter_base4_store(Pulsecounter_base4_t* const input) {
104     base4 = *input - event4;
105 }
106
107 void Pulsecounter_base5_fetch(Pulsecounter_base5_t* const output) {
108     *output = base5;
109 }
110
111 void Pulsecounter_base5_store(Pulsecounter_base5_t* const input) {
112     base5 = *input - event5;
113 }