2019-01-12 07:12:51 -07:00
|
|
|
#include "LED.h"
|
|
|
|
#include "util/time.h"
|
2019-02-08 07:48:13 -07:00
|
|
|
#include "util/Config.h"
|
2019-01-12 07:12:51 -07:00
|
|
|
|
2019-02-07 10:36:40 -07:00
|
|
|
bool LED_softblock_enabled = false;
|
|
|
|
|
2019-02-08 07:48:13 -07:00
|
|
|
//uint8_t ledIntensity = CONFIG_LED_INTENSITY;
|
2019-01-12 08:30:26 -07:00
|
|
|
ticks_t led_status_ticks_top = 0;
|
|
|
|
ticks_t led_status_ticks = 0;
|
|
|
|
ticks_t com_led_timeout = 0;
|
2019-01-12 07:12:51 -07:00
|
|
|
|
|
|
|
void LED_init(void) {
|
2019-01-12 08:30:26 -07:00
|
|
|
led_status_ticks_top = ms_to_ticks(CONFIG_LED_UPDATE_INTERVAL_MS);
|
|
|
|
|
2019-01-12 07:12:51 -07:00
|
|
|
// Enable output for LED pins and drain pin
|
|
|
|
LED_DDR |= _BV(0) | // RX
|
|
|
|
_BV(1) | // TX
|
|
|
|
_BV(2) | // Status
|
|
|
|
_BV(3) | // PWM drain
|
|
|
|
_BV(4); // COM
|
|
|
|
|
|
|
|
LED_PORT &= 0b11100000;
|
|
|
|
|
|
|
|
TCCR0A = _BV(WGM00) |
|
|
|
|
_BV(WGM01) |
|
|
|
|
_BV(COM0A1)|
|
|
|
|
_BV(COM0A0);
|
|
|
|
|
|
|
|
TCCR0B = _BV(CS00);
|
|
|
|
|
2019-02-08 07:48:13 -07:00
|
|
|
OCR0A = config_led_intensity;
|
2019-01-12 07:12:51 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 10:36:40 -07:00
|
|
|
void LED_softblock_on(void) {
|
|
|
|
LED_softblock_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LED_softblock_off(void) {
|
|
|
|
LED_softblock_enabled = false;
|
|
|
|
}
|
2019-01-12 07:12:51 -07:00
|
|
|
|
|
|
|
void LED_setIntensity(uint8_t value) {
|
2019-02-08 07:48:13 -07:00
|
|
|
config_led_intensity = value;
|
|
|
|
OCR0A = config_led_intensity;
|
2019-01-12 07:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void LED_COM_ON(void) {
|
2019-02-07 10:36:40 -07:00
|
|
|
if (!LED_softblock_enabled) {
|
|
|
|
LED_PORT |= _BV(4);
|
|
|
|
com_led_timeout = timer_clock() + ms_to_ticks(CONFIG_COM_LED_TIMEOUT_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LED_COM_OFF(void) {
|
|
|
|
if (!LED_softblock_enabled) LED_PORT &= ~_BV(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LED_F_COM_ON(void) {
|
2019-01-12 07:12:51 -07:00
|
|
|
LED_PORT |= _BV(4);
|
2019-01-12 08:30:26 -07:00
|
|
|
com_led_timeout = timer_clock() + ms_to_ticks(CONFIG_COM_LED_TIMEOUT_MS);
|
2019-01-12 07:12:51 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 10:36:40 -07:00
|
|
|
void LED_F_COM_OFF(void) {
|
2019-01-12 07:12:51 -07:00
|
|
|
LED_PORT &= ~_BV(4);
|
|
|
|
}
|
|
|
|
|
2019-01-12 08:30:26 -07:00
|
|
|
void update_led_status(void) {
|
|
|
|
if (led_status_ticks >= led_status_ticks_top) {
|
|
|
|
if (timer_clock() > com_led_timeout) {
|
2019-01-12 07:12:51 -07:00
|
|
|
LED_COM_OFF();
|
|
|
|
}
|
2019-01-12 08:30:26 -07:00
|
|
|
led_status_ticks = 0;
|
|
|
|
} else {
|
|
|
|
led_status_ticks++;
|
2019-01-12 07:12:51 -07:00
|
|
|
}
|
2019-02-07 05:16:42 -07:00
|
|
|
}
|
|
|
|
|
2019-02-07 10:36:40 -07:00
|
|
|
#define LED_DELAY_E_C_1 200
|
|
|
|
#define LED_DELAY_E_C_2 50
|
|
|
|
void LED_indicate_enabled_crypto(void) {
|
|
|
|
LED_softblock_on();
|
|
|
|
|
|
|
|
LED_F_STATUS_OFF();
|
|
|
|
LED_F_COM_OFF();
|
|
|
|
LED_F_TX_OFF();
|
|
|
|
LED_F_RX_OFF();
|
|
|
|
delay_ms(LED_DELAY_E_C_1);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < 2; i++) {
|
|
|
|
LED_F_STATUS_ON();
|
|
|
|
delay_ms(LED_DELAY_E_C_2);
|
|
|
|
LED_F_STATUS_OFF();
|
|
|
|
LED_F_COM_ON();
|
|
|
|
delay_ms(LED_DELAY_E_C_2);
|
|
|
|
LED_F_COM_OFF();
|
|
|
|
LED_F_RX_ON();
|
|
|
|
delay_ms(LED_DELAY_E_C_2);
|
|
|
|
LED_F_RX_OFF();
|
|
|
|
LED_F_TX_ON();
|
|
|
|
delay_ms(LED_DELAY_E_C_2);
|
|
|
|
LED_F_TX_OFF();
|
|
|
|
delay_ms(LED_DELAY_E_C_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
LED_F_STATUS_ON();
|
|
|
|
LED_softblock_off();
|
|
|
|
}
|
|
|
|
|
2019-02-07 05:16:42 -07:00
|
|
|
void LED_indicate_error_crypto(void) {
|
|
|
|
while (true) {
|
|
|
|
LED_COM_ON();
|
|
|
|
LED_STATUS_OFF();
|
|
|
|
delay_ms(500);
|
|
|
|
LED_COM_OFF();
|
|
|
|
LED_STATUS_ON();
|
|
|
|
delay_ms(500);
|
|
|
|
}
|
2019-01-12 07:12:51 -07:00
|
|
|
}
|