mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-13 05:53:06 +01:00
add api for supplementary tasks, add button task
This commit is contained in:
17
lib/api/GwApi.h
Normal file
17
lib/api/GwApi.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef _GWAPI_H
|
||||
#define _GWAPI_H
|
||||
#include "GwMessage.h"
|
||||
#include "N2kMessages.h"
|
||||
#include "NMEA0183Messages.h"
|
||||
#include "GWConfig.h"
|
||||
//API to be used for additional tasks
|
||||
class GwApi{
|
||||
public:
|
||||
virtual GwRequestQueue *getQueue()=0;
|
||||
virtual void sendN2kMessage(const tN2kMsg &msg)=0;
|
||||
virtual void sendNMEA0183Message(const tNMEA0183Msg &msg, int sourceId)=0;
|
||||
virtual int getSourceId()=0;
|
||||
virtual GwConfigHandler *getConfig()=0;
|
||||
virtual GwLog *getLogger()=0;
|
||||
};
|
||||
#endif
|
||||
86
lib/buttons/GwButtons.cpp
Normal file
86
lib/buttons/GwButtons.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "GwButtons.h"
|
||||
#include "GwHardware.h"
|
||||
#include "GwApi.h"
|
||||
|
||||
class FactoryResetRequest: public GwMessage{
|
||||
private:
|
||||
GwApi *api;
|
||||
public:
|
||||
FactoryResetRequest(GwApi *api):GwMessage(F("reset button")){
|
||||
this->api=api;
|
||||
}
|
||||
virtual ~FactoryResetRequest(){}
|
||||
protected:
|
||||
virtual void processImpl(){
|
||||
api->getLogger()->logDebug(GwLog::LOG,"reset request processing");
|
||||
api->getConfig()->reset(true);
|
||||
xTaskCreate([](void *p){
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
vTaskDelete(NULL);
|
||||
},"reset",1000,NULL,0,NULL);
|
||||
};
|
||||
};
|
||||
void handleButtons(void *param){
|
||||
GwApi *api=(GwApi*)param;
|
||||
GwLog *logger=api->getLogger();
|
||||
#ifndef GWBUTTON_PIN
|
||||
LOG_DEBUG(GwLog::LOG,"no button pin defined, do not watch");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
#else
|
||||
#ifndef GWBUTTON_ACTIVE
|
||||
int activeState=0;
|
||||
#else
|
||||
int activeState=GWBUTTON_ACTIVE;
|
||||
#endif
|
||||
#ifdef GWBUTTON_PULLUPDOWN
|
||||
bool pullUpDown=true;
|
||||
#else
|
||||
bool pullUpDown=false;
|
||||
#endif
|
||||
uint8_t mode=INPUT;
|
||||
if (pullUpDown){
|
||||
mode=activeState?INPUT_PULLDOWN:INPUT_PULLUP;
|
||||
}
|
||||
pinMode(GWBUTTON_PIN,mode);
|
||||
unsigned long lastPressed=0;
|
||||
unsigned long firstPressed=0;
|
||||
unsigned long lastReport=0;
|
||||
const unsigned long OFF_TIME=20;
|
||||
const unsigned long REPORT_TIME=1000;
|
||||
const unsigned long HARD_REST_TIME=10000;
|
||||
while(true){
|
||||
delay(10);
|
||||
int current=digitalRead(GWBUTTON_PIN);
|
||||
unsigned long now=millis();
|
||||
if (current != activeState){
|
||||
if (lastPressed != 0 && (lastPressed+OFF_TIME) < now){
|
||||
lastPressed=0; //finally off
|
||||
firstPressed=0;
|
||||
LOG_DEBUG(GwLog::LOG,"Button press stopped");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
lastPressed=now;
|
||||
if (firstPressed == 0) {
|
||||
firstPressed=now;
|
||||
LOG_DEBUG(GwLog::LOG,"Button press started");
|
||||
lastReport=now;
|
||||
}
|
||||
if (lastReport != 0 && (lastReport + REPORT_TIME) < now ){
|
||||
LOG_DEBUG(GwLog::LOG,"Button active for %ld",(now-firstPressed));
|
||||
lastReport=now;
|
||||
}
|
||||
if (now > (firstPressed+HARD_REST_TIME)){
|
||||
LOG_DEBUG(GwLog::ERROR,"Factory reset by button");
|
||||
GwMessage *r=new FactoryResetRequest(api);
|
||||
api->getQueue()->sendAndForget(r);
|
||||
r->unref();
|
||||
firstPressed=0;
|
||||
lastPressed=0;
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
#endif
|
||||
}
|
||||
5
lib/buttons/GwButtons.h
Normal file
5
lib/buttons/GwButtons.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifndef _GWBUTTONS_H
|
||||
#define _GWBUTTONS_H
|
||||
//task function
|
||||
void handleButtons(void *param);
|
||||
#endif
|
||||
48
lib/hardware/GwHardware.h
Normal file
48
lib/hardware/GwHardware.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
This code is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
This code is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#ifndef _GWHARDWARE_H
|
||||
#define _GWHARDWARE_H
|
||||
|
||||
//SERIAL_MODE can be: UNI (RX or TX only), BI (both), RX, TX
|
||||
//board specific pins
|
||||
#ifdef BOARD_M5ATOM
|
||||
#define ESP32_CAN_TX_PIN GPIO_NUM_22
|
||||
#define ESP32_CAN_RX_PIN GPIO_NUM_19
|
||||
//if using tail485
|
||||
#define GWSERIAL_TX 26
|
||||
#define GWSERIAL_RX 32
|
||||
#define GWSERIAL_MODE "UNI"
|
||||
#elif BOARD_M5ATOM_CANUNIT
|
||||
#define ESP32_CAN_TX_PIN GPIO_NUM_26
|
||||
#define ESP32_CAN_RX_PIN GPIO_NUM_32
|
||||
#elif BOARD_M5STICK_CANUNIT
|
||||
#define ESP32_CAN_TX_PIN GPIO_NUM_32
|
||||
#define ESP32_CAN_RX_PIN GPIO_NUM_33
|
||||
#elif BOARD_HOMBERGER
|
||||
#define ESP32_CAN_TX_PIN GPIO_NUM_5
|
||||
#define ESP32_CAN_RX_PIN GPIO_NUM_4
|
||||
//serial input only
|
||||
#define GWSERIAL_RX 16
|
||||
#define GWSERIAL_MODE "RX"
|
||||
|
||||
#define GWBUTTON_PIN GPIO_NUM_0
|
||||
#define GWBUTTON_ACTIVE LOW
|
||||
//if GWBUTTON_PULLUPDOWN we enable a pulup/pulldown
|
||||
#define GWBUTTON_PULLUPDOWN
|
||||
|
||||
#else
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user