-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblink.c
167 lines (140 loc) · 2.89 KB
/
blink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "esm/hesm.h"
#include "esm/esm_timer.h"
#include "board.h"
ESM_THIS_FILE;
typedef struct {
const uint32_t delay;
const uint8_t led_num;
} blink_cfg_t;
typedef struct {
hesm_t esm;
esm_timer_t timer;
blink_cfg_t const *const cfg;
} blink_esm_t;
ESM_COMPLEX_STATE(active, top, 1);
ESM_LEAF_STATE(paused, top, 1);
ESM_LEAF_STATE(on, active, 2);
ESM_LEAF_STATE(off, active, 2);
static void esm_active_init(esm_t *const esm)
{
(void)esm;
ESM_TRANSITION(on);
}
static void esm_active_entry(esm_t *const esm)
{
(void)esm;
}
static void esm_active_exit(esm_t *const esm)
{
blink_esm_t *self = ESM_CONTAINER_OF(esm, blink_esm_t, esm);
esm_timer_rm(&self->timer);
}
static void esm_active_handle(esm_t *const esm, const esm_signal_t * const sig)
{
(void)esm;
switch(sig->type)
{
case esm_sig_button:
ESM_TRANSITION(paused);
break;
default:
ESM_TRANSITION(unhandled);
break;
}
}
static void esm_on_entry(esm_t *const esm)
{
blink_esm_t *self = ESM_CONTAINER_OF(esm, blink_esm_t, esm);
esm_signal_t sig = {
.type = esm_sig_tmout,
.sender = esm,
.receiver = esm
};
esm_timer_add(&self->timer,
self->cfg->delay>>3, &sig);
BOARD_LED_ON(self->cfg->led_num);
}
static void esm_on_exit(esm_t *const esm)
{
(void)esm;
}
static void esm_on_handle(esm_t *const esm, const esm_signal_t * const sig)
{
switch(sig->type)
{
case esm_sig_tmout:
ESM_TRANSITION(off);
break;
default:
ESM_TRANSITION(unhandled);
break;
}
}
static void esm_off_entry(esm_t *const esm)
{
blink_esm_t *self = ESM_CONTAINER_OF(esm, blink_esm_t, esm);
esm_signal_t sig = {
.type = esm_sig_tmout,
.sender = esm,
.receiver = esm
};
esm_timer_add(&self->timer,
self->cfg->delay, &sig);
BOARD_LED_OFF(self->cfg->led_num);
}
static void esm_off_exit(esm_t *const esm)
{
(void)esm;
}
static void esm_off_handle(esm_t *const esm, const esm_signal_t * const sig)
{
switch(sig->type)
{
case esm_sig_tmout:
ESM_TRANSITION(on);
break;
default:
ESM_TRANSITION(unhandled);
break;
}
}
static void esm_paused_entry(esm_t *const esm)
{
blink_esm_t *self = ESM_CONTAINER_OF(esm, blink_esm_t, esm);
BOARD_LED_OFF(self->cfg->led_num);
}
static void esm_paused_exit(esm_t *const esm)
{
(void)esm;
}
static void esm_paused_handle(esm_t *const esm, const esm_signal_t * const sig)
{
switch(sig->type)
{
case esm_sig_button:
ESM_TRANSITION(active);
break;
default:
ESM_TRANSITION(unhandled);
break;
}
}
static void esm_blink_init(esm_t *const esm)
{
ESM_TRANSITION(active);
}
static const blink_cfg_t blink1_cfg = {
.delay = 300UL,
.led_num = 0
};
ESM_COMPLEX_REGISTER(blink, blink1, esm_gr_blinkers, 2, 3, 0);
static const blink_cfg_t blink2_cfg = {
.delay = 500UL,
.led_num = 1
};
ESM_COMPLEX_REGISTER(blink, blink2, esm_gr_blinkers, 2, 3, 0);
static const blink_cfg_t blink3_cfg = {
.delay = 700UL,
.led_num = 2
};
ESM_COMPLEX_REGISTER(blink, blink3, esm_gr_blinkers, 2, 3, 0);