1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-23 16:16:13 +01:00

update for devel 5.2.17 following I2C DFPlayer update

update for devel 5.2.17
This commit is contained in:
Ash-4 2023-12-29 21:24:34 -06:00 committed by GitHub
commit c08f89d73f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 64 deletions

View File

@ -1 +1 @@
#define GITHUB_SHA "devel-202312131041Z" #define GITHUB_SHA "devel-202312251647Z"

View File

@ -22,13 +22,10 @@
* This device driver will configure the device on startup, along with * This device driver will configure the device on startup, along with
* interacting with the device for all input/output duties. * interacting with the device for all input/output duties.
* *
* To create EX-IOExpander devices, these are defined in myHal.cpp: * To create EX-IOExpander devices, these are defined in myAutomation.h:
* (Note the device driver is included by default) * (Note the device driver is included by default)
* *
* void halSetup() { * HAL(EXIOExpander,800,18,0x65)
* // EXIOExpander::create(vpin, num_vpins, i2c_address);
* EXIOExpander::create(800, 18, 0x65);
* }
* *
* All pins on an EX-IOExpander device are allocated according to the pin map for the specific * All pins on an EX-IOExpander device are allocated according to the pin map for the specific
* device in use. There is no way for the device driver to sanity check pins are used for the * device in use. There is no way for the device driver to sanity check pins are used for the
@ -98,25 +95,30 @@ private:
_numAnaloguePins = receiveBuffer[2]; _numAnaloguePins = receiveBuffer[2];
// See if we already have suitable buffers assigned // See if we already have suitable buffers assigned
size_t digitalBytesNeeded = (_numDigitalPins + 7) / 8; if (_numDigitalPins>0) {
if (_digitalPinBytes < digitalBytesNeeded) { size_t digitalBytesNeeded = (_numDigitalPins + 7) / 8;
// Not enough space, free any existing buffer and allocate a new one if (_digitalPinBytes < digitalBytesNeeded) {
if (_digitalPinBytes > 0) free(_digitalInputStates); // Not enough space, free any existing buffer and allocate a new one
_digitalInputStates = (byte*) calloc(_digitalPinBytes, 1); if (_digitalPinBytes > 0) free(_digitalInputStates);
_digitalPinBytes = digitalBytesNeeded; _digitalInputStates = (byte*) calloc(_digitalPinBytes, 1);
} _digitalPinBytes = digitalBytesNeeded;
size_t analogueBytesNeeded = _numAnaloguePins * 2; }
if (_analoguePinBytes < analogueBytesNeeded) { }
// Free any existing buffers and allocate new ones.
if (_analoguePinBytes > 0) { if (_numAnaloguePins>0) {
free(_analogueInputBuffer); size_t analogueBytesNeeded = _numAnaloguePins * 2;
free(_analogueInputStates); if (_analoguePinBytes < analogueBytesNeeded) {
free(_analoguePinMap); // Free any existing buffers and allocate new ones.
if (_analoguePinBytes > 0) {
free(_analogueInputBuffer);
free(_analogueInputStates);
free(_analoguePinMap);
}
_analogueInputStates = (uint8_t*) calloc(analogueBytesNeeded, 1);
_analogueInputBuffer = (uint8_t*) calloc(analogueBytesNeeded, 1);
_analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1);
_analoguePinBytes = analogueBytesNeeded;
} }
_analogueInputStates = (uint8_t*) calloc(analogueBytesNeeded, 1);
_analogueInputBuffer = (uint8_t*) calloc(analogueBytesNeeded, 1);
_analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1);
_analoguePinBytes = analogueBytesNeeded;
} }
} else { } else {
DIAG(F("EX-IOExpander I2C:%s ERROR configuring device"), _I2CAddress.toString()); DIAG(F("EX-IOExpander I2C:%s ERROR configuring device"), _I2CAddress.toString());
@ -124,8 +126,8 @@ private:
return; return;
} }
} }
// We now need to retrieve the analogue pin map // We now need to retrieve the analogue pin map if there are analogue pins
if (status == I2C_STATUS_OK) { if (status == I2C_STATUS_OK && _numAnaloguePins>0) {
commandBuffer[0] = EXIOINITA; commandBuffer[0] = EXIOINITA;
status = I2CManager.read(_I2CAddress, _analoguePinMap, _numAnaloguePins, commandBuffer, 1); status = I2CManager.read(_I2CAddress, _analoguePinMap, _numAnaloguePins, commandBuffer, 1);
} }
@ -239,7 +241,7 @@ private:
// If we're not doing anything now, check to see if a new input transfer is due. // If we're not doing anything now, check to see if a new input transfer is due.
if (_readState == RDS_IDLE) { if (_readState == RDS_IDLE) {
if (currentMicros - _lastDigitalRead > _digitalRefresh) { // Delay for digital read refresh if (currentMicros - _lastDigitalRead > _digitalRefresh && _numDigitalPins>0) { // Delay for digital read refresh
// Issue new read request for digital states. As the request is non-blocking, the buffer has to // Issue new read request for digital states. As the request is non-blocking, the buffer has to
// be allocated from heap (object state). // be allocated from heap (object state).
_readCommandBuffer[0] = EXIORDD; _readCommandBuffer[0] = EXIORDD;
@ -247,7 +249,7 @@ private:
// non-blocking read // non-blocking read
_lastDigitalRead = currentMicros; _lastDigitalRead = currentMicros;
_readState = RDS_DIGITAL; _readState = RDS_DIGITAL;
} else if (currentMicros - _lastAnalogueRead > _analogueRefresh) { // Delay for analogue read refresh } else if (currentMicros - _lastAnalogueRead > _analogueRefresh && _numAnaloguePins>0) { // Delay for analogue read refresh
// Issue new read for analogue input states // Issue new read for analogue input states
_readCommandBuffer[0] = EXIORDAN; _readCommandBuffer[0] = EXIORDAN;
I2CManager.read(_I2CAddress, _analogueInputBuffer, I2CManager.read(_I2CAddress, _analogueInputBuffer,

View File

@ -74,25 +74,39 @@ class NetworkClient {
public: public:
NetworkClient(WiFiClient c) { NetworkClient(WiFiClient c) {
wifi = c; wifi = c;
};
bool ok() {
return (inUse && wifi.connected());
};
bool recycle(WiFiClient c) {
if (inUse == true) return false;
// return false here until we have
// implemented a LRU timer
// if (LRU too recent) return false;
return false;
wifi = c;
inUse = true; inUse = true;
};
bool active(byte clientId) {
if (!inUse)
return false;
if(!wifi.connected()) {
DIAG(F("Remove client %d"), clientId);
CommandDistributor::forget(clientId);
wifi.stop();
inUse = false;
return false;
}
return true; return true;
}
bool recycle(WiFiClient c) {
if (wifi == c) {
if (inUse == true)
DIAG(F("WARNING: Duplicate"));
else
DIAG(F("Returning"));
inUse = true;
return true;
}
if (inUse == false) {
wifi = c;
inUse = true;
return true;
}
return false;
}; };
WiFiClient wifi; WiFiClient wifi;
bool inUse = true; private:
bool inUse;
}; };
static std::vector<NetworkClient> clients; // a list to hold all clients static std::vector<NetworkClient> clients; // a list to hold all clients
@ -282,37 +296,26 @@ void WifiESP::loop() {
// really no good way to check for LISTEN especially in AP mode? // really no good way to check for LISTEN especially in AP mode?
wl_status_t wlStatus; wl_status_t wlStatus;
if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) { if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) {
// loop over all clients and remove inactive
for (clientId=0; clientId<clients.size(); clientId++){
// check if client is there and alive
if(clients[clientId].inUse && !clients[clientId].wifi.connected()) {
DIAG(F("Remove client %d"), clientId);
CommandDistributor::forget(clientId);
clients[clientId].wifi.stop();
clients[clientId].inUse = false;
//Do NOT clients.erase(clients.begin()+clientId) as
//that would mix up clientIds for later.
}
}
if (server->hasClient()) { if (server->hasClient()) {
WiFiClient client; WiFiClient client;
while (client = server->available()) { while (client = server->available()) {
for (clientId=0; clientId<clients.size(); clientId++){ for (clientId=0; clientId<clients.size(); clientId++){
if (clients[clientId].recycle(client)) { if (clients[clientId].recycle(client)) {
DIAG(F("Recycle client %d %s"), clientId, client.remoteIP().toString().c_str()); DIAG(F("Recycle client %d %s:%d"), clientId, client.remoteIP().toString().c_str(),client.remotePort());
break; break;
} }
} }
if (clientId>=clients.size()) { if (clientId>=clients.size()) {
NetworkClient nc(client); NetworkClient nc(client);
clients.push_back(nc); clients.push_back(nc);
DIAG(F("New client %d, %s"), clientId, client.remoteIP().toString().c_str()); DIAG(F("New client %d, %s:%d"), clientId, client.remoteIP().toString().c_str(),client.remotePort());
} }
} }
} }
// loop over all connected clients // loop over all connected clients
// this removes as a side effect inactive clients when checking ::active()
for (clientId=0; clientId<clients.size(); clientId++){ for (clientId=0; clientId<clients.size(); clientId++){
if(clients[clientId].ok()) { if(clients[clientId].active(clientId)) {
int len; int len;
if ((len = clients[clientId].wifi.available()) > 0) { if ((len = clients[clientId].wifi.available()) > 0) {
// read data from client // read data from client
@ -350,7 +353,7 @@ void WifiESP::loop() {
} }
// buffer filled, end with '\0' so we can use it as C string // buffer filled, end with '\0' so we can use it as C string
buffer[count]='\0'; buffer[count]='\0';
if((unsigned int)clientId <= clients.size() && clients[clientId].ok()) { if((unsigned int)clientId <= clients.size() && clients[clientId].active(clientId)) {
if (Diag::CMD || Diag::WITHROTTLE) if (Diag::CMD || Diag::WITHROTTLE)
DIAG(F("SEND %d:%s"), clientId, buffer); DIAG(F("SEND %d:%s"), clientId, buffer);
clients[clientId].wifi.write(buffer,count); clients[clientId].wifi.write(buffer,count);
@ -383,8 +386,9 @@ void WifiESP::loop() {
// prio task. On core1 this is not a problem // prio task. On core1 this is not a problem
// as there the wdt is disabled by the // as there the wdt is disabled by the
// arduio IDE startup routines. // arduio IDE startup routines.
if (xPortGetCoreID() == 0) if (xPortGetCoreID() == 0) {
feedTheDog0(); feedTheDog0();
yield(); yield();
}
} }
#endif //ESP32 #endif //ESP32

View File

@ -68,7 +68,9 @@ Stream * WifiInterface::wifiStream;
#define NUM_SERIAL 3 #define NUM_SERIAL 3
#define SERIAL1 Serial3 #define SERIAL1 Serial3
#define SERIAL3 Serial5 #define SERIAL3 Serial5
#elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) || defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F412ZG) #elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) \
|| defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F412ZG) \
|| defined(ARDUINO_NUCLEO_F439ZI)
#define NUM_SERIAL 3 #define NUM_SERIAL 3
#define SERIAL1 Serial6 #define SERIAL1 Serial6
#define SERIAL3 Serial2 #define SERIAL3 Serial2

View File

@ -3,11 +3,14 @@
#include "StringFormatter.h" #include "StringFormatter.h"
#define VERSION "5.2.15ethC" #define VERSION "5.2.17ethCdf"
// 5.2.17 - ESP32 simplify network logic
// 5.2.16 - Bugfix to allow for devices using the EX-IOExpander protocol to have no analogue or no digital pins
// df - I2C DFPlayper capability
// 5.2.15 - move call to CommandDistributor::broadcastPower() into the TrackManager::setTrackPower(*) functions // 5.2.15 - move call to CommandDistributor::broadcastPower() into the TrackManager::setTrackPower(*) functions
// - add repeats to function packets that are not reminded in accordance with accessory packets // - add repeats to function packets that are not reminded in accordance with accessory packets
// 5.2.14eth - Initial ethernet code for STM32F429ZI and F439ZI boards // 5.2.14eth - Initial ethernet code for STM32F429ZI and F439ZI boards
// - CMRI RS485 connection // C - CMRI RS485 connection
// 5.2.14 - Reminder window DCC packet optimization // 5.2.14 - Reminder window DCC packet optimization
// - Optional #define DISABLE_FUNCTION_REMINDERS // - Optional #define DISABLE_FUNCTION_REMINDERS
// 5.2.13 - EXRAIL STEALTH // 5.2.13 - EXRAIL STEALTH