Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* LED control |
| 2 | * |
| 3 | * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de> |
| 4 | * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 19 | */ |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <string.h> |
| 22 | #include <assert.h> |
| 23 | |
| 24 | #include <osmocom/core/timer.h> |
| 25 | |
| 26 | #include "board.h" |
| 27 | #include "utils.h" |
| 28 | #include "led.h" |
| 29 | |
| 30 | #ifdef PINS_LEDS |
| 31 | static const Pin pinsLeds[] = { PINS_LEDS } ; |
| 32 | |
| 33 | static void led_set(enum led led, int on) |
| 34 | { |
| 35 | ASSERT(led < PIO_LISTSIZE(pinsLeds)); |
| 36 | |
| 37 | if (on) |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 38 | PIO_Clear(&pinsLeds[led]); |
King Kévin | 4cbdc7c | 2018-07-04 16:36:17 +0200 | [diff] [blame] | 39 | else |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 40 | PIO_Set(&pinsLeds[led]); |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* LED blinking code */ |
| 44 | |
| 45 | /* a single state in a sequence of blinking */ |
| 46 | struct blink_state { |
| 47 | /* duration of the state in ms */ |
| 48 | uint16_t duration; |
Kévin Redon | 11914d9 | 2018-06-27 16:33:01 +0200 | [diff] [blame] | 49 | /* brightness of LED during the state */ |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 50 | uint8_t on; |
| 51 | } __attribute__((packed)); |
| 52 | |
| 53 | static const struct blink_state bs_off[] = { |
| 54 | { 0, 0 } |
| 55 | }; |
| 56 | |
| 57 | static const struct blink_state bs_on[] = { |
| 58 | { 0, 1 } |
| 59 | }; |
| 60 | |
| 61 | static const struct blink_state bs_3on_5off[] = { |
| 62 | { 300, 1 }, { 500, 0 } |
| 63 | }; |
| 64 | |
| 65 | static const struct blink_state bs_3on_30off[] = { |
| 66 | { 300, 1 }, { 3000, 0 } |
| 67 | }; |
| 68 | |
| 69 | static const struct blink_state bs_3on_1off_3on_30off[] = { |
| 70 | { 300, 1 }, { 100, 0 }, { 300, 1 }, { 3000, 0 } |
| 71 | }; |
| 72 | |
| 73 | static const struct blink_state bs_3on_1off_3on_1off_3on_30off[] = { |
| 74 | { 300, 1 }, { 100, 0 }, { 300, 1 }, { 100, 0 }, { 300, 1 }, { 3000, 0 } |
| 75 | }; |
Kévin Redon | d975411 | 2018-07-08 16:29:37 +0200 | [diff] [blame] | 76 | |
Kévin Redon | 11914d9 | 2018-06-27 16:33:01 +0200 | [diff] [blame] | 77 | static const struct blink_state bs_2on_off[] = { |
| 78 | { 200, 1 }, { 0, 0 }, |
| 79 | }; |
Kévin Redon | d975411 | 2018-07-08 16:29:37 +0200 | [diff] [blame] | 80 | |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 81 | static const struct blink_state bs_200on_off[] = { |
| 82 | { 20000, 1 }, { 0, 0 }, |
| 83 | }; |
Kévin Redon | d975411 | 2018-07-08 16:29:37 +0200 | [diff] [blame] | 84 | |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 85 | static const struct blink_state bs_600on_off[] = { |
| 86 | { 60000, 1 }, { 0, 0 }, |
| 87 | }; |
| 88 | |
Kévin Redon | d975411 | 2018-07-08 16:29:37 +0200 | [diff] [blame] | 89 | static const struct blink_state bs_2off_on[] = { |
| 90 | { 200, 0 }, { 0, 1 }, |
| 91 | }; |
| 92 | |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 93 | |
| 94 | /* a blink pattern is an array of blink_states */ |
| 95 | struct blink_pattern { |
| 96 | const struct blink_state *states; |
| 97 | uint16_t size; |
| 98 | }; |
| 99 | |
| 100 | /* compiled-in default blinking patterns */ |
| 101 | static const struct blink_pattern patterns[] = { |
| 102 | [BLINK_ALWAYS_OFF] = { |
| 103 | .states = bs_off, |
| 104 | .size = ARRAY_SIZE(bs_off), |
| 105 | }, |
| 106 | [BLINK_ALWAYS_ON] = { |
| 107 | .states = bs_on, |
| 108 | .size = ARRAY_SIZE(bs_on), |
| 109 | }, |
| 110 | [BLINK_3O_5F] = { |
| 111 | .states = bs_3on_5off, |
| 112 | .size = ARRAY_SIZE(bs_3on_5off), |
| 113 | }, |
| 114 | [BLINK_3O_30F] = { |
| 115 | .states = bs_3on_30off, |
| 116 | .size = ARRAY_SIZE(bs_3on_30off), |
| 117 | }, |
| 118 | [BLINK_3O_1F_3O_30F] = { |
| 119 | .states = bs_3on_1off_3on_30off, |
| 120 | .size = ARRAY_SIZE(bs_3on_1off_3on_30off), |
| 121 | }, |
| 122 | [BLINK_3O_1F_3O_1F_3O_30F] = { |
| 123 | .states = bs_3on_1off_3on_1off_3on_30off, |
| 124 | .size = ARRAY_SIZE(bs_3on_1off_3on_1off_3on_30off), |
| 125 | }, |
Kévin Redon | 11914d9 | 2018-06-27 16:33:01 +0200 | [diff] [blame] | 126 | [BLINK_2O_F] = { |
| 127 | .states = bs_2on_off, |
| 128 | .size = ARRAY_SIZE(bs_2on_off), |
| 129 | }, |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 130 | [BLINK_200O_F] = { |
| 131 | .states = bs_200on_off, |
| 132 | .size = ARRAY_SIZE(bs_200on_off), |
| 133 | }, |
| 134 | [BLINK_600O_F] = { |
| 135 | .states = bs_600on_off, |
| 136 | .size = ARRAY_SIZE(bs_600on_off), |
| 137 | }, |
Kévin Redon | d975411 | 2018-07-08 16:29:37 +0200 | [diff] [blame] | 138 | [BLINK_2F_O] = { |
| 139 | .states = bs_2off_on, |
| 140 | .size = ARRAY_SIZE(bs_2off_on), |
| 141 | }, |
| 142 | |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | struct led_state { |
| 146 | /* which led are we handling */ |
| 147 | enum led led; |
| 148 | |
| 149 | /* timer */ |
| 150 | struct osmo_timer_list timer; |
| 151 | |
| 152 | /* pointer and size of blink array */ |
| 153 | const struct blink_pattern *pattern; |
| 154 | |
| 155 | unsigned int cur_state; |
| 156 | unsigned int illuminated; |
| 157 | |
| 158 | /* static allocated space for custom blinking pattern */ |
| 159 | struct blink_pattern pattern_cust; |
| 160 | struct blink_state blink_cust[10]; |
| 161 | }; |
| 162 | |
| 163 | static unsigned int cur_state_inc(struct led_state *ls) |
| 164 | { |
| 165 | ls->cur_state = (ls->cur_state + 1) % ls->pattern->size; |
| 166 | return ls->cur_state; |
| 167 | } |
| 168 | |
| 169 | static const struct blink_state * |
| 170 | next_blink_state(struct led_state *ls) |
| 171 | { |
| 172 | return &ls->pattern->states[cur_state_inc(ls)]; |
| 173 | } |
| 174 | |
| 175 | /* apply the next state to the LED */ |
| 176 | static void apply_blinkstate(struct led_state *ls, |
| 177 | const struct blink_state *bs) |
| 178 | { |
| 179 | led_set(ls->led, bs->on); |
| 180 | ls->illuminated = bs->on; |
| 181 | |
| 182 | /* re-schedule the timer */ |
| 183 | if (bs->duration) { |
| 184 | uint32_t us = bs->duration * 1000; |
| 185 | osmo_timer_schedule(&ls->timer, us / 1000000, us % 1000000); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static void blink_tmr_cb(void *data) |
| 190 | { |
| 191 | struct led_state *ls = data; |
| 192 | const struct blink_state *next_bs = next_blink_state(ls); |
| 193 | |
| 194 | /* apply the next state to the LED */ |
| 195 | apply_blinkstate(ls, next_bs); |
| 196 | } |
| 197 | |
| 198 | static struct led_state led_state[] = { |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 199 | [LED_RED] = { |
| 200 | .led = LED_RED, |
| 201 | .timer.cb = blink_tmr_cb, |
| 202 | .timer.data = &led_state[LED_RED], |
| 203 | }, |
Kévin Redon | 11914d9 | 2018-06-27 16:33:01 +0200 | [diff] [blame] | 204 | [LED_GREEN] = { |
| 205 | .led = LED_GREEN, |
| 206 | .timer.cb = blink_tmr_cb, |
| 207 | .timer.data = &led_state[LED_GREEN], |
| 208 | }, |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 209 | }; |
| 210 | #endif /* PINS_LEDS */ |
| 211 | |
| 212 | void led_blink(enum led led, enum led_pattern blink) |
| 213 | { |
| 214 | #ifdef PINS_LEDS |
| 215 | struct led_state *ls; |
| 216 | |
| 217 | if (led >= ARRAY_SIZE(led_state)) |
| 218 | return; |
| 219 | ls = &led_state[led]; |
| 220 | |
| 221 | /* stop previous blinking, if any */ |
| 222 | osmo_timer_del(&ls->timer); |
| 223 | led_set(led, 0); |
| 224 | ls->illuminated = 0; |
| 225 | ls->pattern = NULL; |
| 226 | ls->cur_state = 0; |
| 227 | |
| 228 | switch (blink) { |
| 229 | case BLINK_CUSTOM: |
| 230 | ls->pattern = &ls->pattern_cust; |
| 231 | break; |
| 232 | default: |
| 233 | if (blink >= ARRAY_SIZE(patterns)) |
| 234 | return; |
| 235 | ls->pattern = &patterns[blink]; |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | if (ls->pattern && ls->pattern->size > 0) |
| 240 | apply_blinkstate(ls, &ls->pattern->states[0]); |
| 241 | #endif |
| 242 | } |
| 243 | |
| 244 | enum led_pattern led_get(enum led led) |
| 245 | { |
| 246 | #ifdef PINS_LEDS |
| 247 | struct led_state *ls; |
| 248 | unsigned int i; |
| 249 | |
| 250 | if (led >= ARRAY_SIZE(led_state)) |
| 251 | return -1; |
| 252 | ls = &led_state[led]; |
| 253 | |
| 254 | if (ls->pattern == &ls->pattern_cust) |
| 255 | return BLINK_CUSTOM; |
| 256 | |
| 257 | for (i = 0; i < ARRAY_SIZE(patterns); i++) { |
| 258 | if (ls->pattern == &patterns[i]) |
| 259 | return i; |
| 260 | } |
| 261 | #endif |
| 262 | /* default case, shouldn't be reached */ |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | void led_start(void) |
| 267 | { |
| 268 | led_set(LED_GREEN, led_state[LED_GREEN].illuminated); |
| 269 | led_set(LED_RED, led_state[LED_RED].illuminated); |
| 270 | } |
| 271 | |
| 272 | void led_stop(void) |
| 273 | { |
| 274 | led_set(LED_GREEN, 0); |
| 275 | led_set(LED_RED, 0); |
| 276 | } |
| 277 | |
| 278 | void led_init(void) |
| 279 | { |
| 280 | #ifdef PINS_LEDS |
| 281 | PIO_Configure(pinsLeds, PIO_LISTSIZE(pinsLeds)); |
| 282 | led_set(LED_GREEN, 0); |
| 283 | led_set(LED_RED, 0); |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | void led_fini(void) |
| 288 | { |
| 289 | #ifdef PINS_LEDS |
| 290 | /* we don't actually need to do this, but just in case... */ |
| 291 | osmo_timer_del(&led_state[LED_RED].timer); |
| 292 | osmo_timer_del(&led_state[LED_GREEN].timer); |
| 293 | led_set(LED_GREEN, 0); |
| 294 | led_set(LED_RED, 0); |
| 295 | #endif |
| 296 | } |