Reworked Button-Stuff
This commit is contained in:
		| @@ -30,6 +30,7 @@ build_flags = | |||||||
|   -DWIFI_PASSWORD=${wifi_cred.wifi_password} |   -DWIFI_PASSWORD=${wifi_cred.wifi_password} | ||||||
|   -DADMIN_PASSWORD=${wifi_cred.admin_password} |   -DADMIN_PASSWORD=${wifi_cred.admin_password} | ||||||
|   -DWIFI_AP_PASSWORD=${wifi_cred.wifi_ap_password} |   -DWIFI_AP_PASSWORD=${wifi_cred.wifi_ap_password} | ||||||
|  |   -DWIFI_AP_IP_GW=10,0,0,1 | ||||||
|  |  | ||||||
| board_build.filesystem = littlefs | board_build.filesystem = littlefs | ||||||
|  |  | ||||||
|   | |||||||
| @@ -13,10 +13,12 @@ typedef enum eSystem_Status | |||||||
|   sysStat_Error |   sysStat_Error | ||||||
| } tSystem_Status; | } tSystem_Status; | ||||||
|  |  | ||||||
|  |  | ||||||
| typedef struct Globals_s { | typedef struct Globals_s { | ||||||
|   tSystem_Status systemStatus = sysStat_Startup; |   tSystem_Status systemStatus = sysStat_Startup; | ||||||
|   uint8_t purgePulses= 0; |   uint8_t purgePulses= 0; | ||||||
|   tSystem_Status resumeStatus = sysStat_Startup; |   tSystem_Status resumeStatus = sysStat_Startup; | ||||||
|  |   char systemStatustxt[16] = ""; | ||||||
| }Globals_t; | }Globals_t; | ||||||
|  |  | ||||||
| extern Globals_t globals; | extern Globals_t globals; | ||||||
|   | |||||||
| @@ -4,37 +4,6 @@ uint32_t lubePulseTimestamp = 0; | |||||||
|  |  | ||||||
| void RunLubeApp(volatile uint32_t *wheelPulseCounter) | void RunLubeApp(volatile uint32_t *wheelPulseCounter) | ||||||
| { | { | ||||||
|     static uint32_t buttonTimestamp = 0; |  | ||||||
|     static boolean buttonActionDone = false; |  | ||||||
|  |  | ||||||
|     if (digitalRead(GPIO_BUTTON) == LOW) |  | ||||||
|     { |  | ||||||
|         if (buttonTimestamp == 0) |  | ||||||
|             buttonTimestamp = millis() + BUTTON_HOLD_DELAY; |  | ||||||
|  |  | ||||||
|         if (buttonTimestamp < millis() && buttonActionDone == false) |  | ||||||
|         { |  | ||||||
|             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; |  | ||||||
|             } |  | ||||||
|             buttonActionDone = true; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     else |  | ||||||
|     { |  | ||||||
|         buttonTimestamp = 0; |  | ||||||
|         buttonActionDone = false; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // Calculate traveled Distance in mm |     // Calculate traveled Distance in mm | ||||||
|     TravelDistance_highRes += (*wheelPulseCounter * (LubeConfig.DistancePerRevolution_mm / LubeConfig.PulsePerRevolution)); |     TravelDistance_highRes += (*wheelPulseCounter * (LubeConfig.DistancePerRevolution_mm / LubeConfig.PulsePerRevolution)); | ||||||
|     *wheelPulseCounter = 0; |     *wheelPulseCounter = 0; | ||||||
| @@ -43,8 +12,11 @@ void RunLubeApp(volatile uint32_t *wheelPulseCounter) | |||||||
|     switch (globals.systemStatus) |     switch (globals.systemStatus) | ||||||
|     { |     { | ||||||
|     case sysStat_Startup: |     case sysStat_Startup: | ||||||
|         globals.systemStatus = sysStat_Normal; |         if (millis() > STARTUP_DELAY) | ||||||
|         globals.resumeStatus = sysStat_Normal; |         { | ||||||
|  |             globals.systemStatus = sysStat_Normal; | ||||||
|  |             globals.resumeStatus = sysStat_Normal; | ||||||
|  |         } | ||||||
|         break; |         break; | ||||||
|  |  | ||||||
|     case sysStat_Normal: |     case sysStat_Normal: | ||||||
| @@ -81,6 +53,22 @@ void RunLubeApp(volatile uint32_t *wheelPulseCounter) | |||||||
|         break; |         break; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     switch (globals.systemStatus) | ||||||
|  |     { | ||||||
|  |     case sysStat_Normal: | ||||||
|  |         strcpy(globals.systemStatustxt, "Normal"); | ||||||
|  |         break; | ||||||
|  |     case sysStat_Purge: | ||||||
|  |         strcpy(globals.systemStatustxt, "Purge"); | ||||||
|  |         break; | ||||||
|  |     case sysStat_Rain: | ||||||
|  |         strcpy(globals.systemStatustxt, "Rain"); | ||||||
|  |         break; | ||||||
|  |     case sysStat_Startup: | ||||||
|  |         strcpy(globals.systemStatustxt, "Startup"); | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     // maintain Pin-State of Lube-Pump |     // maintain Pin-State of Lube-Pump | ||||||
|     if (lubePulseTimestamp > millis()) |     if (lubePulseTimestamp > millis()) | ||||||
|         digitalWrite(GPIO_PUMP, HIGH); |         digitalWrite(GPIO_PUMP, HIGH); | ||||||
|   | |||||||
| @@ -2,13 +2,14 @@ | |||||||
| #define _LUBEAPP_H_ | #define _LUBEAPP_H_ | ||||||
|  |  | ||||||
| #include <Arduino.h> | #include <Arduino.h> | ||||||
|  | #include <stdlib.h> | ||||||
| #include "config.h" | #include "config.h" | ||||||
| #include "common.h" | #include "common.h" | ||||||
| #include "globals.h" | #include "globals.h" | ||||||
|  |  | ||||||
| #define LUBE_PULSE_LENGHT_MS 160 | #define LUBE_PULSE_LENGHT_MS 160 | ||||||
| #define LUBE_PULSE_PAUSE_MS 100 | #define LUBE_PULSE_PAUSE_MS 100 | ||||||
| #define BUTTON_HOLD_DELAY 1000 | #define STARTUP_DELAY 5000 | ||||||
|  |  | ||||||
| void RunLubeApp(volatile uint32_t * wheelPulseCounter); | void RunLubeApp(volatile uint32_t * wheelPulseCounter); | ||||||
| void LubePulse(); | void LubePulse(); | ||||||
|   | |||||||
| @@ -47,8 +47,10 @@ void RemoteDebug_printWifiInfo(); | |||||||
| void wifiMaintainConnectionTicker_callback(); | void wifiMaintainConnectionTicker_callback(); | ||||||
| void updateWebUITicker_callback(); | void updateWebUITicker_callback(); | ||||||
| void IRAM_ATTR trigger_ISR(); | void IRAM_ATTR trigger_ISR(); | ||||||
| void LED_Process(tSystem_Status newStatus = sysStat_NOP); | void LED_Process(uint8_t override = false, CRGB setColor = CRGB::White); | ||||||
| void DisplayProcess(); | void Display_Process(); | ||||||
|  | void Button_Process(); | ||||||
|  | void startWiFiAP(); | ||||||
|  |  | ||||||
| Ticker WiFiMaintainConnectionTicker(wifiMaintainConnectionTicker_callback, 1000, 0, MILLIS); | Ticker WiFiMaintainConnectionTicker(wifiMaintainConnectionTicker_callback, 1000, 0, MILLIS); | ||||||
| Ticker UpdateWebUITicker(updateWebUITicker_callback, 5000, 0, MILLIS); | Ticker UpdateWebUITicker(updateWebUITicker_callback, 5000, 0, MILLIS); | ||||||
| @@ -142,13 +144,14 @@ void setup() | |||||||
|  |  | ||||||
| void loop() | void loop() | ||||||
| { | { | ||||||
|   LED_Process(globals.systemStatus); |  | ||||||
|   RunLubeApp(&wheel_pulse); |   RunLubeApp(&wheel_pulse); | ||||||
|  |  | ||||||
|   WiFiMaintainConnectionTicker.update(); |   WiFiMaintainConnectionTicker.update(); | ||||||
|   UpdateWebUITicker.update(); |   UpdateWebUITicker.update(); | ||||||
|  |  | ||||||
|   DisplayProcess(); |   Display_Process(); | ||||||
|  |   Button_Process(); | ||||||
|  |   LED_Process(); | ||||||
|  |  | ||||||
|   ArduinoOTA.handle(); |   ArduinoOTA.handle(); | ||||||
|   Debug.handle(); |   Debug.handle(); | ||||||
| @@ -216,10 +219,7 @@ void wifiMaintainConnectionTicker_callback() | |||||||
|     else |     else | ||||||
|     { |     { | ||||||
|       Serial.println("WiFi not connected! - Start AP"); |       Serial.println("WiFi not connected! - Start AP"); | ||||||
|       WiFi.mode(WIFI_OFF); |       startWiFiAP(); | ||||||
|       WiFi.softAP(DeviceName, QUOTE(WIFI_AP_PASSWORD)); |  | ||||||
|       WiFiMaintainConnectionTicker.stop(); |  | ||||||
|       Serial.println("WiFi AP started, stopped Maintain-Timer"); |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -234,7 +234,7 @@ void trigger_ISR() | |||||||
|   wheel_pulse++; |   wheel_pulse++; | ||||||
| } | } | ||||||
|  |  | ||||||
| void LED_Process(tSystem_Status newSysStatus) | void LED_Process(uint8_t override, CRGB SetColor) | ||||||
| { | { | ||||||
|  |  | ||||||
|   typedef enum |   typedef enum | ||||||
| @@ -245,20 +245,43 @@ void LED_Process(tSystem_Status newSysStatus) | |||||||
|     LED_Rain, |     LED_Rain, | ||||||
|     LED_Confirm_Rain, |     LED_Confirm_Rain, | ||||||
|     LED_Purge, |     LED_Purge, | ||||||
|     LED_Error |     LED_Error, | ||||||
|  |     LED_Override | ||||||
|   } tLED_Status; |   } tLED_Status; | ||||||
|  |  | ||||||
|   static tSystem_Status oldSysStatus = sysStat_NOP; |   static tSystem_Status oldSysStatus = sysStat_NOP; | ||||||
|   static tLED_Status LED_Status = LED_Startup; |   static tLED_Status LED_Status = LED_Startup; | ||||||
|  |   static CRGB LED_override_color = 0; | ||||||
|  |   static tLED_Status LED_ResumeOverrideStatus = LED_Startup; | ||||||
|  |  | ||||||
|   uint8_t color = 0; |   uint8_t color = 0; | ||||||
|   uint32_t timer = 0; |   uint32_t timer = 0; | ||||||
|   static uint32_t timestamp = 0; |   static uint32_t timestamp = 0; | ||||||
|   timer = millis(); |   timer = millis(); | ||||||
|  |  | ||||||
|   if (oldSysStatus != newSysStatus) |   if (override == 1) | ||||||
|   { |   { | ||||||
|     switch (newSysStatus) |     if (LED_Status != LED_Override) | ||||||
|  |     { | ||||||
|  |       LED_ResumeOverrideStatus = LED_Status; | ||||||
|  |       debugA("Override LED_Status"); | ||||||
|  |     } | ||||||
|  |     LED_Status = LED_Override; | ||||||
|  |     LED_override_color = SetColor; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if (override == 2) | ||||||
|  |   { | ||||||
|  |     if (LED_Status == LED_Override) | ||||||
|  |     { | ||||||
|  |       LED_Status = LED_ResumeOverrideStatus; | ||||||
|  |       debugA("Resume LED_Status"); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if (oldSysStatus != globals.systemStatus) | ||||||
|  |   { | ||||||
|  |     switch (globals.systemStatus) | ||||||
|     { |     { | ||||||
|     case sysStat_Startup: |     case sysStat_Startup: | ||||||
|       LED_Status = LED_Startup; |       LED_Status = LED_Startup; | ||||||
| @@ -286,16 +309,20 @@ void LED_Process(tSystem_Status newSysStatus) | |||||||
|     default: |     default: | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     oldSysStatus = newSysStatus; |     oldSysStatus = globals.systemStatus; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   uint32_t percentage = LubeConfig.tankRemain_µl / (LubeConfig.tankCapacity_ml * 10); | ||||||
|  |  | ||||||
|   switch (LED_Status) |   switch (LED_Status) | ||||||
|   { |   { | ||||||
|   case LED_Startup: |   case LED_Startup: | ||||||
|     FastLED.setBrightness(255); |     FastLED.setBrightness(255); | ||||||
|     timer = timer % 2000; |  | ||||||
|     color = timer > 500 ? 0 : (uint8_t)(timer / 2); |     if (percentage < LubeConfig.TankRemindAtPercentage) | ||||||
|     leds[0] = CRGB(color, color, color); |       leds[0] = CRGB::OrangeRed; | ||||||
|  |     else | ||||||
|  |       leds[0] = CRGB::White; | ||||||
|     break; |     break; | ||||||
|  |  | ||||||
|   case LED_Confirm_Normal: |   case LED_Confirm_Normal: | ||||||
| @@ -342,19 +369,128 @@ void LED_Process(tSystem_Status newSysStatus) | |||||||
|     leds[0] = timer % 500 > 250 ? CRGB::Red : CRGB::Black; |     leds[0] = timer % 500 > 250 ? CRGB::Red : CRGB::Black; | ||||||
|     break; |     break; | ||||||
|  |  | ||||||
|  |   case LED_Override: | ||||||
|  |     leds[0] = LED_override_color; | ||||||
|  |     break; | ||||||
|  |  | ||||||
|   default: |   default: | ||||||
|     break; |     break; | ||||||
|   } |   } | ||||||
|   FastLED.show(); |   FastLED.show(); | ||||||
| } | } | ||||||
|  |  | ||||||
| void DisplayProcess() | void Display_Process() | ||||||
| { | { | ||||||
|   u8x8.setCursor(0, 3); |   u8x8.setCursor(0, 3); | ||||||
|   uint32_t DistRemain = globals.systemStatus == sysStat_Normal ? LubeConfig.DistancePerLube_Default : LubeConfig.DistancePerLube_Rain; |   uint32_t DistRemain = globals.systemStatus == sysStat_Normal ? LubeConfig.DistancePerLube_Default : LubeConfig.DistancePerLube_Rain; | ||||||
|   DistRemain -= TravelDistance_highRes / 1000; |   DistRemain -= TravelDistance_highRes / 1000; | ||||||
|   u8x8.printf("next Lube: %4dm\n", DistRemain); |   u8x8.printf("Mode: %10s\n\n", globals.systemStatustxt); | ||||||
|   u8x8.printf("Tank: %8dml\n", LubeConfig.tankRemain_µl / 1000); |   u8x8.printf("next Lube: %4dm\n\n", DistRemain); | ||||||
|   u8x8.printf("Purges: %8d\n", globals.purgePulses); |   u8x8.printf("Tank: %8dml\n\n", LubeConfig.tankRemain_µl / 1000); | ||||||
|   u8x8.refreshDisplay(); |   u8x8.refreshDisplay(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void Button_Process() | ||||||
|  | { | ||||||
|  |  | ||||||
|  | #define BUTTON_ACTION_DELAY_TOGGLEMODE 500 | ||||||
|  | #define BUTTON_ACTION_DELAY_PURGE 3500 | ||||||
|  | #define BUTTON_ACTION_DELAY_WIFI 6500 | ||||||
|  | #define BUTTON_ACTION_DELAY_NOTHING 9500 | ||||||
|  |  | ||||||
|  |   typedef enum buttonAction_e | ||||||
|  |   { | ||||||
|  |     BTN_INACTIVE, | ||||||
|  |     BTN_NOTHING, | ||||||
|  |     BTN_TOGGLEMODE, | ||||||
|  |     BTN_TOGGLEWIFI, | ||||||
|  |     BTN_STARTPURGE | ||||||
|  |   } buttonAction_t; | ||||||
|  |  | ||||||
|  |   static uint32_t buttonTimestamp = 0; | ||||||
|  |   static buttonAction_t buttonAction = BTN_INACTIVE; | ||||||
|  |  | ||||||
|  |   if (digitalRead(GPIO_BUTTON) == LOW) | ||||||
|  |   { | ||||||
|  |  | ||||||
|  |     if (buttonTimestamp == 0) | ||||||
|  |       buttonTimestamp = millis(); | ||||||
|  |  | ||||||
|  |     if (buttonTimestamp + BUTTON_ACTION_DELAY_NOTHING < millis()) | ||||||
|  |     { | ||||||
|  |       LED_Process(1, CRGB::White); | ||||||
|  |       buttonAction = BTN_NOTHING; | ||||||
|  |     } | ||||||
|  |     else if (buttonTimestamp + BUTTON_ACTION_DELAY_WIFI < millis()) | ||||||
|  |     { | ||||||
|  |       LED_Process(1, CRGB::Yellow); | ||||||
|  |       buttonAction = BTN_TOGGLEWIFI; | ||||||
|  |     } | ||||||
|  |     else if (buttonTimestamp + BUTTON_ACTION_DELAY_PURGE < millis()) | ||||||
|  |     { | ||||||
|  |       LED_Process(1, CRGB::DeepPink); | ||||||
|  |       buttonAction = BTN_STARTPURGE; | ||||||
|  |     } | ||||||
|  |     else if (buttonTimestamp + BUTTON_ACTION_DELAY_TOGGLEMODE < millis()) | ||||||
|  |     { | ||||||
|  |       CRGB color = globals.systemStatus == sysStat_Normal ? CRGB::Blue : CRGB::Green; | ||||||
|  |       LED_Process(1, color); | ||||||
|  |       buttonAction = BTN_TOGGLEMODE; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   else | ||||||
|  |   { | ||||||
|  |     if (buttonAction != BTN_INACTIVE) | ||||||
|  |     { | ||||||
|  |       switch (buttonAction) | ||||||
|  |       { | ||||||
|  |       case BTN_TOGGLEWIFI: | ||||||
|  |         startWiFiAP(); | ||||||
|  |         debugA("Starting WiFi AP"); | ||||||
|  |         break; | ||||||
|  |  | ||||||
|  |       case BTN_STARTPURGE: | ||||||
|  |         globals.systemStatus = sysStat_Purge; | ||||||
|  |         globals.purgePulses = LubeConfig.BleedingPulses; | ||||||
|  |         debugA("Starting Purge"); | ||||||
|  |         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; | ||||||
|  |         default: | ||||||
|  |           break; | ||||||
|  |         } | ||||||
|  |         debugA("Toggling Mode"); | ||||||
|  |         break; | ||||||
|  |  | ||||||
|  |       case BTN_NOTHING: | ||||||
|  |       default: | ||||||
|  |         debugA("Nothing or invalid"); | ||||||
|  |         break; | ||||||
|  |       } | ||||||
|  |       LED_Process(2); | ||||||
|  |     } | ||||||
|  |     buttonAction = BTN_INACTIVE; | ||||||
|  |     buttonTimestamp = 0; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void startWiFiAP() | ||||||
|  | { | ||||||
|  |   WiFi.mode(WIFI_OFF); | ||||||
|  |   WiFi.mode(WIFI_AP); | ||||||
|  |   WiFi.softAPConfig(IPAddress(WIFI_AP_IP_GW), IPAddress(WIFI_AP_IP_GW), IPAddress(255, 255, 255, 0)); | ||||||
|  |   WiFi.softAP(DeviceName, QUOTE(WIFI_AP_PASSWORD)); | ||||||
|  |   WiFiMaintainConnectionTicker.stop(); | ||||||
|  |   Serial.println("WiFi AP started, stopped Maintain-Timer"); | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user