move serial channel mode to serial channel type (integer)

This commit is contained in:
andreas 2023-08-31 22:03:23 +02:00
parent f36dd37b8b
commit 8c7540d956
4 changed files with 59 additions and 25 deletions

View File

@ -102,6 +102,29 @@ static SerialParam *getSerialParam(int id){
return nullptr; return nullptr;
} }
void GwChannelList:: addSerial(HardwareSerial *stream,int id,int type,int rx,int tx){
const char *mode=nullptr;
switch (type)
{
case GWSERIAL_TYPE_UNI:
mode="UNI";
break;
case GWSERIAL_TYPE_BI:
mode="BI";
break;
case GWSERIAL_TYPE_RX:
mode="RX";
break;
case GWSERIAL_TYPE_TX:
mode="TX";
break;
}
if (mode == nullptr) {
LOG_DEBUG(GwLog::ERROR,"unknown serial type %d",type);
return;
}
addSerial(stream,id,mode,rx,tx);
}
void GwChannelList::addSerial(HardwareSerial *serialStream,int id,const String &mode,int rx,int tx){ void GwChannelList::addSerial(HardwareSerial *serialStream,int id,const String &mode,int rx,int tx){
SerialParam *param=getSerialParam(id); SerialParam *param=getSerialParam(id);
if (param == nullptr){ if (param == nullptr){
@ -112,6 +135,7 @@ void GwChannelList::addSerial(HardwareSerial *serialStream,int id,const String &
logger->logDebug(GwLog::ERROR,"useless config for serial %d: both rx/tx undefined"); logger->logDebug(GwLog::ERROR,"useless config for serial %d: both rx/tx undefined");
return; return;
} }
modes[id]=String(mode);
bool canRead=false; bool canRead=false;
bool canWrite=false; bool canWrite=false;
if (mode == "BI"){ if (mode == "BI"){
@ -155,6 +179,7 @@ void GwChannelList::addSerial(HardwareSerial *serialStream,int id,const String &
LOG_DEBUG(GwLog::LOG, "%s", channel->toString().c_str()); LOG_DEBUG(GwLog::LOG, "%s", channel->toString().c_str());
theChannels.push_back(channel); theChannels.push_back(channel);
} }
void GwChannelList::begin(bool fallbackSerial){ void GwChannelList::begin(bool fallbackSerial){
LOG_DEBUG(GwLog::DEBUG,"GwChannelList::begin"); LOG_DEBUG(GwLog::DEBUG,"GwChannelList::begin");
GwChannel *channel=NULL; GwChannel *channel=NULL;
@ -205,8 +230,12 @@ void GwChannelList::begin(bool fallbackSerial){
#ifndef GWSERIAL_RX #ifndef GWSERIAL_RX
#define GWSERIAL_RX -1 #define GWSERIAL_RX -1
#endif #endif
#ifdef GWSERIAL_MODE #ifdef GWSERIAL_TYPE
addSerial(&Serial1,SERIAL1_CHANNEL_ID,GWSERIAL_MODE,GWSERIAL_RX,GWSERIAL_TX); addSerial(&Serial1,SERIAL1_CHANNEL_ID,GWSERIAL_TYPE,GWSERIAL_RX,GWSERIAL_TX);
#else
#ifdef GWSERIAL_MODE
addSerial(&Serial1,SERIAL1_CHANNEL_ID,GWSERIAL_MODE,GWSERIAL_RX,GWSERIAL_TX);
#endif
#endif #endif
//serial 2 //serial 2
#ifndef GWSERIAL2_TX #ifndef GWSERIAL2_TX
@ -215,8 +244,12 @@ void GwChannelList::begin(bool fallbackSerial){
#ifndef GWSERIAL2_RX #ifndef GWSERIAL2_RX
#define GWSERIAL2_RX -1 #define GWSERIAL2_RX -1
#endif #endif
#ifdef GWSERIAL2_MODE #ifdef GWSERIAL2_TYPE
addSerial(&Serial2,SERIAL2_CHANNEL_ID,GWSERIAL2_MODE,GWSERIAL2_RX,GWSERIAL2_TX); addSerial(&Serial2,SERIAL2_CHANNEL_ID,GWSERIAL2_TYPE,GWSERIAL2_RX,GWSERIAL2_TX);
#else
#ifdef GWSERIAL2_MODE
addSerial(&Serial2,SERIAL2_CHANNEL_ID,GWSERIAL2_MODE,GWSERIAL2_RX,GWSERIAL2_TX);
#endif
#endif #endif
//tcp client //tcp client
bool tclEnabled=config->getBool(config->tclEnabled); bool tclEnabled=config->getBool(config->tclEnabled);
@ -245,6 +278,11 @@ void GwChannelList::begin(bool fallbackSerial){
LOG_DEBUG(GwLog::LOG,"%s",channel->toString().c_str()); LOG_DEBUG(GwLog::LOG,"%s",channel->toString().c_str());
logger->flush(); logger->flush();
} }
String GwChannelList::getMode(int id){
auto it=modes.find(id);
if (it != modes.end()) return it->second;
return "UNKNOWN";
}
int GwChannelList::getJsonSize(){ int GwChannelList::getJsonSize(){
int rt=0; int rt=0;
allChannels([&](GwChannel *c){ allChannels([&](GwChannel *c){

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <map>
#include <WString.h> #include <WString.h>
#include "GwChannel.h" #include "GwChannel.h"
#include "GwLog.h" #include "GwLog.h"
@ -26,10 +27,11 @@ class GwChannelList{
GwConfigHandler *config; GwConfigHandler *config;
typedef std::vector<GwChannel *> ChannelList; typedef std::vector<GwChannel *> ChannelList;
ChannelList theChannels; ChannelList theChannels;
std::map<int,String> modes;
GwSocketServer *sockets; GwSocketServer *sockets;
GwTcpClient *client; GwTcpClient *client;
void addSerial(HardwareSerial *stream,int id,const String &mode,int rx,int tx); void addSerial(HardwareSerial *stream,int id,const String &mode,int rx,int tx);
void addSerial(HardwareSerial *stream,int id,int type,int rx,int tx);
public: public:
GwChannelList(GwLog *logger, GwConfigHandler *config); GwChannelList(GwLog *logger, GwConfigHandler *config);
typedef std::function<void(GwChannel *)> ChannelAction; typedef std::function<void(GwChannel *)> ChannelAction;
@ -42,6 +44,6 @@ class GwChannelList{
//single channel //single channel
GwChannel *getChannelById(int sourceId); GwChannel *getChannelById(int sourceId);
void fillStatus(GwApi::Status &status); void fillStatus(GwApi::Status &status);
String getMode(int id);
}; };

View File

@ -13,6 +13,11 @@
*/ */
#ifndef _GWHARDWARE_H #ifndef _GWHARDWARE_H
#define _GWHARDWARE_H #define _GWHARDWARE_H
#define GWSERIAL_TYPE_UNI 1
#define GWSERIAL_TYPE_BI 2
#define GWSERIAL_TYPE_RX 3
#define GWSERIAL_TYPE_TX 4
#include <HardwareSerial.h> #include <HardwareSerial.h>
#include "GwUserTasks.h" #include "GwUserTasks.h"
@ -117,7 +122,7 @@
#define ESP32_CAN_RX_PIN GPIO_NUM_4 #define ESP32_CAN_RX_PIN GPIO_NUM_4
//serial input only //serial input only
#define GWSERIAL_RX GPIO_NUM_16 #define GWSERIAL_RX GPIO_NUM_16
#define GWSERIAL_MODE "RX" #define GWSERIAL_TYPE GWSERIAL_TYPE_RX
#define GWBUTTON_PIN GPIO_NUM_0 #define GWBUTTON_PIN GPIO_NUM_0
#define GWBUTTON_ACTIVE LOW #define GWBUTTON_ACTIVE LOW
@ -132,26 +137,26 @@
#ifdef SERIAL_GROOVE_485 #ifdef SERIAL_GROOVE_485
#define GWSERIAL_TX GROOVE_PIN_1 #define GWSERIAL_TX GROOVE_PIN_1
#define GWSERIAL_RX GROOVE_PIN_2 #define GWSERIAL_RX GROOVE_PIN_2
#define GWSERIAL_MODE "UNI" #define GWSERIAL_TYPE GWSERIAL_TYPE_UNI
#endif #endif
#ifdef SERIAL_GROOVE_232 #ifdef SERIAL_GROOVE_232
#define GWSERIAL_TX GROOVE_PIN_1 #define GWSERIAL_TX GROOVE_PIN_1
#define GWSERIAL_RX GROOVE_PIN_2 #define GWSERIAL_RX GROOVE_PIN_2
#define GWSERIAL_MODE "BI" #define GWSERIAL_TYPE GWSERIAL_TYPE_BI
#endif #endif
//M5 Serial (Atomic RS232 Base) //M5 Serial (Atomic RS232 Base)
#ifdef M5_SERIAL_KIT_232 #ifdef M5_SERIAL_KIT_232
#define GWSERIAL_TX BOARD_LEFT2 #define GWSERIAL_TX BOARD_LEFT2
#define GWSERIAL_RX BOARD_LEFT1 #define GWSERIAL_RX BOARD_LEFT1
#define GWSERIAL_MODE "BI" #define GWSERIAL_TYPE GWSERIAL_TYPE_BI
#endif #endif
//M5 Serial (Atomic RS485 Base) //M5 Serial (Atomic RS485 Base)
#ifdef M5_SERIAL_KIT_485 #ifdef M5_SERIAL_KIT_485
#define GWSERIAL_TX BOARD_LEFT2 #define GWSERIAL_TX BOARD_LEFT2
#define GWSERIAL_RX BOARD_LEFT1 #define GWSERIAL_RX BOARD_LEFT1
#define GWSERIAL_MODE "UNI" #define GWSERIAL_TYPE GWSERIAL_TYPE_UNI
#endif #endif
//can kit for M5 Atom //can kit for M5 Atom
@ -165,5 +170,4 @@
#define ESP32_CAN_RX_PIN GROOVE_PIN_2 #define ESP32_CAN_RX_PIN GROOVE_PIN_2
#endif #endif
#endif #endif

View File

@ -126,7 +126,7 @@ class Nmea2kTwaiLog : public Nmea2kTwai{
#define ESP32_CAN_RX_PIN GPIO_NUM_NC #define ESP32_CAN_RX_PIN GPIO_NUM_NC
#endif #endif
Nmea2kTwai &NMEA2000=*(new Nmea2kTwaiLog(ESP32_CAN_TX_PIN,ESP32_CAN_RX_PIN,CAN_RECOVERY_PERIOD,&logger)); Nmea2kTwai &NMEA2000=*(new Nmea2kTwaiLog((gpio_num_t)ESP32_CAN_TX_PIN,(gpio_num_t)ESP32_CAN_RX_PIN,CAN_RECOVERY_PERIOD,&logger));
#ifdef GWBUTTON_PIN #ifdef GWBUTTON_PIN
bool fixedApPass=false; bool fixedApPass=false;
@ -433,18 +433,8 @@ class CapabilitiesRequest : public GwRequestMessage{
it != userCodeHandler.getCapabilities()->end();it++){ it != userCodeHandler.getCapabilities()->end();it++){
json[it->first]=it->second; json[it->first]=it->second;
} }
#ifdef GWSERIAL_MODE json["serialmode"]=channels.getMode(SERIAL1_CHANNEL_ID);
String serial(F(GWSERIAL_MODE)); json["serial2mode"]=channels.getMode(SERIAL2_CHANNEL_ID);
#else
String serial(F("NONE"));
#endif
json["serialmode"]=serial;
#ifdef GWSERIAL2_MODE
String serial2(F(GWSERIAL2_MODE));
#else
String serial2(F("NONE"));
#endif
json["serial2mode"]=serial2;
#ifdef GWBUTTON_PIN #ifdef GWBUTTON_PIN
json["hardwareReset"]="true"; json["hardwareReset"]="true";
#endif #endif