From 829e70d11a3440a83d615cb2982fed3a5d3773d4 Mon Sep 17 00:00:00 2001 From: Marcel Peterkau Date: Mon, 10 Jan 2022 00:02:21 +0100 Subject: [PATCH] =?UTF-8?q?LED-Tweak=20and=20some=20first=20Functions=20f?= =?UTF-8?q?=C3=BCr=20LubeApp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Software/ChainLube/src/common.h | 6 +- Software/ChainLube/src/globals.h | 24 ++++++ Software/ChainLube/src/lubeapp.cpp | 31 +++++++- Software/ChainLube/src/lubeapp.h | 14 +--- Software/ChainLube/src/main.cpp | 115 +++++++++++++++++------------ 5 files changed, 127 insertions(+), 63 deletions(-) create mode 100644 Software/ChainLube/src/globals.h diff --git a/Software/ChainLube/src/common.h b/Software/ChainLube/src/common.h index 570888c..5c5db90 100644 --- a/Software/ChainLube/src/common.h +++ b/Software/ChainLube/src/common.h @@ -4,10 +4,10 @@ #define Q(x) #x #define QUOTE(x) Q(x) -#define GPIO_BUTTON 14 +#define GPIO_BUTTON D5 #define GPIO_LED D6 -#define GPIO_TRIGGER 2 -#define GPIO_PUMP 0 +#define GPIO_TRIGGER D4 +#define GPIO_PUMP D3 #ifndef HOST_NAME #define HOST_NAME "ChainLube_%06X" // Use printf-Formatting - Chip-ID (uin32_t) will be added diff --git a/Software/ChainLube/src/globals.h b/Software/ChainLube/src/globals.h new file mode 100644 index 0000000..e6d046a --- /dev/null +++ b/Software/ChainLube/src/globals.h @@ -0,0 +1,24 @@ +#ifndef _GLOBALS_H_ +#define _GLOBALS_H_ + +#include + +typedef enum eSystem_Status +{ + sysStat_NOP, + sysStat_Startup, + sysStat_Normal, + sysStat_Rain, + sysStat_Purge, + sysStat_Error +} tSystem_Status; + +typedef struct Globals_t { + tSystem_Status systemStatus = sysStat_Startup; + uint8_t purgePulses= 0; +}; + +extern Globals_t globals; +extern uint32_t TravelDistance_highRes; + +#endif \ No newline at end of file diff --git a/Software/ChainLube/src/lubeapp.cpp b/Software/ChainLube/src/lubeapp.cpp index 4339648..14440fa 100644 --- a/Software/ChainLube/src/lubeapp.cpp +++ b/Software/ChainLube/src/lubeapp.cpp @@ -1,7 +1,32 @@ #include "lubeapp.h" +extern void LED_Process(); -void RunLubeApp(LubeConfig_t *cfg) +void RunLubeApp(volatile uint32_t *wheelPulseCounter) { - -} \ No newline at end of file + uint32_t LubeDistance = 0; + static uint32_t lubePulseTimestamp = 0; + // Calculate traveled Distance in mm + TravelDistance_highRes += (*wheelPulseCounter * LubeConfig.DistancePerRevolution_mm); + *wheelPulseCounter = 0; + + // check if we have to Lube! + switch (globals.systemStatus) + { + case sysStat_Normal: + LubeDistance = LubeConfig.DistancePerLube_Default; + break; + case sysStat_Rain: + LubeDistance = LubeConfig.DistancePerLube_Rain; + break; + } + + if (TravelDistance_highRes > LubeDistance) + lubePulseTimestamp = millis() + LUBE_PULSE_LENGHT_MS; + + //maintain Pin-State of Lube-Pump + if (lubePulseTimestamp > millis()) + digitalWrite(GPIO_PUMP, HIGH); + else + digitalWrite(GPIO_PUMP, LOW); +} diff --git a/Software/ChainLube/src/lubeapp.h b/Software/ChainLube/src/lubeapp.h index 4ffab84..c2aa06d 100644 --- a/Software/ChainLube/src/lubeapp.h +++ b/Software/ChainLube/src/lubeapp.h @@ -3,17 +3,11 @@ #include #include "config.h" +#include "common.h" +#include "globals.h" -typedef enum eSystem_Status -{ - sysStat_NOP, - sysStat_Startup, - sysStat_Normal, - sysStat_Rain, - sysStat_Purge, - sysStat_Error -} tSystem_Status; +#define LUBE_PULSE_LENGHT_MS 1000 -void RunLubeApp(LubeConfig_t *cfg); +void RunLubeApp(volatile uint32_t * wheelPulseCounter); #endif \ No newline at end of file diff --git a/Software/ChainLube/src/main.cpp b/Software/ChainLube/src/main.cpp index d8f899b..acaf4d3 100644 --- a/Software/ChainLube/src/main.cpp +++ b/Software/ChainLube/src/main.cpp @@ -14,6 +14,7 @@ #include "lubeapp.h" #include "webui.h" #include "config.h" +#include "globals.h" const char *ssid = QUOTE(WIFI_SSID); const char *password = QUOTE(WIFI_PASSWORD); @@ -29,6 +30,8 @@ const bool debug_flag = false; bool startSetupMode = false; char DeviceName[33]; +Globals_t globals; +uint32_t TravelDistance_highRes; volatile uint32_t wheel_pulse = 0; U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(-1); @@ -43,16 +46,19 @@ void RemotDebug_printSystemInfo(); void RemoteDebug_printWifiInfo(); void wifiMaintainConnectionTicker_callback(); void IRAM_ATTR trigger_ISR(); -void LED_Process(tSystem_Status newStatus); +void LED_Process(tSystem_Status newStatus = sysStat_NOP); Ticker WiFiMaintainConnectionTicker(wifiMaintainConnectionTicker_callback, 1000, 0, MILLIS); void setup() { system_update_cpu_freq(SYS_CPU_80MHZ); + snprintf(DeviceName, 32, HOST_NAME, ESP.getChipId()); + WiFi.mode(WIFI_OFF); WiFi.persistent(false); + Serial.begin(115200); Serial.setDebugOutput(true); @@ -76,15 +82,6 @@ void setup() WiFi.setHostname(DeviceName); wifiMulti.addAP(QUOTE(WIFI_SSID), QUOTE(WIFI_PASSWORD)); - Serial.println("Connecting Wifi..."); - if (wifiMulti.run() == WL_CONNECTED) - { - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.print(WiFi.localIP()); - } - WiFiMaintainConnectionTicker.start(); if (MDNS.begin(DeviceName)) @@ -141,16 +138,15 @@ void setup() void loop() { - - tSystem_Status status = sysStat_NOP; - - // status = digitalRead(GPIO_BUTTON) ? Ready_normal : Ready_rain; - - LED_Process(status); - RunLubeApp(&LubeConfig); + LED_Process(); + RunLubeApp(&wheel_pulse); WiFiMaintainConnectionTicker.update(); - FastLED.show(); + + u8x8.setCursor(0, 3); + u8x8.printf("%4d m", TravelDistance_highRes / 1000); + u8x8.refreshDisplay(); + ArduinoOTA.handle(); Debug.handle(); yield(); @@ -230,12 +226,11 @@ void trigger_ISR() wheel_pulse++; } -void LED_Process(tSystem_Status newSysStatus = sysStat_NOP) +void LED_Process(tSystem_Status newSysStatus) { typedef enum { - LED_NOP, LED_Startup, LED_Normal, LED_Confirm_Normal, @@ -246,61 +241,87 @@ void LED_Process(tSystem_Status newSysStatus = sysStat_NOP) } tLED_Status; static tSystem_Status oldSysStatus = sysStat_NOP; - static tLED_Status LED_Status = LED_NOP; - uint8_t color; - uint32_t timer; + static tLED_Status LED_Status = LED_Startup; - if (oldSysStatus != newSysStatus) + uint8_t color = 0; + uint32_t timer = 0; + uint32_t timestamp = 0; + + if (newSysStatus != sysStat_NOP) { - switch (newSysStatus) + if (oldSysStatus != newSysStatus) { - case sysStat_NOP: - break; - case sysStat_Startup: - break; - case sysStat_Normal: - break; - case sysStat_Rain: - break; - case sysStat_Purge: - break; - case sysStat_Error: - break; + switch (newSysStatus) + { + case sysStat_Startup: + LED_Status = LED_Startup; + break; + case sysStat_Normal: + timestamp = millis() + 1500; + LED_Status = LED_Confirm_Normal; + FastLED.setBrightness(64); + break; + case sysStat_Rain: + timestamp = millis() + 1500; + LED_Status = LED_Confirm_Rain; + FastLED.setBrightness(64); + break; + case sysStat_Purge: + LED_Status = LED_Purge; + break; + case sysStat_Error: + LED_Status = LED_Error; + break; + } + oldSysStatus = newSysStatus; } } - - FastLED.setBrightness(64); timer = millis(); switch (LED_Status) { - case LED_NOP: - break; case LED_Startup: timer = timer % 2000; color = timer > 500 ? 0 : (uint8_t)(timer / 2); leds[0] = CRGB(color, color, 0); break; + case LED_Confirm_Normal: - leds[0] = millis() % 2000 > 1900 ? CRGB::Green : CRGB::Black; + if (timestamp < timer) + LED_Status = LED_Normal; + leds[0] = timer % 500 > 250 ? CRGB::Green : CRGB::Black; break; + case LED_Normal: - leds[0] = millis() % 2000 > 1900 ? CRGB::Blue : CRGB::Black; + leds[0] = timer % 2000 > 1950 ? CRGB::Green : CRGB::Black; break; + case LED_Confirm_Rain: - leds[0] = millis() % 2000 > 1900 ? CRGB::Green : CRGB::Black; + if (timestamp < timer) + LED_Status = LED_Rain; + leds[0] = timer % 500 > 250 ? CRGB::Blue : CRGB::Black; break; + case LED_Rain: - leds[0] = millis() % 2000 > 1900 ? CRGB::Blue : CRGB::Black; + leds[0] = timer % 2000 > 1950 ? CRGB::Blue : CRGB::Black; break; + case LED_Purge: - leds[0] = millis() % 2000 > 1900 ? CRGB::HotPink : CRGB::Black; + timer = timer % 500; + color = timer / 2; + leds[0] = CRGB::DeepPink; + if (timer < 250) + FastLED.setBrightness(color); + else + FastLED.setBrightness(250 - color); break; + case LED_Error: - leds[0] = millis() % 500 > 250 ? CRGB::Red : CRGB::Black; + leds[0] = timer % 500 > 250 ? CRGB::Red : CRGB::Black; break; default: break; } + FastLED.show(); }