From 9519ad51ddb1988776b389a027bc77c1e226b4a7 Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Wed, 3 May 2023 16:18:05 -0400 Subject: [PATCH] indication: advanced indication with per-channel status led (#235) * indication: advanced indication with per-channel status led (cherry picked from commit b486dfe682622c9835cf00457834cebf6e34da2f) * f1_dual_rev1: enable advanced indication --------- Co-authored-by: Andrey Gusakov --- firmware/Makefile | 1 + .../f1_dual_rev1/wideband_board_config.h | 5 + firmware/console/binary/tunerstudio.cpp | 6 +- firmware/indication.cpp | 117 ++++++++++++++++++ firmware/indication.h | 4 + firmware/main.cpp | 7 ++ 6 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 firmware/indication.cpp create mode 100644 firmware/indication.h diff --git a/firmware/Makefile b/firmware/Makefile index 91b9129..3feb258 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -155,6 +155,7 @@ CPPSRC = $(ALLCPPSRC) \ uart.cpp \ auxout.cpp \ livedata.cpp \ + indication.cpp \ console/binary/tunerstudio.cpp \ console/binary/tunerstudio_io.cpp \ console/binary/tunerstudio_io_serial.cpp \ diff --git a/firmware/boards/f1_dual_rev1/wideband_board_config.h b/firmware/boards/f1_dual_rev1/wideband_board_config.h index 9bc190f..a1e6e5d 100644 --- a/firmware/boards/f1_dual_rev1/wideband_board_config.h +++ b/firmware/boards/f1_dual_rev1/wideband_board_config.h @@ -74,3 +74,8 @@ // enable BT module setup attempt #define BT_SERIAL_OVER_JDY33 TRUE #define BT_BROADCAST_NAME "RusEFI WBO x2" + +// ******************************* +// Indication: per channel status LED + communication LED +// ******************************* +#define ADVANCED_INDICATION diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 9af681e..dd2531e 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -73,6 +73,8 @@ #include "tunerstudio_impl.h" #include "byteswap.h" +#include "indication.h" + #include #ifndef EFI_BLUETOOTH_SETUP @@ -437,7 +439,7 @@ void TunerstudioThread::ThreadTask() { // Until the end of time, process incoming messages. while (true) { if (tsProcessOne(channel) == 0) { - //onDataArrived(true); + onDataArrived(true); btTimeout = 0; } else { btTimeout += TS_COMMUNICATION_TIMEOUT; @@ -449,7 +451,7 @@ void TunerstudioThread::ThreadTask() { // Try this only once btInitAttempted = true; } - //onDataArrived(false); + onDataArrived(false); } } } diff --git a/firmware/indication.cpp b/firmware/indication.cpp new file mode 100644 index 0000000..6b25ffa --- /dev/null +++ b/firmware/indication.cpp @@ -0,0 +1,117 @@ +#include "ch.h" +#include "hal.h" + +#include "fault.h" +#include "heater_control.h" + +#include "indication.h" + +#include "wideband_config.h" + +using namespace wbo; + +#ifdef ADVANCED_INDICATION + +#define LED_BLINK_FAST (50) +#define LED_BLINK_MEDIUM (300) +#define LED_BLINK_SLOW (700) +#define LED_OFF_TIME (2000) + +struct indicationThreadData { + uint32_t idx; + ioline_t line; +}; + +indicationThreadData indData[] = { + {0, PAL_LINE(LED_GREEN_PORT, LED_GREEN_PIN)}, +#ifdef LED_R_GREEN_PORT + {1, PAL_LINE(LED_R_GREEN_PORT, LED_R_GREEN_PIN)}, +#endif +}; + +static THD_WORKING_AREA(waIndicationThread, 128); +#ifdef LED_R_GREEN_PORT +static THD_WORKING_AREA(waIndicationThread2, 128); +#endif + +static void IndicationThread(void *ptr) +{ + chRegSetThreadName("Indication"); + indicationThreadData *data = (indicationThreadData *)ptr; + + while(true) + { + auto fault = GetCurrentFault(data->idx); + + if (fault == Fault::None) + { + // Green is blinking + palToggleLine(data->line); + + // Slow blink if closed loop, fast if not + chThdSleepMilliseconds(IsRunningClosedLoop(data->idx) ? LED_BLINK_SLOW : LED_BLINK_FAST); + } + else + { + // Start from off state + palClearLine(data->line); + + // Blink out the error code + for (int i = 0; i < 2 * static_cast(fault); i++) + { + // Blue is blinking + palToggleLine(data->line); + + // fast blink + chThdSleepMilliseconds(LED_BLINK_MEDIUM); + } + + chThdSleepMilliseconds(LED_OFF_TIME); + } + } +} + +void InitIndication() +{ + chThdCreateStatic(waIndicationThread, sizeof(waIndicationThread), NORMALPRIO, IndicationThread, &indData[0]); +#ifdef LED_R_GREEN_PORT + chThdCreateStatic(waIndicationThread2, sizeof(waIndicationThread2), NORMALPRIO, IndicationThread, &indData[1]); +#endif +} + +/* Can be calles from two TS channels. */ +void onDataArrived(bool status) +{ + /* to avoid blinking when two TS channels available and only one is communicating + * another one will call this function with status = false every timeout */ + /* TODO: total crap, rework */ + static int filter = 0; + + if (status) { + if (filter < 3) { + filter = 3; + } + } else { + if (filter > 0) { + filter--; + } + } + + if (filter) { + palSetPad(LED_BLUE_PORT, LED_BLUE_PIN); + } else { + palClearPad(LED_BLUE_PORT, LED_BLUE_PIN); + } +} + +#else + +void InitIndication() +{ +} + +void onDataArrived(bool) +{ +} + +#endif \ No newline at end of file diff --git a/firmware/indication.h b/firmware/indication.h new file mode 100644 index 0000000..73e8ce1 --- /dev/null +++ b/firmware/indication.h @@ -0,0 +1,4 @@ +#pragma once + +void InitIndication(); +void onDataArrived(bool status); diff --git a/firmware/main.cpp b/firmware/main.cpp index 187ad06..dbd7a4d 100644 --- a/firmware/main.cpp +++ b/firmware/main.cpp @@ -13,6 +13,7 @@ #include "max31855.h" #include "port.h" #include "tunerstudio.h" +#include "indication.h" #include "wideband_config.h" @@ -41,6 +42,7 @@ int main() { InitCan(); InitUart(); + InitIndication(); #if (EGT_CHANNELS > 0) StartEgt(); @@ -48,6 +50,10 @@ int main() { while(true) { +#ifdef ADVANCED_INDICATION + /* NOP nap */ + chThdSleepMilliseconds(1000); +#else /* TODO: show error for all AFR channels */ /* TODO: show EGT errors */ auto fault = GetCurrentFault(0); @@ -80,6 +86,7 @@ int main() { chThdSleepMilliseconds(2000); } +#endif } }