aboutsummaryrefslogtreecommitdiff
path: root/fw/led.c
blob: b9f15921af5f6eacd0914617d9d86877b9692dcc (plain)
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
/* Megumin LED display firmware
 * Copyright (C) 2018 Sebastian Götte <code@jaseg.net>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "global.h"
#include "led.h"

/* Status LED control */
#define LED_STRETCHING_MS 50
static volatile int error_led_timeout = 0;
static volatile int comm_led_timeout = 0;
static volatile int id_led_timeout = 0;

volatile int led_state = 0;

void trigger_error_led() {
    error_led_timeout = LED_STRETCHING_MS;
}

void trigger_comm_led() {
    comm_led_timeout = LED_STRETCHING_MS;
}

void trigger_id_led() {
    id_led_timeout = LED_STRETCHING_MS;
}

void led_task() {
    static int last_time = 0;
    /* Crude LED logic. The comm, id and error LEDs each have a timeout counter
     * that is reset to the LED_STRETCHING_MS constant on an event (either a
     * frame received correctly or some uart, framing or protocol error). These
     * timeout counters count down in milliseconds and the LEDs are set while
     * they are non-zero. This means a train of several very brief events will
     * make the LED lit permanently.
     */
    int time_now = sys_time; /* Latch sys_time here to avoid race conditions */
    if (last_time != time_now) {
        int diff = (time_now - last_time);

        error_led_timeout -= diff;
        if (error_led_timeout < 0)
            error_led_timeout = 0;

        comm_led_timeout -= diff;
        if (comm_led_timeout < 0)
            comm_led_timeout = 0;

        id_led_timeout -= diff;
        if (id_led_timeout < 0)
            id_led_timeout = 0;

        led_state = (led_state & ~7) | (!!id_led_timeout)<<2 | (!!error_led_timeout)<<1 | (!!comm_led_timeout)<<0;
        last_time = time_now;
    }
}