add led handling for m5atom

This commit is contained in:
andreas 2021-11-06 16:02:30 +01:00
parent 33b2810f97
commit 62151f9c3c
6 changed files with 118 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#include "GwButtons.h"
#include "GwHardware.h"
#include "GwApi.h"
#include "GwLeds.h"
class FactoryResetRequest: public GwMessage{
private:
@ -50,6 +51,7 @@ void handleButtons(void *param){
const unsigned long OFF_TIME=20;
const unsigned long REPORT_TIME=1000;
const unsigned long HARD_REST_TIME=10000;
GwLedMode ledMode=LED_OFF;
while(true){
delay(10);
int current=digitalRead(GWBUTTON_PIN);
@ -58,6 +60,10 @@ void handleButtons(void *param){
if (lastPressed != 0 && (lastPressed+OFF_TIME) < now){
lastPressed=0; //finally off
firstPressed=0;
if (ledMode != LED_OFF){
setLedMode(LED_GREEN); //TODO: better "go back"
ledMode=LED_OFF;
}
LOG_DEBUG(GwLog::LOG,"Button press stopped");
}
continue;
@ -72,6 +78,17 @@ void handleButtons(void *param){
LOG_DEBUG(GwLog::LOG,"Button active for %ld",(now-firstPressed));
lastReport=now;
}
GwLedMode nextMode=ledMode;
if (now > (firstPressed+HARD_REST_TIME/2)){
nextMode=LED_BLUE;
}
if (now > (firstPressed+HARD_REST_TIME*0.9)){
nextMode=LED_RED;
}
if (ledMode != nextMode){
setLedMode(nextMode);
ledMode=nextMode;
}
if (now > (firstPressed+HARD_REST_TIME)){
LOG_DEBUG(GwLog::ERROR,"Factory reset by button");
GwMessage *r=new FactoryResetRequest(api);

View File

@ -26,10 +26,32 @@
#define GWBUTTON_PIN GPIO_NUM_39
#define GWBUTTON_ACTIVE LOW
//if GWBUTTON_PULLUPDOWN we enable a pulup/pulldown
#define GWBUTTON_PULLUPDOWN
#define GWBUTTON_PULLUPDOWN
//led handling
//if we define GWLED_FASTNET the arduino fastnet lib is used
#define GWLED_FASTLED
#define GWLED_TYPE SK6812
//color schema for fastled
#define GWLED_SCHEMA GRB
#define GWLED_PIN GPIO_NUM_27
//brightness 0...255
#define GWLED_BRIGHTNESS 64
#elif BOARD_M5ATOM_CANUNIT
#define ESP32_CAN_TX_PIN GPIO_NUM_26
#define ESP32_CAN_RX_PIN GPIO_NUM_32
#define GWBUTTON_PIN GPIO_NUM_39
#define GWBUTTON_ACTIVE LOW
//if GWBUTTON_PULLUPDOWN we enable a pulup/pulldown
#define GWBUTTON_PULLUPDOWN
//led handling
//if we define GWLED_FASTNET the arduino fastnet lib is used
#define GWLED_FASTLED
#define GWLED_TYPE SK6812
//color schema for fastled
#define GWLED_SCHEMA GRB
#define GWLED_PIN GPIO_NUM_27
//brightness 0...255
#define GWLED_BRIGHTNESS 64
#elif BOARD_M5STICK_CANUNIT
#define ESP32_CAN_TX_PIN GPIO_NUM_32
#define ESP32_CAN_RX_PIN GPIO_NUM_33

60
lib/led/GwLeds.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "GwLeds.h"
#include "GwHardware.h"
#include "GwApi.h"
#include "FastLED.h"
static GwLedMode mode=LED_OFF;
void setLedMode(GwLedMode newMode){
//we consider the mode to an atomic item...
mode=newMode;
}
static CRGB::HTMLColorCode colorFromMode(GwLedMode cmode){
switch(cmode){
case LED_BLUE:
return CRGB::Blue;
case LED_GREEN:
return CRGB::Green;
case LED_RED:
return CRGB::Red;
case LED_WHITE:
return CRGB::White;
default:
return CRGB::Black;
}
}
void handleLeds(void *param){
GwApi *api=(GwApi*)param;
GwLog *logger=api->getLogger();
#ifndef GWLED_FASTLED
LOG_DEBUG(GwLog::LOG,"currently only fastled handling");
vTaskDelete(NULL);
return;
#else
CRGB leds[1];
#ifdef GWLED_SCHEMA
FastLED.addLeds<GWLED_TYPE,GWLED_PIN,GWLED_SCHEMA>(leds,1);
#else
FastLED.addLeds<GWLED_TYPE,GWLED_PIN>(leds,1);
#endif
#ifdef GWLED_BRIGHTNESS
uint8_t brightness=GWLED_BRIGHTNESS;
#else
uint8_t brightness=128; //50%
#endif
GwLedMode currentMode=mode;
leds[0]=colorFromMode(currentMode);
FastLED.setBrightness(brightness);
FastLED.show();
while(true){
delay(50);
GwLedMode newMode=mode;
if (newMode != currentMode){
leds[0]=colorFromMode(newMode);
FastLED.show();
currentMode=newMode;
}
}
vTaskDelete(NULL);
#endif
}

13
lib/led/GwLeds.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _GWLEDS_H
#define _GWLEDS_H
//task function
void handleLeds(void *param);
typedef enum {
LED_OFF,
LED_GREEN,
LED_BLUE,
LED_RED,
LED_WHITE
} GwLedMode;
void setLedMode(GwLedMode mode);
#endif

View File

@ -17,6 +17,7 @@ lib_deps =
ttlappalainen/NMEA0183 @ ^1.7.1
bblanchon/ArduinoJson@^6.18.5
ottowinter/ESPAsyncWebServer-esphome@^2.0.1
fastled/FastLED @ ^3.4.0
board_build.embed_files =
generated/index.html.gz
generated/config.json.gz

View File

@ -12,7 +12,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define VERSION "0.5.6"
#define VERSION "0.5.8"
// #define GW_MESSAGE_DEBUG_ENABLED
// #define FALLBACK_SERIAL
@ -45,6 +45,7 @@ const unsigned long HEAP_REPORT_TIME=2000; //set to 0 to disable heap reporting
#include "NMEA0183DataToN2K.h"
#include "GwApi.h"
#include "GwButtons.h"
#include "GwLeds.h"
//NMEA message channels
@ -495,6 +496,8 @@ void setup() {
});
NMEA2000.Open();
startAddOnTask(handleButtons,100);
setLedMode(LED_GREEN);
startAddOnTask(handleLeds,101);
}
//*****************************************************************************