Files

161 lines
4.7 KiB
C
Raw Permalink Normal View History

2016-10-07 23:44:50 +02:00
#include "LED.h"
#include "Settings.h"
#include "Map.h"
#include "Terminal/CommandLine.h"
#include "System.h"
#define BLINK_PRESCALER 1 /* x LEDTick(); */
2016-10-07 23:44:50 +02:00
LEDActionEnum LEDGreenAction = LED_NO_ACTION;
LEDActionEnum LEDRedAction = LED_NO_ACTION;
static const MapEntryType PROGMEM LEDFunctionMap[] = {
2017-11-07 15:53:23 +01:00
{ .Id = LED_NO_FUNC, .Text = "NONE" },
{ .Id = LED_POWERED, .Text = "POWERED" },
{ .Id = LED_TERMINAL_CONN, .Text = "TERMINAL_CONN" },
{ .Id = LED_TERMINAL_RXTX, .Text = "TERMINAL_RXTX" },
{ .Id = LED_SETTING_CHANGE, .Text = "SETTING_CHANGE" },
{ .Id = LED_MEMORY_STORED, .Text = "MEMORY_STORED" },
{ .Id = LED_MEMORY_CHANGED, .Text = "MEMORY_CHANGED" },
{ .Id = LED_CODEC_RX, .Text = "CODEC_RX" },
{ .Id = LED_CODEC_TX, .Text = "CODEC_TX" },
{ .Id = LED_FIELD_DETECTED, .Text = "FIELD_DETECTED" },
{ .Id = LED_LOG_MEM_FULL, .Text = "LOGMEM_FULL" },
2016-10-07 23:44:50 +02:00
};
2019-11-15 10:21:40 +01:00
INLINE void Tick(uint8_t Mask, LEDActionEnum *Action) {
2017-11-07 15:53:23 +01:00
static uint8_t LEDRedBlinkPrescaler = 0;
static uint8_t LEDGreenBlinkPrescaler = 0;
2019-11-15 10:21:40 +01:00
uint8_t *BlinkPrescaler = (Action == &LEDGreenAction) ? &LEDGreenBlinkPrescaler : &LEDRedBlinkPrescaler;
2016-10-07 23:44:50 +02:00
2019-11-15 10:21:40 +01:00
switch (*Action) {
2017-11-07 15:53:23 +01:00
case LED_NO_ACTION:
/* Do nothing */
break;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
case LED_OFF:
LED_PORT.OUTCLR = Mask;
*Action = LED_NO_ACTION;
break;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
case LED_ON:
LED_PORT.OUTSET = Mask;
*Action = LED_NO_ACTION;
break;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
case LED_TOGGLE:
LED_PORT.OUTTGL = Mask;
*Action = LED_NO_ACTION;
break;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
case LED_PULSE:
if (!(LED_PORT.OUT & Mask)) {
LED_PORT.OUTSET = Mask;
} else {
LED_PORT.OUTCLR = Mask;
*Action = LED_NO_ACTION;
}
break;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
case LED_BLINK_1X ... LED_BLINK_8X:
if (++(*BlinkPrescaler) == BLINK_PRESCALER) {
*BlinkPrescaler = 0;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
/* Blink functionality occurs at slower speed than Tick-frequency */
if (!(LED_PORT.OUT & Mask)) {
/* LED is off, turn it on */
LED_PORT.OUTSET = Mask;
} else {
/* LED is on, turn it off and change state */
LED_PORT.OUTCLR = Mask;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
if (*Action == LED_BLINK_1X) {
*Action = LED_NO_ACTION;
} else {
/* Still some blinks to do. Use the fact that LED_BLINK_XY are ordered sequentially */
*Action = *Action - 1;
}
}
}
break;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
default:
/* Should not happen (TM) */
*Action = LED_NO_ACTION;
break;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
}
2016-10-07 23:44:50 +02:00
}
2019-11-15 10:21:40 +01:00
void LEDInit(void) {
2017-11-07 15:53:23 +01:00
LED_PORT.DIRSET = LED_MASK;
2016-10-07 23:44:50 +02:00
}
2019-11-15 10:21:40 +01:00
void LEDTick(void) {
2017-11-07 15:53:23 +01:00
Tick(LED_RED, &LEDRedAction);
Tick(LED_GREEN, &LEDGreenAction);
2016-10-07 23:44:50 +02:00
}
2019-11-15 10:21:40 +01:00
void LEDGetFuncList(char *List, uint16_t BufferSize) {
2017-11-07 15:53:23 +01:00
MapToString(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap), List, BufferSize);
2016-10-07 23:44:50 +02:00
}
2019-11-15 10:21:40 +01:00
void LEDSetFuncById(uint8_t Mask, LEDHookEnum Function) {
2016-10-07 23:44:50 +02:00
#ifndef LED_SETTING_GLOBAL
2017-11-07 15:53:23 +01:00
if (Mask & LED_GREEN) {
GlobalSettings.ActiveSettingPtr->LEDGreenFunction = Function;
}
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
if (Mask & LED_RED) {
GlobalSettings.ActiveSettingPtr->LEDRedFunction = Function;
}
2016-10-07 23:44:50 +02:00
#else
2017-11-07 15:53:23 +01:00
/* Write LED func to all settings when using global settings */
2019-11-15 10:21:40 +01:00
for (uint8_t i = 0; i < SETTINGS_COUNT; i++) {
2017-11-07 15:53:23 +01:00
if (Mask & LED_GREEN) {
GlobalSettings.Settings[i].LEDGreenFunction = Function;
}
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
if (Mask & LED_RED) {
GlobalSettings.Settings[i].LEDRedFunction = Function;
}
}
2016-10-07 23:44:50 +02:00
#endif
2017-11-07 15:53:23 +01:00
/* Clear modified LED and remove any pending actions */
if (Mask & LED_GREEN) {
LED_PORT.OUTCLR = LED_GREEN;
LEDGreenAction = LED_NO_ACTION;
}
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
if (Mask & LED_RED) {
LED_PORT.OUTCLR = LED_RED;
LEDRedAction = LED_NO_ACTION;
}
2016-10-07 23:44:50 +02:00
}
2019-11-15 10:21:40 +01:00
void LEDGetFuncByName(uint8_t Mask, char *Function, uint16_t BufferSize) {
2017-11-07 15:53:23 +01:00
if (Mask == LED_GREEN) {
MapIdToText(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap),
2019-11-15 10:21:40 +01:00
GlobalSettings.ActiveSettingPtr->LEDGreenFunction, Function, BufferSize);
2017-11-07 15:53:23 +01:00
} else if (Mask == LED_RED) {
MapIdToText(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap),
2019-11-15 10:21:40 +01:00
GlobalSettings.ActiveSettingPtr->LEDRedFunction, Function, BufferSize);
2017-11-07 15:53:23 +01:00
}
2016-10-07 23:44:50 +02:00
}
2019-11-15 10:21:40 +01:00
bool LEDSetFuncByName(uint8_t Mask, const char *Function) {
2017-11-07 15:53:23 +01:00
MapIdType Id;
2016-10-07 23:44:50 +02:00
2017-11-07 15:53:23 +01:00
if (MapTextToId(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap), Function, &Id)) {
LEDSetFuncById(Mask, Id);
return true;
} else {
return false;
}
2016-10-07 23:44:50 +02:00
}