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 | 11914d9 | 2018-06-27 16:33:01 +0200 | [diff] [blame] | 76 | static const struct blink_state bs_2on_off[] = { |
| 77 | { 200, 1 }, { 0, 0 }, |
| 78 | }; |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 79 | static const struct blink_state bs_200on_off[] = { |
| 80 | { 20000, 1 }, { 0, 0 }, |
| 81 | }; |
| 82 | static const struct blink_state bs_600on_off[] = { |
| 83 | { 60000, 1 }, { 0, 0 }, |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | /* a blink pattern is an array of blink_states */ |
| 88 | struct blink_pattern { |
| 89 | const struct blink_state *states; |
| 90 | uint16_t size; |
| 91 | }; |
| 92 | |
| 93 | /* compiled-in default blinking patterns */ |
| 94 | static const struct blink_pattern patterns[] = { |
| 95 | [BLINK_ALWAYS_OFF] = { |
| 96 | .states = bs_off, |
| 97 | .size = ARRAY_SIZE(bs_off), |
| 98 | }, |
| 99 | [BLINK_ALWAYS_ON] = { |
| 100 | .states = bs_on, |
| 101 | .size = ARRAY_SIZE(bs_on), |
| 102 | }, |
| 103 | [BLINK_3O_5F] = { |
| 104 | .states = bs_3on_5off, |
| 105 | .size = ARRAY_SIZE(bs_3on_5off), |
| 106 | }, |
| 107 | [BLINK_3O_30F] = { |
| 108 | .states = bs_3on_30off, |
| 109 | .size = ARRAY_SIZE(bs_3on_30off), |
| 110 | }, |
| 111 | [BLINK_3O_1F_3O_30F] = { |
| 112 | .states = bs_3on_1off_3on_30off, |
| 113 | .size = ARRAY_SIZE(bs_3on_1off_3on_30off), |
| 114 | }, |
| 115 | [BLINK_3O_1F_3O_1F_3O_30F] = { |
| 116 | .states = bs_3on_1off_3on_1off_3on_30off, |
| 117 | .size = ARRAY_SIZE(bs_3on_1off_3on_1off_3on_30off), |
| 118 | }, |
Kévin Redon | 11914d9 | 2018-06-27 16:33:01 +0200 | [diff] [blame] | 119 | [BLINK_2O_F] = { |
| 120 | .states = bs_2on_off, |
| 121 | .size = ARRAY_SIZE(bs_2on_off), |
| 122 | }, |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 123 | [BLINK_200O_F] = { |
| 124 | .states = bs_200on_off, |
| 125 | .size = ARRAY_SIZE(bs_200on_off), |
| 126 | }, |
| 127 | [BLINK_600O_F] = { |
| 128 | .states = bs_600on_off, |
| 129 | .size = ARRAY_SIZE(bs_600on_off), |
| 130 | }, |
| 131 | }; |
| 132 | |
| 133 | struct led_state { |
| 134 | /* which led are we handling */ |
| 135 | enum led led; |
| 136 | |
| 137 | /* timer */ |
| 138 | struct osmo_timer_list timer; |
| 139 | |
| 140 | /* pointer and size of blink array */ |
| 141 | const struct blink_pattern *pattern; |
| 142 | |
| 143 | unsigned int cur_state; |
| 144 | unsigned int illuminated; |
| 145 | |
| 146 | /* static allocated space for custom blinking pattern */ |
| 147 | struct blink_pattern pattern_cust; |
| 148 | struct blink_state blink_cust[10]; |
| 149 | }; |
| 150 | |
| 151 | static unsigned int cur_state_inc(struct led_state *ls) |
| 152 | { |
| 153 | ls->cur_state = (ls->cur_state + 1) % ls->pattern->size; |
| 154 | return ls->cur_state; |
| 155 | } |
| 156 | |
| 157 | static const struct blink_state * |
| 158 | next_blink_state(struct led_state *ls) |
| 159 | { |
| 160 | return &ls->pattern->states[cur_state_inc(ls)]; |
| 161 | } |
| 162 | |
| 163 | /* apply the next state to the LED */ |
| 164 | static void apply_blinkstate(struct led_state *ls, |
| 165 | const struct blink_state *bs) |
| 166 | { |
| 167 | led_set(ls->led, bs->on); |
| 168 | ls->illuminated = bs->on; |
| 169 | |
| 170 | /* re-schedule the timer */ |
| 171 | if (bs->duration) { |
| 172 | uint32_t us = bs->duration * 1000; |
| 173 | osmo_timer_schedule(&ls->timer, us / 1000000, us % 1000000); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void blink_tmr_cb(void *data) |
| 178 | { |
| 179 | struct led_state *ls = data; |
| 180 | const struct blink_state *next_bs = next_blink_state(ls); |
| 181 | |
| 182 | /* apply the next state to the LED */ |
| 183 | apply_blinkstate(ls, next_bs); |
| 184 | } |
| 185 | |
| 186 | static struct led_state led_state[] = { |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 187 | [LED_RED] = { |
| 188 | .led = LED_RED, |
| 189 | .timer.cb = blink_tmr_cb, |
| 190 | .timer.data = &led_state[LED_RED], |
| 191 | }, |
Kévin Redon | 11914d9 | 2018-06-27 16:33:01 +0200 | [diff] [blame] | 192 | [LED_GREEN] = { |
| 193 | .led = LED_GREEN, |
| 194 | .timer.cb = blink_tmr_cb, |
| 195 | .timer.data = &led_state[LED_GREEN], |
| 196 | }, |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 197 | }; |
| 198 | #endif /* PINS_LEDS */ |
| 199 | |
| 200 | void led_blink(enum led led, enum led_pattern blink) |
| 201 | { |
| 202 | #ifdef PINS_LEDS |
| 203 | struct led_state *ls; |
| 204 | |
| 205 | if (led >= ARRAY_SIZE(led_state)) |
| 206 | return; |
| 207 | ls = &led_state[led]; |
| 208 | |
| 209 | /* stop previous blinking, if any */ |
| 210 | osmo_timer_del(&ls->timer); |
| 211 | led_set(led, 0); |
| 212 | ls->illuminated = 0; |
| 213 | ls->pattern = NULL; |
| 214 | ls->cur_state = 0; |
| 215 | |
| 216 | switch (blink) { |
| 217 | case BLINK_CUSTOM: |
| 218 | ls->pattern = &ls->pattern_cust; |
| 219 | break; |
| 220 | default: |
| 221 | if (blink >= ARRAY_SIZE(patterns)) |
| 222 | return; |
| 223 | ls->pattern = &patterns[blink]; |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | if (ls->pattern && ls->pattern->size > 0) |
| 228 | apply_blinkstate(ls, &ls->pattern->states[0]); |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | enum led_pattern led_get(enum led led) |
| 233 | { |
| 234 | #ifdef PINS_LEDS |
| 235 | struct led_state *ls; |
| 236 | unsigned int i; |
| 237 | |
| 238 | if (led >= ARRAY_SIZE(led_state)) |
| 239 | return -1; |
| 240 | ls = &led_state[led]; |
| 241 | |
| 242 | if (ls->pattern == &ls->pattern_cust) |
| 243 | return BLINK_CUSTOM; |
| 244 | |
| 245 | for (i = 0; i < ARRAY_SIZE(patterns); i++) { |
| 246 | if (ls->pattern == &patterns[i]) |
| 247 | return i; |
| 248 | } |
| 249 | #endif |
| 250 | /* default case, shouldn't be reached */ |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | void led_start(void) |
| 255 | { |
| 256 | led_set(LED_GREEN, led_state[LED_GREEN].illuminated); |
| 257 | led_set(LED_RED, led_state[LED_RED].illuminated); |
| 258 | } |
| 259 | |
| 260 | void led_stop(void) |
| 261 | { |
| 262 | led_set(LED_GREEN, 0); |
| 263 | led_set(LED_RED, 0); |
| 264 | } |
| 265 | |
| 266 | void led_init(void) |
| 267 | { |
| 268 | #ifdef PINS_LEDS |
| 269 | PIO_Configure(pinsLeds, PIO_LISTSIZE(pinsLeds)); |
| 270 | led_set(LED_GREEN, 0); |
| 271 | led_set(LED_RED, 0); |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | void led_fini(void) |
| 276 | { |
| 277 | #ifdef PINS_LEDS |
| 278 | /* we don't actually need to do this, but just in case... */ |
| 279 | osmo_timer_del(&led_state[LED_RED].timer); |
| 280 | osmo_timer_del(&led_state[LED_GREEN].timer); |
| 281 | led_set(LED_GREEN, 0); |
| 282 | led_set(LED_RED, 0); |
| 283 | #endif |
| 284 | } |