2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @file main.cpp
|
|
|
|
*
|
|
|
|
* @brief Main source file for the Souko's ChainLube Mk1 ESP8266 project.
|
|
|
|
*
|
|
|
|
* This file includes necessary libraries, defines configuration options, and declares global variables
|
|
|
|
* and function prototypes. It sets up essential components, initializes peripherals, and defines
|
|
|
|
* callbacks for interrupt service routines (ISRs) and timers. The main setup function configures the
|
|
|
|
* project, and the loop function handles the main execution loop, performing various tasks based on
|
|
|
|
* the configured options.
|
|
|
|
*
|
|
|
|
* @author Marcel Peterkau
|
|
|
|
* @date 09.01.2024
|
|
|
|
*/
|
|
|
|
|
2022-01-07 21:02:27 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <Wire.h>
|
2022-08-19 00:10:42 +02:00
|
|
|
#ifdef FEATURE_ENABLE_OLED
|
2022-01-07 21:02:27 +01:00
|
|
|
#include <U8g2lib.h>
|
2022-08-19 00:10:42 +02:00
|
|
|
#endif
|
2022-01-07 21:02:27 +01:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ArduinoOTA.h>
|
2022-02-10 09:54:24 +01:00
|
|
|
|
2023-03-02 22:30:42 +01:00
|
|
|
#include <Adafruit_NeoPixel.h>
|
2022-01-07 23:36:02 +01:00
|
|
|
#include <Ticker.h>
|
2022-01-07 21:02:27 +01:00
|
|
|
|
|
|
|
#include "common.h"
|
2022-02-10 09:54:24 +01:00
|
|
|
|
2022-08-22 09:23:01 +02:00
|
|
|
#include "sanitycheck.h"
|
|
|
|
|
2022-01-08 03:14:26 +01:00
|
|
|
#include "lubeapp.h"
|
|
|
|
#include "webui.h"
|
2022-01-09 20:51:16 +01:00
|
|
|
#include "config.h"
|
2022-01-10 00:02:21 +01:00
|
|
|
#include "globals.h"
|
2023-02-23 23:14:58 +01:00
|
|
|
#include "debugger.h"
|
2022-02-04 21:28:49 +01:00
|
|
|
#include "can.h"
|
2022-02-10 22:32:40 +01:00
|
|
|
#include "gps.h"
|
|
|
|
#include "dtc.h"
|
2023-03-02 22:30:42 +01:00
|
|
|
#include "led_colors.h"
|
2022-01-07 21:02:27 +01:00
|
|
|
|
2022-05-04 23:06:15 +02:00
|
|
|
#ifdef FEATURE_ENABLE_WIFI_CLIENT
|
2022-01-14 21:28:50 +01:00
|
|
|
#include <ESP8266WiFiMulti.h>
|
|
|
|
|
2022-08-21 11:28:58 +02:00
|
|
|
const char *ssid = QUOTE(WIFI_SSID_CLIENT);
|
|
|
|
const char *password = QUOTE(WIFI_PASSWORD_CLIENT);
|
2022-01-07 23:36:02 +01:00
|
|
|
const uint32_t connectTimeoutMs = 5000;
|
2022-01-07 21:02:27 +01:00
|
|
|
|
2022-01-14 21:28:50 +01:00
|
|
|
ESP8266WiFiMulti wifiMulti;
|
2022-01-31 09:26:10 +01:00
|
|
|
#endif
|
2022-01-14 21:28:50 +01:00
|
|
|
|
2024-02-07 22:52:28 +01:00
|
|
|
uint32_t (*wheelSpeedcapture)() = nullptr;
|
|
|
|
|
2022-01-07 21:02:27 +01:00
|
|
|
bool startSetupMode = false;
|
2022-01-07 23:36:02 +01:00
|
|
|
volatile uint32_t wheel_pulse = 0;
|
|
|
|
|
2023-03-02 22:30:42 +01:00
|
|
|
Adafruit_NeoPixel leds(1, GPIO_LED, NEO_RGB + NEO_KHZ800);
|
2022-01-07 21:02:27 +01:00
|
|
|
|
|
|
|
// Function-Prototypes
|
2022-01-07 23:36:02 +01:00
|
|
|
void IRAM_ATTR trigger_ISR();
|
2023-03-02 22:30:42 +01:00
|
|
|
void LED_Process(uint8_t override = false, uint32_t setColor = LED_DEFAULT_COLOR);
|
2022-08-19 00:10:42 +02:00
|
|
|
#ifdef FEATURE_ENABLE_OLED
|
|
|
|
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(-1);
|
2022-01-14 15:36:17 +01:00
|
|
|
void Display_Process();
|
2022-08-19 00:10:42 +02:00
|
|
|
#endif
|
2022-01-14 15:36:17 +01:00
|
|
|
void Button_Process();
|
2024-01-09 12:15:39 +01:00
|
|
|
void toggleWiFiAP(bool shutdown = false);
|
|
|
|
void SystemShutdown(bool restart = false);
|
2022-02-04 21:28:18 +01:00
|
|
|
uint32_t Process_Impulse_WheelSpeed();
|
2022-02-04 22:26:26 +01:00
|
|
|
void EEPROMCyclicPDS_callback();
|
2022-02-10 09:54:24 +01:00
|
|
|
|
2022-05-04 23:06:15 +02:00
|
|
|
#ifdef FEATURE_ENABLE_WIFI_CLIENT
|
2022-01-14 21:28:50 +01:00
|
|
|
void wifiMaintainConnectionTicker_callback();
|
2022-01-07 23:36:02 +01:00
|
|
|
Ticker WiFiMaintainConnectionTicker(wifiMaintainConnectionTicker_callback, 1000, 0, MILLIS);
|
2022-01-14 21:28:50 +01:00
|
|
|
#endif
|
2022-02-04 22:26:26 +01:00
|
|
|
Ticker EEPROMCyclicPDSTicker(EEPROMCyclicPDS_callback, 60000, 0, MILLIS);
|
2022-01-07 21:02:27 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Initializes the ESP8266 project, configuring various components and setting up required services.
|
|
|
|
*
|
|
|
|
* This setup function is responsible for initializing the ESP8266 project, including setting the CPU frequency,
|
|
|
|
* configuring WiFi settings, initializing DTC storage, handling WiFi client functionality (if enabled),
|
|
|
|
* initializing the Serial communication, setting up an OLED display (if enabled), initializing EEPROM,
|
|
|
|
* loading configuration and persistence data from EEPROM, initializing LEDs, setting up the chosen speed source
|
|
|
|
* (CAN, GPS, Impulse), configuring GPIO pins, setting up Over-The-Air (OTA) updates, initializing the web user interface,
|
|
|
|
* initializing global variables, starting cyclic EEPROM updates for Persistence Data Structure (PDS), and printing
|
|
|
|
* initialization status messages to Serial.
|
|
|
|
*/
|
2022-01-07 21:02:27 +01:00
|
|
|
void setup()
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Set CPU frequency to 80MHz
|
2022-01-07 21:02:27 +01:00
|
|
|
system_update_cpu_freq(SYS_CPU_80MHZ);
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Generate a unique device name based on ESP chip ID
|
2022-08-14 17:38:45 +02:00
|
|
|
snprintf(globals.DeviceName, 32, HOST_NAME, ESP.getChipId());
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Disable WiFi persistent storage
|
2022-01-07 21:02:27 +01:00
|
|
|
WiFi.persistent(false);
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Initialize and clear Diagnostic Trouble Code (DTC) storage
|
|
|
|
ClearAllDTC();
|
2022-03-09 20:25:02 +01:00
|
|
|
|
2022-05-04 23:06:15 +02:00
|
|
|
#ifdef FEATURE_ENABLE_WIFI_CLIENT
|
2024-01-09 12:54:05 +01:00
|
|
|
// Configure WiFi settings for client mode if enabled
|
2022-01-14 21:28:50 +01:00
|
|
|
WiFi.mode(WIFI_STA);
|
2022-08-14 17:38:45 +02:00
|
|
|
WiFi.setHostname(globals.DeviceName);
|
2022-08-22 14:29:40 +02:00
|
|
|
wifiMulti.addAP(QUOTE(WIFI_SSID_CLIENT), QUOTE(WIFI_PASSWORD_CLIENT));
|
2022-01-14 21:28:50 +01:00
|
|
|
WiFiMaintainConnectionTicker.start();
|
|
|
|
#else
|
2024-01-09 12:54:05 +01:00
|
|
|
// Disable WiFi if WiFi client feature is not enabled
|
2022-01-14 21:28:50 +01:00
|
|
|
WiFi.mode(WIFI_OFF);
|
|
|
|
#endif
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Initialize Serial communication
|
2022-01-07 21:02:27 +01:00
|
|
|
Serial.begin(115200);
|
2023-05-01 11:29:22 +02:00
|
|
|
Serial.print("\n\nSouko's ChainLube Mk1\n");
|
|
|
|
Serial.print(globals.DeviceName);
|
2022-01-07 21:02:27 +01:00
|
|
|
|
2022-08-19 00:10:42 +02:00
|
|
|
#ifdef FEATURE_ENABLE_OLED
|
2024-01-09 12:54:05 +01:00
|
|
|
// Initialize OLED display if enabled
|
2022-01-07 21:02:27 +01:00
|
|
|
u8x8.begin();
|
|
|
|
u8x8.setFont(u8x8_font_chroma48medium8_r);
|
2023-05-01 11:29:22 +02:00
|
|
|
u8x8.clearDisplay();
|
|
|
|
u8x8.drawString(0, 0, "KTM ChainLube V1");
|
|
|
|
u8x8.refreshDisplay();
|
|
|
|
Serial.print("\nDisplay-Init done");
|
2022-08-19 00:10:42 +02:00
|
|
|
#endif
|
2023-12-03 22:53:35 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Initialize EEPROM, load configuration, and persistence data from EEPROM
|
2023-12-03 22:53:35 +01:00
|
|
|
InitEEPROM();
|
|
|
|
GetConfig_EEPROM();
|
|
|
|
GetPersistence_EEPROM();
|
|
|
|
Serial.print("\nEE-Init done");
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Initialize LEDs
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.begin();
|
2023-05-01 11:29:22 +02:00
|
|
|
Serial.print("\nLED-Init done");
|
2022-02-10 09:54:24 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Initialize based on the chosen speed source (CAN, GPS, Impulse)
|
2023-12-25 00:44:24 +01:00
|
|
|
switch (LubeConfig.SpeedSource)
|
2023-09-25 23:01:23 +02:00
|
|
|
{
|
2023-12-25 00:44:24 +01:00
|
|
|
case SOURCE_CAN:
|
2023-09-25 23:01:23 +02:00
|
|
|
Init_CAN();
|
2024-02-07 22:52:28 +01:00
|
|
|
wheelSpeedcapture = &Process_CAN_WheelSpeed;
|
2023-09-25 23:01:23 +02:00
|
|
|
Serial.print("\nCAN-Init done");
|
2023-12-25 00:44:24 +01:00
|
|
|
break;
|
|
|
|
case SOURCE_GPS:
|
|
|
|
Init_GPS();
|
2024-02-07 22:52:28 +01:00
|
|
|
wheelSpeedcapture = &Process_GPS_WheelSpeed;
|
2023-12-25 00:44:24 +01:00
|
|
|
Serial.print("\nGPS-Init done");
|
|
|
|
break;
|
|
|
|
case SOURCE_IMPULSE:
|
|
|
|
pinMode(GPIO_TRIGGER, INPUT_PULLUP);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(GPIO_TRIGGER), trigger_ISR, FALLING);
|
2024-02-07 22:52:28 +01:00
|
|
|
wheelSpeedcapture = &Process_Impulse_WheelSpeed;
|
2023-12-25 00:44:24 +01:00
|
|
|
Serial.print("\nPulse-Input Init done");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2023-09-25 23:01:23 +02:00
|
|
|
}
|
2023-05-01 11:29:22 +02:00
|
|
|
Serial.print("\nSource-Init done");
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Configure GPIO pins for button and pump control
|
2022-01-07 21:02:27 +01:00
|
|
|
pinMode(GPIO_BUTTON, INPUT_PULLUP);
|
|
|
|
pinMode(GPIO_PUMP, OUTPUT);
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Set up OTA updates
|
2022-01-07 21:02:27 +01:00
|
|
|
ArduinoOTA.setPort(8266);
|
2022-08-14 17:38:45 +02:00
|
|
|
ArduinoOTA.setHostname(globals.DeviceName);
|
2022-01-07 23:36:02 +01:00
|
|
|
ArduinoOTA.setPassword(QUOTE(ADMIN_PASSWORD));
|
2024-01-09 12:54:05 +01:00
|
|
|
|
2022-08-19 00:10:42 +02:00
|
|
|
#ifdef FEATURE_ENABLE_OLED
|
2024-01-09 12:54:05 +01:00
|
|
|
// Set up OTA callbacks for OLED display if enabled
|
2022-01-07 21:02:27 +01:00
|
|
|
ArduinoOTA.onStart([]()
|
|
|
|
{
|
|
|
|
u8x8.clearDisplay();
|
2024-01-09 22:52:28 +01:00
|
|
|
u8x8.drawString(0, 6, "OTA-Update");
|
2022-01-07 21:02:27 +01:00
|
|
|
u8x8.refreshDisplay(); });
|
|
|
|
|
|
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
|
|
|
|
{
|
|
|
|
static bool refreshed = false;
|
|
|
|
if (!refreshed)
|
|
|
|
{
|
|
|
|
u8x8.clearDisplay();
|
|
|
|
refreshed = true;
|
2024-01-09 22:52:28 +01:00
|
|
|
u8x8.drawString(0, 6, "OTA Upload");
|
2022-01-07 21:02:27 +01:00
|
|
|
}
|
|
|
|
uint32_t percent = progress / (total / 100);
|
2024-01-09 22:52:28 +01:00
|
|
|
u8x8.setCursor(0, 7);
|
2022-01-07 21:02:27 +01:00
|
|
|
u8x8.printf("%d %%", percent);
|
|
|
|
u8x8.refreshDisplay(); });
|
|
|
|
|
|
|
|
ArduinoOTA.onEnd([]()
|
|
|
|
{
|
|
|
|
u8x8.clearDisplay();
|
2024-01-09 22:52:28 +01:00
|
|
|
u8x8.drawString(0, 6, "OTA-Restart");
|
2022-01-07 21:02:27 +01:00
|
|
|
u8x8.refreshDisplay(); });
|
2022-08-19 00:10:42 +02:00
|
|
|
#endif
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Begin OTA updates
|
2022-01-07 21:02:27 +01:00
|
|
|
ArduinoOTA.begin();
|
2023-05-01 11:29:22 +02:00
|
|
|
Serial.print("\nOTA-Init done");
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Initialize the web user interface
|
2022-01-07 23:36:02 +01:00
|
|
|
initWebUI();
|
2023-05-01 11:29:22 +02:00
|
|
|
Serial.print("\nWebUI-Init done");
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Initialize global variables
|
2022-05-06 22:38:24 +02:00
|
|
|
initGlobals();
|
2023-05-01 11:29:22 +02:00
|
|
|
Serial.print("\nglobals-Init done");
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Start cyclic EEPROM updates for Persistence Data Structure (PDS)
|
2022-02-04 22:26:26 +01:00
|
|
|
EEPROMCyclicPDSTicker.start();
|
2023-05-01 11:29:22 +02:00
|
|
|
Serial.print("\nSetup Done\n");
|
2022-01-07 21:02:27 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Main execution loop for the ESP8266 project, performing various tasks based on configuration.
|
|
|
|
*
|
|
|
|
* This loop function handles different tasks based on the configured source of speed data (impulse, CAN, time, GPS).
|
|
|
|
* It calculates wheel distance, runs the lubrication application, updates the OLED display (if enabled),
|
|
|
|
* processes CAN messages, handles button input, manages LED behavior, performs EEPROM-related tasks, handles
|
|
|
|
* webserver operations, processes Diagnostic Trouble Codes (DTC), and manages debugging. Additionally, it
|
|
|
|
* integrates functionalities such as Over-The-Air (OTA) updates, cyclic EEPROM updates for Persistence Data
|
|
|
|
* Structure (PDS), WiFi connection maintenance, and system shutdown handling.
|
|
|
|
*/
|
2022-01-07 21:02:27 +01:00
|
|
|
void loop()
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Run lubrication application with the calculated wheel distance
|
2024-02-07 22:52:28 +01:00
|
|
|
RunLubeApp(wheelSpeedcapture());
|
2024-01-09 12:54:05 +01:00
|
|
|
|
2022-08-19 00:10:42 +02:00
|
|
|
#ifdef FEATURE_ENABLE_OLED
|
2024-01-09 12:54:05 +01:00
|
|
|
// Update OLED display if enabled
|
2022-01-14 15:36:17 +01:00
|
|
|
Display_Process();
|
2023-09-25 07:21:33 +02:00
|
|
|
#endif
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Process CAN messages if the speed source is not impulse
|
2023-09-25 23:01:23 +02:00
|
|
|
if (LubeConfig.SpeedSource != SOURCE_IMPULSE)
|
|
|
|
{
|
|
|
|
CAN_Process();
|
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Process button input, manage LED behavior, perform EEPROM tasks, handle webserver operations,
|
|
|
|
// process Diagnostic Trouble Codes (DTC), and manage debugging
|
2022-01-14 15:36:17 +01:00
|
|
|
Button_Process();
|
|
|
|
LED_Process();
|
2022-02-04 21:24:15 +01:00
|
|
|
EEPROM_Process();
|
2023-02-23 23:14:58 +01:00
|
|
|
Webserver_Process();
|
2023-02-24 19:24:26 +01:00
|
|
|
DTC_Process();
|
2023-03-14 23:30:26 +01:00
|
|
|
Debug_Process();
|
2022-01-07 23:36:02 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Handle OTA updates and update cyclic EEPROM tasks for Persistence Data Structure (PDS)
|
2022-01-07 21:02:27 +01:00
|
|
|
ArduinoOTA.handle();
|
2023-02-24 19:24:26 +01:00
|
|
|
EEPROMCyclicPDSTicker.update();
|
2024-01-09 12:54:05 +01:00
|
|
|
|
2022-05-04 23:06:15 +02:00
|
|
|
#ifdef FEATURE_ENABLE_WIFI_CLIENT
|
2024-01-09 12:54:05 +01:00
|
|
|
// Update WiFi connection maintenance ticker if WiFi client feature is enabled
|
2022-01-14 21:28:50 +01:00
|
|
|
WiFiMaintainConnectionTicker.update();
|
|
|
|
#endif
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Perform system shutdown if the status is set to shutdown
|
2022-01-19 22:23:36 +01:00
|
|
|
if (globals.systemStatus == sysStat_Shutdown)
|
2024-01-09 12:54:05 +01:00
|
|
|
SystemShutdown(false);
|
|
|
|
|
|
|
|
// Yield to allow other tasks to run
|
2022-01-07 21:02:27 +01:00
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2022-05-04 23:06:15 +02:00
|
|
|
#ifdef FEATURE_ENABLE_WIFI_CLIENT
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Callback function for maintaining WiFi connection and handling connection failures.
|
|
|
|
*
|
|
|
|
* This callback function is used by a ticker to periodically check the WiFi connection status.
|
|
|
|
* If the device is not connected to WiFi, it counts connection failures. If the number of failures
|
|
|
|
* exceeds a defined threshold, the function triggers the initiation of an Access Point (AP) mode
|
|
|
|
* using the `toggleWiFiAP` function.
|
|
|
|
*/
|
2022-01-07 23:36:02 +01:00
|
|
|
void wifiMaintainConnectionTicker_callback()
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Static variables to track WiFi connection failure count and maximum allowed failures
|
2022-01-07 23:36:02 +01:00
|
|
|
static uint32_t WiFiFailCount = 0;
|
|
|
|
const uint32_t WiFiFailMax = 20;
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Check if the device is connected to WiFi
|
2022-01-07 23:36:02 +01:00
|
|
|
if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
return; // Exit if connected
|
2022-01-07 23:36:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Increment WiFi connection failure count
|
2022-01-07 23:36:02 +01:00
|
|
|
if (WiFiFailCount < WiFiFailMax)
|
|
|
|
{
|
|
|
|
WiFiFailCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Trigger AP mode if the maximum failures are reached
|
2024-01-12 19:52:25 +01:00
|
|
|
Debug_pushMessage("WiFi not connected! - Start AP\n");
|
2022-01-19 22:23:36 +01:00
|
|
|
toggleWiFiAP();
|
2022-01-07 23:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
|
2022-01-14 21:28:50 +01:00
|
|
|
#endif
|
2022-01-07 23:36:02 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Callback function for cyclically storing Persistence Data Structure (PDS) to EEPROM.
|
|
|
|
*
|
|
|
|
* This callback function is invoked periodically to store the Persistence Data Structure (PDS)
|
|
|
|
* to the EEPROM. It ensures that essential data is saved persistently, allowing the system to
|
|
|
|
* recover its state after power cycles or resets.
|
|
|
|
*/
|
2022-02-04 22:26:26 +01:00
|
|
|
void EEPROMCyclicPDS_callback()
|
|
|
|
{
|
|
|
|
StorePersistence_EEPROM();
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Interrupt Service Routine (ISR) triggered by wheel speed sensor pulses.
|
|
|
|
*
|
|
|
|
* This ISR is called whenever a pulse is detected from the wheel speed sensor. It increments
|
|
|
|
* the `wheel_pulse` variable, which is used to track the number of pulses received.
|
|
|
|
*/
|
2022-01-07 23:36:02 +01:00
|
|
|
void trigger_ISR()
|
|
|
|
{
|
|
|
|
wheel_pulse++;
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Manages LED behavior based on the current system status and user overrides.
|
|
|
|
*
|
|
|
|
* This function handles LED behavior, including startup animations, confirmation animations for
|
|
|
|
* normal and rain modes, indication for purge, error, shutdown, and normal operation. It supports
|
|
|
|
* user overrides to set a specific LED color. The LED status is determined by the current system
|
|
|
|
* status, and specific LED patterns are displayed accordingly.
|
|
|
|
*
|
|
|
|
* @param override Flag indicating whether to override the LED behavior (0: No override, 1: Override, 2: Resume previous state).
|
|
|
|
* @param SetColor The color to set when overriding the LED behavior.
|
|
|
|
*/
|
2023-03-02 22:30:42 +01:00
|
|
|
void LED_Process(uint8_t override, uint32_t SetColor)
|
2022-01-09 20:51:16 +01:00
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Enumeration to represent LED status
|
2022-01-09 20:51:16 +01:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
LED_Startup,
|
|
|
|
LED_Normal,
|
|
|
|
LED_Confirm_Normal,
|
|
|
|
LED_Rain,
|
|
|
|
LED_Confirm_Rain,
|
|
|
|
LED_Purge,
|
2022-01-14 15:36:17 +01:00
|
|
|
LED_Error,
|
2023-03-03 11:39:43 +01:00
|
|
|
LED_Shutdown,
|
2022-01-14 15:36:17 +01:00
|
|
|
LED_Override
|
2022-01-09 20:51:16 +01:00
|
|
|
} tLED_Status;
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Static variables to track LED status, system status, override color, and previous LED status
|
2022-01-19 22:23:36 +01:00
|
|
|
static tSystem_Status oldSysStatus = sysStat_Startup;
|
2022-01-10 00:02:21 +01:00
|
|
|
static tLED_Status LED_Status = LED_Startup;
|
2023-03-02 22:30:42 +01:00
|
|
|
static uint32_t LED_override_color = 0;
|
2022-01-14 15:36:17 +01:00
|
|
|
static tLED_Status LED_ResumeOverrideStatus = LED_Startup;
|
2022-01-09 20:51:16 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Variables for managing LED animation timing
|
2022-01-10 00:02:21 +01:00
|
|
|
uint8_t color = 0;
|
|
|
|
uint32_t timer = 0;
|
2023-03-02 22:30:42 +01:00
|
|
|
uint32_t animtimer = 0;
|
2022-01-13 21:59:53 +01:00
|
|
|
static uint32_t timestamp = 0;
|
|
|
|
timer = millis();
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Handle LED overrides
|
2022-01-14 15:36:17 +01:00
|
|
|
if (override == 1)
|
2022-01-09 20:51:16 +01:00
|
|
|
{
|
2022-01-14 15:36:17 +01:00
|
|
|
if (LED_Status != LED_Override)
|
|
|
|
{
|
|
|
|
LED_ResumeOverrideStatus = LED_Status;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("Override LED_Status\n");
|
2022-01-14 15:36:17 +01:00
|
|
|
}
|
|
|
|
LED_Status = LED_Override;
|
|
|
|
LED_override_color = SetColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (override == 2)
|
|
|
|
{
|
|
|
|
if (LED_Status == LED_Override)
|
|
|
|
{
|
|
|
|
LED_Status = LED_ResumeOverrideStatus;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("Resume LED_Status\n");
|
2022-01-14 15:36:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Update LED status when system status changes
|
2022-01-14 15:36:17 +01:00
|
|
|
if (oldSysStatus != globals.systemStatus)
|
|
|
|
{
|
|
|
|
switch (globals.systemStatus)
|
2022-01-09 20:51:16 +01:00
|
|
|
{
|
2022-01-10 00:55:04 +01:00
|
|
|
case sysStat_Startup:
|
|
|
|
LED_Status = LED_Startup;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("sysStat: Startup\n");
|
2022-01-10 00:55:04 +01:00
|
|
|
break;
|
|
|
|
case sysStat_Normal:
|
2022-01-13 21:59:53 +01:00
|
|
|
timestamp = timer + 3500;
|
2022-01-10 00:55:04 +01:00
|
|
|
LED_Status = LED_Confirm_Normal;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("sysStat: Normal\n");
|
2022-01-10 00:55:04 +01:00
|
|
|
break;
|
|
|
|
case sysStat_Rain:
|
2022-01-13 21:59:53 +01:00
|
|
|
timestamp = timer + 3500;
|
2022-01-10 00:55:04 +01:00
|
|
|
LED_Status = LED_Confirm_Rain;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("sysStat: Rain\n");
|
2022-01-10 00:55:04 +01:00
|
|
|
break;
|
|
|
|
case sysStat_Purge:
|
|
|
|
LED_Status = LED_Purge;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("sysStat: Purge\n");
|
2022-01-10 00:55:04 +01:00
|
|
|
break;
|
|
|
|
case sysStat_Error:
|
|
|
|
LED_Status = LED_Error;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("sysStat: Error\n");
|
2022-01-10 00:55:04 +01:00
|
|
|
break;
|
2022-01-19 22:23:36 +01:00
|
|
|
case sysStat_Shutdown:
|
2023-03-03 11:39:43 +01:00
|
|
|
LED_Status = LED_Shutdown;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("sysStat: Shutdown\n");
|
2023-03-03 11:39:43 +01:00
|
|
|
break;
|
2022-01-12 00:52:27 +01:00
|
|
|
default:
|
|
|
|
break;
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|
2022-01-14 15:36:17 +01:00
|
|
|
oldSysStatus = globals.systemStatus;
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|
2022-01-10 00:55:04 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Handle different LED statuses
|
2022-01-09 20:51:16 +01:00
|
|
|
switch (LED_Status)
|
|
|
|
{
|
|
|
|
case LED_Startup:
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness);
|
2022-01-14 15:36:17 +01:00
|
|
|
|
2022-08-22 14:13:55 +02:00
|
|
|
if (globals.TankPercentage < LubeConfig.TankRemindAtPercentage)
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setPixelColor(0, LED_STARTUP_TANKWARN);
|
2022-01-14 15:36:17 +01:00
|
|
|
else
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setPixelColor(0, LED_STARTUP_NORMAL);
|
2022-01-09 20:51:16 +01:00
|
|
|
break;
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
case LED_Confirm_Normal:
|
2023-03-02 22:30:42 +01:00
|
|
|
animtimer = timer % 500;
|
|
|
|
color = map(animtimer / 2, 0, 250, 0, LubeConfig.LED_Max_Brightness);
|
|
|
|
leds.setPixelColor(0, LED_NORMAL_COLOR);
|
|
|
|
if (animtimer < 250)
|
|
|
|
leds.setBrightness(color);
|
|
|
|
else
|
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness - color);
|
|
|
|
|
2022-01-10 00:02:21 +01:00
|
|
|
if (timestamp < timer)
|
2022-01-10 23:17:07 +01:00
|
|
|
{
|
2022-01-10 00:02:21 +01:00
|
|
|
LED_Status = LED_Normal;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("LED_Status: Confirm -> Normal\n");
|
2022-01-10 23:17:07 +01:00
|
|
|
}
|
2022-01-09 20:51:16 +01:00
|
|
|
break;
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
case LED_Normal:
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setBrightness(LubeConfig.LED_Min_Brightness);
|
|
|
|
leds.setPixelColor(0, LED_NORMAL_COLOR);
|
|
|
|
|
|
|
|
if (timer % 2000 > 1950 && LubeConfig.LED_Mode_Flash == true)
|
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness);
|
|
|
|
else if (timer % 2000 > 1500 && WiFi.getMode() != WIFI_OFF)
|
|
|
|
leds.setPixelColor(0, LED_WIFI_BLINK);
|
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
break;
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
case LED_Confirm_Rain:
|
2023-03-02 22:30:42 +01:00
|
|
|
animtimer = timer % 500;
|
|
|
|
color = map(animtimer / 2, 0, 250, 0, LubeConfig.LED_Max_Brightness);
|
|
|
|
leds.setPixelColor(0, LED_RAIN_COLOR);
|
|
|
|
if (animtimer < 250)
|
|
|
|
leds.setBrightness(color);
|
|
|
|
else
|
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness - color);
|
2022-01-10 00:02:21 +01:00
|
|
|
if (timestamp < timer)
|
2022-01-10 23:17:07 +01:00
|
|
|
{
|
2022-01-10 00:02:21 +01:00
|
|
|
LED_Status = LED_Rain;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("LED_Status: Confirm -> Rain\n");
|
2022-01-10 23:17:07 +01:00
|
|
|
}
|
2022-01-09 20:51:16 +01:00
|
|
|
break;
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
case LED_Rain:
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setBrightness(LubeConfig.LED_Min_Brightness);
|
|
|
|
leds.setPixelColor(0, LED_RAIN_COLOR);
|
|
|
|
|
|
|
|
if (timer % 2000 > 1950 && LubeConfig.LED_Mode_Flash == true)
|
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness);
|
|
|
|
else if (timer % 2000 > 1500 && WiFi.getMode() != WIFI_OFF)
|
|
|
|
leds.setPixelColor(0, LED_WIFI_BLINK);
|
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
break;
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
case LED_Purge:
|
2022-01-10 00:02:21 +01:00
|
|
|
timer = timer % 500;
|
2023-03-02 22:30:42 +01:00
|
|
|
color = map(timer / 2, 0, 250, LubeConfig.LED_Min_Brightness, LubeConfig.LED_Max_Brightness);
|
|
|
|
leds.setPixelColor(0, LED_PURGE_COLOR);
|
2022-01-10 00:02:21 +01:00
|
|
|
if (timer < 250)
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setBrightness(color);
|
2022-01-10 00:02:21 +01:00
|
|
|
else
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness - color);
|
2022-01-09 20:51:16 +01:00
|
|
|
break;
|
2022-01-10 00:02:21 +01:00
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
case LED_Error:
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness);
|
|
|
|
leds.setPixelColor(0, timer % 500 > 250 ? LED_ERROR_BLINK : 0);
|
2022-01-09 20:51:16 +01:00
|
|
|
break;
|
|
|
|
|
2023-03-03 11:39:43 +01:00
|
|
|
case LED_Shutdown:
|
|
|
|
timer = timer % 600;
|
|
|
|
leds.setPixelColor(0, LED_SHUTDOWN_BLINK);
|
|
|
|
if (timer < 500)
|
|
|
|
{
|
|
|
|
color = map(timer, 0, 500, LubeConfig.LED_Max_Brightness, LubeConfig.LED_Min_Brightness);
|
|
|
|
leds.setBrightness(color);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
leds.setBrightness(LubeConfig.LED_Min_Brightness);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-01-14 15:36:17 +01:00
|
|
|
case LED_Override:
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.setBrightness(LubeConfig.LED_Max_Brightness);
|
|
|
|
leds.setPixelColor(0, LED_override_color);
|
2022-01-14 15:36:17 +01:00
|
|
|
break;
|
|
|
|
|
2022-01-09 20:51:16 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-03-02 22:30:42 +01:00
|
|
|
leds.show();
|
2022-01-09 20:51:16 +01:00
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
|
2022-08-19 00:10:42 +02:00
|
|
|
#ifdef FEATURE_ENABLE_OLED
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Manages the display content based on the current system status and updates the OLED display.
|
|
|
|
*
|
|
|
|
* This function handles the content to be displayed on the OLED screen, taking into account the
|
|
|
|
* current system status. It clears the display and prints relevant information such as system mode,
|
|
|
|
* remaining lubrication distance, tank level, WiFi status, speed source, and IP address. Additionally,
|
|
|
|
* it refreshes the OLED display with the updated content.
|
|
|
|
*/
|
2022-01-14 15:36:17 +01:00
|
|
|
void Display_Process()
|
2022-01-10 00:55:04 +01:00
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Static variable to track the previous system status
|
2022-02-10 22:32:40 +01:00
|
|
|
static tSystem_Status oldSysStatus = sysStat_Startup;
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Check if the system status has changed since the last update
|
2022-02-10 22:32:40 +01:00
|
|
|
if (oldSysStatus != globals.systemStatus)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Clear the display and print the system title when the status changes
|
2022-02-10 22:32:40 +01:00
|
|
|
u8x8.clearDisplay();
|
|
|
|
u8x8.drawString(0, 0, "KTM ChainLube V1");
|
|
|
|
oldSysStatus = globals.systemStatus;
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Set the cursor position for displaying information on the OLED screen
|
2022-02-04 22:26:26 +01:00
|
|
|
u8x8.setCursor(0, 1);
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Calculate remaining lubrication distance based on system mode
|
2022-01-10 23:17:07 +01:00
|
|
|
uint32_t DistRemain = globals.systemStatus == sysStat_Normal ? LubeConfig.DistancePerLube_Default : LubeConfig.DistancePerLube_Rain;
|
2023-02-23 17:46:28 +01:00
|
|
|
DistRemain = DistRemain - (PersistenceData.TravelDistance_highRes_mm / 1000);
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Display relevant information on the OLED screen based on system status
|
2022-02-10 22:32:40 +01:00
|
|
|
u8x8.printf(PSTR("Mode: %10s\n"), globals.systemStatustxt);
|
|
|
|
if (globals.systemStatus == sysStat_Error)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Display the last Diagnostic Trouble Code (DTC) in case of an error
|
2022-02-10 22:32:40 +01:00
|
|
|
u8x8.printf(PSTR("last DTC: %6d\n"), getlastDTC(false));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Display information such as next lubrication distance, tank level, WiFi status, speed source, and IP address
|
2022-02-10 22:32:40 +01:00
|
|
|
u8x8.printf(PSTR("next Lube: %4dm\n"), DistRemain);
|
2023-02-19 14:29:38 +01:00
|
|
|
u8x8.printf(PSTR("Tank: %8dml\n"), PersistenceData.tankRemain_microL / 1000);
|
2022-02-10 22:32:40 +01:00
|
|
|
u8x8.printf(PSTR("WiFi: %10s\n"), (WiFi.getMode() == WIFI_AP ? "AP" : WiFi.getMode() == WIFI_OFF ? "OFF"
|
2022-03-08 23:03:10 +01:00
|
|
|
: WiFi.getMode() == WIFI_STA ? "CLIENT"
|
|
|
|
: "UNKNOWN"));
|
2022-02-10 22:32:40 +01:00
|
|
|
u8x8.printf(PSTR("Source: %8s\n"), SpeedSourceString[LubeConfig.SpeedSource]);
|
|
|
|
u8x8.printf("%s\n", WiFi.localIP().toString().c_str());
|
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Refresh the OLED display with the updated content
|
2022-01-10 00:55:04 +01:00
|
|
|
u8x8.refreshDisplay();
|
2022-01-14 15:36:17 +01:00
|
|
|
}
|
2022-08-19 00:10:42 +02:00
|
|
|
#endif
|
2022-01-14 15:36:17 +01:00
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Processes the button input and performs corresponding actions based on button state and timing.
|
|
|
|
*
|
|
|
|
* This function handles the button input, detecting button presses and executing actions based on
|
|
|
|
* predefined time delays. Actions include toggling WiFi, starting purge, toggling operating modes,
|
|
|
|
* and displaying feedback through LEDs. The function utilizes an enumeration to track button actions
|
|
|
|
* and manages the timing for different actions.
|
|
|
|
*/
|
2022-01-14 15:36:17 +01:00
|
|
|
void Button_Process()
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Time delays for different button actions
|
2022-01-14 15:36:17 +01:00
|
|
|
#define BUTTON_ACTION_DELAY_TOGGLEMODE 500
|
|
|
|
#define BUTTON_ACTION_DELAY_PURGE 3500
|
|
|
|
#define BUTTON_ACTION_DELAY_WIFI 6500
|
|
|
|
#define BUTTON_ACTION_DELAY_NOTHING 9500
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Enumeration to represent button actions
|
2022-01-14 15:36:17 +01:00
|
|
|
typedef enum buttonAction_e
|
|
|
|
{
|
|
|
|
BTN_INACTIVE,
|
|
|
|
BTN_NOTHING,
|
|
|
|
BTN_TOGGLEMODE,
|
|
|
|
BTN_TOGGLEWIFI,
|
|
|
|
BTN_STARTPURGE
|
|
|
|
} buttonAction_t;
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Static variables to track button state and timing
|
2022-01-14 15:36:17 +01:00
|
|
|
static uint32_t buttonTimestamp = 0;
|
|
|
|
static buttonAction_t buttonAction = BTN_INACTIVE;
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Check if button is pressed (LOW)
|
2022-01-14 15:36:17 +01:00
|
|
|
if (digitalRead(GPIO_BUTTON) == LOW)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Update button timestamp on the first button press
|
2022-01-14 15:36:17 +01:00
|
|
|
if (buttonTimestamp == 0)
|
|
|
|
buttonTimestamp = millis();
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Check and execute actions based on predefined time delays
|
2022-01-14 15:36:17 +01:00
|
|
|
if (buttonTimestamp + BUTTON_ACTION_DELAY_NOTHING < millis())
|
|
|
|
{
|
2023-03-02 22:30:42 +01:00
|
|
|
LED_Process(1, COLOR_WARM_WHITE);
|
2022-01-14 15:36:17 +01:00
|
|
|
buttonAction = BTN_NOTHING;
|
|
|
|
}
|
|
|
|
else if (buttonTimestamp + BUTTON_ACTION_DELAY_WIFI < millis())
|
|
|
|
{
|
2023-03-02 22:30:42 +01:00
|
|
|
LED_Process(1, LED_WIFI_BLINK);
|
2022-01-14 15:36:17 +01:00
|
|
|
buttonAction = BTN_TOGGLEWIFI;
|
|
|
|
}
|
|
|
|
else if (buttonTimestamp + BUTTON_ACTION_DELAY_PURGE < millis())
|
|
|
|
{
|
2023-03-02 22:30:42 +01:00
|
|
|
LED_Process(1, LED_PURGE_COLOR);
|
2022-01-14 15:36:17 +01:00
|
|
|
buttonAction = BTN_STARTPURGE;
|
|
|
|
}
|
|
|
|
else if (buttonTimestamp + BUTTON_ACTION_DELAY_TOGGLEMODE < millis())
|
|
|
|
{
|
2023-03-02 22:30:42 +01:00
|
|
|
uint32_t color = globals.systemStatus == sysStat_Normal ? LED_RAIN_COLOR : LED_NORMAL_COLOR;
|
2022-01-14 15:36:17 +01:00
|
|
|
LED_Process(1, color);
|
|
|
|
buttonAction = BTN_TOGGLEMODE;
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
else // Button is released
|
2022-01-14 15:36:17 +01:00
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Execute corresponding actions based on the detected button action
|
2022-01-14 15:36:17 +01:00
|
|
|
if (buttonAction != BTN_INACTIVE)
|
|
|
|
{
|
|
|
|
switch (buttonAction)
|
|
|
|
{
|
|
|
|
case BTN_TOGGLEWIFI:
|
2022-01-19 22:23:36 +01:00
|
|
|
toggleWiFiAP();
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("Starting WiFi AP\n");
|
2022-01-14 15:36:17 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BTN_STARTPURGE:
|
|
|
|
globals.systemStatus = sysStat_Purge;
|
|
|
|
globals.purgePulses = LubeConfig.BleedingPulses;
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("Starting Purge\n");
|
2022-01-14 15:36:17 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BTN_TOGGLEMODE:
|
|
|
|
switch (globals.systemStatus)
|
|
|
|
{
|
|
|
|
case sysStat_Normal:
|
|
|
|
globals.systemStatus = sysStat_Rain;
|
|
|
|
globals.resumeStatus = sysStat_Rain;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sysStat_Rain:
|
|
|
|
globals.systemStatus = sysStat_Normal;
|
|
|
|
globals.resumeStatus = sysStat_Normal;
|
|
|
|
break;
|
2024-01-09 12:54:05 +01:00
|
|
|
|
2022-01-14 15:36:17 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("Toggling Mode\n");
|
2022-01-14 15:36:17 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BTN_NOTHING:
|
|
|
|
default:
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("Nothing or invalid\n");
|
2022-01-14 15:36:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Display feedback through LEDs
|
2022-01-14 15:36:17 +01:00
|
|
|
LED_Process(2);
|
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Reset button state and timestamp
|
2022-01-14 15:36:17 +01:00
|
|
|
buttonAction = BTN_INACTIVE;
|
|
|
|
buttonTimestamp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Toggles the WiFi functionality based on the current status.
|
|
|
|
*
|
|
|
|
* This function manages the WiFi state, either turning it off or starting it as an Access Point (AP),
|
|
|
|
* depending on the current mode. If the WiFi is turned off, it can be started in AP mode with the
|
|
|
|
* device name and password configured. Additionally, it may stop certain operations related to WiFi
|
|
|
|
* maintenance or display debug messages based on the defined features.
|
|
|
|
*
|
|
|
|
* @param shutdown Flag indicating whether the system is in a shutdown state.
|
|
|
|
*/
|
2024-01-09 12:15:39 +01:00
|
|
|
void toggleWiFiAP(bool shutdown)
|
2022-01-14 15:36:17 +01:00
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Check if WiFi is currently active
|
2022-01-14 21:28:50 +01:00
|
|
|
if (WiFi.getMode() != WIFI_OFF)
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Turn off WiFi
|
2022-01-14 21:28:50 +01:00
|
|
|
WiFi.mode(WIFI_OFF);
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("WiFi turned off\n");
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Stop WiFi maintenance connection ticker if enabled
|
2022-05-04 23:06:15 +02:00
|
|
|
#ifdef FEATURE_ENABLE_WIFI_CLIENT
|
2022-01-19 22:23:36 +01:00
|
|
|
WiFiMaintainConnectionTicker.stop();
|
|
|
|
#endif
|
2022-01-14 21:28:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-09 12:54:05 +01:00
|
|
|
// Start WiFi in Access Point (AP) mode
|
2022-01-14 21:28:50 +01:00
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
WiFi.softAPConfig(IPAddress(WIFI_AP_IP_GW), IPAddress(WIFI_AP_IP_GW), IPAddress(255, 255, 255, 0));
|
2022-08-14 17:38:45 +02:00
|
|
|
WiFi.softAP(globals.DeviceName, QUOTE(WIFI_AP_PASSWORD));
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Stop WiFi maintenance connection ticker if enabled and display debug messages
|
2022-05-04 23:06:15 +02:00
|
|
|
#ifdef FEATURE_ENABLE_WIFI_CLIENT
|
2022-01-14 21:28:50 +01:00
|
|
|
WiFiMaintainConnectionTicker.stop();
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("WiFi AP started, stopped Maintain-Timer\n");
|
2022-01-14 21:28:50 +01:00
|
|
|
#else
|
2023-09-25 07:18:46 +02:00
|
|
|
Debug_pushMessage("WiFi AP started\n");
|
2022-01-14 21:28:50 +01:00
|
|
|
#endif
|
|
|
|
}
|
2022-01-19 22:23:36 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Performs necessary tasks before shutting down and optionally restarts the ESP.
|
|
|
|
*
|
|
|
|
* This function initiates a system shutdown, performing tasks such as storing configuration
|
|
|
|
* and persistence data to EEPROM before shutting down. If a restart is requested, the ESP
|
|
|
|
* will be restarted; otherwise, the system will enter an indefinite loop.
|
|
|
|
*
|
|
|
|
* @param restart Flag indicating whether to restart the ESP after shutdown (default: false).
|
|
|
|
*/
|
2024-01-09 12:15:39 +01:00
|
|
|
void SystemShutdown(bool restart)
|
2022-01-19 22:23:36 +01:00
|
|
|
{
|
2022-08-19 08:16:33 +02:00
|
|
|
static uint32_t shutdown_delay = 0;
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
// Initialize shutdown delay on the first call
|
2022-08-19 08:16:33 +02:00
|
|
|
if (shutdown_delay == 0)
|
|
|
|
{
|
|
|
|
shutdown_delay = millis() + SHUTDOWN_DELAY_MS;
|
2022-08-24 23:08:57 +02:00
|
|
|
Serial.printf("Shutdown requested - Restarting in %d seconds\n", SHUTDOWN_DELAY_MS / 1000);
|
2022-08-19 08:16:33 +02:00
|
|
|
}
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Check if the shutdown delay has elapsed
|
2022-08-19 08:16:33 +02:00
|
|
|
if (shutdown_delay < millis())
|
|
|
|
{
|
2024-01-12 13:29:22 +01:00
|
|
|
Webserver_Shutdown();
|
|
|
|
|
|
|
|
// Store persistence data to EEPROM
|
2022-08-19 08:16:33 +02:00
|
|
|
StorePersistence_EEPROM();
|
2024-01-09 12:54:05 +01:00
|
|
|
|
|
|
|
// Perform restart if requested, otherwise enter an indefinite loop
|
2024-01-09 12:15:39 +01:00
|
|
|
if (restart)
|
|
|
|
ESP.restart();
|
|
|
|
else
|
|
|
|
while (1)
|
|
|
|
;
|
2022-08-19 08:16:33 +02:00
|
|
|
}
|
2022-01-31 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 12:54:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Processes the impulses from the wheel speed sensor and converts them into traveled distance.
|
|
|
|
*
|
|
|
|
* This function takes the pulse count from the wheel speed sensor and converts it into distance
|
|
|
|
* traveled in millimeters. The conversion is based on the configured parameters such as the number
|
|
|
|
* of pulses per revolution and the distance traveled per revolution.
|
|
|
|
*
|
|
|
|
* @return The calculated distance traveled in millimeters.
|
|
|
|
*/
|
2022-02-04 21:28:18 +01:00
|
|
|
uint32_t Process_Impulse_WheelSpeed()
|
2022-01-31 09:26:10 +01:00
|
|
|
{
|
2023-03-10 10:25:01 +01:00
|
|
|
uint32_t add_milimeters = 0;
|
2022-02-04 21:28:18 +01:00
|
|
|
// Calculate traveled Distance in mm
|
2023-03-10 10:25:01 +01:00
|
|
|
if (LubeConfig.PulsePerRevolution != 0)
|
|
|
|
add_milimeters = (wheel_pulse * (LubeConfig.DistancePerRevolution_mm / LubeConfig.PulsePerRevolution));
|
2023-03-03 10:51:16 +01:00
|
|
|
|
|
|
|
if (globals.measurementActive == true)
|
|
|
|
globals.measuredPulses = globals.measuredPulses + wheel_pulse;
|
|
|
|
|
2022-02-04 21:28:18 +01:00
|
|
|
wheel_pulse = 0;
|
2022-01-31 09:26:10 +01:00
|
|
|
|
2022-02-04 21:28:18 +01:00
|
|
|
return add_milimeters;
|
2022-05-01 15:15:32 +02:00
|
|
|
}
|