/*
* © 2023 Thierry Paris / Locoduino
* All rights reserved.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see .
*/
#ifndef Z21Throttle_h
#define Z21Throttle_h
#include "CircularBuffer.hpp"
#include "WiFiClient.h"
#define UDPBYTE_SIZE 1500
#define UDP_BUFFERSIZE 2048
// Broadcast flags
#define BROADCAST_BASE 0x00000001
#define BROADCAST_RBUS 0x00000002
#define BROADCAST_RAILCOM 0x00000004
#define BROADCAST_SYSTEM 0x00000100
#define BROADCAST_BASE_LOCOINFO 0x00010000
#define BROADCAST_LOCONET 0x01000000
#define BROADCAST_LOCONET_LOCO 0x02000000
#define BROADCAST_LOCONET_SWITCH 0x04000000
#define BROADCAST_LOCONET_DETECTOR 0x08000000
#define BROADCAST_RAILCOM_AUTO 0x00040000
#define BROADCAST_CAN 0x00080000
struct MYLOCOZ21 {
char throttle; //indicates which throttle letter on client, '0' + clientid
int cab; //address of this loco
bool broadcastPending;
uint32_t functionMap;
uint32_t functionToggles;
};
class NetworkClientUDP {
public:
NetworkClientUDP() {
this->pudpBuffer = new CircularBuffer(UDP_BUFFERSIZE);
this->pudpBuffer->begin(true);
};
bool ok() {
return (inUse);
};
bool inUse = true;
bool connected = false;
CircularBuffer *pudpBuffer = NULL;
IPAddress remoteIP;
int remotePort;
static WiFiUDP client;
};
class Z21Throttle {
public:
static void loop();
static Z21Throttle* getOrAddThrottle(int clientId);
static void markForBroadcast(int cab);
static void forget(byte clientId);
static void findUniqThrottle(int id, char *u);
static void setup(IPAddress ip, int port);
void notifyCvNACK(int inCvAddress);
void notifyCvRead(int inCvAddress, int inValue);
bool parse();
static Z21Throttle *readWriteThrottle; // NULL if no throttle is reading or writing a CV...
static int cvAddress;
static int cvValue;
private:
Z21Throttle(int clientId);
~Z21Throttle();
static const int MAX_MY_LOCO=10; // maximum number of locos assigned to a single client
static const int HEARTBEAT_SECONDS=10; // heartbeat at 4secs to provide messaging transport
static const int ESTOP_SECONDS=20; // eStop if no incoming messages for more than 8secs
static Z21Throttle* firstThrottle;
static byte commBuffer[100];
static byte replyBuffer[20];
static char LorS(int cab);
static bool isThrottleInUse(int cab);
static void setSendTurnoutList();
bool areYouUsingThrottle(int cab);
Z21Throttle* nextThrottle;
int clientid;
char uniq[17] = "";
MYLOCOZ21 myLocos[MAX_MY_LOCO];
int CountLocos() {
int count = 0;
for (int loco=0;loco