mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2025-07-30 02:43:45 +02:00
Compare commits
236 Commits
EX-RAIL-ha
...
v3.2.0rc9
Author | SHA1 | Date | |
---|---|---|---|
|
5f878b5911 | ||
|
61390cb0e2 | ||
|
d45585ce3d | ||
|
434c292fd9 | ||
|
b0915e8332 | ||
|
1934fdd0e1 | ||
|
ff102dbf88 | ||
|
85f0712d31 | ||
|
14ede75643 | ||
|
503d4c56cf | ||
|
e78c3001cf | ||
|
b0e81eec46 | ||
|
823420615e | ||
|
9cd3e4b7c1 | ||
|
024313deac | ||
|
bbd569cc88 | ||
|
e3bca1592c | ||
|
7017c6bbf5 | ||
|
230a119cd0 | ||
|
1ad4e57332 | ||
|
a806af6f2f | ||
|
582d30916e | ||
|
06a07a49cd | ||
|
a003d54fdd | ||
|
7fc2d32ad3 | ||
|
a45a43f6d4 | ||
|
b7077565b9 | ||
|
00e3c80b44 | ||
|
7a7ca6a436 | ||
|
cc1cdc35ec | ||
|
7b8fa200f2 | ||
|
52cc1ecd7b | ||
|
a4fcff902c | ||
|
0912ad484a | ||
|
b05cbc1fdf | ||
|
52e7929b08 | ||
|
c15d536e9b | ||
|
e24e1669f7 | ||
|
cbf9f39ea6 | ||
|
65ce238bfb | ||
|
10828bc6b8 | ||
|
aa40231ac7 | ||
|
89cf6016e8 | ||
|
988510112d | ||
|
2c47c309dc | ||
|
f755c291d5 | ||
|
94a2839bca | ||
|
0eacda0cf9 | ||
|
6bfe18bb21 | ||
|
82092075bf | ||
|
1b07d0a5c6 | ||
|
e5c66a2755 | ||
|
0947467bfa | ||
|
4d809b85b3 | ||
|
2ddf583fbc | ||
|
bb2c85d973 | ||
|
b0c9806f3b | ||
|
96933ed516 | ||
|
985f0e777c | ||
|
2049cc89b3 | ||
|
18695888dd | ||
|
b8293d07f2 | ||
|
a4fc10d466 | ||
|
0a40ef5ceb | ||
|
0f36ccdc57 | ||
|
92591c8a2e | ||
|
0f728c1c15 | ||
|
b5af39dfc9 | ||
|
4924cc7779 | ||
|
7d665fe577 | ||
|
5c18f4a19d | ||
|
58afea135c | ||
|
9018ec9757 | ||
|
aa734b25e4 | ||
|
67e48d34f4 | ||
|
0237c9721f | ||
|
419822ef06 | ||
|
d4ee215ae6 | ||
|
259696a117 | ||
|
4a8065d33b | ||
|
43538d3b32 | ||
|
c363ea4714 | ||
|
fd43a9b88b | ||
|
8a17965cd2 | ||
|
a4e94610e6 | ||
|
92d6a15ee5 | ||
|
3bddeeda3e | ||
|
f05b3d1730 | ||
|
a2f8a8ec91 | ||
|
746350b846 | ||
|
97f3450621 | ||
|
2be3e276f9 | ||
|
88fa5ad37c | ||
|
ef1719f6fc | ||
|
39c7bf3983 | ||
|
a4f746c00c | ||
|
106fb612dc | ||
|
53113e981d | ||
|
0018ba676b | ||
|
5cb427f774 | ||
|
4ea458b140 | ||
|
d7fd9e1538 | ||
|
197228c3b0 | ||
|
620dcbf925 | ||
|
82f121c8ef | ||
|
6c98f90151 | ||
|
c90ea0c6df | ||
|
d08f14be3b | ||
|
fb97ba11de | ||
|
ee5db61349 | ||
|
b384d6c14d | ||
|
58fe81bf06 | ||
|
1807189183 | ||
|
0e78cf6e55 | ||
|
6c75563779 | ||
|
89dcafb2d7 | ||
|
37904b5fa6 | ||
|
fbca15d2a7 | ||
|
177c8c0367 | ||
|
7ea3faf177 | ||
|
d3381c6b2d | ||
|
8853b23f88 | ||
|
a16f6c8749 | ||
|
e3d771a24d | ||
|
055bc7bfe2 | ||
|
79ce71c2f9 | ||
|
e3cbaf5f24 | ||
|
250c372f5c | ||
|
a9c31eb1ae | ||
|
b9fed47d24 | ||
|
151f7d7f86 | ||
|
b7bcd13347 | ||
|
4f16a4ca06 | ||
|
9097a62f42 | ||
|
80472a76dc | ||
|
6dde811279 | ||
|
7aed7de6cd | ||
|
bfc2b75eb5 | ||
|
9fc805831d | ||
|
ffc5d91561 | ||
|
e11fd18849 | ||
|
32eb8fe8c7 | ||
|
e287af83ff | ||
|
e59e07b971 | ||
|
302b16547e | ||
|
08835e25c6 | ||
|
bda3c05265 | ||
|
f947c5bae5 | ||
|
afe2ecdc14 | ||
|
fa650673eb | ||
|
ad7cd5f401 | ||
|
d077e3a2ff | ||
|
07cc45d861 | ||
|
f3658aaee7 | ||
|
3dc0b1619c | ||
|
592f87303e | ||
|
02a715d54d | ||
|
f7d34b92ee | ||
|
d316b72069 | ||
|
fc9aa71d9f | ||
|
72528658be | ||
|
4121a5f4da | ||
|
2ed578821f | ||
|
70b59d491c | ||
|
254d83b6fc | ||
|
ebabbbe59e | ||
|
f8311b8c56 | ||
|
f38bf512ab | ||
|
9b3c6fe896 | ||
|
81dc512c86 | ||
|
222eca6524 | ||
|
4dff8a2b50 | ||
|
8d471d9f3f | ||
|
9ba13a62c9 | ||
|
99222bd37f | ||
|
9d5781a87c | ||
|
b4fb76b6c8 | ||
|
4b87c879a9 | ||
|
08810dafd7 | ||
|
afe9141671 | ||
|
1bb7b5cc77 | ||
|
09eae0ea91 | ||
|
0f55835b8b | ||
|
40c6bb7f2e | ||
|
7dea284ba8 | ||
|
fb6ab85c4a | ||
|
23ed4e61af | ||
|
b2ddb34273 | ||
|
f8858b952e | ||
|
6ebf908802 | ||
|
93dfdcce53 | ||
|
7e601c38c4 | ||
|
1dd574dc03 | ||
|
0aea9169b1 | ||
|
0c218e1e13 | ||
|
0a9fcf6ebc | ||
|
5e30740c5b | ||
|
2469629cbb | ||
|
bad9e866f8 | ||
|
77d4d7c400 | ||
|
fa04fa5084 | ||
|
80fc9e8a68 | ||
|
d0fed2dd38 | ||
|
08cfe41cf3 | ||
|
777d189cc5 | ||
|
c45337d5d4 | ||
|
8b498b8b49 | ||
|
425de3fcc7 | ||
|
0d235b65d3 | ||
|
69c4733f2b | ||
|
f0cd96fed3 | ||
|
161b35ae84 | ||
|
214e6c643f | ||
|
50a9e08d1f | ||
|
ca55834051 | ||
|
2829716ea6 | ||
|
00138be90d | ||
|
fdaa7b51b9 | ||
|
7b47b86143 | ||
|
3e50a6bdad | ||
|
240b18a0df | ||
|
b35ce88fdd | ||
|
0875d27b0a | ||
|
39a69e340e | ||
|
dbabfdca80 | ||
|
60718f5eac | ||
|
071389a04b | ||
|
d8366f33c8 | ||
|
133c65bc42 | ||
|
482f4b1c79 | ||
|
b4a3b503bc | ||
|
7f6173825f | ||
|
fd36ca2b92 | ||
|
776a098a72 | ||
|
683f9d33fe | ||
|
33b5f4fdf0 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,6 +10,7 @@ config.h
|
||||
.vscode/extensions.json
|
||||
mySetup.h
|
||||
mySetup.cpp
|
||||
myHal.cpp
|
||||
myAutomation.h
|
||||
myFilter.cpp
|
||||
myAutomation.h
|
||||
|
@@ -1,6 +1,9 @@
|
||||
/*
|
||||
* © 2020,Gregor Baues, Chris Harlow. All rights reserved.
|
||||
*
|
||||
* © 2022 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2020 Gregor Baues
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
@@ -16,16 +19,119 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "CommandDistributor.h"
|
||||
#include "SerialManager.h"
|
||||
#include "WiThrottle.h"
|
||||
#include "DIAG.h"
|
||||
#include "defines.h"
|
||||
#include "DCCWaveform.h"
|
||||
#include "DCC.h"
|
||||
|
||||
DCCEXParser * CommandDistributor::parser=0;
|
||||
#if defined(BIG_MEMORY) | defined(WIFI_ON) | defined(ETHERNET_ON)
|
||||
// This section of CommandDistributor is simply not relevant on a uno or similar
|
||||
const byte NO_CLIENT=255;
|
||||
|
||||
void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * streamer) {
|
||||
if (buffer[0] == '<') {
|
||||
if (!parser) parser = new DCCEXParser();
|
||||
parser->parse(streamer, buffer, streamer);
|
||||
RingStream * CommandDistributor::ring=0;
|
||||
byte CommandDistributor::ringClient=NO_CLIENT;
|
||||
CommandDistributor::clientType CommandDistributor::clients[8]={
|
||||
NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE};
|
||||
RingStream * CommandDistributor::broadcastBufferWriter=new RingStream(100);
|
||||
|
||||
void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream) {
|
||||
ring=stream;
|
||||
ringClient=stream->peekTargetMark();
|
||||
if (buffer[0] == '<') {
|
||||
clients[clientId]=COMMAND_TYPE;
|
||||
DCCEXParser::parse(stream, buffer, ring);
|
||||
} else {
|
||||
clients[clientId]=WITHROTTLE_TYPE;
|
||||
WiThrottle::getThrottle(clientId)->parse(ring, buffer);
|
||||
}
|
||||
else WiThrottle::getThrottle(clientId)->parse(streamer, buffer);
|
||||
ringClient=NO_CLIENT;
|
||||
}
|
||||
|
||||
void CommandDistributor::forget(byte clientId) {
|
||||
clients[clientId]=NONE_TYPE;
|
||||
}
|
||||
|
||||
|
||||
void CommandDistributor::broadcast(bool includeWithrottleClients) {
|
||||
broadcastBufferWriter->write((byte)'\0');
|
||||
|
||||
/* Boadcast to Serials */
|
||||
SerialManager::broadcast(broadcastBufferWriter);
|
||||
|
||||
#if defined(WIFI_ON) | defined(ETHERNET_ON)
|
||||
// If we are broadcasting from a wifi/eth process we need to complete its output
|
||||
// before merging broadcasts in the ring, then reinstate it in case
|
||||
// the process continues to output to its client.
|
||||
if (ringClient!=NO_CLIENT) ring->commit();
|
||||
|
||||
/* loop through ring clients */
|
||||
for (byte clientId=0; clientId<sizeof(clients); clientId++) {
|
||||
if (clients[clientId]==NONE_TYPE) continue;
|
||||
if ( clients[clientId]==WITHROTTLE_TYPE && !includeWithrottleClients) continue;
|
||||
ring->mark(clientId);
|
||||
broadcastBufferWriter->printBuffer(ring);
|
||||
ring->commit();
|
||||
}
|
||||
if (ringClient!=NO_CLIENT) ring->mark(ringClient);
|
||||
|
||||
#endif
|
||||
broadcastBufferWriter->flush();
|
||||
}
|
||||
#else
|
||||
// For a UNO/NANO we can broadcast direct to just one Serial instead of the ring
|
||||
// Redirect ring output ditrect to Serial
|
||||
#define broadcastBufferWriter &Serial
|
||||
// and ignore the internal broadcast call.
|
||||
void CommandDistributor::broadcast(bool includeWithrottleClients) {
|
||||
(void)includeWithrottleClients;
|
||||
}
|
||||
#endif
|
||||
|
||||
void CommandDistributor::broadcastSensor(int16_t id, bool on ) {
|
||||
StringFormatter::send(broadcastBufferWriter,F("<%c %d>\n"), on?'Q':'q', id);
|
||||
broadcast(false);
|
||||
}
|
||||
|
||||
void CommandDistributor::broadcastTurnout(int16_t id, bool isClosed ) {
|
||||
// For DCC++ classic compatibility, state reported to JMRI is 1 for thrown and 0 for closed;
|
||||
// The string below contains serial and Withrottle protocols which should
|
||||
// be safe for both types.
|
||||
StringFormatter::send(broadcastBufferWriter,F("<H %d %d>\n"),id, !isClosed);
|
||||
#if defined(WIFI_ON) | defined(ETHERNET_ON)
|
||||
StringFormatter::send(broadcastBufferWriter,F("PTA%c%d\n"), isClosed?'2':'4', id);
|
||||
#endif
|
||||
broadcast(true);
|
||||
}
|
||||
|
||||
void CommandDistributor::broadcastLoco(byte slot) {
|
||||
DCC::LOCO * sp=&DCC::speedTable[slot];
|
||||
StringFormatter::send(broadcastBufferWriter,F("<l %d %d %d %l>\n"),
|
||||
sp->loco,slot,sp->speedCode,sp->functions);
|
||||
broadcast(false);
|
||||
#if defined(WIFI_ON) | defined(ETHERNET_ON)
|
||||
WiThrottle::markForBroadcast(sp->loco);
|
||||
#endif
|
||||
}
|
||||
|
||||
void CommandDistributor::broadcastPower() {
|
||||
bool main=DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON;
|
||||
bool prog=DCCWaveform::progTrack.getPowerMode()==POWERMODE::ON;
|
||||
bool join=DCCWaveform::progTrackSyncMain;
|
||||
const FSH * reason=F("");
|
||||
char state='1';
|
||||
if (main && prog && join) reason=F(" JOIN");
|
||||
else if (main && prog);
|
||||
else if (main) reason=F(" MAIN");
|
||||
else if (prog) reason=F(" PROG");
|
||||
else state='0';
|
||||
|
||||
StringFormatter::send(broadcastBufferWriter,
|
||||
F("<p%c%S>\nPPA%c\n"),state,reason, main?'1':'0');
|
||||
LCD(2,F("Power %S%S"),state=='1'?F("On"):F("Off"),reason);
|
||||
broadcast(true);
|
||||
}
|
||||
|
@@ -1,6 +1,9 @@
|
||||
/*
|
||||
* © 2020,Gregor Baues, Chris Harlow. All rights reserved.
|
||||
*
|
||||
* © 2022 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2020 Gregor Baues
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
@@ -24,9 +27,21 @@
|
||||
class CommandDistributor {
|
||||
|
||||
public :
|
||||
static void parse(byte clientId,byte* buffer, RingStream * streamer);
|
||||
static void parse(byte clientId,byte* buffer, RingStream * ring);
|
||||
static void broadcastLoco(byte slot);
|
||||
static void broadcastSensor(int16_t id, bool value);
|
||||
static void broadcastTurnout(int16_t id, bool isClosed);
|
||||
static void broadcastPower();
|
||||
static void forget(byte clientId);
|
||||
private:
|
||||
static DCCEXParser * parser;
|
||||
static void broadcast(bool includeWithrottleClients);
|
||||
static RingStream * ring;
|
||||
static RingStream * broadcastBufferWriter;
|
||||
static byte ringClient;
|
||||
|
||||
// each bit in broadcastlist = 1<<clientid
|
||||
enum clientType: byte {NONE_TYPE,COMMAND_TYPE,WITHROTTLE_TYPE};
|
||||
static clientType clients[8];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -1,33 +1,35 @@
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// DCC-EX CommandStation-EX Please see https://DCC-EX.com
|
||||
// DCC-EX CommandStation-EX Please see https://DCC-EX.com
|
||||
//
|
||||
// This file is the main sketch for the Command Station.
|
||||
//
|
||||
// CONFIGURATION:
|
||||
//
|
||||
// CONFIGURATION:
|
||||
// Configuration is normally performed by editing a file called config.h.
|
||||
// This file is NOT shipped with the code so that if you pull a later version
|
||||
// of the code, your configuration will not be overwritten.
|
||||
//
|
||||
// If you used the automatic installer program, config.h will have been created automatically.
|
||||
//
|
||||
// To obtain a starting copy of config.h please copy the file config.example.h which is
|
||||
// shipped with the code and may be updated as new features are added.
|
||||
//
|
||||
//
|
||||
// To obtain a starting copy of config.h please copy the file config.example.h which is
|
||||
// shipped with the code and may be updated as new features are added.
|
||||
//
|
||||
// If config.h is not found, config.example.h will be used with all defaults.
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if __has_include ( "config.h")
|
||||
#include "config.h"
|
||||
#else
|
||||
#warning config.h not found. Using defaults from config.example.h
|
||||
#warning config.h not found. Using defaults from config.example.h
|
||||
#include "config.example.h"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* © 2020,2021 Chris Harlow, Harald Barth, David Cutting,
|
||||
* Fred Decker, Gregor Baues, Anthony W - Dayton All rights reserved.
|
||||
*
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2020-2021 Chris Harlow, Harald Barth, David Cutting,
|
||||
* Fred Decker, Gregor Baues, Anthony W - Dayton
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
@@ -45,11 +47,15 @@
|
||||
*/
|
||||
|
||||
#include "DCCEX.h"
|
||||
|
||||
// Create a serial command parser for the USB connection,
|
||||
// This supports JMRI or manual diagnostics and commands
|
||||
// to be issued from the USB serial console.
|
||||
DCCEXParser serialParser;
|
||||
#ifdef WIFI_WARNING
|
||||
#warning You have defined that you want WiFi but your hardware has not enough memory to do that, so WiFi DISABLED
|
||||
#endif
|
||||
#ifdef ETHERNET_WARNING
|
||||
#warning You have defined that you want Ethernet but your hardware has not enough memory to do that, so Ethernet DISABLED
|
||||
#endif
|
||||
#ifdef EXRAIL_WARNING
|
||||
#warning You have myAutomation.h but your hardware has not enough memory to do that, so EX-RAIL DISABLED
|
||||
#endif
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -57,15 +63,15 @@ void setup()
|
||||
|
||||
// Responsibility 1: Start the usb connection for diagnostics
|
||||
// This is normally Serial but uses SerialUSB on a SAMD processor
|
||||
Serial.begin(115200);
|
||||
SerialManager::init();
|
||||
|
||||
DIAG(F("License GPLv3 fsf.org (c) dcc-ex.com"));
|
||||
|
||||
CONDITIONAL_LCD_START {
|
||||
// This block is still executed for DIAGS if LCD not in use
|
||||
// This block is still executed for DIAGS if LCD not in use
|
||||
LCD(0,F("DCC++ EX v%S"),F(VERSION));
|
||||
LCD(1,F("Lic GPLv3"));
|
||||
}
|
||||
LCD(1,F("Lic GPLv3"));
|
||||
}
|
||||
|
||||
// Responsibility 2: Start all the communications before the DCC engine
|
||||
// Start the WiFi interface on a MEGA, Uno cannot currently handle WiFi
|
||||
@@ -84,32 +90,26 @@ void setup()
|
||||
// detailed pin mappings and may also require modified subclasses of the MotorDriver to implement specialist logic.
|
||||
// STANDARD_MOTOR_SHIELD, POLOLU_MOTOR_SHIELD, FIREBOX_MK1, FIREBOX_MK1S are pre defined in MotorShields.h
|
||||
DCC::begin(MOTOR_SHIELD_TYPE);
|
||||
|
||||
|
||||
// Start RMFT (ignored if no automnation)
|
||||
RMFT::begin();
|
||||
|
||||
// Link to and call mySetup() function (if defined in the build in mySetup.cpp).
|
||||
// The contents will depend on the user's system hardware configuration.
|
||||
// The mySetup.cpp file is a standard C++ module so has access to all of the DCC++EX APIs.
|
||||
extern __attribute__((weak)) void mySetup();
|
||||
if (mySetup) {
|
||||
mySetup();
|
||||
}
|
||||
|
||||
// Invoke any DCC++EX commands in the form "SETUP("xxxx");"" found in optional file mySetup.h.
|
||||
// This can be used to create turnouts, outputs, sensors etc. throught the normal text commands.
|
||||
|
||||
// Invoke any DCC++EX commands in the form "SETUP("xxxx");"" found in optional file mySetup.h.
|
||||
// This can be used to create turnouts, outputs, sensors etc. through the normal text commands.
|
||||
#if __has_include ( "mySetup.h")
|
||||
#define SETUP(cmd) serialParser.parse(F(cmd))
|
||||
#include "mySetup.h"
|
||||
#undef SETUP
|
||||
#endif
|
||||
|
||||
#if defined(LCN_SERIAL)
|
||||
LCN_SERIAL.begin(115200);
|
||||
LCN::init(LCN_SERIAL);
|
||||
#define SETUP(cmd) DCCEXParser::parse(F(cmd))
|
||||
#include "mySetup.h"
|
||||
#undef SETUP
|
||||
#endif
|
||||
|
||||
LCD(1,F("Ready"));
|
||||
#if defined(LCN_SERIAL)
|
||||
LCN_SERIAL.begin(115200);
|
||||
LCN::init(LCN_SERIAL);
|
||||
#endif
|
||||
|
||||
LCD(3,F("Ready"));
|
||||
CommandDistributor::broadcastPower();
|
||||
}
|
||||
|
||||
void loop()
|
||||
@@ -121,9 +121,9 @@ void loop()
|
||||
DCC::loop();
|
||||
|
||||
// Responsibility 2: handle any incoming commands on USB connection
|
||||
serialParser.loop(Serial);
|
||||
SerialManager::loop();
|
||||
|
||||
// Responsibility 3: Optionally handle any incoming WiFi traffic
|
||||
// Responsibility 3: Optionally handle any incoming WiFi traffic
|
||||
#if WIFI_ON
|
||||
WifiInterface::loop();
|
||||
#endif
|
||||
@@ -133,22 +133,23 @@ void loop()
|
||||
|
||||
RMFT::loop(); // ignored if no automation
|
||||
|
||||
#if defined(LCN_SERIAL)
|
||||
LCN::loop();
|
||||
#if defined(LCN_SERIAL)
|
||||
LCN::loop();
|
||||
#endif
|
||||
|
||||
LCDDisplay::loop(); // ignored if LCD not in use
|
||||
LCDDisplay::loop(); // ignored if LCD not in use
|
||||
|
||||
// Handle/update IO devices.
|
||||
IODevice::loop();
|
||||
|
||||
|
||||
Sensor::checkAll(); // Update and print changes
|
||||
|
||||
// Report any decrease in memory (will automatically trigger on first call)
|
||||
static int ramLowWatermark = __INT_MAX__; // replaced on first loop
|
||||
static int ramLowWatermark = __INT_MAX__; // replaced on first loop
|
||||
|
||||
int freeNow = minimumFreeMemory();
|
||||
if (freeNow < ramLowWatermark)
|
||||
{
|
||||
if (freeNow < ramLowWatermark) {
|
||||
ramLowWatermark = freeNow;
|
||||
LCD(2,F("Free RAM=%5db"), ramLowWatermark);
|
||||
LCD(3,F("Free RAM=%5db"), ramLowWatermark);
|
||||
}
|
||||
}
|
||||
|
442
DCC.cpp
442
DCC.cpp
@@ -1,7 +1,13 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth
|
||||
*
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2021 Herb Morton
|
||||
* © 2020-2022 Harald Barth
|
||||
* © 2020-2021 M Steve Todd
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
@@ -17,21 +23,18 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if __has_include ( "config.h")
|
||||
#include "config.h"
|
||||
#else
|
||||
#include "config.example.h"
|
||||
#endif
|
||||
|
||||
#include "DIAG.h"
|
||||
#include "DCC.h"
|
||||
#include "DCCWaveform.h"
|
||||
#ifndef DISABLE_EEPROM
|
||||
#include "EEStore.h"
|
||||
#endif
|
||||
#include "GITHUB_SHA.h"
|
||||
#include "version.h"
|
||||
#include "FSH.h"
|
||||
#include "IODevice.h"
|
||||
#include "RMFT2.h"
|
||||
#include "CommandDistributor.h"
|
||||
|
||||
// This module is responsible for converting API calls into
|
||||
// messages to be sent to the waveform generator.
|
||||
@@ -46,11 +49,11 @@
|
||||
// Obtaining ACKs from the prog track using a function
|
||||
// There are no volatiles here.
|
||||
|
||||
const byte FN_GROUP_1=0x01;
|
||||
const byte FN_GROUP_2=0x02;
|
||||
const byte FN_GROUP_3=0x04;
|
||||
const byte FN_GROUP_4=0x08;
|
||||
const byte FN_GROUP_5=0x10;
|
||||
const byte FN_GROUP_1=0x01;
|
||||
const byte FN_GROUP_2=0x02;
|
||||
const byte FN_GROUP_3=0x04;
|
||||
const byte FN_GROUP_4=0x08;
|
||||
const byte FN_GROUP_5=0x10;
|
||||
|
||||
FSH* DCC::shieldName=NULL;
|
||||
byte DCC::joinRelay=UNUSED_PIN;
|
||||
@@ -63,11 +66,13 @@ void DCC::begin(const FSH * motorShieldName, MotorDriver * mainDriver, MotorDriv
|
||||
// Initialise HAL layer before reading EEprom.
|
||||
IODevice::begin();
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Load stuff from EEprom
|
||||
(void)EEPROM; // tell compiler not to warn this is unused
|
||||
EEStore::init();
|
||||
#endif
|
||||
|
||||
DCCWaveform::begin(mainDriver,progDriver);
|
||||
DCCWaveform::begin(mainDriver,progDriver);
|
||||
}
|
||||
|
||||
void DCC::setJoinRelayPin(byte joinRelayPin) {
|
||||
@@ -79,7 +84,7 @@ void DCC::setJoinRelayPin(byte joinRelayPin) {
|
||||
}
|
||||
|
||||
void DCC::setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection) {
|
||||
byte speedCode = (tSpeed & 0x7F) + tDirection * 128;
|
||||
byte speedCode = (tSpeed & 0x7F) + tDirection * 128;
|
||||
setThrottle2(cab, speedCode);
|
||||
// retain speed for loco reminders
|
||||
updateLocoReminder(cab, speedCode );
|
||||
@@ -90,8 +95,8 @@ void DCC::setThrottle2( uint16_t cab, byte speedCode) {
|
||||
uint8_t b[4];
|
||||
uint8_t nB = 0;
|
||||
// DIAG(F("setSpeedInternal %d %x"),cab,speedCode);
|
||||
|
||||
if (cab > 127)
|
||||
|
||||
if (cab > HIGHEST_SHORT_ADDR)
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
b[nB++] = lowByte(cab);
|
||||
|
||||
@@ -131,7 +136,7 @@ void DCC::setFunctionInternal(int cab, byte byte1, byte byte2) {
|
||||
byte b[4];
|
||||
byte nB = 0;
|
||||
|
||||
if (cab > 127)
|
||||
if (cab > HIGHEST_SHORT_ADDR)
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
b[nB++] = lowByte(cab);
|
||||
if (byte1!=0) b[nB++] = byte1;
|
||||
@@ -148,93 +153,74 @@ uint8_t DCC::getThrottleSpeed(int cab) {
|
||||
|
||||
bool DCC::getThrottleDirection(int cab) {
|
||||
int reg=lookupSpeedTable(cab);
|
||||
if (reg<0) return false ;
|
||||
if (reg<0) return true;
|
||||
return (speedTable[reg].speedCode & 0x80) !=0;
|
||||
}
|
||||
|
||||
// Set function to value on or off
|
||||
void DCC::setFn( int cab, int16_t functionNumber, bool on) {
|
||||
if (cab<=0 ) return;
|
||||
|
||||
if (functionNumber>28) {
|
||||
//non reminding advanced binary bit set
|
||||
|
||||
if (functionNumber>28) {
|
||||
//non reminding advanced binary bit set
|
||||
byte b[5];
|
||||
byte nB = 0;
|
||||
if (cab > 127)
|
||||
if (cab > HIGHEST_SHORT_ADDR)
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
b[nB++] = lowByte(cab);
|
||||
if (functionNumber <= 127) {
|
||||
b[nB++] = 0b11011101; // Binary State Control Instruction short form
|
||||
b[nB++] = 0b11011101; // Binary State Control Instruction short form
|
||||
b[nB++] = functionNumber | (on ? 0x80 : 0);
|
||||
}
|
||||
else {
|
||||
b[nB++] = 0b11000000; // Binary State Control Instruction long form
|
||||
b[nB++] = 0b11000000; // Binary State Control Instruction long form
|
||||
b[nB++] = (functionNumber & 0x7F) | (on ? 0x80 : 0); // low order bits and state flag
|
||||
b[nB++] = functionNumber >>7 ; // high order bits
|
||||
}
|
||||
DCCWaveform::mainTrack.schedulePacket(b, nB, 4);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int reg = lookupSpeedTable(cab);
|
||||
if (reg<0) return;
|
||||
if (reg<0) return;
|
||||
|
||||
// Take care of functions:
|
||||
// Set state of function
|
||||
unsigned long previous=speedTable[reg].functions;
|
||||
unsigned long funcmask = (1UL<<functionNumber);
|
||||
if (on) {
|
||||
speedTable[reg].functions |= funcmask;
|
||||
} else {
|
||||
speedTable[reg].functions &= ~funcmask;
|
||||
}
|
||||
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
|
||||
return;
|
||||
if (speedTable[reg].functions != previous) {
|
||||
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
|
||||
CommandDistributor::broadcastLoco(reg);
|
||||
}
|
||||
}
|
||||
|
||||
// Change function according to how button was pressed,
|
||||
// typically in WiThrottle.
|
||||
// Returns new state or -1 if nothing was changed.
|
||||
int DCC::changeFn( int cab, int16_t functionNumber, bool pressed) {
|
||||
int funcstate = -1;
|
||||
if (cab<=0 || functionNumber>28) return funcstate;
|
||||
// Flip function state
|
||||
void DCC::changeFn( int cab, int16_t functionNumber) {
|
||||
if (cab<=0 || functionNumber>28) return;
|
||||
int reg = lookupSpeedTable(cab);
|
||||
if (reg<0) return funcstate;
|
||||
|
||||
// Take care of functions:
|
||||
// Imitate how many command stations do it: Button press is
|
||||
// toggle but for F2 where it is momentary
|
||||
if (reg<0) return;
|
||||
unsigned long funcmask = (1UL<<functionNumber);
|
||||
if (functionNumber == 2) {
|
||||
// turn on F2 on press and off again at release of button
|
||||
if (pressed) {
|
||||
speedTable[reg].functions |= funcmask;
|
||||
funcstate = 1;
|
||||
} else {
|
||||
speedTable[reg].functions &= ~funcmask;
|
||||
funcstate = 0;
|
||||
}
|
||||
} else {
|
||||
// toggle function on press, ignore release
|
||||
if (pressed) {
|
||||
speedTable[reg].functions ^= funcmask;
|
||||
}
|
||||
funcstate = (speedTable[reg].functions & funcmask)? 1 : 0;
|
||||
}
|
||||
speedTable[reg].functions ^= funcmask;
|
||||
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
|
||||
return funcstate;
|
||||
CommandDistributor::broadcastLoco(reg);
|
||||
}
|
||||
|
||||
int DCC::getFn( int cab, int16_t functionNumber) {
|
||||
if (cab<=0 || functionNumber>28) return -1; // unknown
|
||||
int reg = lookupSpeedTable(cab);
|
||||
if (reg<0) return -1;
|
||||
if (reg<0) return -1;
|
||||
|
||||
unsigned long funcmask = (1UL<<functionNumber);
|
||||
return (speedTable[reg].functions & funcmask)? 1 : 0;
|
||||
}
|
||||
|
||||
// Set the group flag to say we have touched the particular group.
|
||||
// A group will be reminded only if it has been touched.
|
||||
// A group will be reminded only if it has been touched.
|
||||
void DCC::updateGroupflags(byte & flags, int16_t functionNumber) {
|
||||
byte groupMask;
|
||||
if (functionNumber<=4) groupMask=FN_GROUP_1;
|
||||
@@ -242,10 +228,19 @@ void DCC::updateGroupflags(byte & flags, int16_t functionNumber) {
|
||||
else if (functionNumber<=12) groupMask=FN_GROUP_3;
|
||||
else if (functionNumber<=20) groupMask=FN_GROUP_4;
|
||||
else groupMask=FN_GROUP_5;
|
||||
flags |= groupMask;
|
||||
flags |= groupMask;
|
||||
}
|
||||
|
||||
uint32_t DCC::getFunctionMap(int cab) {
|
||||
if (cab<=0) return 0; // unknown pretend all functions off
|
||||
int reg = lookupSpeedTable(cab);
|
||||
return (reg<0)?0:speedTable[reg].functions;
|
||||
}
|
||||
|
||||
void DCC::setAccessory(int address, byte number, bool activate) {
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DCC::setAccessory(%d,%d,%d)"), address, number, activate);
|
||||
#endif
|
||||
// use masks to detect wrong values and do nothing
|
||||
if(address != (address & 511))
|
||||
return;
|
||||
@@ -253,17 +248,13 @@ void DCC::setAccessory(int address, byte number, bool activate) {
|
||||
return;
|
||||
byte b[2];
|
||||
|
||||
// first byte is of the form 10AAAAAA, where AAAAAA represent 6 least signifcant bits of accessory address
|
||||
b[0] = address % 64 + 128;
|
||||
// second byte is of the form 1AAACDDD, where C should be 1, and the least significant D represent activate/deactivate
|
||||
// if we follow RCN-213, activate has to be reversed because in DCC++/DCC-EX activate=1 is "thrown, diverging",
|
||||
// but in RCN-213, 1 means "closed, straight" and 0 "thrown, diverging"
|
||||
#ifdef TURNOUTS_RCN_213
|
||||
activate = !activate;
|
||||
#endif
|
||||
b[1] = ((((address / 64) % 8) << 4) + (number % 4 << 1) + activate % 2) ^ 0xF8;
|
||||
b[0] = address % 64 + 128; // first byte is of the form 10AAAAAA, where AAAAAA represent 6 least signifcant bits of accessory address
|
||||
b[1] = ((((address / 64) % 8) << 4) + (number % 4 << 1) + activate % 2) ^ 0xF8; // second byte is of the form 1AAACDDD, where C should be 1, and the least significant D represent activate/deactivate
|
||||
|
||||
DCCWaveform::mainTrack.schedulePacket(b, 2, 4); // Repeat the packet four times
|
||||
#if defined(RMFT_ACTIVE)
|
||||
RMFT2::activateEvent(address<<2|number,activate);
|
||||
#endif
|
||||
}
|
||||
|
||||
//
|
||||
@@ -273,7 +264,7 @@ void DCC::setAccessory(int address, byte number, bool activate) {
|
||||
void DCC::writeCVByteMain(int cab, int cv, byte bValue) {
|
||||
byte b[5];
|
||||
byte nB = 0;
|
||||
if (cab > 127)
|
||||
if (cab > HIGHEST_SHORT_ADDR)
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
|
||||
b[nB++] = lowByte(cab);
|
||||
@@ -294,7 +285,7 @@ void DCC::writeCVBitMain(int cab, int cv, byte bNum, bool bValue) {
|
||||
bValue = bValue % 2;
|
||||
bNum = bNum % 8;
|
||||
|
||||
if (cab > 127)
|
||||
if (cab > HIGHEST_SHORT_ADDR)
|
||||
b[nB++] = highByte(cab) | 0xC0; // convert train number into a two-byte address
|
||||
|
||||
b[nB++] = lowByte(cab);
|
||||
@@ -316,63 +307,64 @@ void DCC::setProgTrackBoost(bool on) {
|
||||
FSH* DCC::getMotorShieldName() {
|
||||
return shieldName;
|
||||
}
|
||||
|
||||
|
||||
const ackOp FLASH WRITE_BIT0_PROG[] = {
|
||||
BASELINE,
|
||||
W0,WACK,
|
||||
V0, WACK, // validate bit is 0
|
||||
V0, WACK, // validate bit is 0
|
||||
ITC1, // if acked, callback(1)
|
||||
FAIL // callback (-1)
|
||||
};
|
||||
const ackOp FLASH WRITE_BIT1_PROG[] = {
|
||||
BASELINE,
|
||||
W1,WACK,
|
||||
V1, WACK, // validate bit is 1
|
||||
V1, WACK, // validate bit is 1
|
||||
ITC1, // if acked, callback(1)
|
||||
FAIL // callback (-1)
|
||||
};
|
||||
|
||||
const ackOp FLASH VERIFY_BIT0_PROG[] = {
|
||||
BASELINE,
|
||||
V0, WACK, // validate bit is 0
|
||||
V0, WACK, // validate bit is 0
|
||||
ITC0, // if acked, callback(0)
|
||||
V1, WACK, // validate bit is 1
|
||||
ITC1,
|
||||
ITC1,
|
||||
FAIL // callback (-1)
|
||||
};
|
||||
const ackOp FLASH VERIFY_BIT1_PROG[] = {
|
||||
BASELINE,
|
||||
V1, WACK, // validate bit is 1
|
||||
V1, WACK, // validate bit is 1
|
||||
ITC1, // if acked, callback(1)
|
||||
V0, WACK,
|
||||
V0, WACK,
|
||||
ITC0,
|
||||
FAIL // callback (-1)
|
||||
};
|
||||
|
||||
const ackOp FLASH READ_BIT_PROG[] = {
|
||||
BASELINE,
|
||||
V1, WACK, // validate bit is 1
|
||||
V1, WACK, // validate bit is 1
|
||||
ITC1, // if acked, callback(1)
|
||||
V0, WACK, // validate bit is zero
|
||||
ITC0, // if acked callback 0
|
||||
FAIL // bit not readable
|
||||
FAIL // bit not readable
|
||||
};
|
||||
|
||||
|
||||
const ackOp FLASH WRITE_BYTE_PROG[] = {
|
||||
BASELINE,
|
||||
WB,WACK,ITC1, // Write and callback(1) if ACK
|
||||
// handle decoders that dont ack a write
|
||||
VB,WACK,ITC1, // validate byte and callback(1) if correct
|
||||
WB,WACK,ITC1, // Write and callback(1) if ACK
|
||||
// handle decoders that dont ack a write
|
||||
VB,WACK,ITC1, // validate byte and callback(1) if correct
|
||||
FAIL // callback (-1)
|
||||
};
|
||||
|
||||
|
||||
const ackOp FLASH VERIFY_BYTE_PROG[] = {
|
||||
BASELINE,
|
||||
VB,WACK, // validate byte
|
||||
ITCB, // if ok callback value
|
||||
BIV, // ackManagerByte initial value
|
||||
VB,WACK, // validate byte
|
||||
ITCB, // if ok callback value
|
||||
STARTMERGE, //clear bit and byte values ready for merge pass
|
||||
// each bit is validated against 0 and the result inverted in MERGE
|
||||
// this is because there tend to be more zeros in cv values than ones.
|
||||
// this is because there tend to be more zeros in cv values than ones.
|
||||
// There is no need for one validation as entire byte is validated at the end
|
||||
V0, WACK, MERGE, // read and merge first tested bit (7)
|
||||
ITSKIP, // do small excursion if there was no ack
|
||||
@@ -387,15 +379,15 @@ const ackOp FLASH VERIFY_BYTE_PROG[] = {
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
VB, WACK, ITCB, // verify merged byte and return it if acked ok
|
||||
VB, WACK, ITCBV, // verify merged byte and return it if acked ok - with retry report
|
||||
FAIL };
|
||||
|
||||
|
||||
|
||||
|
||||
const ackOp FLASH READ_CV_PROG[] = {
|
||||
BASELINE,
|
||||
STARTMERGE, //clear bit and byte values ready for merge pass
|
||||
// each bit is validated against 0 and the result inverted in MERGE
|
||||
// this is because there tend to be more zeros in cv values than ones.
|
||||
// this is because there tend to be more zeros in cv values than ones.
|
||||
// There is no need for one validation as entire byte is validated at the end
|
||||
V0, WACK, MERGE, // read and merge first tested bit (7)
|
||||
ITSKIP, // do small excursion if there was no ack
|
||||
@@ -410,20 +402,20 @@ const ackOp FLASH READ_CV_PROG[] = {
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
VB, WACK, ITCB, // verify merged byte and return it if acked ok
|
||||
VB, WACK, ITCB, // verify merged byte and return it if acked ok
|
||||
FAIL }; // verification failed
|
||||
|
||||
|
||||
const ackOp FLASH LOCO_ID_PROG[] = {
|
||||
BASELINE,
|
||||
SETCV, (ackOp)19, // CV 19 is consist setting
|
||||
SETBYTE, (ackOp)0,
|
||||
SETBYTE, (ackOp)0,
|
||||
VB, WACK, ITSKIP, // ignore consist if cv19 is zero (no consist)
|
||||
SETBYTE, (ackOp)128,
|
||||
VB, WACK, ITSKIP, // ignore consist if cv19 is 128 (no consist, direction bit set)
|
||||
STARTMERGE, // Setup to read cv 19
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
@@ -431,13 +423,13 @@ const ackOp FLASH LOCO_ID_PROG[] = {
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
VB, WACK, ITCB7, // return 7 bits only, No_ACK means CV19 not supported so ignore it
|
||||
|
||||
SKIPTARGET, // continue here if CV 19 is zero or fails all validation
|
||||
|
||||
SKIPTARGET, // continue here if CV 19 is zero or fails all validation
|
||||
SETCV,(ackOp)29,
|
||||
SETBIT,(ackOp)5,
|
||||
V0, WACK, ITSKIP, // Skip to SKIPTARGET if bit 5 of CV29 is zero
|
||||
|
||||
// Long locoid
|
||||
|
||||
// Long locoid
|
||||
SETCV, (ackOp)17, // CV 17 is part of locoid
|
||||
STARTMERGE,
|
||||
V0, WACK, MERGE, // read and merge bit 1 etc
|
||||
@@ -449,8 +441,8 @@ const ackOp FLASH LOCO_ID_PROG[] = {
|
||||
V0, WACK, MERGE,
|
||||
V0, WACK, MERGE,
|
||||
VB, WACK, NAKFAIL, // verify merged byte and return -1 it if not acked ok
|
||||
STASHLOCOID, // keep stashed cv 17 for later
|
||||
// Read 2nd part from CV 18
|
||||
STASHLOCOID, // keep stashed cv 17 for later
|
||||
// Read 2nd part from CV 18
|
||||
SETCV, (ackOp)18,
|
||||
STARTMERGE,
|
||||
V0, WACK, MERGE, // read and merge bit 1 etc
|
||||
@@ -463,8 +455,8 @@ const ackOp FLASH LOCO_ID_PROG[] = {
|
||||
V0, WACK, MERGE,
|
||||
VB, WACK, NAKFAIL, // verify merged byte and return -1 it if not acked ok
|
||||
COMBINELOCOID, // Combile byte with stash to make long locoid and callback
|
||||
|
||||
// ITSKIP Skips to here if CV 29 bit 5 was zero. so read CV 1 and return that
|
||||
|
||||
// ITSKIP Skips to here if CV 29 bit 5 was zero. so read CV 1 and return that
|
||||
SKIPTARGET,
|
||||
SETCV, (ackOp)1,
|
||||
STARTMERGE,
|
||||
@@ -478,7 +470,7 @@ const ackOp FLASH LOCO_ID_PROG[] = {
|
||||
V0, WACK, MERGE,
|
||||
VB, WACK, ITCB, // verify merged byte and callback
|
||||
FAIL
|
||||
};
|
||||
};
|
||||
|
||||
const ackOp FLASH SHORT_LOCO_ID_PROG[] = {
|
||||
BASELINE,
|
||||
@@ -490,12 +482,12 @@ const ackOp FLASH SHORT_LOCO_ID_PROG[] = {
|
||||
SETBIT,(ackOp)5,
|
||||
W0,WACK,
|
||||
V0,WACK,NAKFAIL,
|
||||
SETCV, (ackOp)1,
|
||||
SETBYTEL, // low byte of word
|
||||
SETCV, (ackOp)1,
|
||||
SETBYTEL, // low byte of word
|
||||
WB,WACK, // some decoders don't ACK writes
|
||||
VB,WACK,ITCB,
|
||||
FAIL
|
||||
};
|
||||
};
|
||||
|
||||
const ackOp FLASH LONG_LOCO_ID_PROG[] = {
|
||||
BASELINE,
|
||||
@@ -510,16 +502,16 @@ const ackOp FLASH LONG_LOCO_ID_PROG[] = {
|
||||
V1,WACK,NAKFAIL,
|
||||
// Store high byte of address in cv 17
|
||||
SETCV, (ackOp)17,
|
||||
SETBYTEH, // high byte of word
|
||||
SETBYTEH, // high byte of word
|
||||
WB,WACK,
|
||||
VB,WACK,NAKFAIL,
|
||||
// store
|
||||
// store
|
||||
SETCV, (ackOp)18,
|
||||
SETBYTEL, // low byte of word
|
||||
SETBYTEL, // low byte of word
|
||||
WB,WACK,
|
||||
VB,WACK,ITC1, // callback(1) means Ok
|
||||
FAIL
|
||||
};
|
||||
};
|
||||
|
||||
void DCC::writeCVByte(int16_t cv, byte byteValue, ACK_CALLBACK callback) {
|
||||
ackManagerSetup(cv, byteValue, WRITE_BYTE_PROG, callback);
|
||||
@@ -558,24 +550,24 @@ void DCC::setLocoId(int id,ACK_CALLBACK callback) {
|
||||
callback(-1);
|
||||
return;
|
||||
}
|
||||
if (id<=127)
|
||||
if (id<=HIGHEST_SHORT_ADDR)
|
||||
ackManagerSetup(id, SHORT_LOCO_ID_PROG, callback);
|
||||
else
|
||||
ackManagerSetup(id | 0xc000,LONG_LOCO_ID_PROG, callback);
|
||||
}
|
||||
|
||||
void DCC::forgetLoco(int cab) { // removes any speed reminders for this loco
|
||||
setThrottle2(cab,1); // ESTOP this loco if still on track
|
||||
setThrottle2(cab,1); // ESTOP this loco if still on track
|
||||
int reg=lookupSpeedTable(cab);
|
||||
if (reg>=0) speedTable[reg].loco=0;
|
||||
setThrottle2(cab,1); // ESTOP if this loco still on track
|
||||
}
|
||||
void DCC::forgetAllLocos() { // removes all speed reminders
|
||||
setThrottle2(0,1); // ESTOP all locos still on track
|
||||
setThrottle2(0,1); // ESTOP all locos still on track
|
||||
for (int i=0;i<MAX_LOCOS;i++) speedTable[i].loco=0;
|
||||
}
|
||||
|
||||
byte DCC::loopStatus=0;
|
||||
byte DCC::loopStatus=0;
|
||||
|
||||
void DCC::loop() {
|
||||
DCCWaveform::loop(ackManagerProg!=NULL); // power overload checks
|
||||
@@ -590,58 +582,58 @@ void DCC::issueReminders() {
|
||||
// This loop searches for a loco in the speed table starting at nextLoco and cycling back around
|
||||
for (int reg=0;reg<MAX_LOCOS;reg++) {
|
||||
int slot=reg+nextLoco;
|
||||
if (slot>=MAX_LOCOS) slot-=MAX_LOCOS;
|
||||
if (slot>=MAX_LOCOS) slot-=MAX_LOCOS;
|
||||
if (speedTable[slot].loco > 0) {
|
||||
// have found the next loco to remind
|
||||
// have found the next loco to remind
|
||||
// issueReminder will return true if this loco is completed (ie speed and functions)
|
||||
if (issueReminder(slot)) nextLoco=slot+1;
|
||||
if (issueReminder(slot)) nextLoco=slot+1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool DCC::issueReminder(int reg) {
|
||||
unsigned long functions=speedTable[reg].functions;
|
||||
int loco=speedTable[reg].loco;
|
||||
byte flags=speedTable[reg].groupFlags;
|
||||
|
||||
|
||||
switch (loopStatus) {
|
||||
case 0:
|
||||
// DIAG(F("Reminder %d speed %d"),loco,speedTable[reg].speedCode);
|
||||
setThrottle2(loco, speedTable[reg].speedCode);
|
||||
break;
|
||||
case 1: // remind function group 1 (F0-F4)
|
||||
if (flags & FN_GROUP_1)
|
||||
if (flags & FN_GROUP_1)
|
||||
setFunctionInternal(loco,0, 128 | ((functions>>1)& 0x0F) | ((functions & 0x01)<<4)); // 100D DDDD
|
||||
break;
|
||||
break;
|
||||
case 2: // remind function group 2 F5-F8
|
||||
if (flags & FN_GROUP_2)
|
||||
if (flags & FN_GROUP_2)
|
||||
setFunctionInternal(loco,0, 176 | ((functions>>5)& 0x0F)); // 1011 DDDD
|
||||
break;
|
||||
break;
|
||||
case 3: // remind function group 3 F9-F12
|
||||
if (flags & FN_GROUP_3)
|
||||
if (flags & FN_GROUP_3)
|
||||
setFunctionInternal(loco,0, 160 | ((functions>>9)& 0x0F)); // 1010 DDDD
|
||||
break;
|
||||
break;
|
||||
case 4: // remind function group 4 F13-F20
|
||||
if (flags & FN_GROUP_4)
|
||||
setFunctionInternal(loco,222, ((functions>>13)& 0xFF));
|
||||
if (flags & FN_GROUP_4)
|
||||
setFunctionInternal(loco,222, ((functions>>13)& 0xFF));
|
||||
flags&= ~FN_GROUP_4; // dont send them again
|
||||
break;
|
||||
break;
|
||||
case 5: // remind function group 5 F21-F28
|
||||
if (flags & FN_GROUP_5)
|
||||
setFunctionInternal(loco,223, ((functions>>21)& 0xFF));
|
||||
setFunctionInternal(loco,223, ((functions>>21)& 0xFF));
|
||||
flags&= ~FN_GROUP_5; // dont send them again
|
||||
break;
|
||||
break;
|
||||
}
|
||||
loopStatus++;
|
||||
// if we reach status 6 then this loco is done so
|
||||
// reset status to 0 for next loco and return true so caller
|
||||
// moves on to next loco.
|
||||
// reset status to 0 for next loco and return true so caller
|
||||
// moves on to next loco.
|
||||
if (loopStatus>5) loopStatus=0;
|
||||
return loopStatus==0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
///// Private helper functions below here /////////////////////
|
||||
@@ -676,20 +668,28 @@ int DCC::lookupSpeedTable(int locoId) {
|
||||
}
|
||||
return reg;
|
||||
}
|
||||
|
||||
|
||||
void DCC::updateLocoReminder(int loco, byte speedCode) {
|
||||
|
||||
|
||||
if (loco==0) {
|
||||
// broadcast stop/estop but dont change direction
|
||||
for (int reg = 0; reg < MAX_LOCOS; reg++) {
|
||||
speedTable[reg].speedCode = (speedTable[reg].speedCode & 0x80) | (speedCode & 0x7f);
|
||||
if (speedTable[reg].loco==0) continue;
|
||||
byte newspeed=(speedTable[reg].speedCode & 0x80) | (speedCode & 0x7f);
|
||||
if (speedTable[reg].speedCode != newspeed) {
|
||||
speedTable[reg].speedCode = newspeed;
|
||||
CommandDistributor::broadcastLoco(reg);
|
||||
}
|
||||
}
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// determine speed reg for this loco
|
||||
int reg=lookupSpeedTable(loco);
|
||||
if (reg>=0) speedTable[reg].speedCode = speedCode;
|
||||
int reg=lookupSpeedTable(loco);
|
||||
if (reg>=0 && speedTable[reg].speedCode!=speedCode) {
|
||||
speedTable[reg].speedCode = speedCode;
|
||||
CommandDistributor::broadcastLoco(reg);
|
||||
}
|
||||
}
|
||||
|
||||
DCC::LOCO DCC::speedTable[MAX_LOCOS];
|
||||
@@ -697,9 +697,15 @@ int DCC::nextLoco = 0;
|
||||
|
||||
//ACK MANAGER
|
||||
ackOp const * DCC::ackManagerProg;
|
||||
ackOp const * DCC::ackManagerProgStart;
|
||||
byte DCC::ackManagerByte;
|
||||
byte DCC::ackManagerByteVerify;
|
||||
byte DCC::ackManagerStash;
|
||||
int DCC::ackManagerWord;
|
||||
byte DCC::ackManagerRetry;
|
||||
byte DCC::ackRetry = 2;
|
||||
int16_t DCC::ackRetrySum;
|
||||
int16_t DCC::ackRetryPSum;
|
||||
int DCC::ackManagerCv;
|
||||
byte DCC::ackManagerBitNum;
|
||||
bool DCC::ackReceived;
|
||||
@@ -715,24 +721,27 @@ void DCC::ackManagerSetup(int cv, byte byteValueOrBitnum, ackOp const program[]
|
||||
return;
|
||||
}
|
||||
|
||||
ackManagerRejoin=DCCWaveform::progTrackSyncMain;
|
||||
ackManagerRejoin=DCCWaveform::progTrackSyncMain;
|
||||
if (ackManagerRejoin ) {
|
||||
// Change from JOIN must zero resets packet.
|
||||
setProgTrackSyncMain(false);
|
||||
DCCWaveform::progTrack.sentResetsSincePacket = 0;
|
||||
DCCWaveform::progTrack.sentResetsSincePacket = 0;
|
||||
}
|
||||
|
||||
DCCWaveform::progTrack.autoPowerOff=false;
|
||||
|
||||
DCCWaveform::progTrack.autoPowerOff=false;
|
||||
if (DCCWaveform::progTrack.getPowerMode() == POWERMODE::OFF) {
|
||||
DCCWaveform::progTrack.autoPowerOff=true; // power off afterwards
|
||||
DCCWaveform::progTrack.autoPowerOff=true; // power off afterwards
|
||||
if (Diag::ACK) DIAG(F("Auto Prog power on"));
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
|
||||
DCCWaveform::progTrack.sentResetsSincePacket = 0;
|
||||
DCCWaveform::progTrack.sentResetsSincePacket = 0;
|
||||
}
|
||||
|
||||
ackManagerCv = cv;
|
||||
ackManagerProg = program;
|
||||
ackManagerProgStart = program;
|
||||
ackManagerRetry = ackRetry;
|
||||
ackManagerByte = byteValueOrBitnum;
|
||||
ackManagerByteVerify = byteValueOrBitnum;
|
||||
ackManagerBitNum=byteValueOrBitnum;
|
||||
ackManagerCallback = callback;
|
||||
}
|
||||
@@ -752,66 +761,67 @@ bool DCC::checkResets(uint8_t numResets) {
|
||||
void DCC::ackManagerLoop() {
|
||||
while (ackManagerProg) {
|
||||
byte opcode=GETFLASH(ackManagerProg);
|
||||
|
||||
|
||||
// breaks from this switch will step to next prog entry
|
||||
// returns from this switch will stay on same entry
|
||||
// (typically waiting for a reset counter or ACK waiting, or when all finished.)
|
||||
switch (opcode) {
|
||||
case BASELINE:
|
||||
if (DCCWaveform::progTrack.getPowerMode()==POWERMODE::OVERLOAD) return;
|
||||
if (checkResets(DCCWaveform::progTrack.autoPowerOff || ackManagerRejoin ? 20 : 3)) return;
|
||||
DCCWaveform::progTrack.setAckBaseline();
|
||||
callbackState=READY;
|
||||
break;
|
||||
case W0: // write 0 bit
|
||||
case W1: // write 1 bit
|
||||
break;
|
||||
case W0: // write 0 bit
|
||||
case W1: // write 1 bit
|
||||
{
|
||||
if (checkResets(RESET_MIN)) return;
|
||||
if (Diag::ACK) DIAG(F("W%d cv=%d bit=%d"),opcode==W1, ackManagerCv,ackManagerBitNum);
|
||||
if (Diag::ACK) DIAG(F("W%d cv=%d bit=%d"),opcode==W1, ackManagerCv,ackManagerBitNum);
|
||||
byte instruction = WRITE_BIT | (opcode==W1 ? BIT_ON : BIT_OFF) | ackManagerBitNum;
|
||||
byte message[] = {cv1(BIT_MANIPULATE, ackManagerCv), cv2(ackManagerCv), instruction };
|
||||
DCCWaveform::progTrack.schedulePacket(message, sizeof(message), PROG_REPEATS);
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
callbackState=AFTER_WRITE;
|
||||
}
|
||||
break;
|
||||
|
||||
case WB: // write byte
|
||||
break;
|
||||
|
||||
case WB: // write byte
|
||||
{
|
||||
if (checkResets( RESET_MIN)) return;
|
||||
if (Diag::ACK) DIAG(F("WB cv=%d value=%d"),ackManagerCv,ackManagerByte);
|
||||
byte message[] = {cv1(WRITE_BYTE, ackManagerCv), cv2(ackManagerCv), ackManagerByte};
|
||||
DCCWaveform::progTrack.schedulePacket(message, sizeof(message), PROG_REPEATS);
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
callbackState=AFTER_WRITE;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case VB: // Issue validate Byte packet
|
||||
{
|
||||
if (checkResets( RESET_MIN)) return;
|
||||
if (checkResets( RESET_MIN)) return;
|
||||
if (Diag::ACK) DIAG(F("VB cv=%d value=%d"),ackManagerCv,ackManagerByte);
|
||||
byte message[] = { cv1(VERIFY_BYTE, ackManagerCv), cv2(ackManagerCv), ackManagerByte};
|
||||
DCCWaveform::progTrack.schedulePacket(message, sizeof(message), PROG_REPEATS);
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case V0:
|
||||
case V1: // Issue validate bit=0 or bit=1 packet
|
||||
{
|
||||
if (checkResets(RESET_MIN)) return;
|
||||
if (Diag::ACK) DIAG(F("V%d cv=%d bit=%d"),opcode==V1, ackManagerCv,ackManagerBitNum);
|
||||
if (checkResets(RESET_MIN)) return;
|
||||
if (Diag::ACK) DIAG(F("V%d cv=%d bit=%d"),opcode==V1, ackManagerCv,ackManagerBitNum);
|
||||
byte instruction = VERIFY_BIT | (opcode==V0?BIT_OFF:BIT_ON) | ackManagerBitNum;
|
||||
byte message[] = {cv1(BIT_MANIPULATE, ackManagerCv), cv2(ackManagerCv), instruction };
|
||||
DCCWaveform::progTrack.schedulePacket(message, sizeof(message), PROG_REPEATS);
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
DCCWaveform::progTrack.setAckPending();
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case WACK: // wait for ack (or absence of ack)
|
||||
{
|
||||
byte ackState=2; // keep polling
|
||||
|
||||
|
||||
ackState=DCCWaveform::progTrack.getAck();
|
||||
if (ackState==2) return; // keep polling
|
||||
ackReceived=ackState==1;
|
||||
@@ -824,103 +834,127 @@ void DCC::ackManagerLoop() {
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case ITCB: // If True callback(byte)
|
||||
if (ackReceived) {
|
||||
callback(ackManagerByte);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case ITCBV: // If True callback(byte) - Verify
|
||||
if (ackReceived) {
|
||||
if (ackManagerByte == ackManagerByteVerify) {
|
||||
ackRetrySum ++;
|
||||
LCD(1, F("v %d %d Sum=%d"), ackManagerCv, ackManagerByte, ackRetrySum);
|
||||
}
|
||||
callback(ackManagerByte);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
case ITCB7: // If True callback(byte & 0x7F)
|
||||
if (ackReceived) {
|
||||
callback(ackManagerByte & 0x7F);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case NAKFAIL: // If nack callback(-1)
|
||||
if (!ackReceived) {
|
||||
callback(-1);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case FAIL: // callback(-1)
|
||||
callback(-1);
|
||||
return;
|
||||
|
||||
|
||||
case BIV: // ackManagerByte initial value
|
||||
ackManagerByte = ackManagerByteVerify;
|
||||
break;
|
||||
|
||||
case STARTMERGE:
|
||||
ackManagerBitNum=7;
|
||||
ackManagerByte=0;
|
||||
ackManagerByte=0;
|
||||
break;
|
||||
|
||||
|
||||
case MERGE: // Merge previous Validate zero wack response with byte value and update bit number (use for reading CV bytes)
|
||||
ackManagerByte <<= 1;
|
||||
// ackReceived means bit is zero.
|
||||
// ackReceived means bit is zero.
|
||||
if (!ackReceived) ackManagerByte |= 1;
|
||||
ackManagerBitNum--;
|
||||
break;
|
||||
|
||||
case SETBIT:
|
||||
ackManagerProg++;
|
||||
ackManagerProg++;
|
||||
ackManagerBitNum=GETFLASH(ackManagerProg);
|
||||
break;
|
||||
|
||||
case SETCV:
|
||||
ackManagerProg++;
|
||||
ackManagerProg++;
|
||||
ackManagerCv=GETFLASH(ackManagerProg);
|
||||
break;
|
||||
|
||||
case SETBYTE:
|
||||
ackManagerProg++;
|
||||
ackManagerProg++;
|
||||
ackManagerByte=GETFLASH(ackManagerProg);
|
||||
break;
|
||||
|
||||
case SETBYTEH:
|
||||
ackManagerByte=highByte(ackManagerWord);
|
||||
break;
|
||||
|
||||
|
||||
case SETBYTEL:
|
||||
ackManagerByte=lowByte(ackManagerWord);
|
||||
break;
|
||||
|
||||
case STASHLOCOID:
|
||||
ackManagerStash=ackManagerByte; // stash value from CV17
|
||||
ackManagerStash=ackManagerByte; // stash value from CV17
|
||||
break;
|
||||
|
||||
case COMBINELOCOID:
|
||||
|
||||
case COMBINELOCOID:
|
||||
// ackManagerStash is cv17, ackManagerByte is CV 18
|
||||
callback( ackManagerByte + ((ackManagerStash - 192) << 8));
|
||||
return;
|
||||
callback( LONG_ADDR_MARKER | ( ackManagerByte + ((ackManagerStash - 192) << 8)));
|
||||
return;
|
||||
|
||||
case ITSKIP:
|
||||
if (!ackReceived) break;
|
||||
if (!ackReceived) break;
|
||||
// SKIP opcodes until SKIPTARGET found
|
||||
while (opcode!=SKIPTARGET) {
|
||||
ackManagerProg++;
|
||||
ackManagerProg++;
|
||||
opcode=GETFLASH(ackManagerProg);
|
||||
}
|
||||
break;
|
||||
case SKIPTARGET:
|
||||
break;
|
||||
default:
|
||||
case SKIPTARGET:
|
||||
break;
|
||||
default:
|
||||
DIAG(F("!! ackOp %d FAULT!!"),opcode);
|
||||
callback( -1);
|
||||
return;
|
||||
|
||||
return;
|
||||
|
||||
} // end of switch
|
||||
ackManagerProg++;
|
||||
}
|
||||
}
|
||||
|
||||
void DCC::callback(int value) {
|
||||
// check for automatic retry
|
||||
if (value == -1 && ackManagerRetry > 0) {
|
||||
ackRetrySum ++;
|
||||
LCD(0, F("Retry %d %d Sum=%d"), ackManagerCv, ackManagerRetry, ackRetrySum);
|
||||
ackManagerRetry --;
|
||||
ackManagerProg = ackManagerProgStart;
|
||||
return;
|
||||
}
|
||||
|
||||
static unsigned long callbackStart;
|
||||
// We are about to leave programming mode
|
||||
// Rule 1: If we have written to a decoder we must maintain power for 100mS
|
||||
// Rule 2: If we are re-joining the main track we must power off for 30mS
|
||||
|
||||
switch (callbackState) {
|
||||
switch (callbackState) {
|
||||
case AFTER_WRITE: // first attempt to callback after a write operation
|
||||
if (!ackManagerRejoin && !DCCWaveform::progTrack.autoPowerOff) {
|
||||
callbackState=READY;
|
||||
@@ -930,7 +964,7 @@ void DCC::callback(int value) {
|
||||
callbackState=WAITING_100;
|
||||
if (Diag::ACK) DIAG(F("Stable 100mS"));
|
||||
break;
|
||||
|
||||
|
||||
case WAITING_100: // waiting for 100mS
|
||||
if (millis()-callbackStart < 100) break;
|
||||
// stable after power maintained for 100mS
|
||||
@@ -939,20 +973,20 @@ void DCC::callback(int value) {
|
||||
// but if we will keep the power on, we must off it for 30mS
|
||||
if (DCCWaveform::progTrack.autoPowerOff) callbackState=READY;
|
||||
else { // Need to cycle power off and on
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF);
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF);
|
||||
callbackStart=millis();
|
||||
callbackState=WAITING_30;
|
||||
if (Diag::ACK) DIAG(F("OFF 30mS"));
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case WAITING_30: // waiting for 30mS with power off
|
||||
if (millis()-callbackStart < 30) break;
|
||||
//power has been off for 30mS
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
|
||||
callbackState=READY;
|
||||
break;
|
||||
|
||||
|
||||
case READY: // ready after read, or write after power delay and off period.
|
||||
// power off if we powered it on
|
||||
if (DCCWaveform::progTrack.autoPowerOff) {
|
||||
@@ -963,8 +997,8 @@ void DCC::callback(int value) {
|
||||
if (ackManagerRejoin) {
|
||||
setProgTrackSyncMain(true);
|
||||
if (Diag::ACK) DIAG(F("Auto JOIN"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ackManagerProg=NULL; // no more steps to execute
|
||||
if (Diag::ACK) DIAG(F("Callback(%d)"),value);
|
||||
(ackManagerCallback)( value);
|
||||
@@ -977,10 +1011,10 @@ void DCC::displayCabList(Print * stream) {
|
||||
for (int reg = 0; reg < MAX_LOCOS; reg++) {
|
||||
if (speedTable[reg].loco>0) {
|
||||
used ++;
|
||||
StringFormatter::send(stream,F("cab=%d, speed=%d, dir=%c \n"),
|
||||
StringFormatter::send(stream,F("cab=%d, speed=%d, dir=%c \n"),
|
||||
speedTable[reg].loco, speedTable[reg].speedCode & 0x7f,(speedTable[reg].speedCode & 0x80) ? 'F':'R');
|
||||
}
|
||||
}
|
||||
StringFormatter::send(stream,F("Used=%d, max=%d\n"),used,MAX_LOCOS);
|
||||
|
||||
|
||||
}
|
||||
|
43
DCC.h
43
DCC.h
@@ -1,5 +1,10 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2021 Herb Morton
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
@@ -23,6 +28,16 @@
|
||||
#include "MotorDrivers.h"
|
||||
#include "FSH.h"
|
||||
|
||||
#include "defines.h"
|
||||
#ifndef HIGHEST_SHORT_ADDR
|
||||
#define HIGHEST_SHORT_ADDR 127
|
||||
#else
|
||||
#if HIGHEST_SHORT_ADDR > 127
|
||||
#error short addr greater than 127 does not make sense
|
||||
#endif
|
||||
#endif
|
||||
const uint16_t LONG_ADDR_MARKER = 0x4000;
|
||||
|
||||
typedef void (*ACK_CALLBACK)(int16_t result);
|
||||
|
||||
enum ackOp : byte
|
||||
@@ -38,9 +53,11 @@ enum ackOp : byte
|
||||
ITC1, // If True Callback(1) (if prevous WACK got an ACK)
|
||||
ITC0, // If True callback(0);
|
||||
ITCB, // If True callback(byte)
|
||||
ITCBV, // If True callback(byte) - end of Verify Byte
|
||||
ITCB7, // If True callback(byte &0x7F)
|
||||
NAKFAIL, // if false callback(-1)
|
||||
FAIL, // callback(-1)
|
||||
BIV, // Set ackManagerByte to initial value for Verify retry
|
||||
STARTMERGE, // Clear bit and byte settings ready for merge pass
|
||||
MERGE, // Merge previous wack response with byte value and decrement bit number (use for readimng CV bytes)
|
||||
SETBIT, // sets bit number to next prog byte
|
||||
@@ -64,8 +81,10 @@ enum CALLBACK_STATE : byte {
|
||||
|
||||
// Allocations with memory implications..!
|
||||
// Base system takes approx 900 bytes + 8 per loco. Turnouts, Sensors etc are dynamically created
|
||||
#ifdef ARDUINO_AVR_UNO
|
||||
#if defined(ARDUINO_AVR_UNO)
|
||||
const byte MAX_LOCOS = 20;
|
||||
#elif defined(ARDUINO_AVR_NANO)
|
||||
const byte MAX_LOCOS = 30;
|
||||
#else
|
||||
const byte MAX_LOCOS = 50;
|
||||
#endif
|
||||
@@ -85,8 +104,9 @@ public:
|
||||
static void writeCVBitMain(int cab, int cv, byte bNum, bool bValue);
|
||||
static void setFunction(int cab, byte fByte, byte eByte);
|
||||
static void setFn(int cab, int16_t functionNumber, bool on);
|
||||
static int changeFn(int cab, int16_t functionNumber, bool pressed);
|
||||
static void changeFn(int cab, int16_t functionNumber);
|
||||
static int getFn(int cab, int16_t functionNumber);
|
||||
static uint32_t getFunctionMap(int cab);
|
||||
static void updateGroupflags(byte &flags, int16_t functionNumber);
|
||||
static void setAccessory(int aAdd, byte aNum, bool activate);
|
||||
static bool writeTextPacket(byte *b, int nBytes);
|
||||
@@ -113,8 +133,13 @@ public:
|
||||
static inline void setGlobalSpeedsteps(byte s) {
|
||||
globalSpeedsteps = s;
|
||||
};
|
||||
static inline int16_t setAckRetry(byte retry) {
|
||||
ackRetry = retry;
|
||||
ackRetryPSum = ackRetrySum;
|
||||
ackRetrySum = 0; // reset running total
|
||||
return ackRetryPSum;
|
||||
};
|
||||
|
||||
private:
|
||||
struct LOCO
|
||||
{
|
||||
int loco;
|
||||
@@ -122,6 +147,9 @@ private:
|
||||
byte groupFlags;
|
||||
unsigned long functions;
|
||||
};
|
||||
static LOCO speedTable[MAX_LOCOS];
|
||||
|
||||
private:
|
||||
static byte joinRelay;
|
||||
static byte loopStatus;
|
||||
static void setThrottle2(uint16_t cab, uint8_t speedCode);
|
||||
@@ -132,7 +160,6 @@ private:
|
||||
static FSH *shieldName;
|
||||
static byte globalSpeedsteps;
|
||||
|
||||
static LOCO speedTable[MAX_LOCOS];
|
||||
static byte cv1(byte opcode, int cv);
|
||||
static byte cv2(int cv);
|
||||
static int lookupSpeedTable(int locoId);
|
||||
@@ -141,9 +168,15 @@ private:
|
||||
|
||||
// ACK MANAGER
|
||||
static ackOp const *ackManagerProg;
|
||||
static ackOp const *ackManagerProgStart;
|
||||
static byte ackManagerByte;
|
||||
static byte ackManagerByteVerify;
|
||||
static byte ackManagerBitNum;
|
||||
static int ackManagerCv;
|
||||
static byte ackManagerRetry;
|
||||
static byte ackRetry;
|
||||
static int16_t ackRetrySum;
|
||||
static int16_t ackRetryPSum;
|
||||
static int ackManagerWord;
|
||||
static byte ackManagerStash;
|
||||
static bool ackReceived;
|
||||
|
10
DCCEX.h
10
DCCEX.h
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* (c) 2020 Chris Harlow. All rights reserved.
|
||||
* (c) 2021 Fred Decker. All rights reserved.
|
||||
* (c) 2020 Harald Barth. All rights reserved.
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -29,6 +30,7 @@
|
||||
#include "DCC.h"
|
||||
#include "DIAG.h"
|
||||
#include "DCCEXParser.h"
|
||||
#include "SerialManager.h"
|
||||
#include "version.h"
|
||||
#include "WifiInterface.h"
|
||||
#if ETHERNET_ON == true
|
||||
@@ -42,6 +44,6 @@
|
||||
#include "Sensors.h"
|
||||
#include "Outputs.h"
|
||||
#include "RMFT.h"
|
||||
|
||||
#include "CommandDistributor.h"
|
||||
|
||||
#endif
|
||||
|
400
DCCEXParser.cpp
400
DCCEXParser.cpp
@@ -1,6 +1,12 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Mike S
|
||||
* © 2021 Herb Morton
|
||||
* © 2020-2022 Harald Barth
|
||||
* © 2020-2021 M Steve Todd
|
||||
* © 2020-2021 Fred Decker
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -27,11 +33,22 @@
|
||||
#include "freeMemory.h"
|
||||
#include "GITHUB_SHA.h"
|
||||
#include "version.h"
|
||||
|
||||
#include "defines.h"
|
||||
#include "CommandDistributor.h"
|
||||
#include "EEStore.h"
|
||||
#include "DIAG.h"
|
||||
#include <avr/wdt.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Figure out if we have enough memory for advanced features
|
||||
//
|
||||
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO)
|
||||
// nope
|
||||
#else
|
||||
#define HAS_ENOUGH_MEMORY
|
||||
#endif
|
||||
|
||||
// These keywords are used in the <1> command. The number is what you get if you use the keyword as a parameter.
|
||||
// To discover new keyword numbers , use the <$ YOURKEYWORD> command
|
||||
const int16_t HASH_KEYWORD_PROG = -29718;
|
||||
@@ -40,27 +57,36 @@ const int16_t HASH_KEYWORD_JOIN = -30750;
|
||||
const int16_t HASH_KEYWORD_CABS = -11981;
|
||||
const int16_t HASH_KEYWORD_RAM = 25982;
|
||||
const int16_t HASH_KEYWORD_CMD = 9962;
|
||||
const int16_t HASH_KEYWORD_WIT = 31594;
|
||||
const int16_t HASH_KEYWORD_WIFI = -5583;
|
||||
const int16_t HASH_KEYWORD_ACK = 3113;
|
||||
const int16_t HASH_KEYWORD_ON = 2657;
|
||||
const int16_t HASH_KEYWORD_DCC = 6436;
|
||||
const int16_t HASH_KEYWORD_SLOW = -17209;
|
||||
const int16_t HASH_KEYWORD_PROGBOOST = -6353;
|
||||
#ifndef DISABLE_EEPROM
|
||||
const int16_t HASH_KEYWORD_EEPROM = -7168;
|
||||
#endif
|
||||
const int16_t HASH_KEYWORD_LIMIT = 27413;
|
||||
const int16_t HASH_KEYWORD_ETHERNET = -30767;
|
||||
const int16_t HASH_KEYWORD_MAX = 16244;
|
||||
const int16_t HASH_KEYWORD_MIN = 15978;
|
||||
const int16_t HASH_KEYWORD_LCN = 15137;
|
||||
const int16_t HASH_KEYWORD_RESET = 26133;
|
||||
const int16_t HASH_KEYWORD_RETRY = 25704;
|
||||
const int16_t HASH_KEYWORD_SPEED28 = -17064;
|
||||
const int16_t HASH_KEYWORD_SPEED128 = 25816;
|
||||
const int16_t HASH_KEYWORD_SERVO = 27709;
|
||||
const int16_t HASH_KEYWORD_SERVO=27709;
|
||||
const int16_t HASH_KEYWORD_VPIN=-415;
|
||||
const int16_t HASH_KEYWORD_C=67;
|
||||
const int16_t HASH_KEYWORD_T=84;
|
||||
const int16_t HASH_KEYWORD_LCN = 15137;
|
||||
const int16_t HASH_KEYWORD_HAL = 10853;
|
||||
const int16_t HASH_KEYWORD_SHOW = -21309;
|
||||
const int16_t HASH_KEYWORD_ANIN = -10424;
|
||||
const int16_t HASH_KEYWORD_ANOUT = -26399;
|
||||
const int16_t HASH_KEYWORD_WIFI = -5583;
|
||||
const int16_t HASH_KEYWORD_ETHERNET = -30767;
|
||||
const int16_t HASH_KEYWORD_WIT = 31594;
|
||||
|
||||
int16_t DCCEXParser::stashP[MAX_COMMAND_PARAMS];
|
||||
bool DCCEXParser::stashBusy;
|
||||
|
||||
Print *DCCEXParser::stashStream = NULL;
|
||||
RingStream *DCCEXParser::stashRingStream = NULL;
|
||||
byte DCCEXParser::stashTarget=0;
|
||||
@@ -71,46 +97,8 @@ byte DCCEXParser::stashTarget=0;
|
||||
// calls the corresponding DCC api.
|
||||
// Non-DCC things like turnouts, pins and sensors are handled in additional JMRI interface classes.
|
||||
|
||||
DCCEXParser::DCCEXParser() {}
|
||||
void DCCEXParser::flush()
|
||||
{
|
||||
if (Diag::CMD)
|
||||
DIAG(F("Buffer flush"));
|
||||
bufferLength = 0;
|
||||
inCommandPayload = false;
|
||||
}
|
||||
|
||||
void DCCEXParser::loop(Stream &stream)
|
||||
{
|
||||
while (stream.available())
|
||||
{
|
||||
if (bufferLength == MAX_BUFFER)
|
||||
{
|
||||
flush();
|
||||
}
|
||||
char ch = stream.read();
|
||||
if (ch == '<')
|
||||
{
|
||||
inCommandPayload = true;
|
||||
bufferLength = 0;
|
||||
buffer[0] = '\0';
|
||||
}
|
||||
else if (ch == '>')
|
||||
{
|
||||
buffer[bufferLength] = '\0';
|
||||
parse(&stream, buffer, NULL); // Parse this (No ringStream for serial)
|
||||
inCommandPayload = false;
|
||||
break;
|
||||
}
|
||||
else if (inCommandPayload)
|
||||
{
|
||||
buffer[bufferLength++] = ch;
|
||||
}
|
||||
}
|
||||
Sensor::checkAll(&stream); // Update and print changes
|
||||
}
|
||||
|
||||
int16_t DCCEXParser::splitValues(int16_t result[MAX_COMMAND_PARAMS], const byte *cmd)
|
||||
int16_t DCCEXParser::splitValues(int16_t result[MAX_COMMAND_PARAMS], const byte *cmd, bool usehex)
|
||||
{
|
||||
byte state = 1;
|
||||
byte parameterCount = 0;
|
||||
@@ -148,10 +136,15 @@ int16_t DCCEXParser::splitValues(int16_t result[MAX_COMMAND_PARAMS], const byte
|
||||
case 3: // building a parameter
|
||||
if (hot >= '0' && hot <= '9')
|
||||
{
|
||||
runningValue = 10 * runningValue + (hot - '0');
|
||||
runningValue = (usehex?16:10) * runningValue + (hot - '0');
|
||||
break;
|
||||
}
|
||||
if (hot >= 'a' && hot <= 'z') hot=hot-'a'+'A'; // uppercase a..z
|
||||
if (usehex && hot>='A' && hot<='F') {
|
||||
// treat A..F as hex not keyword
|
||||
runningValue = 16 * runningValue + (hot - 'A' + 10);
|
||||
break;
|
||||
}
|
||||
if (hot >= 'A' && hot <= 'Z')
|
||||
{
|
||||
// Since JMRI got modified to send keywords in some rare cases, we need this
|
||||
@@ -169,66 +162,6 @@ int16_t DCCEXParser::splitValues(int16_t result[MAX_COMMAND_PARAMS], const byte
|
||||
return parameterCount;
|
||||
}
|
||||
|
||||
int16_t DCCEXParser::splitHexValues(int16_t result[MAX_COMMAND_PARAMS], const byte *cmd)
|
||||
{
|
||||
byte state = 1;
|
||||
byte parameterCount = 0;
|
||||
int16_t runningValue = 0;
|
||||
const byte *remainingCmd = cmd + 1; // skips the opcode
|
||||
|
||||
// clear all parameters in case not enough found
|
||||
for (int16_t i = 0; i < MAX_COMMAND_PARAMS; i++)
|
||||
result[i] = 0;
|
||||
|
||||
while (parameterCount < MAX_COMMAND_PARAMS)
|
||||
{
|
||||
byte hot = *remainingCmd;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
|
||||
case 1: // skipping spaces before a param
|
||||
if (hot == ' ')
|
||||
break;
|
||||
if (hot == '\0' || hot == '>')
|
||||
return parameterCount;
|
||||
state = 2;
|
||||
continue;
|
||||
|
||||
case 2: // checking first hex digit
|
||||
runningValue = 0;
|
||||
state = 3;
|
||||
continue;
|
||||
|
||||
case 3: // building a parameter
|
||||
if (hot >= '0' && hot <= '9')
|
||||
{
|
||||
runningValue = 16 * runningValue + (hot - '0');
|
||||
break;
|
||||
}
|
||||
if (hot >= 'A' && hot <= 'F')
|
||||
{
|
||||
runningValue = 16 * runningValue + 10 + (hot - 'A');
|
||||
break;
|
||||
}
|
||||
if (hot >= 'a' && hot <= 'f')
|
||||
{
|
||||
runningValue = 16 * runningValue + 10 + (hot - 'a');
|
||||
break;
|
||||
}
|
||||
if (hot==' ' || hot=='>' || hot=='\0') {
|
||||
result[parameterCount] = runningValue;
|
||||
parameterCount++;
|
||||
state = 1;
|
||||
continue;
|
||||
}
|
||||
return -1; // invalid hex digit
|
||||
}
|
||||
remainingCmd++;
|
||||
}
|
||||
return parameterCount;
|
||||
}
|
||||
|
||||
FILTER_CALLBACK DCCEXParser::filterCallback = 0;
|
||||
FILTER_CALLBACK DCCEXParser::filterRMFTCallback = 0;
|
||||
AT_COMMAND_CALLBACK DCCEXParser::atCommandCallback = 0;
|
||||
@@ -247,6 +180,7 @@ void DCCEXParser::setAtCommandCallback(AT_COMMAND_CALLBACK callback)
|
||||
|
||||
// Parse an F() string
|
||||
void DCCEXParser::parse(const FSH * cmd) {
|
||||
DIAG(F("SETUP(\"%S\")"),cmd);
|
||||
int size=strlen_P((char *)cmd)+1;
|
||||
char buffer[size];
|
||||
strcpy_P(buffer,(char *)cmd);
|
||||
@@ -254,17 +188,20 @@ void DCCEXParser::parse(const FSH * cmd) {
|
||||
}
|
||||
|
||||
// See documentation on DCC class for info on this section
|
||||
|
||||
void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
{
|
||||
#ifndef DISABLE_EEPROM
|
||||
(void)EEPROM; // tell compiler not to warn this is unused
|
||||
#endif
|
||||
if (Diag::CMD)
|
||||
DIAG(F("PARSING:%s"), com);
|
||||
int16_t p[MAX_COMMAND_PARAMS];
|
||||
while (com[0] == '<' || com[0] == ' ')
|
||||
com++; // strip off any number of < or spaces
|
||||
byte params = splitValues(p, com);
|
||||
byte opcode = com[0];
|
||||
|
||||
byte params = splitValues(p, com, opcode=='M' || opcode=='P');
|
||||
|
||||
if (filterCallback)
|
||||
filterCallback(stream, opcode, params, p);
|
||||
if (filterRMFTCallback && opcode!='\0')
|
||||
@@ -312,10 +249,9 @@ void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
break; // invalid direction code
|
||||
|
||||
DCC::setThrottle(cab, tspeed, direction);
|
||||
if (params == 4)
|
||||
if (params == 4) // send obsolete format T response
|
||||
StringFormatter::send(stream, F("<T %d %d %d>\n"), p[0], p[2], p[3]);
|
||||
else
|
||||
StringFormatter::send(stream, F("<O>\n"));
|
||||
// speed change will be broadcast anyway in new <l > format
|
||||
return;
|
||||
}
|
||||
case 'f': // FUNCTION <f CAB BYTE1 [BYTE2]>
|
||||
@@ -345,8 +281,12 @@ void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
|| ((subaddress & 0x03) != subaddress) // invalid subaddress (limit 2 bits )
|
||||
|| ((p[activep] & 0x01) != p[activep]) // invalid activate 0|1
|
||||
) break;
|
||||
// TODO: Trigger configurable range of addresses on local VPins.
|
||||
// Honour the configuration option (config.h) which allows the <a> command to be reversed
|
||||
#ifdef DCC_ACCESSORY_COMMAND_REVERSE
|
||||
DCC::setAccessory(address, subaddress,p[activep]==0);
|
||||
#else
|
||||
DCC::setAccessory(address, subaddress,p[activep]==1);
|
||||
#endif
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -375,8 +315,8 @@ void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
|
||||
case 'M': // WRITE TRANSPARENT DCC PACKET MAIN <M REG X1 ... X9>
|
||||
case 'P': // WRITE TRANSPARENT DCC PACKET PROG <P REG X1 ... X9>
|
||||
// Re-parse the command using a hex-only splitter
|
||||
params=splitHexValues(p,com)-1; // drop REG
|
||||
// NOTE: this command was parsed in HEX instead of decimal
|
||||
params--; // drop REG
|
||||
if (params<1) break;
|
||||
{
|
||||
byte packet[params];
|
||||
@@ -437,54 +377,67 @@ void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
}
|
||||
break;
|
||||
|
||||
case '1': // POWERON <1 [MAIN|PROG]>
|
||||
case '0': // POWEROFF <0 [MAIN | PROG] >
|
||||
if (params > 1)
|
||||
break;
|
||||
case '1': // POWERON <1 [MAIN|PROG|JOIN]>
|
||||
{
|
||||
POWERMODE mode = opcode == '1' ? POWERMODE::ON : POWERMODE::OFF;
|
||||
DCC::setProgTrackSyncMain(false); // Only <1 JOIN> will set this on, all others set it off
|
||||
if (params == 0 ||
|
||||
(MotorDriver::commonFaultPin && p[0] != HASH_KEYWORD_JOIN)) // commonFaultPin prevents individual track handling
|
||||
{
|
||||
DCCWaveform::mainTrack.setPowerMode(mode);
|
||||
DCCWaveform::progTrack.setPowerMode(mode);
|
||||
if (mode == POWERMODE::OFF)
|
||||
DCC::setProgTrackBoost(false); // Prog track boost mode will not outlive prog track off
|
||||
StringFormatter::send(stream, F("<p%c>\n"), opcode);
|
||||
return;
|
||||
}
|
||||
switch (p[0])
|
||||
{
|
||||
case HASH_KEYWORD_MAIN:
|
||||
DCCWaveform::mainTrack.setPowerMode(mode);
|
||||
StringFormatter::send(stream, F("<p%c MAIN>\n"), opcode);
|
||||
return;
|
||||
|
||||
case HASH_KEYWORD_PROG:
|
||||
DCCWaveform::progTrack.setPowerMode(mode);
|
||||
if (mode == POWERMODE::OFF)
|
||||
DCC::setProgTrackBoost(false); // Prog track boost mode will not outlive prog track off
|
||||
StringFormatter::send(stream, F("<p%c PROG>\n"), opcode);
|
||||
return;
|
||||
case HASH_KEYWORD_JOIN:
|
||||
DCCWaveform::mainTrack.setPowerMode(mode);
|
||||
DCCWaveform::progTrack.setPowerMode(mode);
|
||||
if (mode == POWERMODE::ON)
|
||||
{
|
||||
DCC::setProgTrackSyncMain(true);
|
||||
StringFormatter::send(stream, F("<p1 JOIN>\n"), opcode);
|
||||
}
|
||||
else
|
||||
StringFormatter::send(stream, F("<p0>\n"));
|
||||
return;
|
||||
}
|
||||
break;
|
||||
bool main=false;
|
||||
bool prog=false;
|
||||
bool join=false;
|
||||
if (params > 1) break;
|
||||
if (params==0) { // <1>
|
||||
main=true;
|
||||
prog=true;
|
||||
}
|
||||
else if (p[0] == HASH_KEYWORD_JOIN) { // <1 JOIN>
|
||||
main=true;
|
||||
prog=true;
|
||||
join=!MotorDriver::commonFaultPin;
|
||||
}
|
||||
else if (p[0]==HASH_KEYWORD_MAIN) { // <1 MAIN>
|
||||
main=true;
|
||||
}
|
||||
else if (p[0]==HASH_KEYWORD_PROG) { // <1 PROG>
|
||||
prog=true;
|
||||
}
|
||||
else break; // will reply <X>
|
||||
|
||||
if (main) DCCWaveform::mainTrack.setPowerMode(POWERMODE::ON);
|
||||
if (prog) DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
|
||||
DCC::setProgTrackSyncMain(join);
|
||||
|
||||
CommandDistributor::broadcastPower();
|
||||
return;
|
||||
}
|
||||
|
||||
case '0': // POWEROFF <0 [MAIN | PROG] >
|
||||
{
|
||||
bool main=false;
|
||||
bool prog=false;
|
||||
if (params > 1) break;
|
||||
if (params==0) { // <0>
|
||||
main=true;
|
||||
prog=true;
|
||||
}
|
||||
else if (p[0]==HASH_KEYWORD_MAIN) { // <0 MAIN>
|
||||
main=true;
|
||||
}
|
||||
else if (p[0]==HASH_KEYWORD_PROG) { // <0 PROG>
|
||||
prog=true;
|
||||
}
|
||||
else break; // will reply <X>
|
||||
|
||||
if (main) DCCWaveform::mainTrack.setPowerMode(POWERMODE::OFF);
|
||||
if (prog) {
|
||||
DCC::setProgTrackBoost(false); // Prog track boost mode will not outlive prog track off
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF);
|
||||
}
|
||||
DCC::setProgTrackSyncMain(false);
|
||||
|
||||
CommandDistributor::broadcastPower();
|
||||
return;
|
||||
}
|
||||
|
||||
case '!': // ESTOP ALL <!>
|
||||
DCC::setThrottle(0,1,1); // this broadcasts speed 1(estop) and sets all reminders to speed 1.
|
||||
DCC::setThrottle(0,1,1); // this broadcasts speed 1(estop) and sets all reminders to speed 1.
|
||||
return;
|
||||
|
||||
case 'c': // SEND METER RESPONSES <c>
|
||||
@@ -507,6 +460,7 @@ void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
// TODO Send stats of speed reminders table
|
||||
return;
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
case 'E': // STORE EPROM <E>
|
||||
EEStore::store();
|
||||
StringFormatter::send(stream, F("<e %d %d %d>\n"), EEStore::eeStore->data.nTurnouts, EEStore::eeStore->data.nSensors, EEStore::eeStore->data.nOutputs);
|
||||
@@ -516,7 +470,7 @@ void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
EEStore::clear();
|
||||
StringFormatter::send(stream, F("<O>\n"));
|
||||
return;
|
||||
|
||||
#endif
|
||||
case ' ': // < >
|
||||
StringFormatter::send(stream, F("\n"));
|
||||
return;
|
||||
@@ -537,16 +491,17 @@ void DCCEXParser::parse(Print *stream, byte *com, RingStream * ringStream)
|
||||
return;
|
||||
|
||||
case 'F': // New command to call the new Loco Function API <F cab func 1|0>
|
||||
if(params!=3) break;
|
||||
if (Diag::CMD)
|
||||
DIAG(F("Setting loco %d F%d %S"), p[0], p[1], p[2] ? F("ON") : F("OFF"));
|
||||
DCC::setFn(p[0], p[1], p[2] == 1);
|
||||
return;
|
||||
|
||||
case '+': // Complex Wifi interface command (not usual parse)
|
||||
if (atCommandCallback) {
|
||||
if (atCommandCallback && !ringStream) {
|
||||
DCCWaveform::mainTrack.setPowerMode(POWERMODE::OFF);
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF);
|
||||
atCommandCallback(com);
|
||||
atCommandCallback((HardwareSerial *)stream,com);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
@@ -658,7 +613,7 @@ bool DCCEXParser::parseT(Print *stream, int16_t params, int16_t p[])
|
||||
case 0: // <T> list turnout definitions
|
||||
{
|
||||
bool gotOne = false;
|
||||
for (Turnout *tt = Turnout::firstTurnout; tt != NULL; tt = tt->nextTurnout)
|
||||
for (Turnout *tt = Turnout::first(); tt != NULL; tt = tt->next())
|
||||
{
|
||||
gotOne = true;
|
||||
tt->print(stream);
|
||||
@@ -672,17 +627,60 @@ bool DCCEXParser::parseT(Print *stream, int16_t params, int16_t p[])
|
||||
StringFormatter::send(stream, F("<O>\n"));
|
||||
return true;
|
||||
|
||||
case 2: // <T id 0|1> turnout 0=CLOSE,1=THROW
|
||||
if (p[1]>1 || p[1]<0 ) return false;
|
||||
if (!Turnout::setClosed(p[0],p[1]==0)) return false;
|
||||
StringFormatter::send(stream, F("<H %d %d>\n"), p[0], p[1]);
|
||||
return true;
|
||||
case 2: // <T id 0|1|T|C>
|
||||
{
|
||||
bool state = false;
|
||||
switch (p[1]) {
|
||||
// Turnout messages use 1=throw, 0=close.
|
||||
case 0:
|
||||
case HASH_KEYWORD_C:
|
||||
state = true;
|
||||
break;
|
||||
case 1:
|
||||
case HASH_KEYWORD_T:
|
||||
state= false;
|
||||
break;
|
||||
default:
|
||||
return false; // Invalid parameter
|
||||
}
|
||||
if (!Turnout::setClosed(p[0], state)) return false;
|
||||
|
||||
default: // Anything else is handled by Turnout class.
|
||||
if (!Turnout::create(p[0], params-1, &p[1]))
|
||||
return false;
|
||||
StringFormatter::send(stream, F("<O>\n"));
|
||||
return true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
default: // Anything else is some kind of turnout create function.
|
||||
if (params == 6 && p[1] == HASH_KEYWORD_SERVO) { // <T id SERVO n n n n>
|
||||
if (!ServoTurnout::create(p[0], (VPIN)p[2], (uint16_t)p[3], (uint16_t)p[4], (uint8_t)p[5]))
|
||||
return false;
|
||||
} else
|
||||
if (params == 3 && p[1] == HASH_KEYWORD_VPIN) { // <T id VPIN n>
|
||||
if (!VpinTurnout::create(p[0], p[2])) return false;
|
||||
} else
|
||||
if (params >= 3 && p[1] == HASH_KEYWORD_DCC) {
|
||||
// <T id DCC addr subadd> 0<=addr<=511, 0<=subadd<=3 (like <a> command).<T>
|
||||
if (params==4 && p[2]>=0 && p[2]<512 && p[3]>=0 && p[3]<4) { // <T id DCC n m>
|
||||
if (!DCCTurnout::create(p[0], p[2], p[3])) return false;
|
||||
} else if (params==3 && p[2]>0 && p[2]<=512*4) { // <T id DCC nn>, 1<=nn<=2048
|
||||
// Linearaddress 1 maps onto decoder address 1/0 (not 0/0!).
|
||||
if (!DCCTurnout::create(p[0], (p[2]-1)/4+1, (p[2]-1)%4)) return false;
|
||||
} else
|
||||
return false;
|
||||
} else
|
||||
if (params==3) { // legacy <T id addr subadd> for DCC accessory
|
||||
if (p[1]>=0 && p[1]<512 && p[2]>=0 && p[2]<4) {
|
||||
if (!DCCTurnout::create(p[0], p[1], p[2])) return false;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
else
|
||||
if (params==4) { // legacy <T id n n n> for Servo
|
||||
if (!ServoTurnout::create(p[0], (VPIN)p[1], (uint16_t)p[2], (uint16_t)p[3], 1)) return false;
|
||||
} else
|
||||
return false;
|
||||
|
||||
StringFormatter::send(stream, F("<O>\n"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -733,17 +731,20 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
|
||||
StringFormatter::send(stream, F("Free memory=%d\n"), minimumFreeMemory());
|
||||
break;
|
||||
|
||||
case HASH_KEYWORD_ACK: // <D ACK ON/OFF> <D ACK [LIMIT|MIN|MAX] Value>
|
||||
case HASH_KEYWORD_ACK: // <D ACK ON/OFF> <D ACK [LIMIT|MIN|MAX|RETRY] Value>
|
||||
if (params >= 3) {
|
||||
if (p[1] == HASH_KEYWORD_LIMIT) {
|
||||
DCCWaveform::progTrack.setAckLimit(p[2]);
|
||||
StringFormatter::send(stream, F("Ack limit=%dmA\n"), p[2]);
|
||||
LCD(1, F("Ack Limit=%dmA"), p[2]); // <D ACK LIMIT 42>
|
||||
} else if (p[1] == HASH_KEYWORD_MIN) {
|
||||
DCCWaveform::progTrack.setMinAckPulseDuration(p[2]);
|
||||
StringFormatter::send(stream, F("Ack min=%dus\n"), p[2]);
|
||||
LCD(0, F("Ack Min=%dus"), p[2]); // <D ACK MIN 1500>
|
||||
} else if (p[1] == HASH_KEYWORD_MAX) {
|
||||
DCCWaveform::progTrack.setMaxAckPulseDuration(p[2]);
|
||||
StringFormatter::send(stream, F("Ack max=%dus\n"), p[2]);
|
||||
LCD(0, F("Ack Max=%dus"), p[2]); // <D ACK MAX 9000>
|
||||
} else if (p[1] == HASH_KEYWORD_RETRY) {
|
||||
if (p[2] >255) p[2]=3;
|
||||
LCD(0, F("Ack Retry=%d Sum=%d"), p[2], DCC::setAckRetry(p[2])); // <D ACK RETRY 2>
|
||||
}
|
||||
} else {
|
||||
StringFormatter::send(stream, F("Ack diag %S\n"), onOff ? F("on") : F("off"));
|
||||
@@ -755,21 +756,23 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
|
||||
Diag::CMD = onOff;
|
||||
return true;
|
||||
|
||||
#ifdef HAS_ENOUGH_MEMORY
|
||||
case HASH_KEYWORD_WIFI: // <D WIFI ON/OFF>
|
||||
Diag::WIFI = onOff;
|
||||
return true;
|
||||
|
||||
case HASH_KEYWORD_ETHERNET: // <D ETHERNET ON/OFF>
|
||||
case HASH_KEYWORD_ETHERNET: // <D ETHERNET ON/OFF>
|
||||
Diag::ETHERNET = onOff;
|
||||
return true;
|
||||
|
||||
case HASH_KEYWORD_WIT: // <D WIT ON/OFF>
|
||||
Diag::WITHROTTLE = onOff;
|
||||
return true;
|
||||
|
||||
|
||||
case HASH_KEYWORD_LCN: // <D LCN ON/OFF>
|
||||
Diag::LCN = onOff;
|
||||
return true;
|
||||
#endif
|
||||
|
||||
case HASH_KEYWORD_PROGBOOST:
|
||||
DCC::setProgTrackBoost(true);
|
||||
@@ -781,11 +784,13 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
|
||||
delay(50); // wait for the prescaller time to expire
|
||||
break; // and <X> if we didnt restart
|
||||
}
|
||||
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
case HASH_KEYWORD_EEPROM: // <D EEPROM NumEntries>
|
||||
if (params >= 2)
|
||||
EEStore::dump(p[1]);
|
||||
return true;
|
||||
#endif
|
||||
|
||||
case HASH_KEYWORD_SPEED28:
|
||||
DCC::setGlobalSpeedsteps(28);
|
||||
@@ -797,10 +802,22 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
|
||||
StringFormatter::send(stream, F("128 Speedsteps"));
|
||||
return true;
|
||||
|
||||
case HASH_KEYWORD_SERVO:
|
||||
case HASH_KEYWORD_SERVO: // <D SERVO vpin position [profile]>
|
||||
case HASH_KEYWORD_ANOUT: // <D ANOUT vpin position [profile]>
|
||||
IODevice::writeAnalogue(p[1], p[2], params>3 ? p[3] : 0);
|
||||
break;
|
||||
|
||||
case HASH_KEYWORD_ANIN: // <D ANIN vpin> Display analogue input value
|
||||
DIAG(F("VPIN=%d value=%d"), p[1], IODevice::readAnalogue(p[1]));
|
||||
break;
|
||||
|
||||
#if !defined(IO_MINIMAL_HAL)
|
||||
case HASH_KEYWORD_HAL:
|
||||
if (p[1] == HASH_KEYWORD_SHOW)
|
||||
IODevice::DumpAll();
|
||||
break;
|
||||
#endif
|
||||
|
||||
default: // invalid/unknown
|
||||
break;
|
||||
}
|
||||
@@ -863,10 +880,21 @@ void DCCEXParser::callback_R(int16_t result)
|
||||
commitAsyncReplyStream();
|
||||
}
|
||||
|
||||
void DCCEXParser::callback_Rloco(int16_t result)
|
||||
{
|
||||
StringFormatter::send(getAsyncReplyStream(), F("<r %d>\n"), result);
|
||||
commitAsyncReplyStream();
|
||||
void DCCEXParser::callback_Rloco(int16_t result) {
|
||||
const FSH * detail;
|
||||
if (result<=0) {
|
||||
detail=F("<r ERROR %d>\n");
|
||||
} else {
|
||||
bool longAddr=result & LONG_ADDR_MARKER; //long addr
|
||||
if (longAddr)
|
||||
result = result^LONG_ADDR_MARKER;
|
||||
if (longAddr && result <= HIGHEST_SHORT_ADDR)
|
||||
detail=F("<r LONG %d UNSUPPORTED>\n");
|
||||
else
|
||||
detail=F("<r %d>\n");
|
||||
}
|
||||
StringFormatter::send(getAsyncReplyStream(), detail, result);
|
||||
commitAsyncReplyStream();
|
||||
}
|
||||
|
||||
void DCCEXParser::callback_Wloco(int16_t result)
|
||||
|
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
@@ -23,15 +26,13 @@
|
||||
#include "RingStream.h"
|
||||
|
||||
typedef void (*FILTER_CALLBACK)(Print * stream, byte & opcode, byte & paramCount, int16_t p[]);
|
||||
typedef void (*AT_COMMAND_CALLBACK)(const byte * command);
|
||||
typedef void (*AT_COMMAND_CALLBACK)(HardwareSerial * stream,const byte * command);
|
||||
|
||||
struct DCCEXParser
|
||||
{
|
||||
DCCEXParser();
|
||||
void loop(Stream & stream);
|
||||
void parse(Print * stream, byte * command, RingStream * ringStream);
|
||||
void parse(const FSH * cmd);
|
||||
void flush();
|
||||
|
||||
static void parse(Print * stream, byte * command, RingStream * ringStream);
|
||||
static void parse(const FSH * cmd);
|
||||
static void setFilter(FILTER_CALLBACK filter);
|
||||
static void setRMFTFilter(FILTER_CALLBACK filter);
|
||||
static void setAtCommandCallback(AT_COMMAND_CALLBACK filter);
|
||||
@@ -40,17 +41,13 @@ struct DCCEXParser
|
||||
private:
|
||||
|
||||
static const int16_t MAX_BUFFER=50; // longest command sent in
|
||||
byte bufferLength=0;
|
||||
bool inCommandPayload=false;
|
||||
byte buffer[MAX_BUFFER+2];
|
||||
int16_t splitValues( int16_t result[MAX_COMMAND_PARAMS], const byte * command);
|
||||
int16_t splitHexValues( int16_t result[MAX_COMMAND_PARAMS], const byte * command);
|
||||
static int16_t splitValues( int16_t result[MAX_COMMAND_PARAMS], const byte * command, bool usehex);
|
||||
|
||||
bool parseT(Print * stream, int16_t params, int16_t p[]);
|
||||
bool parseZ(Print * stream, int16_t params, int16_t p[]);
|
||||
bool parseS(Print * stream, int16_t params, int16_t p[]);
|
||||
bool parsef(Print * stream, int16_t params, int16_t p[]);
|
||||
bool parseD(Print * stream, int16_t params, int16_t p[]);
|
||||
static bool parseT(Print * stream, int16_t params, int16_t p[]);
|
||||
static bool parseZ(Print * stream, int16_t params, int16_t p[]);
|
||||
static bool parseS(Print * stream, int16_t params, int16_t p[]);
|
||||
static bool parsef(Print * stream, int16_t params, int16_t p[]);
|
||||
static bool parseD(Print * stream, int16_t params, int16_t p[]);
|
||||
|
||||
static Print * getAsyncReplyStream();
|
||||
static void commitAsyncReplyStream();
|
||||
@@ -61,7 +58,7 @@ struct DCCEXParser
|
||||
static RingStream * stashRingStream;
|
||||
|
||||
static int16_t stashP[MAX_COMMAND_PARAMS];
|
||||
bool stashCallback(Print * stream, int16_t p[MAX_COMMAND_PARAMS], RingStream * ringStream);
|
||||
static bool stashCallback(Print * stream, int16_t p[MAX_COMMAND_PARAMS], RingStream * ringStream);
|
||||
static void callback_W(int16_t result);
|
||||
static void callback_B(int16_t result);
|
||||
static void callback_R(int16_t result);
|
||||
|
@@ -1,5 +1,10 @@
|
||||
/*
|
||||
* © 2021, Chris Harlow & David Cutting. All rights reserved.
|
||||
* © 2021 Mike S
|
||||
* © 2021 Harald Barth
|
||||
* © 2021 Fred Decker
|
||||
* © 2021 Chris Harlow
|
||||
* © 2021 David Cutting
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
|
@@ -1,6 +1,8 @@
|
||||
/*
|
||||
* (c) 2021 Mike S. All rights reserved.
|
||||
* (c) 2021 Fred Decker. All rights reserved.
|
||||
* © 2021 Mike S
|
||||
* © 2021 Harald Barth
|
||||
* © 2021 Fred Decker
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
|
@@ -1,8 +1,12 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +21,7 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma GCC optimize ("-O3")
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "DCCWaveform.h"
|
||||
@@ -46,10 +50,8 @@ void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) {
|
||||
&& (mainDriver->getFaultPin() != UNUSED_PIN));
|
||||
// Only use PWM if both pins are PWM capable. Otherwise JOIN does not work
|
||||
MotorDriver::usePWM= mainDriver->isPWMCapable() && progDriver->isPWMCapable();
|
||||
if (MotorDriver::usePWM)
|
||||
DIAG(F("Signal pin config: high accuracy waveform"));
|
||||
else
|
||||
DIAG(F("Signal pin config: normal accuracy waveform"));
|
||||
DIAG(F("Signal pin config: %S accuracy waveform"),
|
||||
MotorDriver::usePWM ? F("high") : F("normal") );
|
||||
DCCTimer::begin(DCCWaveform::interruptHandler);
|
||||
}
|
||||
|
||||
@@ -58,6 +60,8 @@ void DCCWaveform::loop(bool ackManagerActive) {
|
||||
progTrack.checkPowerOverload(ackManagerActive);
|
||||
}
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("-O3")
|
||||
void DCCWaveform::interruptHandler() {
|
||||
// call the timer edge sensitive actions for progtrack and maintrack
|
||||
// member functions would be cleaner but have more overhead
|
||||
@@ -79,7 +83,7 @@ void DCCWaveform::interruptHandler() {
|
||||
else if (progTrack.ackPending) progTrack.checkAck();
|
||||
|
||||
}
|
||||
|
||||
#pragma GCC push_options
|
||||
|
||||
// An instance of this class handles the DCC transmissions for one track. (main or prog)
|
||||
// Interrupts are marshalled via the statics.
|
||||
@@ -114,6 +118,7 @@ void DCCWaveform::setPowerMode(POWERMODE mode) {
|
||||
powerMode = mode;
|
||||
bool ison = (mode == POWERMODE::ON);
|
||||
motorDriver->setPower( ison);
|
||||
sentResetsSincePacket=0;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,6 +129,8 @@ void DCCWaveform::checkPowerOverload(bool ackManagerActive) {
|
||||
if (!isMainTrack && !ackManagerActive && !progTrackSyncMain && !progTrackBoosted)
|
||||
tripValue=progTripValue;
|
||||
|
||||
// Trackname for diag messages later
|
||||
const FSH*trackname = isMainTrack ? F("MAIN") : F("PROG");
|
||||
switch (powerMode) {
|
||||
case POWERMODE::OFF:
|
||||
sampleDelay = POWER_SAMPLE_OFF_WAIT;
|
||||
@@ -141,9 +148,9 @@ void DCCWaveform::checkPowerOverload(bool ackManagerActive) {
|
||||
}
|
||||
// Write this after the fact as we want to turn on as fast as possible
|
||||
// because we don't know which output actually triggered the fault pin
|
||||
DIAG(F("*** COMMON FAULT PIN ACTIVE - TOGGLED POWER on %S ***"), isMainTrack ? F("MAIN") : F("PROG"));
|
||||
DIAG(F("COMMON FAULT PIN ACTIVE - TOGGLED POWER on %S"), trackname);
|
||||
} else {
|
||||
DIAG(F("*** %S FAULT PIN ACTIVE - OVERLOAD ***"), isMainTrack ? F("MAIN") : F("PROG"));
|
||||
DIAG(F("%S FAULT PIN ACTIVE - OVERLOAD"), trackname);
|
||||
if (lastCurrent < tripValue) {
|
||||
lastCurrent = tripValue; // exaggerate
|
||||
}
|
||||
@@ -161,7 +168,7 @@ void DCCWaveform::checkPowerOverload(bool ackManagerActive) {
|
||||
unsigned int maxmA=motorDriver->raw2mA(tripValue);
|
||||
power_good_counter=0;
|
||||
sampleDelay = power_sample_overload_wait;
|
||||
DIAG(F("*** %S TRACK POWER OVERLOAD current=%d max=%d offtime=%d ***"), isMainTrack ? F("MAIN") : F("PROG"), mA, maxmA, sampleDelay);
|
||||
DIAG(F("%S TRACK POWER OVERLOAD current=%d max=%d offtime=%d"), trackname, mA, maxmA, sampleDelay);
|
||||
if (power_sample_overload_wait >= 10000)
|
||||
power_sample_overload_wait = 10000;
|
||||
else
|
||||
@@ -173,7 +180,7 @@ void DCCWaveform::checkPowerOverload(bool ackManagerActive) {
|
||||
setPowerMode(POWERMODE::ON);
|
||||
sampleDelay = POWER_SAMPLE_ON_WAIT;
|
||||
// Debug code....
|
||||
DIAG(F("*** %S TRACK POWER RESET delay=%d ***"), isMainTrack ? F("MAIN") : F("PROG"), sampleDelay);
|
||||
DIAG(F("%S TRACK POWER RESET delay=%d"), trackname, sampleDelay);
|
||||
break;
|
||||
default:
|
||||
sampleDelay = 999; // cant get here..meaningless statement to avoid compiler warning.
|
||||
@@ -197,6 +204,8 @@ const bool DCCWaveform::signalTransform[]={
|
||||
/* WAVE_LOW_0 -> */ LOW,
|
||||
/* WAVE_PENDING (should not happen) -> */ LOW};
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("-O3")
|
||||
void DCCWaveform::interrupt2() {
|
||||
// calculate the next bit to be sent:
|
||||
// set state WAVE_MID_1 for a 1=bit
|
||||
@@ -252,7 +261,7 @@ void DCCWaveform::interrupt2() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma GCC pop_options
|
||||
|
||||
|
||||
// Wait until there is no packet pending, then make this pending
|
||||
@@ -306,6 +315,8 @@ byte DCCWaveform::getAck() {
|
||||
return(0); // pending set off but not detected means no ACK.
|
||||
}
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize ("-O3")
|
||||
void DCCWaveform::checkAck() {
|
||||
// This function operates in interrupt() time so must be fast and can't DIAG
|
||||
if (sentResetsSincePacket > 6) { //ACK timeout
|
||||
@@ -355,3 +366,4 @@ void DCCWaveform::checkAck() {
|
||||
}
|
||||
ackPulseStart=0; // We have detected a too-short or too-long pulse so ignore and wait for next leading edge
|
||||
}
|
||||
#pragma GCC pop_options
|
||||
|
@@ -1,8 +1,12 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth.
|
||||
* © 2021 M Steve Todd
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
6
DIAG.h
6
DIAG.h
@@ -1,7 +1,9 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Fred Decker
|
||||
* © 2020 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
@@ -1,5 +1,7 @@
|
||||
/*
|
||||
* © 2021, Chris Harlow, Neil McKechnie. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -19,4 +21,4 @@
|
||||
|
||||
#include "DisplayInterface.h"
|
||||
|
||||
DisplayInterface *DisplayInterface::lcdDisplay = 0;
|
||||
DisplayInterface *DisplayInterface::lcdDisplay = 0;
|
||||
|
@@ -1,5 +1,7 @@
|
||||
/*
|
||||
* © 2021, Chris Harlow, Neil McKechnie. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -32,4 +34,4 @@ public:
|
||||
static DisplayInterface *lcdDisplay;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
113
EEStore.cpp
113
EEStore.cpp
@@ -1,9 +1,12 @@
|
||||
/*
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2013-2016 Gregg E. Berman
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -18,90 +21,92 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "defines.h"
|
||||
#ifndef DISABLE_EEPROM
|
||||
#include "EEStore.h"
|
||||
#include "Turnouts.h"
|
||||
#include "Sensors.h"
|
||||
#include "Outputs.h"
|
||||
|
||||
#include "DIAG.h"
|
||||
#include "Outputs.h"
|
||||
#include "Sensors.h"
|
||||
#include "Turnouts.h"
|
||||
|
||||
#if defined(ARDUINO_ARCH_SAMD)
|
||||
ExternalEEPROM EEPROM;
|
||||
#endif
|
||||
|
||||
void EEStore::init(){
|
||||
void EEStore::init() {
|
||||
#if defined(ARDUINO_ARCH_SAMD)
|
||||
EEPROM.begin(0x50); // Address for Microchip 24-series EEPROM with all three A pins grounded (0b1010000 = 0x50)
|
||||
EEPROM.begin(0x50); // Address for Microchip 24-series EEPROM with all three
|
||||
// A pins grounded (0b1010000 = 0x50)
|
||||
#endif
|
||||
|
||||
eeStore=(EEStore *)calloc(1,sizeof(EEStore));
|
||||
|
||||
EEPROM.get(0,eeStore->data); // get eeStore data
|
||||
eeStore = (EEStore *)calloc(1, sizeof(EEStore));
|
||||
|
||||
if(strncmp(eeStore->data.id,EESTORE_ID,sizeof(EESTORE_ID))!=0){ // check to see that eeStore contains valid DCC++ ID
|
||||
sprintf(eeStore->data.id,EESTORE_ID); // if not, create blank eeStore structure (no turnouts, no sensors) and save it back to EEPROM
|
||||
eeStore->data.nTurnouts=0;
|
||||
eeStore->data.nSensors=0;
|
||||
eeStore->data.nOutputs=0;
|
||||
EEPROM.put(0,eeStore->data);
|
||||
}
|
||||
EEPROM.get(0, eeStore->data); // get eeStore data
|
||||
|
||||
reset(); // set memory pointer to first free EEPROM space
|
||||
Turnout::load(); // load turnout definitions
|
||||
Sensor::load(); // load sensor definitions
|
||||
Output::load(); // load output definitions
|
||||
// check to see that eeStore contains valid DCC++ ID
|
||||
if (strncmp(eeStore->data.id, EESTORE_ID, sizeof(EESTORE_ID)) != 0) {
|
||||
// if not, create blank eeStore structure (no
|
||||
// turnouts, no sensors) and save it back to EEPROM
|
||||
strncpy(eeStore->data.id, EESTORE_ID, sizeof(EESTORE_ID));
|
||||
eeStore->data.nTurnouts = 0;
|
||||
eeStore->data.nSensors = 0;
|
||||
eeStore->data.nOutputs = 0;
|
||||
EEPROM.put(0, eeStore->data);
|
||||
}
|
||||
|
||||
reset(); // set memory pointer to first free EEPROM space
|
||||
Turnout::load(); // load turnout definitions
|
||||
Sensor::load(); // load sensor definitions
|
||||
Output::load(); // load output definitions
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void EEStore::clear(){
|
||||
|
||||
sprintf(eeStore->data.id,EESTORE_ID); // create blank eeStore structure (no turnouts, no sensors) and save it back to EEPROM
|
||||
eeStore->data.nTurnouts=0;
|
||||
eeStore->data.nSensors=0;
|
||||
eeStore->data.nOutputs=0;
|
||||
EEPROM.put(0,eeStore->data);
|
||||
|
||||
void EEStore::clear() {
|
||||
sprintf(eeStore->data.id,
|
||||
EESTORE_ID); // create blank eeStore structure (no turnouts, no
|
||||
// sensors) and save it back to EEPROM
|
||||
eeStore->data.nTurnouts = 0;
|
||||
eeStore->data.nSensors = 0;
|
||||
eeStore->data.nOutputs = 0;
|
||||
EEPROM.put(0, eeStore->data);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void EEStore::store(){
|
||||
reset();
|
||||
Turnout::store();
|
||||
Sensor::store();
|
||||
Output::store();
|
||||
EEPROM.put(0,eeStore->data);
|
||||
DIAG(F("EEPROM used: %d bytes"), EEStore::pointer());
|
||||
void EEStore::store() {
|
||||
reset();
|
||||
Turnout::store();
|
||||
Sensor::store();
|
||||
Output::store();
|
||||
EEPROM.put(0, eeStore->data);
|
||||
DIAG(F("EEPROM used: %d/%d bytes"), EEStore::pointer(), EEPROM.length());
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void EEStore::advance(int n){
|
||||
eeAddress+=n;
|
||||
}
|
||||
void EEStore::advance(int n) { eeAddress += n; }
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void EEStore::reset(){
|
||||
eeAddress=sizeof(EEStore);
|
||||
}
|
||||
void EEStore::reset() { eeAddress = sizeof(EEStore); }
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int EEStore::pointer(){
|
||||
return(eeAddress);
|
||||
}
|
||||
int EEStore::pointer() { return (eeAddress); }
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void EEStore::dump(int num) {
|
||||
byte b;
|
||||
DIAG(F("Addr 0x char"));
|
||||
for (int n=0 ; n<num; n++) {
|
||||
EEPROM.get(n, b);
|
||||
DIAG(F("%d %x %c"),n,b,isprint(b) ? b : ' ');
|
||||
}
|
||||
byte b;
|
||||
DIAG(F("Addr 0x char"));
|
||||
for (int n = 0; n < num; n++) {
|
||||
EEPROM.get(n, b);
|
||||
DIAG(F("%d %x %c"), n, b, isprint(b) ? b : ' ');
|
||||
}
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
EEStore *EEStore::eeStore=NULL;
|
||||
int EEStore::eeAddress=0;
|
||||
EEStore *EEStore::eeStore = NULL;
|
||||
int EEStore::eeAddress = 0;
|
||||
#endif
|
||||
|
11
EEStore.h
11
EEStore.h
@@ -1,6 +1,9 @@
|
||||
/*
|
||||
* (c) 2020 Chris Harlow. All rights reserved.
|
||||
* (c) 2020 Harald Barth. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -17,6 +20,7 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef DISABLE_EEPROM
|
||||
#ifndef EEStore_h
|
||||
#define EEStore_h
|
||||
|
||||
@@ -29,7 +33,7 @@ extern ExternalEEPROM EEPROM;
|
||||
#include <EEPROM.h>
|
||||
#endif
|
||||
|
||||
#define EESTORE_ID "DCC++0"
|
||||
#define EESTORE_ID "DCC++1"
|
||||
|
||||
struct EEStoreData{
|
||||
char id[sizeof(EESTORE_ID)];
|
||||
@@ -52,3 +56,4 @@ struct EEStore{
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // DISABLE_EEPROM
|
||||
|
@@ -1,5 +1,9 @@
|
||||
/*
|
||||
* © 2020,Gregor Baues, Chris Harlow. All rights reserved.
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2020 Gregor Baues
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of DCC-EX/CommandStation-EX
|
||||
*
|
||||
@@ -17,12 +21,6 @@
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#if __has_include ( "config.h")
|
||||
#include "config.h"
|
||||
#else
|
||||
#warning config.h not found. Using defaults from config.example.h
|
||||
#include "config.example.h"
|
||||
#endif
|
||||
#include "defines.h"
|
||||
#if ETHERNET_ON == true
|
||||
#include "EthernetInterface.h"
|
||||
@@ -176,6 +174,7 @@ void EthernetInterface::loop()
|
||||
for (int socket = 0; socket<MAX_SOCK_NUM; socket++) {
|
||||
if (clients[socket] && !clients[socket].connected()) {
|
||||
clients[socket].stop();
|
||||
CommandDistributor::forget(socket);
|
||||
if (Diag::ETHERNET) DIAG(F("Ethernet: disconnect %d "), socket);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,10 @@
|
||||
/*
|
||||
* © 2020,Gregor Baues, Chris Harlow. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2020 Gregor Baues
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of DCC-EX/CommandStation-EX
|
||||
*
|
||||
@@ -22,12 +27,8 @@
|
||||
|
||||
#ifndef EthernetInterface_h
|
||||
#define EthernetInterface_h
|
||||
#if __has_include ( "config.h")
|
||||
#include "config.h"
|
||||
#else
|
||||
#warning config.h not found. Using defaults from config.example.h
|
||||
#include "config.example.h"
|
||||
#endif
|
||||
|
||||
#include "defines.h"
|
||||
#include "DCCEXParser.h"
|
||||
#include <Arduino.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
5
FSH.h
5
FSH.h
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* (c) 2021 Fred Decker. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Harald Barth
|
||||
* © 2021 Fred Decker
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
|
@@ -1 +1 @@
|
||||
#define GITHUB_SHA "50fcbc0"
|
||||
#define GITHUB_SHA "61390cb"
|
||||
|
@@ -147,6 +147,25 @@ uint8_t I2CManagerClass::finishRB(I2CRB *rb, uint8_t status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* Get a message corresponding to the error status
|
||||
***************************************************************************/
|
||||
const FSH *I2CManagerClass::getErrorMessage(uint8_t status) {
|
||||
switch (status) {
|
||||
case I2C_STATUS_OK: return F("OK");
|
||||
case I2C_STATUS_TRUNCATED: return F("Transmission truncated");
|
||||
case I2C_STATUS_NEGATIVE_ACKNOWLEDGE: return F("No response from device (address NAK)");
|
||||
case I2C_STATUS_TRANSMIT_ERROR: return F("Transmit error (data NAK)");
|
||||
case I2C_STATUS_OTHER_TWI_ERROR: return F("Other Wire/TWI error");
|
||||
case I2C_STATUS_TIMEOUT: return F("Timeout");
|
||||
case I2C_STATUS_ARBITRATION_LOST: return F("Arbitration lost");
|
||||
case I2C_STATUS_BUS_ERROR: return F("I2C bus error");
|
||||
case I2C_STATUS_UNEXPECTED_ERROR: return F("Unexpected error");
|
||||
case I2C_STATUS_PENDING: return F("Request pending");
|
||||
default: return F("Error code not recognised");
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* Declare singleton class instance.
|
||||
***************************************************************************/
|
||||
@@ -158,12 +177,25 @@ I2CManagerClass I2CManager = I2CManagerClass();
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/***************************************************************************
|
||||
* Block waiting for request block to complete, and return completion status
|
||||
* Block waiting for request block to complete, and return completion status.
|
||||
* Since such a loop could potentially last for ever if the RB status doesn't
|
||||
* change, we set a high limit (1sec, 1000ms) on the wait time and, if it
|
||||
* hasn't changed by that time we assume it's not going to, and just return
|
||||
* a timeout status. This means that CS will not lock up.
|
||||
***************************************************************************/
|
||||
uint8_t I2CRB::wait() {
|
||||
do
|
||||
unsigned long waitStart = millis();
|
||||
do {
|
||||
I2CManager.loop();
|
||||
while (status==I2C_STATUS_PENDING);
|
||||
// Rather than looping indefinitely, let's set a very high timeout (1s).
|
||||
if ((millis() - waitStart) > 1000UL) {
|
||||
DIAG(F("I2C TIMEOUT I2C:x%x I2CRB:x%x"), i2cAddress, this);
|
||||
status = I2C_STATUS_TIMEOUT;
|
||||
// Note that, although the timeout is posted, the request may yet complete.
|
||||
// TODO: Ideally we would like to cancel the request.
|
||||
return status;
|
||||
}
|
||||
} while (status==I2C_STATUS_PENDING);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
32
I2CManager.h
32
I2CManager.h
@@ -107,23 +107,31 @@
|
||||
* the loop() function is called, and may be adequate under some circumstances.
|
||||
* The advantage of NOT using interrupts is that the impact of I2C upon the DCC waveform (when accurate timing mode isn't in use)
|
||||
* becomes almost zero.
|
||||
* This mechanism is under evaluation and should not be relied upon as yet.
|
||||
*
|
||||
*/
|
||||
|
||||
// Uncomment following line to enable Wire library instead of native I2C drivers
|
||||
//#define I2C_USE_WIRE
|
||||
|
||||
// Uncomment following line to disable the use of interrupts by the native I2C drivers.
|
||||
//#define I2C_NO_INTERRUPTS
|
||||
|
||||
// Default to use interrupts within the native I2C drivers.
|
||||
#ifndef I2C_NO_INTERRUPTS
|
||||
#define I2C_USE_INTERRUPTS
|
||||
#endif
|
||||
|
||||
// Status codes for I2CRB structures.
|
||||
enum : uint8_t {
|
||||
// Codes used by Wire and by native drivers
|
||||
I2C_STATUS_OK=0,
|
||||
I2C_STATUS_TRUNCATED=1,
|
||||
I2C_STATUS_DEVICE_NOT_PRESENT=2,
|
||||
I2C_STATUS_NEGATIVE_ACKNOWLEDGE=2,
|
||||
I2C_STATUS_TRANSMIT_ERROR=3,
|
||||
I2C_STATUS_NEGATIVE_ACKNOWLEDGE=4,
|
||||
I2C_STATUS_TIMEOUT=5,
|
||||
// Code used by Wire only
|
||||
I2C_STATUS_OTHER_TWI_ERROR=4, // catch-all error
|
||||
// Codes used by native drivers only
|
||||
I2C_STATUS_ARBITRATION_LOST=6,
|
||||
I2C_STATUS_BUS_ERROR=7,
|
||||
I2C_STATUS_UNEXPECTED_ERROR=8,
|
||||
@@ -151,14 +159,16 @@ typedef enum : uint8_t
|
||||
#define I2C_FREQ 400000L
|
||||
#endif
|
||||
|
||||
// Struct defining a request context for an I2C operation.
|
||||
struct I2CRB {
|
||||
// Class defining a request context for an I2C operation.
|
||||
class I2CRB {
|
||||
public:
|
||||
volatile uint8_t status; // Completion status, or pending flag (updated from IRC)
|
||||
volatile uint8_t nBytes; // Number of bytes read (updated from IRC)
|
||||
|
||||
inline I2CRB() { status = I2C_STATUS_OK; };
|
||||
uint8_t wait();
|
||||
bool isBusy();
|
||||
inline void init() { status = I2C_STATUS_OK; };
|
||||
|
||||
void setReadParams(uint8_t i2cAddress, uint8_t *readBuffer, uint8_t readLen);
|
||||
void setRequestParams(uint8_t i2cAddress, uint8_t *readBuffer, uint8_t readLen, const uint8_t *writeBuffer, uint8_t writeLen);
|
||||
void setWriteParams(uint8_t i2cAddress, const uint8_t *writeBuffer, uint8_t writeLen);
|
||||
@@ -213,6 +223,10 @@ public:
|
||||
// Loop method
|
||||
void loop();
|
||||
|
||||
// Expand error codes into text. Note that they are in flash so
|
||||
// need to be printed using FSH.
|
||||
static const FSH *getErrorMessage(uint8_t status);
|
||||
|
||||
private:
|
||||
bool _beginCompleted = false;
|
||||
bool _clockSpeedFixed = false;
|
||||
@@ -235,7 +249,7 @@ private:
|
||||
// Mark volatile as they are updated by IRC and read/written elsewhere.
|
||||
static I2CRB * volatile queueHead;
|
||||
static I2CRB * volatile queueTail;
|
||||
static volatile uint8_t status;
|
||||
static volatile uint8_t state;
|
||||
|
||||
static I2CRB * volatile currentRequest;
|
||||
static volatile uint8_t txCount;
|
||||
@@ -258,7 +272,9 @@ private:
|
||||
static void I2C_close();
|
||||
|
||||
public:
|
||||
void setTimeout(unsigned long value) { timeout = value;};
|
||||
// setTimeout sets the timout value for I2C transactions.
|
||||
// TODO: Get I2C timeout working before uncommenting the code below.
|
||||
void setTimeout(unsigned long value) { (void)value; /* timeout = value; */ };
|
||||
|
||||
// handleInterrupt needs to be public to be called from the ISR function!
|
||||
static void handleInterrupt();
|
||||
|
@@ -62,7 +62,18 @@
|
||||
* Set I2C clock speed register.
|
||||
***************************************************************************/
|
||||
void I2CManagerClass::I2C_setClock(unsigned long i2cClockSpeed) {
|
||||
TWBR = ((F_CPU / i2cClockSpeed) - 16) / 2;
|
||||
unsigned long temp = ((F_CPU / i2cClockSpeed) - 16) / 2;
|
||||
for (uint8_t preScaler = 0; preScaler<=3; preScaler++) {
|
||||
if (temp <= 255) {
|
||||
TWBR = temp;
|
||||
TWSR = (TWSR & 0xfc) | preScaler;
|
||||
return;
|
||||
} else
|
||||
temp /= 4;
|
||||
}
|
||||
// Set slowest speed ~= 500 bits/sec
|
||||
TWBR = 255;
|
||||
TWSR |= 0x03;
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
@@ -136,7 +147,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
} else { // Nothing left to send or receive
|
||||
TWDR = 0xff; // Default condition = SDA released
|
||||
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
|
||||
status = I2C_STATUS_OK;
|
||||
state = I2C_STATUS_OK;
|
||||
}
|
||||
break;
|
||||
case TWI_MRX_DATA_ACK: // Data byte has been received and ACK transmitted
|
||||
@@ -159,7 +170,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
bytesToReceive--;
|
||||
}
|
||||
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
|
||||
status = I2C_STATUS_OK;
|
||||
state = I2C_STATUS_OK;
|
||||
break;
|
||||
case TWI_START: // START has been transmitted
|
||||
case TWI_REP_START: // Repeated START has been transmitted
|
||||
@@ -175,7 +186,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
case TWI_MTX_DATA_NACK: // Data byte has been transmitted and NACK received
|
||||
TWDR = 0xff; // Default condition = SDA released
|
||||
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
|
||||
status = I2C_STATUS_NEGATIVE_ACKNOWLEDGE;
|
||||
state = I2C_STATUS_NEGATIVE_ACKNOWLEDGE;
|
||||
break;
|
||||
case TWI_ARB_LOST: // Arbitration lost
|
||||
// Restart transaction from start.
|
||||
@@ -185,7 +196,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
default:
|
||||
TWDR = 0xff; // Default condition = SDA released
|
||||
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
|
||||
status = I2C_STATUS_TRANSMIT_ERROR;
|
||||
state = I2C_STATUS_TRANSMIT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -105,14 +105,14 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
I2C_sendStart(); // Reinitiate request
|
||||
} else if (currentStatus & TWI_BUSERR_bm) {
|
||||
// Bus error
|
||||
status = I2C_STATUS_BUS_ERROR;
|
||||
state = I2C_STATUS_BUS_ERROR;
|
||||
TWI0.MSTATUS = currentStatus; // clear all flags
|
||||
} else if (currentStatus & TWI_WIF_bm) {
|
||||
// Master write completed
|
||||
if (currentStatus & TWI_RXACK_bm) {
|
||||
// Nacked, send stop.
|
||||
TWI0.MCTRLB = TWI_MCMD_STOP_gc;
|
||||
status = I2C_STATUS_NEGATIVE_ACKNOWLEDGE;
|
||||
state = I2C_STATUS_NEGATIVE_ACKNOWLEDGE;
|
||||
} else if (bytesToSend) {
|
||||
// Acked, so send next byte
|
||||
if (currentRequest->operation == OPERATION_SEND_P)
|
||||
@@ -126,7 +126,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
} else {
|
||||
// No more data to send/receive. Initiate a STOP condition.
|
||||
TWI0.MCTRLB = TWI_MCMD_STOP_gc;
|
||||
status = I2C_STATUS_OK; // Done
|
||||
state = I2C_STATUS_OK; // Done
|
||||
}
|
||||
} else if (currentStatus & TWI_RIF_bm) {
|
||||
// Master read completed without errors
|
||||
@@ -136,7 +136,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
} else {
|
||||
// Buffer full, issue nack/stop
|
||||
TWI0.MCTRLB = TWI_ACKACT_bm | TWI_MCMD_STOP_gc;
|
||||
status = I2C_STATUS_OK;
|
||||
state = I2C_STATUS_OK;
|
||||
}
|
||||
if (bytesToReceive) {
|
||||
// More bytes to receive, issue ack and start another read
|
||||
@@ -144,7 +144,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
|
||||
} else {
|
||||
// Transaction finished, issue NACK and STOP.
|
||||
TWI0.MCTRLB = TWI_ACKACT_bm | TWI_MCMD_STOP_gc;
|
||||
status = I2C_STATUS_OK;
|
||||
state = I2C_STATUS_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -41,7 +41,7 @@
|
||||
void I2CManagerClass::_initialise()
|
||||
{
|
||||
queueHead = queueTail = NULL;
|
||||
status = I2C_STATE_FREE;
|
||||
state = I2C_STATE_FREE;
|
||||
I2C_init();
|
||||
}
|
||||
|
||||
@@ -59,10 +59,9 @@ void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) {
|
||||
***************************************************************************/
|
||||
void I2CManagerClass::startTransaction() {
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
I2CRB *t = queueHead;
|
||||
if ((status == I2C_STATE_FREE) && (t != NULL)) {
|
||||
status = I2C_STATE_ACTIVE;
|
||||
currentRequest = t;
|
||||
if ((state == I2C_STATE_FREE) && (queueHead != NULL)) {
|
||||
state = I2C_STATE_ACTIVE;
|
||||
currentRequest = queueHead;
|
||||
rxCount = txCount = 0;
|
||||
// Copy key fields to static data for speed.
|
||||
operation = currentRequest->operation;
|
||||
@@ -85,9 +84,9 @@ void I2CManagerClass::queueRequest(I2CRB *req) {
|
||||
queueHead = queueTail = req; // Only item on queue
|
||||
else
|
||||
queueTail = queueTail->nextRequest = req; // Add to end
|
||||
startTransaction();
|
||||
}
|
||||
|
||||
startTransaction();
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
@@ -130,12 +129,16 @@ uint8_t I2CManagerClass::read(uint8_t i2cAddress, uint8_t *readBuffer, uint8_t r
|
||||
/***************************************************************************
|
||||
* checkForTimeout() function, called from isBusy() and wait() to cancel
|
||||
* requests that are taking too long to complete.
|
||||
* This function doesn't fully work as intended so is not currently called.
|
||||
* Instead we check for an I2C hang-up and report an error from
|
||||
* I2CRB::wait(), but we aren't able to recover from the hang-up. Such faults
|
||||
* may be caused by an I2C wire short for example.
|
||||
***************************************************************************/
|
||||
void I2CManagerClass::checkForTimeout() {
|
||||
unsigned long currentMicros = micros();
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
I2CRB *t = queueHead;
|
||||
if (t && timeout > 0) {
|
||||
if (state==I2C_STATE_ACTIVE && t!=0 && t==currentRequest && timeout > 0) {
|
||||
// Check for timeout
|
||||
if (currentMicros - startTime > timeout) {
|
||||
// Excessive time. Dequeue request
|
||||
@@ -148,9 +151,9 @@ void I2CManagerClass::checkForTimeout() {
|
||||
// Try close and init, not entirely satisfactory but sort of works...
|
||||
I2C_close(); // Shutdown and restart twi interface
|
||||
I2C_init();
|
||||
status = I2C_STATE_FREE;
|
||||
state = I2C_STATE_FREE;
|
||||
|
||||
// Initiate next queued request
|
||||
// Initiate next queued request if any.
|
||||
startTransaction();
|
||||
}
|
||||
}
|
||||
@@ -164,9 +167,10 @@ void I2CManagerClass::loop() {
|
||||
#if !defined(I2C_USE_INTERRUPTS)
|
||||
handleInterrupt();
|
||||
#endif
|
||||
// If free, initiate next transaction
|
||||
startTransaction();
|
||||
checkForTimeout();
|
||||
// Timeout is now reported in I2CRB::wait(), not here.
|
||||
// I've left the code, commented out, as a reminder to look at this again
|
||||
// in the future.
|
||||
//checkForTimeout();
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
@@ -175,27 +179,32 @@ void I2CManagerClass::loop() {
|
||||
***************************************************************************/
|
||||
void I2CManagerClass::handleInterrupt() {
|
||||
|
||||
// Update hardware state machine
|
||||
I2C_handleInterrupt();
|
||||
|
||||
// Experimental -- perform the post processing with interrupts enabled.
|
||||
//interrupts();
|
||||
// Enable interrupts to minimise effect on other interrupt code
|
||||
interrupts();
|
||||
|
||||
if (status!=I2C_STATUS_PENDING) {
|
||||
// Check if current request has completed. If there's a current request
|
||||
// and state isn't active then state contains the completion status of the request.
|
||||
if (state != I2C_STATE_ACTIVE && currentRequest != NULL) {
|
||||
// Remove completed request from head of queue
|
||||
I2CRB * t;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
t = queueHead;
|
||||
if (t != NULL) {
|
||||
I2CRB * t = queueHead;
|
||||
if (t == queueHead) {
|
||||
queueHead = t->nextRequest;
|
||||
if (!queueHead) queueTail = queueHead;
|
||||
t->nBytes = rxCount;
|
||||
t->status = status;
|
||||
t->status = state;
|
||||
|
||||
// I2C state machine is now free for next request
|
||||
currentRequest = NULL;
|
||||
state = I2C_STATE_FREE;
|
||||
|
||||
// Start next request (if any)
|
||||
I2CManager.startTransaction();
|
||||
}
|
||||
// I2C state machine is now free for next request
|
||||
status = I2C_STATE_FREE;
|
||||
}
|
||||
// Start next request (if any)
|
||||
I2CManager.startTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +212,7 @@ void I2CManagerClass::handleInterrupt() {
|
||||
I2CRB * volatile I2CManagerClass::queueHead = NULL;
|
||||
I2CRB * volatile I2CManagerClass::queueTail = NULL;
|
||||
I2CRB * volatile I2CManagerClass::currentRequest = NULL;
|
||||
volatile uint8_t I2CManagerClass::status = I2C_STATE_FREE;
|
||||
volatile uint8_t I2CManagerClass::state = I2C_STATE_FREE;
|
||||
volatile uint8_t I2CManagerClass::txCount;
|
||||
volatile uint8_t I2CManagerClass::rxCount;
|
||||
volatile uint8_t I2CManagerClass::operation;
|
||||
|
229
IODevice.cpp
229
IODevice.cpp
@@ -1,5 +1,7 @@
|
||||
/*
|
||||
* © 2021, Neil McKechnie. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Harald Barth
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of DCC++EX API
|
||||
*
|
||||
@@ -28,6 +30,10 @@
|
||||
#define USE_FAST_IO
|
||||
#endif
|
||||
|
||||
// Link to halSetup function. If not defined, the function reference will be NULL.
|
||||
extern __attribute__((weak)) void halSetup();
|
||||
extern __attribute__((weak)) void mySetup(); // Deprecated function name, output warning if it's declared
|
||||
|
||||
//==================================================================================================================
|
||||
// Static methods
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
@@ -57,6 +63,16 @@ void IODevice::begin() {
|
||||
dev->_begin();
|
||||
}
|
||||
_initPhase = false;
|
||||
|
||||
// Check for presence of deprecated mySetup() function, and output warning.
|
||||
if (mySetup)
|
||||
DIAG(F("WARNING: mySetup() function should be renamed to halSetup()"));
|
||||
|
||||
// Call user's halSetup() function (if defined in the build in myHal.cpp).
|
||||
// The contents will depend on the user's system hardware configuration.
|
||||
// The myHal.cpp file is a standard C++ module so has access to all of the DCC++EX APIs.
|
||||
if (halSetup)
|
||||
halSetup();
|
||||
}
|
||||
|
||||
// Overarching static loop() method for the IODevice subsystem. Works through the
|
||||
@@ -67,32 +83,54 @@ void IODevice::begin() {
|
||||
// doesn't need to invoke it.
|
||||
void IODevice::loop() {
|
||||
unsigned long currentMicros = micros();
|
||||
// Call every device's loop function in turn, one per entry.
|
||||
if (!_nextLoopDevice) _nextLoopDevice = _firstDevice;
|
||||
_nextLoopDevice->_loop(currentMicros);
|
||||
_nextLoopDevice = _nextLoopDevice->_nextDevice;
|
||||
|
||||
IODevice *lastLoopDevice = _nextLoopDevice; // So we know when to stop...
|
||||
// Loop through devices until we find one ready to be serviced.
|
||||
do {
|
||||
if (!_nextLoopDevice) _nextLoopDevice = _firstDevice;
|
||||
if (_nextLoopDevice) {
|
||||
if (_nextLoopDevice->_deviceState != DEVSTATE_FAILED
|
||||
&& ((long)(currentMicros - _nextLoopDevice->_nextEntryTime)) >= 0) {
|
||||
// Found one ready to run, so invoke its _loop method.
|
||||
_nextLoopDevice->_nextEntryTime = currentMicros;
|
||||
_nextLoopDevice->_loop(currentMicros);
|
||||
_nextLoopDevice = _nextLoopDevice->_nextDevice;
|
||||
break;
|
||||
}
|
||||
// Not this one, move to next one
|
||||
_nextLoopDevice = _nextLoopDevice->_nextDevice;
|
||||
}
|
||||
} while (_nextLoopDevice != lastLoopDevice); // Stop looking when we've done all.
|
||||
|
||||
// Report loop time if diags enabled
|
||||
#if defined(DIAG_LOOPTIMES)
|
||||
static unsigned long lastMicros = 0;
|
||||
static unsigned long maxElapsed = 0;
|
||||
// Measure time since loop() method started.
|
||||
unsigned long halElapsed = micros() - currentMicros;
|
||||
// Measure time between loop() method entries.
|
||||
unsigned long elapsed = currentMicros - lastMicros;
|
||||
static unsigned long maxElapsed = 0, maxHalElapsed = 0;
|
||||
static unsigned long lastOutputTime = 0;
|
||||
static unsigned long halTotal = 0, total = 0;
|
||||
static unsigned long count = 0;
|
||||
const unsigned long interval = (unsigned long)5 * 1000 * 1000; // 5 seconds in microsec
|
||||
unsigned long elapsed = currentMicros - lastMicros;
|
||||
|
||||
// Ignore long loop counts while message is still outputting
|
||||
if (currentMicros - lastOutputTime > 3000UL) {
|
||||
if (elapsed > maxElapsed) maxElapsed = elapsed;
|
||||
if (halElapsed > maxHalElapsed) maxHalElapsed = halElapsed;
|
||||
halTotal += halElapsed;
|
||||
total += elapsed;
|
||||
count++;
|
||||
}
|
||||
count++;
|
||||
if (currentMicros - lastOutputTime > interval) {
|
||||
if (lastOutputTime > 0)
|
||||
LCD(1,F("Loop=%lus,%lus max"), interval/count, maxElapsed);
|
||||
maxElapsed = 0;
|
||||
count = 0;
|
||||
DIAG(F("Loop Total:%lus (%lus max) HAL:%lus (%lus max)"),
|
||||
total/count, maxElapsed, halTotal/count, maxHalElapsed);
|
||||
maxElapsed = maxHalElapsed = total = halTotal = count = 0;
|
||||
lastOutputTime = currentMicros;
|
||||
}
|
||||
lastMicros = micros();
|
||||
lastMicros = currentMicros;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -112,12 +150,13 @@ bool IODevice::exists(VPIN vpin) {
|
||||
bool IODevice::hasCallback(VPIN vpin) {
|
||||
IODevice *dev = findDevice(vpin);
|
||||
if (!dev) return false;
|
||||
return dev->_hasCallback(vpin);
|
||||
return dev->_hasCallback;
|
||||
}
|
||||
|
||||
// Display (to diagnostics) details of the device.
|
||||
void IODevice::_display() {
|
||||
DIAG(F("Unknown device Vpins:%d-%d"), (int)_firstVpin, (int)_firstVpin+_nPins-1);
|
||||
DIAG(F("Unknown device Vpins:%d-%d %S"),
|
||||
(int)_firstVpin, (int)_firstVpin+_nPins-1, _deviceState==DEVSTATE_FAILED ? F("OFFLINE") : F(""));
|
||||
}
|
||||
|
||||
// Find device associated with nominated Vpin and pass configuration values on to it.
|
||||
@@ -125,6 +164,33 @@ void IODevice::_display() {
|
||||
bool IODevice::configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) {
|
||||
IODevice *dev = findDevice(vpin);
|
||||
if (dev) return dev->_configure(vpin, configType, paramCount, params);
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("IODevice::configure(): Vpin ID %d not found!"), (int)vpin);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read value from virtual pin.
|
||||
int IODevice::read(VPIN vpin) {
|
||||
for (IODevice *dev = _firstDevice; dev != 0; dev = dev->_nextDevice) {
|
||||
if (dev->owns(vpin))
|
||||
return dev->_read(vpin);
|
||||
}
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("IODevice::read(): Vpin %d not found!"), (int)vpin);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read analogue value from virtual pin.
|
||||
int IODevice::readAnalogue(VPIN vpin) {
|
||||
for (IODevice *dev = _firstDevice; dev != 0; dev = dev->_nextDevice) {
|
||||
if (dev->owns(vpin))
|
||||
return dev->_readAnalogue(vpin);
|
||||
}
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("IODevice::readAnalogue(): Vpin %d not found!"), (int)vpin);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -137,29 +203,36 @@ void IODevice::write(VPIN vpin, int value) {
|
||||
return;
|
||||
}
|
||||
#ifdef DIAG_IO
|
||||
//DIAG(F("IODevice::write(): Vpin ID %d not found!"), (int)vpin);
|
||||
DIAG(F("IODevice::write(): Vpin ID %d not found!"), (int)vpin);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Write analogue value to virtual pin(s). If multiple devices are allocated the same pin
|
||||
// then only the first one found will be used.
|
||||
void IODevice::writeAnalogue(VPIN vpin, int value, int profile) {
|
||||
// Write analogue value to virtual pin(s). If multiple devices are allocated
|
||||
// the same pin then only the first one found will be used.
|
||||
//
|
||||
// The significance of param1 and param2 may vary from device to device.
|
||||
// For servo controllers, param1 is the profile of the transition and param2
|
||||
// the duration, i.e. the time that the operation is to be animated over
|
||||
// in deciseconds (0-3276 sec)
|
||||
//
|
||||
void IODevice::writeAnalogue(VPIN vpin, int value, uint8_t param1, uint16_t param2) {
|
||||
IODevice *dev = findDevice(vpin);
|
||||
if (dev) {
|
||||
dev->_writeAnalogue(vpin, value, profile);
|
||||
dev->_writeAnalogue(vpin, value, param1, param2);
|
||||
return;
|
||||
}
|
||||
#ifdef DIAG_IO
|
||||
//DIAG(F("IODevice::writeAnalogue(): Vpin ID %d not found!"), (int)vpin);
|
||||
DIAG(F("IODevice::writeAnalogue(): Vpin ID %d not found!"), (int)vpin);
|
||||
#endif
|
||||
}
|
||||
|
||||
// isActive returns true if the device is currently in an animation of some sort, e.g. is changing
|
||||
// the output over a period of time.
|
||||
bool IODevice::isActive(VPIN vpin) {
|
||||
// isBusy, when called for a device pin is always a digital output or analogue output,
|
||||
// returns input feedback state of the pin, i.e. whether the pin is busy performing
|
||||
// an animation or fade over a period of time.
|
||||
bool IODevice::isBusy(VPIN vpin) {
|
||||
IODevice *dev = findDevice(vpin);
|
||||
if (dev)
|
||||
return dev->_isActive(vpin);
|
||||
return dev->_read(vpin);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
@@ -191,10 +264,12 @@ void IODevice::addDevice(IODevice *newDevice) {
|
||||
newDevice->_begin();
|
||||
}
|
||||
|
||||
// Private helper function to locate a device by VPIN. Returns NULL if not found
|
||||
// Private helper function to locate a device by VPIN. Returns NULL if not found.
|
||||
// This is performance-critical, so minimises the calculation and function calls necessary.
|
||||
IODevice *IODevice::findDevice(VPIN vpin) {
|
||||
for (IODevice *dev = _firstDevice; dev != 0; dev = dev->_nextDevice) {
|
||||
if (dev->owns(vpin))
|
||||
VPIN firstVpin = dev->_firstVpin;
|
||||
if (vpin >= firstVpin && vpin < firstVpin+dev->_nPins)
|
||||
return dev;
|
||||
}
|
||||
return NULL;
|
||||
@@ -226,54 +301,45 @@ bool IODevice::owns(VPIN id) {
|
||||
return (id >= _firstVpin && id < _firstVpin + _nPins);
|
||||
}
|
||||
|
||||
// Read value from virtual pin.
|
||||
int IODevice::read(VPIN vpin) {
|
||||
for (IODevice *dev = _firstDevice; dev != 0; dev = dev->_nextDevice) {
|
||||
if (dev->owns(vpin))
|
||||
return dev->_read(vpin);
|
||||
}
|
||||
#ifdef DIAG_IO
|
||||
//DIAG(F("IODevice::read(): Vpin %d not found!"), (int)vpin);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#else // !defined(IO_NO_HAL)
|
||||
|
||||
// Minimal implementations of public HAL interface, to support Arduino pin I/O and nothing more.
|
||||
|
||||
void IODevice::begin() { DIAG(F("NO HAL CONFIGURED!")); }
|
||||
bool IODevice::configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) {
|
||||
(void)vpin; (void)paramCount; (void)params; // Avoid compiler warnings
|
||||
if (configType == CONFIGURE_INPUT || configType == CONFIGURE_OUTPUT)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
bool IODevice::configure(VPIN pin, ConfigTypeEnum configType, int nParams, int p[]) {
|
||||
if (configType!=CONFIGURE_INPUT || nParams!=1 || pin >= NUM_DIGITAL_PINS) return false;
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("Arduino _configurePullup Pin:%d Val:%d"), pin, p[0]);
|
||||
#endif
|
||||
pinMode(pin, p[0] ? INPUT_PULLUP : INPUT);
|
||||
return true;
|
||||
}
|
||||
void IODevice::write(VPIN vpin, int value) {
|
||||
if (vpin >= NUM_DIGITAL_PINS) return;
|
||||
digitalWrite(vpin, value);
|
||||
pinMode(vpin, OUTPUT);
|
||||
}
|
||||
void IODevice::writeAnalogue(VPIN vpin, int value, int profile) {
|
||||
(void)vpin; (void)value; (void)profile; // Avoid compiler warnings
|
||||
}
|
||||
bool IODevice::hasCallback(VPIN vpin) {
|
||||
(void)vpin; // Avoid compiler warnings
|
||||
return false;
|
||||
}
|
||||
void IODevice::writeAnalogue(VPIN, int, uint8_t, uint16_t) {}
|
||||
bool IODevice::isBusy(VPIN) { return false; }
|
||||
bool IODevice::hasCallback(VPIN) { return false; }
|
||||
int IODevice::read(VPIN vpin) {
|
||||
pinMode(vpin, INPUT_PULLUP);
|
||||
if (vpin >= NUM_DIGITAL_PINS) return 0;
|
||||
return !digitalRead(vpin); // Return inverted state (5v=0, 0v=1)
|
||||
}
|
||||
int IODevice::readAnalogue(VPIN vpin) {
|
||||
pinMode(vpin, INPUT);
|
||||
noInterrupts();
|
||||
int value = analogRead(vpin);
|
||||
interrupts();
|
||||
return value;
|
||||
}
|
||||
void IODevice::loop() {}
|
||||
void IODevice::DumpAll() {
|
||||
DIAG(F("NO HAL CONFIGURED!"));
|
||||
}
|
||||
bool IODevice::exists(VPIN vpin) { return (vpin > 2 && vpin < 49); }
|
||||
void IODevice::setGPIOInterruptPin(int16_t pinNumber) {
|
||||
(void) pinNumber; // Avoid compiler warning
|
||||
}
|
||||
bool IODevice::exists(VPIN vpin) { return (vpin > 2 && vpin < NUM_DIGITAL_PINS); }
|
||||
void IODevice::setGPIOInterruptPin(int16_t) {}
|
||||
|
||||
// Chain of callback blocks (identifying registered callback functions for state changes)
|
||||
// Not used in IO_NO_HAL but must be declared.
|
||||
@@ -288,12 +354,14 @@ IONotifyCallback *IONotifyCallback::first = 0;
|
||||
ArduinoPins::ArduinoPins(VPIN firstVpin, int nPins) {
|
||||
_firstVpin = firstVpin;
|
||||
_nPins = nPins;
|
||||
uint8_t arrayLen = (_nPins+7)/8;
|
||||
_pinPullups = (uint8_t *)calloc(2, arrayLen);
|
||||
int arrayLen = (_nPins+7)/8;
|
||||
_pinPullups = (uint8_t *)calloc(3, arrayLen);
|
||||
_pinModes = (&_pinPullups[0]) + arrayLen;
|
||||
_pinInUse = (&_pinPullups[0]) + 2*arrayLen;
|
||||
for (int i=0; i<arrayLen; i++) {
|
||||
_pinPullups[i] = 0;
|
||||
_pinPullups[i] = 0xff; // default to pullup on, for inputs
|
||||
_pinModes[i] = 0;
|
||||
_pinInUse[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,6 +386,7 @@ bool ArduinoPins::_configure(VPIN vpin, ConfigTypeEnum configType, int paramCoun
|
||||
_pinPullups[index] &= ~mask;
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
_pinInUse[index] |= mask;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -336,11 +405,35 @@ void ArduinoPins::_write(VPIN vpin, int value) {
|
||||
_pinModes[index] |= mask;
|
||||
// Since mode changes should be infrequent, use standard pinMode function
|
||||
pinMode(pin, OUTPUT);
|
||||
_pinInUse[index] |= mask;
|
||||
}
|
||||
}
|
||||
|
||||
// Device-specific read function.
|
||||
// Device-specific read function (digital input).
|
||||
int ArduinoPins::_read(VPIN vpin) {
|
||||
int pin = vpin;
|
||||
uint8_t mask = 1 << ((pin-_firstVpin) % 8);
|
||||
uint8_t index = (pin-_firstVpin) / 8;
|
||||
if ((_pinModes[index] | ~_pinInUse[index]) & mask) {
|
||||
// Currently in write mode or not initialised, change to read mode
|
||||
_pinModes[index] &= ~mask;
|
||||
// Since mode changes should be infrequent, use standard pinMode function
|
||||
if (_pinPullups[index] & mask)
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
else
|
||||
pinMode(pin, INPUT);
|
||||
_pinInUse[index] |= mask;
|
||||
}
|
||||
int value = !fastReadDigital(pin); // Invert (5v=0, 0v=1)
|
||||
|
||||
#ifdef DIAG_IO
|
||||
//DIAG(F("Arduino Read Pin:%d Value:%d"), pin, value);
|
||||
#endif
|
||||
return value;
|
||||
}
|
||||
|
||||
// Device-specific readAnalogue function (analogue input)
|
||||
int ArduinoPins::_readAnalogue(VPIN vpin) {
|
||||
int pin = vpin;
|
||||
uint8_t mask = 1 << ((pin-_firstVpin) % 8);
|
||||
uint8_t index = (pin-_firstVpin) / 8;
|
||||
@@ -353,10 +446,22 @@ int ArduinoPins::_read(VPIN vpin) {
|
||||
else
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
int value = !fastReadDigital(pin); // Invert (5v=0, 0v=1)
|
||||
|
||||
// Since AnalogRead is also called from interrupt code, disable interrupts
|
||||
// while we're using it. There's only one ADC shared by all analogue inputs
|
||||
// on the Arduino, so we don't want interruptions.
|
||||
//******************************************************************************
|
||||
// NOTE: If the HAL is running on a computer without the DCC signal generator,
|
||||
// then interrupts needn't be disabled. Also, the DCC signal generator puts
|
||||
// the ADC into fast mode, so if it isn't present, analogueRead calls will be much
|
||||
// slower!!
|
||||
//******************************************************************************
|
||||
noInterrupts();
|
||||
int value = analogRead(pin);
|
||||
interrupts();
|
||||
|
||||
#ifdef DIAG_IO
|
||||
//DIAG(F("Arduino Read Pin:%d Value:%d"), pin, value);
|
||||
DIAG(F("Arduino Read Pin:%d Value:%d"), pin, value);
|
||||
#endif
|
||||
return value;
|
||||
}
|
||||
|
104
IODevice.h
104
IODevice.h
@@ -42,6 +42,7 @@
|
||||
#include "DIAG.h"
|
||||
#include "FSH.h"
|
||||
#include "I2CManager.h"
|
||||
#include "inttypes.h"
|
||||
|
||||
typedef uint16_t VPIN;
|
||||
// Limit VPIN number to max 32767. Above this number, printing often gives negative values.
|
||||
@@ -112,15 +113,27 @@ public:
|
||||
// configure is used invoke an IODevice instance's _configure method
|
||||
static bool configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]);
|
||||
|
||||
// User-friendly function for configuring an input pin.
|
||||
inline static bool configureInput(VPIN vpin, bool pullupEnable) {
|
||||
int params[] = {pullupEnable};
|
||||
return IODevice::configure(vpin, CONFIGURE_INPUT, 1, params);
|
||||
}
|
||||
|
||||
// User-friendly function for configuring a servo pin.
|
||||
inline static bool configureServo(VPIN vpin, uint16_t activePosition, uint16_t inactivePosition, uint8_t profile=0, uint16_t duration=0, uint8_t initialState=0) {
|
||||
int params[] = {(int)activePosition, (int)inactivePosition, profile, (int)duration, initialState};
|
||||
return IODevice::configure(vpin, CONFIGURE_SERVO, 5, params);
|
||||
}
|
||||
|
||||
// write invokes the IODevice instance's _write method.
|
||||
static void write(VPIN vpin, int value);
|
||||
|
||||
// write invokes the IODevice instance's _writeAnalogue method (not applicable for digital outputs)
|
||||
static void writeAnalogue(VPIN vpin, int value, int profile);
|
||||
static void writeAnalogue(VPIN vpin, int value, uint8_t profile=0, uint16_t duration=0);
|
||||
|
||||
// isActive returns true if the device is currently in an animation of some sort, e.g. is changing
|
||||
// isBusy returns true if the device is currently in an animation of some sort, e.g. is changing
|
||||
// the output over a period of time.
|
||||
static bool isActive(VPIN vpin);
|
||||
static bool isBusy(VPIN vpin);
|
||||
|
||||
// check whether the pin supports notification. If so, then regular _read calls are not required.
|
||||
static bool hasCallback(VPIN vpin);
|
||||
@@ -128,6 +141,9 @@ public:
|
||||
// read invokes the IODevice instance's _read method.
|
||||
static int read(VPIN vpin);
|
||||
|
||||
// read invokes the IODevice instance's _readAnalogue method.
|
||||
static int readAnalogue(VPIN vpin);
|
||||
|
||||
// loop invokes the IODevice instance's _loop method.
|
||||
static void loop();
|
||||
|
||||
@@ -147,7 +163,14 @@ public:
|
||||
|
||||
protected:
|
||||
|
||||
// Method to perform initialisation of the device (optionally implemented within device class)
|
||||
// Constructor
|
||||
IODevice(VPIN firstVpin=0, int nPins=0) {
|
||||
_firstVpin = firstVpin;
|
||||
_nPins = nPins;
|
||||
_nextEntryTime = 0;
|
||||
}
|
||||
|
||||
// Method to perform initialisation of the device (optionally implemented within device class)
|
||||
virtual void _begin() {}
|
||||
|
||||
// Method to configure device (optionally implemented within device class)
|
||||
@@ -161,41 +184,26 @@ protected:
|
||||
(void)vpin; (void)value;
|
||||
};
|
||||
|
||||
// Method to write an analogue value (optionally implemented within device class)
|
||||
virtual void _writeAnalogue(VPIN vpin, int value, int profile) {
|
||||
(void)vpin; (void)value; (void) profile;
|
||||
// Method to write an 'analogue' value (optionally implemented within device class)
|
||||
virtual void _writeAnalogue(VPIN vpin, int value, uint8_t param1, uint16_t param2) {
|
||||
(void)vpin; (void)value; (void) param1; (void)param2;
|
||||
};
|
||||
|
||||
// Method called from within a filter device to trigger its output (which may
|
||||
// have the same VPIN id as the input to the filter). It works through the
|
||||
// later devices in the chain only.
|
||||
void writeDownstream(VPIN vpin, int value);
|
||||
|
||||
// Function called to check whether callback notification is supported by this pin.
|
||||
// Defaults to no, if not overridden by the device.
|
||||
// The same value should be returned by all pins on the device, so only one need
|
||||
// be checked.
|
||||
virtual bool _hasCallback(VPIN vpin) {
|
||||
(void) vpin;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Method to read pin state (optionally implemented within device class)
|
||||
// Method to read digital pin state (optionally implemented within device class)
|
||||
virtual int _read(VPIN vpin) {
|
||||
(void)vpin;
|
||||
return 0;
|
||||
};
|
||||
|
||||
// _isActive returns true if the device is currently in an animation of some sort, e.g. is changing
|
||||
// the output over a period of time. Returns false unless overridden in sub class.
|
||||
virtual bool _isActive(VPIN vpin) {
|
||||
(void)vpin;
|
||||
return false;
|
||||
}
|
||||
// Method to read analogue pin state (optionally implemented within device class)
|
||||
virtual int _readAnalogue(VPIN vpin) {
|
||||
(void)vpin;
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Method to perform updates on an ongoing basis (optionally implemented within device class)
|
||||
virtual void _loop(unsigned long currentMicros) {
|
||||
(void)currentMicros; // Suppress compiler warning.
|
||||
delayUntil(currentMicros + 0x7fffffff); // Largest time in the future! Effectively disable _loop calls.
|
||||
};
|
||||
|
||||
// Method for displaying info on DIAG output (optionally implemented within device class)
|
||||
@@ -203,11 +211,19 @@ protected:
|
||||
|
||||
// Destructor
|
||||
virtual ~IODevice() {};
|
||||
|
||||
// Non-virtual function
|
||||
void delayUntil(unsigned long futureMicrosCount) {
|
||||
_nextEntryTime = futureMicrosCount;
|
||||
}
|
||||
|
||||
// Common object fields.
|
||||
VPIN _firstVpin;
|
||||
int _nPins;
|
||||
|
||||
// Flag whether the device supports callbacks.
|
||||
bool _hasCallback = false;
|
||||
|
||||
// Pin number of interrupt pin for GPIO extender devices. The extender module will pull this
|
||||
// pin low if an input changes state.
|
||||
int16_t _gpioInterruptPin = -1;
|
||||
@@ -225,6 +241,7 @@ private:
|
||||
static IODevice *findDevice(VPIN vpin);
|
||||
|
||||
IODevice *_nextDevice = 0;
|
||||
unsigned long _nextEntryTime;
|
||||
static IODevice *_firstDevice;
|
||||
|
||||
static IODevice *_nextLoopDevice;
|
||||
@@ -242,12 +259,14 @@ public:
|
||||
static void create(VPIN vpin, int nPins, uint8_t I2CAddress);
|
||||
// Constructor
|
||||
PCA9685(VPIN vpin, int nPins, uint8_t I2CAddress);
|
||||
enum ProfileType {
|
||||
Instant = 0, // Moves immediately between positions
|
||||
enum ProfileType : uint8_t {
|
||||
Instant = 0, // Moves immediately between positions (if duration not specified)
|
||||
UseDuration = 0, // Use specified duration
|
||||
Fast = 1, // Takes around 500ms end-to-end
|
||||
Medium = 2, // 1 second end-to-end
|
||||
Slow = 3, // 2 seconds end-to-end
|
||||
Bounce = 4 // For semaphores/turnouts with a bit of bounce!!
|
||||
Bounce = 4, // For semaphores/turnouts with a bit of bounce!!
|
||||
NoPowerOff = 0x80, // Flag to be ORed in to suppress power off after move.
|
||||
};
|
||||
|
||||
private:
|
||||
@@ -256,8 +275,8 @@ private:
|
||||
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override;
|
||||
// Device-specific write functions.
|
||||
void _write(VPIN vpin, int value) override;
|
||||
void _writeAnalogue(VPIN vpin, int value, int profile) override;
|
||||
bool _isActive(VPIN vpin) override;
|
||||
void _writeAnalogue(VPIN vpin, int value, uint8_t profile, uint16_t duration) override;
|
||||
int _read(VPIN vpin) override; // returns the digital state or busy status of the device
|
||||
void _loop(unsigned long currentMicros) override;
|
||||
void updatePosition(uint8_t pin);
|
||||
void writeDevice(uint8_t pin, int value);
|
||||
@@ -272,21 +291,18 @@ private:
|
||||
uint16_t fromPosition : 12;
|
||||
uint16_t toPosition : 12;
|
||||
uint8_t profile; // Config parameter
|
||||
uint8_t stepNumber; // Index of current step (starting from 0)
|
||||
uint8_t numSteps; // Number of steps in animation, or 0 if none in progress.
|
||||
int8_t state;
|
||||
}; // 12 bytes per element, i.e. per pin in use
|
||||
uint16_t stepNumber; // Index of current step (starting from 0)
|
||||
uint16_t numSteps; // Number of steps in animation, or 0 if none in progress.
|
||||
uint8_t currentProfile; // profile being used for current animation.
|
||||
uint16_t duration; // time (tenths of a second) for animation to complete.
|
||||
}; // 14 bytes per element, i.e. per pin in use
|
||||
|
||||
struct ServoData *_servoData [16];
|
||||
|
||||
static const uint16_t _defaultActivePosition = 410;
|
||||
static const uint16_t _defaultInactivePosition = 205;
|
||||
|
||||
static const uint8_t _catchupSteps = 5; // number of steps to wait before switching servo off
|
||||
static const byte FLASH _bounceProfile[30];
|
||||
|
||||
const unsigned int refreshInterval = 50; // refresh every 50ms
|
||||
unsigned long _lastRefreshTime; // last seen value of micros() count
|
||||
|
||||
// structures for setting up non-blocking writes to servo controller
|
||||
I2CRB requestBlock;
|
||||
@@ -335,13 +351,15 @@ private:
|
||||
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override;
|
||||
// Device-specific write function.
|
||||
void _write(VPIN vpin, int value) override;
|
||||
// Device-specific read function.
|
||||
// Device-specific read functions.
|
||||
int _read(VPIN vpin) override;
|
||||
int _readAnalogue(VPIN vpin) override;
|
||||
void _display() override;
|
||||
|
||||
|
||||
uint8_t *_pinPullups;
|
||||
uint8_t *_pinModes; // each bit is 1 for output, 0 for input
|
||||
uint8_t *_pinInUse;
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
165
IO_AnalogueInputs.h
Normal file
165
IO_AnalogueInputs.h
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* © 2021, Neil McKechnie. All rights reserved.
|
||||
*
|
||||
* This file is part of DCC++EX API
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef io_analogueinputs_h
|
||||
#define io_analogueinputs_h
|
||||
|
||||
// Uncomment following line to slow the scan cycle down to 1second ADC samples, with
|
||||
// diagnostic output of scanned values.
|
||||
//#define IO_ANALOGUE_SLOW
|
||||
|
||||
#include "IODevice.h"
|
||||
#include "I2CManager.h"
|
||||
#include "DIAG.h"
|
||||
#include "FSH.h"
|
||||
|
||||
/**********************************************************************************************
|
||||
* ADS111x class for I2C-connected analogue input modules ADS1113, ADS1114 and ADS1115.
|
||||
*
|
||||
* ADS1113 and ADS1114 are restricted to 1 input. ADS1115 has a multiplexer which allows
|
||||
* any of four input pins to be read by its ADC.
|
||||
*
|
||||
* The driver polls the device in accordance with the constant 'scanInterval' below. On first loop
|
||||
* entry, the multiplexer is set to pin A0 and the ADC is triggered. On second and subsequent
|
||||
* entries, the analogue value is read from the conversion register and then the multiplexer and
|
||||
* ADC are set up to read the next pin.
|
||||
*
|
||||
* The ADS111x is set up as follows:
|
||||
* Single-shot scan
|
||||
* Data rate 128 samples/sec (7.8ms/sample, but scanned every 10ms)
|
||||
* Comparator off
|
||||
* Gain FSR=6.144V
|
||||
* The gain means that the maximum input voltage of 5V (when Vss=5V) gives a reading
|
||||
* of 32767*(5.0/6.144) = 26666.
|
||||
*
|
||||
* A device is configured by the following:
|
||||
* ADS111x::create(firstVpin, nPins, i2cAddress);
|
||||
* for example
|
||||
* ADS111x::create(300, 1, 0x48); // single-input ADS1113
|
||||
* ADS111x::create(300, 4, 0x48); // four-input ADS1115
|
||||
*
|
||||
* Note: The device is simple and does not need initial configuration, so it should recover from
|
||||
* temporary loss of communications or power.
|
||||
**********************************************************************************************/
|
||||
class ADS111x: public IODevice {
|
||||
public:
|
||||
ADS111x(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
|
||||
_firstVpin = firstVpin;
|
||||
_nPins = min(nPins,4);
|
||||
_i2cAddress = i2cAddress;
|
||||
_currentPin = 0;
|
||||
for (int8_t i=0; i<_nPins; i++)
|
||||
_value[i] = -1;
|
||||
addDevice(this);
|
||||
}
|
||||
static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
|
||||
new ADS111x(firstVpin, nPins, i2cAddress);
|
||||
}
|
||||
private:
|
||||
void _begin() {
|
||||
// Initialise ADS device
|
||||
if (I2CManager.exists(_i2cAddress)) {
|
||||
_nextState = STATE_STARTSCAN;
|
||||
#ifdef DIAG_IO
|
||||
_display();
|
||||
#endif
|
||||
} else {
|
||||
DIAG(F("ADS111x device not found, I2C:%x"), _i2cAddress);
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
}
|
||||
}
|
||||
void _loop(unsigned long currentMicros) override {
|
||||
|
||||
// Check that previous non-blocking write has completed, if not then wait
|
||||
uint8_t status = _i2crb.status;
|
||||
if (status == I2C_STATUS_PENDING) return; // Busy, so don't do anything.
|
||||
if (status == I2C_STATUS_OK) {
|
||||
switch (_nextState) {
|
||||
case STATE_STARTSCAN:
|
||||
// Configure ADC and multiplexer for next scan. See ADS111x datasheet for details
|
||||
// of configuration register settings.
|
||||
_outBuffer[0] = 0x01; // Config register address
|
||||
_outBuffer[1] = 0xC0 + (_currentPin << 4); // Trigger single-shot, channel n
|
||||
_outBuffer[2] = 0xA3; // 250 samples/sec, comparator off
|
||||
// Write command, without waiting for completion.
|
||||
I2CManager.write(_i2cAddress, _outBuffer, 3, &_i2crb);
|
||||
|
||||
delayUntil(currentMicros + scanInterval);
|
||||
_nextState = STATE_STARTREAD;
|
||||
break;
|
||||
|
||||
case STATE_STARTREAD:
|
||||
// Reading the pin value
|
||||
_outBuffer[0] = 0x00; // Conversion register address
|
||||
I2CManager.read(_i2cAddress, _inBuffer, 2, _outBuffer, 1, &_i2crb); // Read register
|
||||
_nextState = STATE_GETVALUE;
|
||||
break;
|
||||
|
||||
case STATE_GETVALUE:
|
||||
_value[_currentPin] = ((uint16_t)_inBuffer[0] << 8) + (uint16_t)_inBuffer[1];
|
||||
#ifdef IO_ANALOGUE_SLOW
|
||||
DIAG(F("ADS111x pin:%d value:%d"), _currentPin, _value[_currentPin]);
|
||||
#endif
|
||||
|
||||
// Move to next pin
|
||||
if (++_currentPin >= _nPins) _currentPin = 0;
|
||||
_nextState = STATE_STARTSCAN;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else { // error status
|
||||
DIAG(F("ADS111x I2C:x%d Error:%d %S"), _i2cAddress, status, I2CManager.getErrorMessage(status));
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
int _readAnalogue(VPIN vpin) override {
|
||||
int pin = vpin - _firstVpin;
|
||||
return _value[pin];
|
||||
}
|
||||
|
||||
void _display() override {
|
||||
DIAG(F("ADS111x I2C:x%x Configured on Vpins:%d-%d %S"), _i2cAddress, _firstVpin, _firstVpin+_nPins-1,
|
||||
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
|
||||
}
|
||||
|
||||
// ADC conversion rate is 250SPS, or 4ms per conversion. Set the period between updates to 10ms.
|
||||
// This is enough to allow the conversion to reliably complete in time.
|
||||
#ifndef IO_ANALOGUE_SLOW
|
||||
const unsigned long scanInterval = 10000UL; // Period between successive ADC scans in microseconds.
|
||||
#else
|
||||
const unsigned long scanInterval = 1000000UL; // Period between successive ADC scans in microseconds.
|
||||
#endif
|
||||
enum : uint8_t {
|
||||
STATE_STARTSCAN,
|
||||
STATE_STARTREAD,
|
||||
STATE_GETVALUE,
|
||||
};
|
||||
uint16_t _value[4];
|
||||
uint8_t _i2cAddress;
|
||||
uint8_t _outBuffer[3];
|
||||
uint8_t _inBuffer[2];
|
||||
uint8_t _currentPin; // ADC pin currently being scanned
|
||||
I2CRB _i2crb;
|
||||
uint8_t _nextState;
|
||||
};
|
||||
|
||||
#endif // io_analogueinputs_h
|
@@ -20,49 +20,49 @@
|
||||
#include "DCC.h"
|
||||
#include "IODevice.h"
|
||||
#include "DIAG.h"
|
||||
#include "defines.h"
|
||||
|
||||
// Note: For DCC Accessory Decoders, a particular output can be specified by
|
||||
// a linear address, or by an address/subaddress pair, where the subaddress is
|
||||
// in the range 0 to 3 and specifies an output within a group of 4.
|
||||
// NMRA and DCC++EX accepts addresses in the range 0-511. Linear addresses
|
||||
// are not specified by the NMRA and so different manufacturers may calculate them
|
||||
// in different ways. DCC+EX uses a range of 1-2044 which excludes decoder address 0.
|
||||
// Therefore, I've avoided using linear addresses here because of the ambiguities
|
||||
// involved. Instead I've used the term 'packedAddress'.
|
||||
#define PACKEDADDRESS(addr, subaddr) (((addr) << 2) + (subaddr))
|
||||
#define ADDRESS(packedaddr) ((packedaddr) >> 2)
|
||||
#define SUBADDRESS(packedaddr) ((packedaddr) % 4)
|
||||
|
||||
void DCCAccessoryDecoder::create(VPIN vpin, int nPins, int DCCAddress, int DCCSubaddress) {
|
||||
new DCCAccessoryDecoder(vpin, nPins, DCCAddress, DCCSubaddress);
|
||||
}
|
||||
|
||||
// Constructor
|
||||
// Constructors
|
||||
DCCAccessoryDecoder::DCCAccessoryDecoder(VPIN vpin, int nPins, int DCCAddress, int DCCSubaddress) {
|
||||
_firstVpin = vpin;
|
||||
_nPins = nPins;
|
||||
_packedAddress = (DCCAddress << 2) + DCCSubaddress;
|
||||
_packedAddress = PACKEDADDRESS(DCCAddress, DCCSubaddress);
|
||||
addDevice(this);
|
||||
}
|
||||
|
||||
void DCCAccessoryDecoder::_begin() {
|
||||
int endAddress = _packedAddress + _nPins - 1;
|
||||
int DCCAddress = _packedAddress >> 2;
|
||||
int DCCSubaddress = _packedAddress & 3;
|
||||
DIAG(F("DCC Accessory Decoder configured Vpins:%d-%d Linear Address:%d-%d (%d/%d-%d/%d)"), _firstVpin, _firstVpin+_nPins-1,
|
||||
_packedAddress, _packedAddress+_nPins-1,
|
||||
DCCAddress, DCCSubaddress, endAddress >> 2, endAddress % 4);
|
||||
#if defined(DIAG_IO)
|
||||
_display();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Device-specific write function.
|
||||
// Device-specific write function. State 1=closed, 0=thrown. Adjust for RCN-213 compliance
|
||||
void DCCAccessoryDecoder::_write(VPIN id, int state) {
|
||||
int packedAddress = _packedAddress + id - _firstVpin;
|
||||
#ifdef DIAG_IO
|
||||
#if defined(HAL_ACCESSORY_COMMAND_REVERSE)
|
||||
state = !state;
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DCC Write Linear Address:%d State:%d (inverted)"), packedAddress, state);
|
||||
#endif
|
||||
#else
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DCC Write Linear Address:%d State:%d"), packedAddress, state);
|
||||
#endif
|
||||
DCC::setAccessory(packedAddress >> 2, packedAddress % 4, state);
|
||||
#endif
|
||||
#endif
|
||||
DCC::setAccessory(ADDRESS(packedAddress), SUBADDRESS(packedAddress), state);
|
||||
}
|
||||
|
||||
void DCCAccessoryDecoder::_display() {
|
||||
int endAddress = _packedAddress + _nPins - 1;
|
||||
DIAG(F("DCC Accessory Vpins:%d-%d Linear Address:%d-%d (%d/%d-%d/%d)"), _firstVpin, _firstVpin+_nPins-1,
|
||||
_packedAddress, _packedAddress+_nPins-1,
|
||||
_packedAddress >> 2, _packedAddress % 4, endAddress >> 2, endAddress % 4);
|
||||
DIAG(F("DCCAccessoryDecoder Configured on Vpins:%d-%d Addresses %d/%d-%d/%d)"), _firstVpin, _firstVpin+_nPins-1,
|
||||
ADDRESS(_packedAddress), SUBADDRESS(_packedAddress), ADDRESS(endAddress), SUBADDRESS(endAddress));
|
||||
}
|
||||
|
||||
|
255
IO_DFPlayer.h
Normal file
255
IO_DFPlayer.h
Normal file
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* © 2021, Neil McKechnie. All rights reserved.
|
||||
*
|
||||
* This file is part of DCC++EX API
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* DFPlayer is an MP3 player module with an SD card holder. It also has an integrated
|
||||
* amplifier, so it only needs a power supply and a speaker.
|
||||
*
|
||||
* This driver allows the device to be controlled through IODevice::write() and
|
||||
* IODevice::writeAnalogue() calls.
|
||||
*
|
||||
* The driver is configured as follows:
|
||||
*
|
||||
* DFPlayer::create(firstVpin, nPins, Serialn);
|
||||
*
|
||||
* Where firstVpin is the first vpin reserved for reading the device,
|
||||
* nPins is the number of pins to be allocated (max 5)
|
||||
* and Serialn is the name of the Serial port connected to the DFPlayer (e.g. Serial1).
|
||||
*
|
||||
* Example:
|
||||
* In mySetup function within mySetup.cpp:
|
||||
* DFPlayer::create(3500, 5, Serial1);
|
||||
*
|
||||
* Writing an analogue value 0-2999 to the first pin will select a numbered file from the SD card;
|
||||
* Writing an analogue value 0-30 to the second pin will set the volume of the output;
|
||||
* Writing a digital value to the first pin will play or stop the file;
|
||||
* Reading a digital value from any pin will return true(1) if the player is playing, false(0) otherwise.
|
||||
*
|
||||
* From EX-RAIL, the following commands may be used:
|
||||
* SET(3500) -- starts playing the first file on the SD card
|
||||
* SET(3501) -- starts playing the second file on the SD card
|
||||
* etc.
|
||||
* RESET(3500) -- stops all playing on the player
|
||||
* WAITFOR(3500) -- wait for the file currently being played by the player to complete
|
||||
* SERVO(3500,23,0) -- plays file 23 at current volume
|
||||
* SERVO(3500,23,30) -- plays file 23 at volume 30 (maximum)
|
||||
* SERVO(3501,20,0) -- Sets the volume to 20
|
||||
*
|
||||
* NB The DFPlayer's serial lines are not 5V safe, so connecting the Arduino TX directly
|
||||
* to the DFPlayer's RX terminal will cause lots of noise over the speaker, or worse.
|
||||
* A 1k resistor in series with the module's RX terminal will alleviate this.
|
||||
*/
|
||||
|
||||
#ifndef IO_DFPlayer_h
|
||||
#define IO_DFPlayer_h
|
||||
|
||||
#include "IODevice.h"
|
||||
|
||||
class DFPlayer : public IODevice {
|
||||
private:
|
||||
HardwareSerial *_serial;
|
||||
bool _playing = false;
|
||||
uint8_t _inputIndex = 0;
|
||||
unsigned long _commandSendTime; // Allows timeout processing
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
DFPlayer(VPIN firstVpin, int nPins, HardwareSerial &serial) :
|
||||
IODevice(firstVpin, nPins),
|
||||
_serial(&serial)
|
||||
{
|
||||
addDevice(this);
|
||||
}
|
||||
|
||||
static void create(VPIN firstVpin, int nPins, HardwareSerial &serial) {
|
||||
new DFPlayer(firstVpin, nPins, serial);
|
||||
}
|
||||
|
||||
protected:
|
||||
void _begin() override {
|
||||
_serial->begin(9600);
|
||||
_deviceState = DEVSTATE_INITIALISING;
|
||||
|
||||
// Send a query to the device to see if it responds
|
||||
sendPacket(0x42);
|
||||
_commandSendTime = micros();
|
||||
}
|
||||
|
||||
void _loop(unsigned long currentMicros) override {
|
||||
// Check for incoming data on _serial, and update busy flag accordingly.
|
||||
// Expected message is in the form "7F FF 06 3D xx xx xx xx xx EF"
|
||||
while (_serial->available()) {
|
||||
int c = _serial->read();
|
||||
if (c == 0x7E)
|
||||
_inputIndex = 1;
|
||||
else if ((c==0xFF && _inputIndex==1)
|
||||
|| (c==0x3D && _inputIndex==3)
|
||||
|| (_inputIndex >=4 && _inputIndex <= 8))
|
||||
_inputIndex++;
|
||||
else if (c==0x06 && _inputIndex==2) {
|
||||
// Valid message prefix, so consider the device online
|
||||
if (_deviceState==DEVSTATE_INITIALISING) {
|
||||
_deviceState = DEVSTATE_NORMAL;
|
||||
#ifdef DIAG_IO
|
||||
_display();
|
||||
#endif
|
||||
}
|
||||
_inputIndex++;
|
||||
} else if (c==0xEF && _inputIndex==9) {
|
||||
// End of play
|
||||
if (_playing) {
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DFPlayer: Finished"));
|
||||
#endif
|
||||
_playing = false;
|
||||
}
|
||||
_inputIndex = 0;
|
||||
} else
|
||||
_inputIndex = 0; // Unrecognised character sequence, start again!
|
||||
}
|
||||
// Check if the initial prompt to device has timed out. Allow 1 second
|
||||
if (_deviceState == DEVSTATE_INITIALISING && currentMicros - _commandSendTime > 1000000UL) {
|
||||
DIAG(F("DFPlayer device not responding on serial port"));
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
}
|
||||
delayUntil(currentMicros + 10000); // Only enter every 10ms
|
||||
}
|
||||
|
||||
// Write with value 1 starts playing a song. The relative pin number is the file number.
|
||||
// Write with value 0 stops playing.
|
||||
void _write(VPIN vpin, int value) override {
|
||||
int pin = vpin - _firstVpin;
|
||||
if (value) {
|
||||
// Value 1, start playing
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DFPlayer: Play %d"), pin+1);
|
||||
#endif
|
||||
sendPacket(0x03, pin+1);
|
||||
_playing = true;
|
||||
} else {
|
||||
// Value 0, stop playing
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DFPlayer: Stop"));
|
||||
#endif
|
||||
sendPacket(0x16);
|
||||
_playing = false;
|
||||
}
|
||||
}
|
||||
|
||||
// WriteAnalogue on first pin uses the nominated value as a file number to start playing, if file number > 0.
|
||||
// Volume may be specified as second parameter to writeAnalogue.
|
||||
// If value is zero, the player stops playing.
|
||||
// WriteAnalogue on second pin sets the output volume.
|
||||
void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t=0) override {
|
||||
uint8_t pin = vpin - _firstVpin;
|
||||
|
||||
// Validate parameter.
|
||||
volume = min(30,volume);
|
||||
|
||||
if (pin == 0) {
|
||||
// Play track
|
||||
if (value > 0) {
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DFPlayer: Play %d"), value);
|
||||
#endif
|
||||
sendPacket(0x03, value); // Play track
|
||||
_playing = true;
|
||||
if (volume > 0) {
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DFPlayer: Volume %d"), volume);
|
||||
#endif
|
||||
sendPacket(0x06, volume); // Set volume
|
||||
}
|
||||
} else {
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DFPlayer: Stop"));
|
||||
#endif
|
||||
sendPacket(0x16); // Stop play
|
||||
_playing = false;
|
||||
}
|
||||
} else if (pin == 1) {
|
||||
// Set volume (0-30)
|
||||
if (value > 30) value = 30;
|
||||
else if (value < 0) value = 0;
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("DFPlayer: Volume %d"), value);
|
||||
#endif
|
||||
sendPacket(0x06, value);
|
||||
}
|
||||
}
|
||||
|
||||
// A read on any pin indicates whether the player is still playing.
|
||||
int _read(VPIN) override {
|
||||
return _playing;
|
||||
}
|
||||
|
||||
void _display() override {
|
||||
DIAG(F("DFPlayer Configured on Vpins:%d-%d %S"), _firstVpin, _firstVpin+_nPins-1,
|
||||
(_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F(""));
|
||||
}
|
||||
|
||||
private:
|
||||
// 7E FF 06 0F 00 01 01 xx xx EF
|
||||
// 0 -> 7E is start code
|
||||
// 1 -> FF is version
|
||||
// 2 -> 06 is length
|
||||
// 3 -> 0F is command
|
||||
// 4 -> 00 is no receive
|
||||
// 5~6 -> 01 01 is argument
|
||||
// 7~8 -> checksum = 0 - ( FF+06+0F+00+01+01 )
|
||||
// 9 -> EF is end code
|
||||
|
||||
void sendPacket(uint8_t command, uint16_t arg = 0)
|
||||
{
|
||||
uint8_t out[] = { 0x7E,
|
||||
0xFF,
|
||||
06,
|
||||
command,
|
||||
00,
|
||||
static_cast<uint8_t>(arg >> 8),
|
||||
static_cast<uint8_t>(arg & 0x00ff),
|
||||
00,
|
||||
00,
|
||||
0xEF };
|
||||
|
||||
setChecksum(out);
|
||||
|
||||
_serial->write(out, sizeof(out));
|
||||
}
|
||||
|
||||
uint16_t calcChecksum(uint8_t* packet)
|
||||
{
|
||||
uint16_t sum = 0;
|
||||
for (int i = 1; i < 7; i++)
|
||||
{
|
||||
sum += packet[i];
|
||||
}
|
||||
return -sum;
|
||||
}
|
||||
|
||||
void setChecksum(uint8_t* out)
|
||||
{
|
||||
uint16_t sum = calcChecksum(out);
|
||||
|
||||
out[7] = (sum >> 8);
|
||||
out[8] = (sum & 0xff);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // IO_DFPlayer_h
|
@@ -42,7 +42,9 @@ void IO_ExampleSerial::create(VPIN firstVpin, int nPins, HardwareSerial *serial,
|
||||
// Device-specific initialisation
|
||||
void IO_ExampleSerial::_begin() {
|
||||
_serial->begin(_baud);
|
||||
DIAG(F("ExampleSerial configured Vpins:%d-%d"), _firstVpin, _firstVpin+_nPins-1);
|
||||
#if defined(DIAG_IO)
|
||||
_display();
|
||||
#endif
|
||||
|
||||
// Send a few # characters to the output
|
||||
for (uint8_t i=0; i<3; i++)
|
||||
@@ -121,7 +123,7 @@ void IO_ExampleSerial::_loop(unsigned long currentMicros) {
|
||||
}
|
||||
|
||||
void IO_ExampleSerial::_display() {
|
||||
DIAG(F("IO_ExampleSerial VPins:%d-%d"), (int)_firstVpin,
|
||||
DIAG(F("IO_ExampleSerial Configured on VPins:%d-%d"), (int)_firstVpin,
|
||||
(int)_firstVpin+_nPins-1);
|
||||
}
|
||||
|
||||
|
@@ -45,10 +45,6 @@ protected:
|
||||
int _read(VPIN vpin) override;
|
||||
void _display() override;
|
||||
void _loop(unsigned long currentMicros) override;
|
||||
bool _hasCallback(VPIN vpin) {
|
||||
(void)vpin; // suppress compiler warning
|
||||
return true; // Enable callback if caller wants to use it.
|
||||
}
|
||||
|
||||
// Data fields
|
||||
uint8_t _I2CAddress;
|
||||
@@ -57,9 +53,9 @@ protected:
|
||||
T _portOutputState;
|
||||
T _portMode;
|
||||
T _portPullup;
|
||||
T _portInUse;
|
||||
// Interval between refreshes of each input port
|
||||
static const int _portTickTime = 4000;
|
||||
unsigned long _lastLoopEntry = 0;
|
||||
|
||||
// Virtual functions for interfacing with I2C GPIO Device
|
||||
virtual void _writeGpioPort() = 0;
|
||||
@@ -80,12 +76,13 @@ protected:
|
||||
|
||||
// Constructor
|
||||
template <class T>
|
||||
GPIOBase<T>::GPIOBase(FSH *deviceName, VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin) {
|
||||
GPIOBase<T>::GPIOBase(FSH *deviceName, VPIN firstVpin, uint8_t nPins, uint8_t I2CAddress, int interruptPin) :
|
||||
IODevice(firstVpin, nPins)
|
||||
{
|
||||
_deviceName = deviceName;
|
||||
_firstVpin = firstVpin;
|
||||
_nPins = nPins;
|
||||
_I2CAddress = I2CAddress;
|
||||
_gpioInterruptPin = interruptPin;
|
||||
_hasCallback = true;
|
||||
// Add device to list of devices.
|
||||
addDevice(this);
|
||||
}
|
||||
@@ -99,14 +96,19 @@ void GPIOBase<T>::_begin() {
|
||||
I2CManager.begin();
|
||||
I2CManager.setClock(400000);
|
||||
if (I2CManager.exists(_I2CAddress)) {
|
||||
#if defined(DIAG_IO)
|
||||
_display();
|
||||
#endif
|
||||
_portMode = 0; // default to input mode
|
||||
_portPullup = -1; // default to pullup enabled
|
||||
_portInputState = -1;
|
||||
_portInputState = -1;
|
||||
_portInUse = 0;
|
||||
_setupDevice();
|
||||
_deviceState = DEVSTATE_NORMAL;
|
||||
} else {
|
||||
DIAG(F("%S I2C:x%x Device not detected"), _deviceName, _I2CAddress);
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
}
|
||||
_setupDevice();
|
||||
_deviceState = DEVSTATE_NORMAL;
|
||||
_lastLoopEntry = micros();
|
||||
}
|
||||
|
||||
// Configuration parameters for inputs:
|
||||
@@ -126,11 +128,15 @@ bool GPIOBase<T>::_configure(VPIN vpin, ConfigTypeEnum configType, int paramCoun
|
||||
_portPullup |= mask;
|
||||
else
|
||||
_portPullup &= ~mask;
|
||||
// Mark that port has been accessed
|
||||
_portInUse |= mask;
|
||||
// Set input mode
|
||||
_portMode &= ~mask;
|
||||
|
||||
// Call subclass's virtual function to write to device
|
||||
_writePortModes();
|
||||
_writePullups();
|
||||
// Re-read port following change
|
||||
_readGpioPort();
|
||||
// Port change will be notified on next loop entry.
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -145,9 +151,12 @@ void GPIOBase<T>::_loop(unsigned long currentMicros) {
|
||||
_deviceState = DEVSTATE_NORMAL;
|
||||
} else {
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
DIAG(F("%S I2C:x%x Error:%d"), _deviceName, _I2CAddress, status);
|
||||
DIAG(F("%S I2C:x%x Error:%d %S"), _deviceName, _I2CAddress, status,
|
||||
I2CManager.getErrorMessage(status));
|
||||
}
|
||||
_processCompletion(status);
|
||||
// Set unused pin and write mode pin value to 1
|
||||
_portInputState |= ~_portInUse | _portMode;
|
||||
|
||||
// Scan for changes in input states and invoke callback (if present)
|
||||
T differences = lastPortStates ^ _portInputState;
|
||||
@@ -169,27 +178,25 @@ void GPIOBase<T>::_loop(unsigned long currentMicros) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Check if interrupt configured. If so, and pin is not pulled down, finish.
|
||||
if (_gpioInterruptPin >= 0) {
|
||||
if (digitalRead(_gpioInterruptPin)) return;
|
||||
} else
|
||||
// No interrupt pin. Check if tick has elapsed. If not, finish.
|
||||
if (currentMicros - _lastLoopEntry < _portTickTime) return;
|
||||
// Check if interrupt configured. If not, or if it is active (pulled down), then
|
||||
// initiate a scan.
|
||||
if (_gpioInterruptPin < 0 || !digitalRead(_gpioInterruptPin)) {
|
||||
// TODO: Could suppress reads if there are no pins configured as inputs!
|
||||
|
||||
// TODO: Could suppress reads if there are no pins configured as inputs!
|
||||
|
||||
// Read input
|
||||
_lastLoopEntry = currentMicros;
|
||||
if (_deviceState == DEVSTATE_NORMAL) {
|
||||
_readGpioPort(false); // Initiate non-blocking read
|
||||
_deviceState= DEVSTATE_SCANNING;
|
||||
// Read input
|
||||
if (_deviceState == DEVSTATE_NORMAL) {
|
||||
_readGpioPort(false); // Initiate non-blocking read
|
||||
_deviceState= DEVSTATE_SCANNING;
|
||||
}
|
||||
}
|
||||
// Delay next entry until tick elapsed.
|
||||
delayUntil(currentMicros + _portTickTime);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void GPIOBase<T>::_display() {
|
||||
DIAG(F("%S I2C:x%x Configured on Vpins:%d-%d"), _deviceName, _I2CAddress,
|
||||
_firstVpin, _firstVpin+_nPins-1);
|
||||
DIAG(F("%S I2C:x%x Configured on Vpins:%d-%d %S"), _deviceName, _I2CAddress,
|
||||
_firstVpin, _firstVpin+_nPins-1, (_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F(""));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -200,8 +207,9 @@ void GPIOBase<T>::_write(VPIN vpin, int value) {
|
||||
DIAG(F("%S I2C:x%x Write Pin:%d Val:%d"), _deviceName, _I2CAddress, pin, value);
|
||||
#endif
|
||||
|
||||
// Set port mode output
|
||||
// Set port mode output if currently not output mode
|
||||
if (!(_portMode & mask)) {
|
||||
_portInUse |= mask;
|
||||
_portMode |= mask;
|
||||
_writePortModes();
|
||||
}
|
||||
@@ -221,12 +229,16 @@ int GPIOBase<T>::_read(VPIN vpin) {
|
||||
int pin = vpin - _firstVpin;
|
||||
T mask = 1 << pin;
|
||||
|
||||
// Set port mode to input
|
||||
if (_portMode & mask) {
|
||||
// Set port mode to input if currently output or first use
|
||||
if ((_portMode | ~_portInUse) & mask) {
|
||||
_portMode &= ~mask;
|
||||
_portInUse |= mask;
|
||||
_writePullups();
|
||||
_writePortModes();
|
||||
// Port won't have been read yet, so read it now.
|
||||
_readGpioPort();
|
||||
// Set unused pin and write mode pin value to 1
|
||||
_portInputState |= ~_portInUse | _portMode;
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("%S I2C:x%x PortStates:%x"), _deviceName, _I2CAddress, _portInputState);
|
||||
#endif
|
||||
|
113
IO_HCSR04.h
113
IO_HCSR04.h
@@ -17,31 +17,37 @@
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
/*
|
||||
* The HC-SR04 module has an ultrasonic transmitter (40kHz) and a receiver.
|
||||
* It is operated through two signal pins. When the transmit pin is set to 1 for
|
||||
* 10us, on the falling edge the transmitter sends a short transmission of
|
||||
* It is operated through two signal pins. When the transmit pin is set to 1
|
||||
* for 10us, on the falling edge the transmitter sends a short transmission of
|
||||
* 8 pulses (like a sonar 'ping'). This is reflected off objects and received
|
||||
* by the receiver. A pulse is sent on the receive pin whose length is equal
|
||||
* to the delay between the transmission of the pulse and the detection of
|
||||
* its echo. The distance of the reflecting object is calculated by halving
|
||||
* the time (to allow for the out and back distance), then multiplying by the
|
||||
* speed of sound (assumed to be constant).
|
||||
*
|
||||
*
|
||||
* This driver polls the HC-SR04 by sending the trigger pulse and then measuring
|
||||
* the length of the received pulse. If the calculated distance is less than the
|
||||
* threshold, the output changes to 1. If it is greater than the threshold plus
|
||||
* a hysteresis margin, the output changes to 0.
|
||||
*
|
||||
* The measurement would be more reliable if interrupts were disabled while the
|
||||
* pulse is being timed. However, this would affect other functions in the CS
|
||||
* so the measurement is being performed with interrupts enabled. Also, we could
|
||||
* use an interrupt pin in the Arduino for the timing, but the same consideration
|
||||
* applies.
|
||||
*
|
||||
* Note: The timing accuracy required by this means that the pins have to be
|
||||
* direct Arduino pins; GPIO pins on an IO Extender cannot provide the required
|
||||
* accuracy.
|
||||
* the length of the received pulse. If the calculated distance is less than
|
||||
* the threshold, the output state returned by a read() call changes to 1. If
|
||||
* the distance is greater than the threshold plus a hysteresis margin, the
|
||||
* output changes to 0. The device also supports readAnalogue(), which returns
|
||||
* the measured distance in cm, or 32767 if the distance exceeds the
|
||||
* offThreshold.
|
||||
*
|
||||
* It might be thought that the measurement would be more reliable if interrupts
|
||||
* were disabled while the pulse is being timed. However, this would affect
|
||||
* other functions in the CS so the measurement is being performed with
|
||||
* interrupts enabled. Also, we could use an interrupt pin in the Arduino for
|
||||
* the timing, but the same consideration applies. In any case, the DCC
|
||||
* interrupt occurs once every 58us, so any IRC code is much faster than that.
|
||||
* And 58us corresponds to 1cm in the calculation, so the effect of
|
||||
* interrupts is negligible.
|
||||
*
|
||||
* Note: The timing accuracy required for measuring the pulse length means that
|
||||
* the pins have to be direct Arduino pins; GPIO pins on an IO Extender cannot
|
||||
* provide the required accuracy.
|
||||
*/
|
||||
|
||||
#ifndef IO_HCSR04_H
|
||||
@@ -53,45 +59,45 @@ class HCSR04 : public IODevice {
|
||||
|
||||
private:
|
||||
// pins must be arduino GPIO pins, not extender pins or HAL pins.
|
||||
int _transmitPin = -1;
|
||||
int _receivePin = -1;
|
||||
int _trigPin = -1;
|
||||
int _echoPin = -1;
|
||||
// Thresholds for setting active state in cm.
|
||||
uint8_t _onThreshold; // cm
|
||||
uint8_t _offThreshold; // cm
|
||||
// Last measured distance in cm.
|
||||
uint16_t _distance;
|
||||
// Active=1/inactive=0 state
|
||||
uint8_t _value = 0;
|
||||
// Time of last loop execution
|
||||
unsigned long _lastExecutionTime;
|
||||
// Factor for calculating the distance (cm) from echo time (ms).
|
||||
// Based on a speed of sound of 345 metres/second.
|
||||
const uint16_t factor = 58; // ms/cm
|
||||
|
||||
public:
|
||||
// Constructor perfroms static initialisation of the device object
|
||||
HCSR04 (VPIN vpin, int transmitPin, int receivePin, uint16_t onThreshold, uint16_t offThreshold) {
|
||||
HCSR04 (VPIN vpin, int trigPin, int echoPin, uint16_t onThreshold, uint16_t offThreshold) {
|
||||
_firstVpin = vpin;
|
||||
_nPins = 1;
|
||||
_transmitPin = transmitPin;
|
||||
_receivePin = receivePin;
|
||||
_trigPin = trigPin;
|
||||
_echoPin = echoPin;
|
||||
_onThreshold = onThreshold;
|
||||
_offThreshold = offThreshold;
|
||||
addDevice(this);
|
||||
}
|
||||
|
||||
// Static create function provides alternative way to create object
|
||||
static void create(VPIN vpin, int transmitPin, int receivePin, uint16_t onThreshold, uint16_t offThreshold) {
|
||||
new HCSR04(vpin, transmitPin, receivePin, onThreshold, offThreshold);
|
||||
static void create(VPIN vpin, int trigPin, int echoPin, uint16_t onThreshold, uint16_t offThreshold) {
|
||||
new HCSR04(vpin, trigPin, echoPin, onThreshold, offThreshold);
|
||||
}
|
||||
|
||||
protected:
|
||||
// _begin function called to perform dynamic initialisation of the device
|
||||
void _begin() override {
|
||||
pinMode(_transmitPin, OUTPUT);
|
||||
pinMode(_receivePin, INPUT);
|
||||
ArduinoPins::fastWriteDigital(_transmitPin, 0);
|
||||
_lastExecutionTime = micros();
|
||||
DIAG(F("HCSR04 configured on VPIN:%d TXpin:%d RXpin:%d On:%dcm Off:%dcm"),
|
||||
_firstVpin, _transmitPin, _receivePin, _onThreshold, _offThreshold);
|
||||
pinMode(_trigPin, OUTPUT);
|
||||
pinMode(_echoPin, INPUT);
|
||||
ArduinoPins::fastWriteDigital(_trigPin, 0);
|
||||
#if defined(DIAG_IO)
|
||||
_display();
|
||||
#endif
|
||||
}
|
||||
|
||||
// _read function - just return _value (calculated in _loop).
|
||||
@@ -100,13 +106,21 @@ protected:
|
||||
return _value;
|
||||
}
|
||||
|
||||
int _readAnalogue(VPIN vpin) override {
|
||||
(void)vpin; // avoid compiler warning
|
||||
return _distance;
|
||||
}
|
||||
|
||||
// _loop function - read HC-SR04 once every 50 milliseconds.
|
||||
void _loop(unsigned long currentMicros) override {
|
||||
if (currentMicros - _lastExecutionTime > 50000) {
|
||||
_lastExecutionTime = currentMicros;
|
||||
read_HCSR04device();
|
||||
// Delay next loop entry until 50ms have elapsed.
|
||||
delayUntil(currentMicros + 50000UL);
|
||||
}
|
||||
|
||||
_value = read_HCSR04device();
|
||||
}
|
||||
void _display() override {
|
||||
DIAG(F("HCSR04 Configured on Vpin:%d TrigPin:%d EchoPin:%d On:%dcm Off:%dcm"),
|
||||
_firstVpin, _trigPin, _echoPin, _onThreshold, _offThreshold);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -121,51 +135,52 @@ private:
|
||||
// measured distance is less than the onThreshold, and is set to 0 if the measured distance is
|
||||
// greater than the offThreshold.
|
||||
//
|
||||
uint8_t read_HCSR04device() {
|
||||
void read_HCSR04device() {
|
||||
// uint16 enough to time up to 65ms
|
||||
uint16_t startTime, waitTime, currentTime, maxTime;
|
||||
|
||||
// If receive pin is still set on from previous call, abort the read.
|
||||
if (ArduinoPins::fastReadDigital(_receivePin)) return _value;
|
||||
if (ArduinoPins::fastReadDigital(_echoPin))
|
||||
return;
|
||||
|
||||
// Send 10us pulse to trigger transmitter
|
||||
ArduinoPins::fastWriteDigital(_transmitPin, 1);
|
||||
ArduinoPins::fastWriteDigital(_trigPin, 1);
|
||||
delayMicroseconds(10);
|
||||
ArduinoPins::fastWriteDigital(_transmitPin, 0);
|
||||
ArduinoPins::fastWriteDigital(_trigPin, 0);
|
||||
|
||||
// Wait for receive pin to be set
|
||||
startTime = currentTime = micros();
|
||||
maxTime = factor * _offThreshold * 2;
|
||||
while (!ArduinoPins::fastReadDigital(_receivePin)) {
|
||||
while (!ArduinoPins::fastReadDigital(_echoPin)) {
|
||||
// lastTime = currentTime;
|
||||
currentTime = micros();
|
||||
waitTime = currentTime - startTime;
|
||||
if (waitTime > maxTime) {
|
||||
// Timeout waiting for pulse start, abort the read
|
||||
return _value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for receive pin to reset, and measure length of pulse
|
||||
startTime = currentTime = micros();
|
||||
maxTime = factor * _offThreshold;
|
||||
while (ArduinoPins::fastReadDigital(_receivePin)) {
|
||||
while (ArduinoPins::fastReadDigital(_echoPin)) {
|
||||
currentTime = micros();
|
||||
waitTime = currentTime - startTime;
|
||||
// If pulse is too long then set return value to zero,
|
||||
// and finish without waiting for end of pulse.
|
||||
if (waitTime > maxTime) {
|
||||
// Pulse length longer than maxTime, reset value.
|
||||
return 0;
|
||||
_value = 0;
|
||||
_distance = 32767;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Check if pulse length is below threshold, if so set value.
|
||||
//DIAG(F("HCSR04: Pulse Len=%l Distance=%d"), waitTime, distance);
|
||||
uint16_t distance = waitTime / factor; // in centimetres
|
||||
if (distance < _onThreshold)
|
||||
return 1;
|
||||
|
||||
return _value;
|
||||
_distance = waitTime / factor; // in centimetres
|
||||
if (_distance < _onThreshold)
|
||||
_value = 1;
|
||||
}
|
||||
|
||||
};
|
||||
|
@@ -42,14 +42,18 @@ private:
|
||||
I2CManager.write(_I2CAddress, 2, REG_GPIO, _portOutputState);
|
||||
}
|
||||
void _writePullups() override {
|
||||
I2CManager.write(_I2CAddress, 2, REG_GPPU, _portPullup);
|
||||
// Set pullups only for in-use pins. This prevents pullup being set for a pin that
|
||||
// is intended for use as an output but hasn't been written to yet.
|
||||
I2CManager.write(_I2CAddress, 2, REG_GPPU, _portPullup & _portInUse);
|
||||
}
|
||||
void _writePortModes() override {
|
||||
// Each bit is 1 for an input, 0 for an output, i.e. inverted.
|
||||
I2CManager.write(_I2CAddress, 2, REG_IODIR, ~_portMode);
|
||||
// Enable interrupt-on-change for pins that are inputs (_portMode=0)
|
||||
// Write 0 to IODIR for in-use pins that are outputs, 1 for others.
|
||||
uint8_t temp = ~(_portMode & _portInUse);
|
||||
I2CManager.write(_I2CAddress, 2, REG_IODIR, temp);
|
||||
// Enable interrupt-on-change for in-use pins that are inputs (_portMode=0)
|
||||
temp = ~_portMode & _portInUse;
|
||||
I2CManager.write(_I2CAddress, 2, REG_INTCON, 0x00);
|
||||
I2CManager.write(_I2CAddress, 2, REG_GPINTEN, ~_portMode);
|
||||
I2CManager.write(_I2CAddress, 2, REG_GPINTEN, temp);
|
||||
}
|
||||
void _readGpioPort(bool immediate) override {
|
||||
if (immediate) {
|
||||
|
@@ -48,14 +48,19 @@ private:
|
||||
I2CManager.write(_I2CAddress, 3, REG_GPIOA, _portOutputState, _portOutputState>>8);
|
||||
}
|
||||
void _writePullups() override {
|
||||
I2CManager.write(_I2CAddress, 3, REG_GPPUA, _portPullup, _portPullup>>8);
|
||||
// Set pullups only for in-use pins. This prevents pullup being set for a pin that
|
||||
// is intended for use as an output but hasn't been written to yet.
|
||||
uint16_t temp = _portPullup & _portInUse;
|
||||
I2CManager.write(_I2CAddress, 3, REG_GPPUA, temp, temp>>8);
|
||||
}
|
||||
void _writePortModes() override {
|
||||
// Write 1 to IODIR for pins that are inputs, 0 for outputs (i.e. _portMode inverted)
|
||||
I2CManager.write(_I2CAddress, 3, REG_IODIRA, ~_portMode, (~_portMode)>>8);
|
||||
// Enable interrupt for those pins which are inputs (_portMode=0)
|
||||
// Write 0 to IODIR for in-use pins that are outputs, 1 for others.
|
||||
uint16_t temp = ~(_portMode & _portInUse);
|
||||
I2CManager.write(_I2CAddress, 3, REG_IODIRA, temp, temp>>8);
|
||||
// Enable interrupt for in-use pins which are inputs (_portMode=0)
|
||||
temp = ~_portMode & _portInUse;
|
||||
I2CManager.write(_I2CAddress, 3, REG_INTCONA, 0x00, 0x00);
|
||||
I2CManager.write(_I2CAddress, 3, REG_GPINTENA, ~_portMode, (~_portMode)>>8);
|
||||
I2CManager.write(_I2CAddress, 3, REG_GPINTENA, temp, temp>>8);
|
||||
}
|
||||
void _readGpioPort(bool immediate) override {
|
||||
if (immediate) {
|
||||
|
166
IO_PCA9685.cpp
166
IO_PCA9685.cpp
@@ -45,28 +45,30 @@ void PCA9685::create(VPIN firstVpin, int nPins, uint8_t I2CAddress) {
|
||||
// Configure a port on the PCA9685.
|
||||
bool PCA9685::_configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) {
|
||||
if (configType != CONFIGURE_SERVO) return false;
|
||||
if (paramCount != 4) return false;
|
||||
if (paramCount != 5) return false;
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("PCA9685 Configure VPIN:%d Apos:%d Ipos:%d Profile:%d state:%d"),
|
||||
vpin, params[0], params[1], params[2], params[3]);
|
||||
DIAG(F("PCA9685 Configure VPIN:%d Apos:%d Ipos:%d Profile:%d Duration:%d state:%d"),
|
||||
vpin, params[0], params[1], params[2], params[3], params[4]);
|
||||
#endif
|
||||
|
||||
int8_t pin = vpin - _firstVpin;
|
||||
struct ServoData *s = _servoData[pin];
|
||||
if (!s) {
|
||||
if (s == NULL) {
|
||||
_servoData[pin] = (struct ServoData *)calloc(1, sizeof(struct ServoData));
|
||||
s = _servoData[pin];
|
||||
if (!s) return false; // Check for failed memory allocation
|
||||
}
|
||||
|
||||
s->activePosition = params[0];
|
||||
s->currentPosition = s->inactivePosition = params[1];
|
||||
s->inactivePosition = params[1];
|
||||
s->profile = params[2];
|
||||
s->duration = params[3];
|
||||
int state = params[4];
|
||||
|
||||
// Position servo to initial state
|
||||
s->state = -1; // Set unknown state, to force reposition
|
||||
_write(vpin, params[3]);
|
||||
|
||||
if (state != -1) {
|
||||
// Position servo to initial state
|
||||
_writeAnalogue(vpin, state ? s->activePosition : s->inactivePosition, 0, 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -75,6 +77,8 @@ PCA9685::PCA9685(VPIN firstVpin, int nPins, uint8_t I2CAddress) {
|
||||
_firstVpin = firstVpin;
|
||||
_nPins = min(nPins, 16);
|
||||
_I2CAddress = I2CAddress;
|
||||
// To save RAM, space for servo configuration is not allocated unless a pin is used.
|
||||
// Initialise the pointers to NULL.
|
||||
for (int i=0; i<_nPins; i++)
|
||||
_servoData[i] = NULL;
|
||||
|
||||
@@ -93,7 +97,6 @@ void PCA9685::_begin() {
|
||||
|
||||
// Initialise I/O module here.
|
||||
if (I2CManager.exists(_I2CAddress)) {
|
||||
DIAG(F("PCA9685 I2C:%x configured Vpins:%d-%d"), _I2CAddress, _firstVpin, _firstVpin+_nPins-1);
|
||||
writeRegister(_I2CAddress, PCA9685_MODE1, MODE1_SLEEP | MODE1_AI);
|
||||
writeRegister(_I2CAddress, PCA9685_PRESCALE, PRESCALE_50HZ); // 50Hz clock, 20ms pulse period.
|
||||
writeRegister(_I2CAddress, PCA9685_MODE1, MODE1_AI);
|
||||
@@ -101,11 +104,17 @@ void PCA9685::_begin() {
|
||||
// In theory, we should wait 500us before sending any other commands to each device, to allow
|
||||
// the PWM oscillator to get running. However, we don't do any specific wait, as there's
|
||||
// plenty of other stuff to do before we will send a command.
|
||||
}
|
||||
#if defined(DIAG_IO)
|
||||
_display();
|
||||
#endif
|
||||
} else
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
}
|
||||
|
||||
// Device-specific write function, invoked from IODevice::write().
|
||||
// Device-specific write function, invoked from IODevice::write().
|
||||
// For this function, the configured profile is used.
|
||||
void PCA9685::_write(VPIN vpin, int value) {
|
||||
if (_deviceState == DEVSTATE_FAILED) return;
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("PCA9685 Write Vpin:%d Value:%d"), vpin, value);
|
||||
#endif
|
||||
@@ -113,97 +122,93 @@ void PCA9685::_write(VPIN vpin, int value) {
|
||||
if (value) value = 1;
|
||||
|
||||
struct ServoData *s = _servoData[pin];
|
||||
if (!s) {
|
||||
// Pin not configured, just write default positions to servo controller
|
||||
if (value)
|
||||
writeDevice(pin, _defaultActivePosition);
|
||||
else
|
||||
writeDevice(pin, _defaultInactivePosition);
|
||||
} else {
|
||||
// Use configured parameters for advanced transitions
|
||||
uint8_t profile = s->profile;
|
||||
// If current position not known, go straight to selected position.
|
||||
if (s->state == -1) profile = Instant;
|
||||
|
||||
// Animated profile. Initiate the appropriate action.
|
||||
s->numSteps = profile==Fast ? 10 :
|
||||
profile==Medium ? 20 :
|
||||
profile==Slow ? 40 :
|
||||
profile==Bounce ? sizeof(_bounceProfile)-1 :
|
||||
1;
|
||||
s->state = value;
|
||||
s->stepNumber = 0;
|
||||
|
||||
// Update new from/to positions to initiate or change animation.
|
||||
s->fromPosition = s->currentPosition;
|
||||
s->toPosition = s->state ? s->activePosition : s->inactivePosition;
|
||||
}
|
||||
if (s != NULL) {
|
||||
// Use configured parameters
|
||||
_writeAnalogue(vpin, value ? s->activePosition : s->inactivePosition, s->profile, s->duration);
|
||||
} // else { /* ignorethe request */ }
|
||||
}
|
||||
|
||||
// Device-specific writeAnalogue function, invoked from IODevice::writeAnalogue().
|
||||
void PCA9685::_writeAnalogue(VPIN vpin, int value, int profile) {
|
||||
// Profile is as follows:
|
||||
// Bit 7: 0=Set PWM to 0% to power off servo motor when finished
|
||||
// 1=Keep PWM pulses on (better when using PWM to drive an LED)
|
||||
// Bits 6-0: 0 Use specified duration (defaults to 0 deciseconds)
|
||||
// 1 (Fast) Move servo in 0.5 seconds
|
||||
// 2 (Medium) Move servo in 1.0 seconds
|
||||
// 3 (Slow) Move servo in 2.0 seconds
|
||||
// 4 (Bounce) Servo 'bounces' at extremes.
|
||||
//
|
||||
void PCA9685::_writeAnalogue(VPIN vpin, int value, uint8_t profile, uint16_t duration) {
|
||||
if (_deviceState == DEVSTATE_FAILED) return;
|
||||
#ifdef DIAG_IO
|
||||
DIAG(F("PCA9685 WriteAnalogue Vpin:%d Value:%d Profile:%d"), vpin, value, profile);
|
||||
DIAG(F("PCA9685 WriteAnalogue Vpin:%d Value:%d Profile:%d Duration:%d"),
|
||||
vpin, value, profile, duration);
|
||||
#endif
|
||||
int pin = vpin - _firstVpin;
|
||||
if (value > 4095) value = 4095;
|
||||
else if (value < 0) value = 0;
|
||||
|
||||
struct ServoData *s = _servoData[pin];
|
||||
|
||||
if (!s) {
|
||||
// Servo pin not configured, so configure now.
|
||||
if (s == NULL) {
|
||||
// Servo pin not configured, so configure now using defaults
|
||||
s = _servoData[pin] = (struct ServoData *) calloc(sizeof(struct ServoData), 1);
|
||||
s->activePosition = _defaultActivePosition;
|
||||
s->inactivePosition = _defaultInactivePosition;
|
||||
s->currentPosition = value; // Don't know where we're moving from.
|
||||
if (s == NULL) return; // Check for memory allocation failure
|
||||
s->activePosition = 0;
|
||||
s->inactivePosition = 0;
|
||||
s->currentPosition = value;
|
||||
s->profile = Instant; // Use instant profile (but not this time)
|
||||
}
|
||||
s->profile = profile;
|
||||
|
||||
// Animated profile. Initiate the appropriate action.
|
||||
s->numSteps = profile==Fast ? 10 :
|
||||
profile==Medium ? 20 :
|
||||
profile==Slow ? 40 :
|
||||
profile==Bounce ? sizeof(_bounceProfile)-1 :
|
||||
1;
|
||||
s->currentProfile = profile;
|
||||
uint8_t profileValue = profile & ~NoPowerOff; // Mask off 'don't-power-off' bit.
|
||||
s->numSteps = profileValue==Fast ? 10 : // 0.5 seconds
|
||||
profileValue==Medium ? 20 : // 1.0 seconds
|
||||
profileValue==Slow ? 40 : // 2.0 seconds
|
||||
profileValue==Bounce ? sizeof(_bounceProfile)-1 : // ~ 1.5 seconds
|
||||
duration * 2 + 1; // Convert from deciseconds (100ms) to refresh cycles (50ms)
|
||||
s->stepNumber = 0;
|
||||
s->toPosition = value;
|
||||
s->fromPosition = s->currentPosition;
|
||||
}
|
||||
|
||||
// _isActive returns true if the device is currently in executing an animation,
|
||||
// _read returns true if the device is currently in executing an animation,
|
||||
// changing the output over a period of time.
|
||||
bool PCA9685::_isActive(VPIN vpin) {
|
||||
int PCA9685::_read(VPIN vpin) {
|
||||
if (_deviceState == DEVSTATE_FAILED) return 0;
|
||||
int pin = vpin - _firstVpin;
|
||||
struct ServoData *s = _servoData[pin];
|
||||
if (!s)
|
||||
if (s == NULL)
|
||||
return false; // No structure means no animation!
|
||||
else
|
||||
return (s->numSteps != 0);
|
||||
return (s->stepNumber < s->numSteps);
|
||||
}
|
||||
|
||||
void PCA9685::_loop(unsigned long currentMicros) {
|
||||
if (currentMicros - _lastRefreshTime >= refreshInterval * 1000) {
|
||||
for (int pin=0; pin<_nPins; pin++) {
|
||||
updatePosition(pin);
|
||||
}
|
||||
_lastRefreshTime = currentMicros;
|
||||
for (int pin=0; pin<_nPins; pin++) {
|
||||
updatePosition(pin);
|
||||
}
|
||||
delayUntil(currentMicros + refreshInterval * 1000UL);
|
||||
}
|
||||
|
||||
// Private function to reposition servo
|
||||
// TODO: Could calculate step number from elapsed time, to allow for erratic loop timing.
|
||||
void PCA9685::updatePosition(uint8_t pin) {
|
||||
struct ServoData *s = _servoData[pin];
|
||||
if (!s) return;
|
||||
|
||||
if (s == NULL) return; // No pin configuration/state data
|
||||
|
||||
if (s->numSteps == 0) return; // No animation in progress
|
||||
|
||||
if (s->stepNumber == 0 && s->fromPosition == s->toPosition) {
|
||||
// No movement required, so go straight to final step
|
||||
s->stepNumber = s->numSteps;
|
||||
// Go straight to end of sequence, output final position.
|
||||
s->stepNumber = s->numSteps-1;
|
||||
}
|
||||
|
||||
if (s->stepNumber < s->numSteps) {
|
||||
// Animation in progress, reposition servo
|
||||
s->stepNumber++;
|
||||
if (s->profile == Bounce) {
|
||||
if ((s->currentProfile & ~NoPowerOff) == Bounce) {
|
||||
// Retrieve step positions from array in flash
|
||||
byte profileValue = GETFLASH(&_bounceProfile[s->stepNumber]);
|
||||
s->currentPosition = map(profileValue, 0, 100, s->fromPosition, s->toPosition);
|
||||
@@ -217,10 +222,12 @@ void PCA9685::updatePosition(uint8_t pin) {
|
||||
// We've finished animation, wait a little to allow servo to catch up
|
||||
s->stepNumber++;
|
||||
} else if (s->stepNumber == s->numSteps + _catchupSteps
|
||||
&& s->currentPosition != 4095 && s->currentPosition != 0) {
|
||||
&& s->currentPosition != 0) {
|
||||
#ifdef IO_SWITCH_OFF_SERVO
|
||||
// Wait has finished, so switch off PWM to prevent annoying servo buzz
|
||||
writeDevice(pin, 0);
|
||||
if ((s->currentProfile & NoPowerOff) == 0) {
|
||||
// Wait has finished, so switch off PWM to prevent annoying servo buzz
|
||||
writeDevice(pin, 0);
|
||||
}
|
||||
#endif
|
||||
s->numSteps = 0; // Done now.
|
||||
}
|
||||
@@ -233,20 +240,25 @@ void PCA9685::writeDevice(uint8_t pin, int value) {
|
||||
DIAG(F("PCA9685 I2C:x%x WriteDevice Pin:%d Value:%d"), _I2CAddress, pin, value);
|
||||
#endif
|
||||
// Wait for previous request to complete
|
||||
requestBlock.wait();
|
||||
// Set up new request.
|
||||
outputBuffer[0] = PCA9685_FIRST_SERVO + 4 * pin;
|
||||
outputBuffer[1] = 0;
|
||||
outputBuffer[2] = (value == 4095 ? 0x10 : 0); // 4095=full on
|
||||
outputBuffer[3] = value & 0xff;
|
||||
outputBuffer[4] = value >> 8;
|
||||
I2CManager.queueRequest(&requestBlock);
|
||||
uint8_t status = requestBlock.wait();
|
||||
if (status != I2C_STATUS_OK) {
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
DIAG(F("PCA9685 I2C:x%x failed %S"), _I2CAddress, I2CManager.getErrorMessage(status));
|
||||
} else {
|
||||
// Set up new request.
|
||||
outputBuffer[0] = PCA9685_FIRST_SERVO + 4 * pin;
|
||||
outputBuffer[1] = 0;
|
||||
outputBuffer[2] = (value == 4095 ? 0x10 : 0); // 4095=full on
|
||||
outputBuffer[3] = value & 0xff;
|
||||
outputBuffer[4] = value >> 8;
|
||||
I2CManager.queueRequest(&requestBlock);
|
||||
}
|
||||
}
|
||||
|
||||
// Display details of this device.
|
||||
void PCA9685::_display() {
|
||||
DIAG(F("PCA9685 I2C:x%x Vpins:%d-%d"), _I2CAddress, (int)_firstVpin,
|
||||
(int)_firstVpin+_nPins-1);
|
||||
DIAG(F("PCA9685 I2C:x%x Configured on Vpins:%d-%d %S"), _I2CAddress, (int)_firstVpin,
|
||||
(int)_firstVpin+_nPins-1, (_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F(""));
|
||||
}
|
||||
|
||||
// Internal helper function for this device
|
||||
|
24
IO_PCF8574.h
24
IO_PCF8574.h
@@ -17,6 +17,24 @@
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The PCF8574 is a simple device; it only has one register. The device
|
||||
* input/output mode and pullup are configured through this, and the
|
||||
* output state is written and the input state read through it too.
|
||||
*
|
||||
* This is accomplished by having a weak resistor in series with the output,
|
||||
* and a read-back of the other end of the resistor. As an output, the
|
||||
* pin state is set to 1 or 0, and the output voltage goes to +5V or 0V
|
||||
* (through the weak resistor).
|
||||
*
|
||||
* In order to use the pin as an input, the output is written as
|
||||
* a '1' in order to pull up the resistor. Therefore the input will be
|
||||
* 1 unless the pin is pulled down externally, in which case it will be 0.
|
||||
*
|
||||
* As a consequence of this approach, it is not possible to use the device for
|
||||
* inputs without pullups.
|
||||
*/
|
||||
|
||||
#ifndef IO_PCF8574_H
|
||||
#define IO_PCF8574_H
|
||||
|
||||
@@ -57,7 +75,7 @@ private:
|
||||
if (immediate) {
|
||||
uint8_t buffer[1];
|
||||
I2CManager.read(_I2CAddress, buffer, 1);
|
||||
_portInputState = ((uint16_t)buffer) & 0xff;
|
||||
_portInputState = buffer[0];
|
||||
} else {
|
||||
requestBlock.wait(); // Wait for preceding operation to complete
|
||||
// Issue new request to read GPIO register
|
||||
@@ -68,9 +86,9 @@ private:
|
||||
// This function is invoked when an I/O operation on the requestBlock completes.
|
||||
void _processCompletion(uint8_t status) override {
|
||||
if (status == I2C_STATUS_OK)
|
||||
_portInputState = ((uint16_t)inputBuffer[0]) & 0xff;
|
||||
_portInputState = inputBuffer[0];
|
||||
else
|
||||
_portInputState = 0xff;
|
||||
_portInputState = 0xff;
|
||||
}
|
||||
|
||||
// Set up device ports
|
||||
|
299
IO_VL53L0X.h
Normal file
299
IO_VL53L0X.h
Normal file
@@ -0,0 +1,299 @@
|
||||
/*
|
||||
* © 2021, Neil McKechnie. All rights reserved.
|
||||
*
|
||||
* This file is part of DCC++EX API
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The VL53L0X Time-Of-Flight sensor operates by sending a short laser pulse and detecting
|
||||
* the reflection of the pulse. The time between the pulse and the receipt of reflections
|
||||
* is measured and used to determine the distance to the reflecting object.
|
||||
*
|
||||
* For economy of memory and processing time, this driver includes only part of the code
|
||||
* that ST provide in their API. Also, the API code isn't very clear and it is not easy
|
||||
* to identify what operations are useful and what are not.
|
||||
* The operation shown here doesn't include any calibration, so is probably not as accurate
|
||||
* as using the full driver, but it's probably accurate enough for the purpose.
|
||||
*
|
||||
* The device driver allocates up to 3 vpins to the device. A digital read on the first pin
|
||||
* will return a value that indicates whether the object is within the threshold range (1)
|
||||
* or not (0). An analogue read on the first pin returns the last measured distance (in mm),
|
||||
* the second pin returns the signal strength, and the third pin returns detected
|
||||
* ambient light level. By default the device takes around 60ms to complete a ranging
|
||||
* operation, so we do a 100ms cycle (10 samples per second).
|
||||
*
|
||||
* The VL53L0X is initially set to respond to I2C address 0x29. If you only have one module,
|
||||
* you can use this address. However, the address can be modified by software. If
|
||||
* you select another address, that address will be written to the device and used until the device is reset.
|
||||
*
|
||||
* If you have more than one module, then you will need to specify a digital VPIN (Arduino
|
||||
* digital output or I/O extender pin) which you connect to the module's XSHUT pin. Now,
|
||||
* when the device driver starts, the XSHUT pin is set LOW to turn the module off. Once
|
||||
* all VL53L0X modules are turned off, the driver works through each module in turn by
|
||||
* setting XSHUT to HIGH to turn the module on,, then writing the module's desired I2C address.
|
||||
* In this way, many VL53L0X modules can be connected to the one I2C bus, each one
|
||||
* using a distinct I2C address.
|
||||
*
|
||||
* WARNING: If the device's XSHUT pin is not connected, then it is very prone to noise,
|
||||
* and the device may even reset when handled. If you're not using XSHUT, then it's
|
||||
* best to tie it to +5V.
|
||||
*
|
||||
* The driver is configured as follows:
|
||||
*
|
||||
* Single VL53L0X module:
|
||||
* VL53L0X::create(firstVpin, nPins, i2cAddress, lowThreshold, highThreshold);
|
||||
* Where firstVpin is the first vpin reserved for reading the device,
|
||||
* nPins is 1, 2 or 3,
|
||||
* i2cAddress is the address of the device (normally 0x29),
|
||||
* lowThreshold is the distance at which the digital vpin state is set to 1 (in mm),
|
||||
* and highThreshold is the distance at which the digital vpin state is set to 0 (in mm).
|
||||
*
|
||||
* Multiple VL53L0X modules:
|
||||
* VL53L0X::create(firstVpin, nPins, i2cAddress, lowThreshold, highThreshold, xshutPin);
|
||||
* ...
|
||||
* Where firstVpin is the first vpin reserved for reading the device,
|
||||
* nPins is 1, 2 or 3,
|
||||
* i2cAddress is the address of the device (any valid address except 0x29),
|
||||
* lowThreshold is the distance at which the digital vpin state is set to 1 (in mm),
|
||||
* highThreshold is the distance at which the digital vpin state is set to 0 (in mm),
|
||||
* and xshutPin is the VPIN number corresponding to a digital output that is connected to the
|
||||
* XSHUT terminal on the module.
|
||||
*
|
||||
* Example:
|
||||
* In mySetup function within mySetup.cpp:
|
||||
* VL53L0X::create(4000, 3, 0x29, 200, 250);
|
||||
* Sensor::create(4000, 4000, 0); // Create a sensor
|
||||
*
|
||||
* When an object comes within 200mm of the sensor, a message
|
||||
* <Q 4000>
|
||||
* will be sent over the serial USB, and when the object moves more than 250mm from the sensor,
|
||||
* a message
|
||||
* <q 4000>
|
||||
* will be sent.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef IO_VL53L0X_h
|
||||
#define IO_VL53L0X_h
|
||||
|
||||
#include "IODevice.h"
|
||||
|
||||
class VL53L0X : public IODevice {
|
||||
private:
|
||||
uint8_t _i2cAddress;
|
||||
uint16_t _ambient;
|
||||
uint16_t _distance;
|
||||
uint16_t _signal;
|
||||
uint16_t _onThreshold;
|
||||
uint16_t _offThreshold;
|
||||
VPIN _xshutPin;
|
||||
bool _value;
|
||||
uint8_t _nextState = 0;
|
||||
I2CRB _rb;
|
||||
uint8_t _inBuffer[12];
|
||||
uint8_t _outBuffer[2];
|
||||
// State machine states.
|
||||
enum : uint8_t {
|
||||
STATE_INIT = 0,
|
||||
STATE_CONFIGUREADDRESS = 1,
|
||||
STATE_SKIP = 2,
|
||||
STATE_CONFIGUREDEVICE = 3,
|
||||
STATE_INITIATESCAN = 4,
|
||||
STATE_CHECKSTATUS = 5,
|
||||
STATE_GETRESULTS = 6,
|
||||
STATE_DECODERESULTS = 7,
|
||||
};
|
||||
|
||||
// Register addresses
|
||||
enum : uint8_t {
|
||||
VL53L0X_REG_SYSRANGE_START=0x00,
|
||||
VL53L0X_REG_RESULT_INTERRUPT_STATUS=0x13,
|
||||
VL53L0X_REG_RESULT_RANGE_STATUS=0x14,
|
||||
VL53L0X_CONFIG_PAD_SCL_SDA__EXTSUP_HV=0x89,
|
||||
VL53L0X_REG_I2C_SLAVE_DEVICE_ADDRESS=0x8A,
|
||||
};
|
||||
const uint8_t VL53L0X_I2C_DEFAULT_ADDRESS=0x29;
|
||||
|
||||
public:
|
||||
VL53L0X(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint16_t onThreshold, uint16_t offThreshold, VPIN xshutPin = VPIN_NONE) {
|
||||
_firstVpin = firstVpin;
|
||||
_nPins = min(nPins, 3);
|
||||
_i2cAddress = i2cAddress;
|
||||
_onThreshold = onThreshold;
|
||||
_offThreshold = offThreshold;
|
||||
_xshutPin = xshutPin;
|
||||
_value = 0;
|
||||
addDevice(this);
|
||||
}
|
||||
static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress, uint16_t onThreshold, uint16_t offThreshold, VPIN xshutPin = VPIN_NONE) {
|
||||
new VL53L0X(firstVpin, nPins, i2cAddress, onThreshold, offThreshold, xshutPin);
|
||||
}
|
||||
|
||||
protected:
|
||||
void _begin() override {
|
||||
if (_xshutPin == VPIN_NONE) {
|
||||
// Check if device is already responding on the nominated address.
|
||||
if (I2CManager.exists(_i2cAddress)) {
|
||||
// Yes, it's already on this address, so skip the address initialisation.
|
||||
_nextState = STATE_CONFIGUREDEVICE;
|
||||
} else {
|
||||
_nextState = STATE_INIT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _loop(unsigned long currentMicros) override {
|
||||
uint8_t status;
|
||||
switch (_nextState) {
|
||||
case STATE_INIT:
|
||||
// On first entry to loop, reset this module by pulling XSHUT low. All modules
|
||||
// will be reset in turn.
|
||||
if (_xshutPin != VPIN_NONE) IODevice::write(_xshutPin, 0);
|
||||
_nextState = STATE_CONFIGUREADDRESS;
|
||||
break;
|
||||
case STATE_CONFIGUREADDRESS:
|
||||
// On second entry, set XSHUT pin high to allow the module to restart.
|
||||
// On the module, there is a diode in series with the XSHUT pin to
|
||||
// protect the low-voltage pin against +5V.
|
||||
if (_xshutPin != VPIN_NONE) IODevice::write(_xshutPin, 1);
|
||||
// Allow the module time to restart
|
||||
delay(10);
|
||||
// Then write the desired I2C address to the device, while this is the only
|
||||
// module responding to the default address.
|
||||
I2CManager.write(VL53L0X_I2C_DEFAULT_ADDRESS, 2, VL53L0X_REG_I2C_SLAVE_DEVICE_ADDRESS, _i2cAddress);
|
||||
_nextState = STATE_SKIP;
|
||||
break;
|
||||
case STATE_SKIP:
|
||||
// Do nothing on the third entry.
|
||||
_nextState = STATE_CONFIGUREDEVICE;
|
||||
break;
|
||||
case STATE_CONFIGUREDEVICE:
|
||||
// On next entry, check if device address has been set.
|
||||
if (I2CManager.exists(_i2cAddress)) {
|
||||
#ifdef DIAG_IO
|
||||
_display();
|
||||
#endif
|
||||
// Set 2.8V mode
|
||||
write_reg(VL53L0X_CONFIG_PAD_SCL_SDA__EXTSUP_HV,
|
||||
read_reg(VL53L0X_CONFIG_PAD_SCL_SDA__EXTSUP_HV) | 0x01);
|
||||
} else {
|
||||
DIAG(F("VL53L0X I2C:x%x device not responding"), _i2cAddress);
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
}
|
||||
_nextState = STATE_INITIATESCAN;
|
||||
break;
|
||||
case STATE_INITIATESCAN:
|
||||
// Not scanning, so initiate a scan
|
||||
_outBuffer[0] = VL53L0X_REG_SYSRANGE_START;
|
||||
_outBuffer[1] = 0x01;
|
||||
I2CManager.write(_i2cAddress, _outBuffer, 2, &_rb);
|
||||
_nextState = STATE_CHECKSTATUS;
|
||||
break;
|
||||
case STATE_CHECKSTATUS:
|
||||
status = _rb.status;
|
||||
if (status == I2C_STATUS_PENDING) return; // try next time
|
||||
if (status != I2C_STATUS_OK) {
|
||||
DIAG(F("VL53L0X I2C:x%x Error:%d %S"), _i2cAddress, status, I2CManager.getErrorMessage(status));
|
||||
_deviceState = DEVSTATE_FAILED;
|
||||
_value = false;
|
||||
} else
|
||||
_nextState = 2;
|
||||
delayUntil(currentMicros + 95000); // wait for 95 ms before checking.
|
||||
_nextState = STATE_GETRESULTS;
|
||||
break;
|
||||
case STATE_GETRESULTS:
|
||||
// Ranging completed. Request results
|
||||
_outBuffer[0] = VL53L0X_REG_RESULT_RANGE_STATUS;
|
||||
I2CManager.read(_i2cAddress, _inBuffer, 12, _outBuffer, 1, &_rb);
|
||||
_nextState = 3;
|
||||
delayUntil(currentMicros + 5000); // Allow 5ms to get data
|
||||
_nextState = STATE_DECODERESULTS;
|
||||
break;
|
||||
case STATE_DECODERESULTS:
|
||||
// If I2C write still busy, return.
|
||||
status = _rb.status;
|
||||
if (status == I2C_STATUS_PENDING) return; // try again next time
|
||||
if (status == I2C_STATUS_OK) {
|
||||
if (!(_inBuffer[0] & 1)) return; // device still busy
|
||||
uint8_t deviceRangeStatus = ((_inBuffer[0] & 0x78) >> 3);
|
||||
if (deviceRangeStatus == 0x0b) {
|
||||
// Range status OK, so use data
|
||||
_ambient = makeuint16(_inBuffer[7], _inBuffer[6]);
|
||||
_signal = makeuint16(_inBuffer[9], _inBuffer[8]);
|
||||
_distance = makeuint16(_inBuffer[11], _inBuffer[10]);
|
||||
if (_distance <= _onThreshold)
|
||||
_value = true;
|
||||
else if (_distance > _offThreshold)
|
||||
_value = false;
|
||||
}
|
||||
}
|
||||
// Completed. Restart scan on next loop entry.
|
||||
_nextState = STATE_INITIATESCAN;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// For analogue read, first pin returns distance, second pin is signal strength, and third is ambient level.
|
||||
int _readAnalogue(VPIN vpin) override {
|
||||
int pin = vpin - _firstVpin;
|
||||
switch (pin) {
|
||||
case 0:
|
||||
return _distance;
|
||||
case 1:
|
||||
return _signal;
|
||||
case 2:
|
||||
return _ambient;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// For digital read, return zero for all but first pin.
|
||||
int _read(VPIN vpin) override {
|
||||
if (vpin == _firstVpin)
|
||||
return _value;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _display() override {
|
||||
DIAG(F("VL53L0X I2C:x%x Configured on Vpins:%d-%d On:%dmm Off:%dmm %S"),
|
||||
_i2cAddress, _firstVpin, _firstVpin+_nPins-1, _onThreshold, _offThreshold,
|
||||
(_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F(""));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
inline uint16_t makeuint16(byte lsb, byte msb) {
|
||||
return (((uint16_t)msb) << 8) | lsb;
|
||||
}
|
||||
uint8_t write_reg(uint8_t reg, uint8_t data) {
|
||||
// write byte to register
|
||||
uint8_t outBuffer[2];
|
||||
outBuffer[0] = reg;
|
||||
outBuffer[1] = data;
|
||||
return I2CManager.write(_i2cAddress, outBuffer, 2);
|
||||
}
|
||||
uint8_t read_reg(uint8_t reg) {
|
||||
// read byte from register and return value
|
||||
I2CManager.read(_i2cAddress, _inBuffer, 1, ®, 1);
|
||||
return _inBuffer[0];
|
||||
}
|
||||
};
|
||||
|
||||
#endif // IO_VL53L0X_h
|
@@ -19,15 +19,12 @@
|
||||
#ifndef LCDDisplay_h
|
||||
#define LCDDisplay_h
|
||||
#include <Arduino.h>
|
||||
#include "defines.h"
|
||||
#include "DisplayInterface.h"
|
||||
|
||||
#if __has_include ( "config.h")
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
// Allow maximum message length to be overridden from config.h
|
||||
#if !defined(MAX_MSG_SIZE)
|
||||
#define MAX_MSG_SIZE 16
|
||||
#define MAX_MSG_SIZE 20
|
||||
#endif
|
||||
|
||||
// Set default scroll mode (overridable in config.h)
|
||||
|
3
LCN.cpp
3
LCN.cpp
@@ -48,8 +48,7 @@ void LCN::loop() {
|
||||
}
|
||||
else if (ch == 't' || ch == 'T') { // Turnout opcodes
|
||||
if (Diag::LCN) DIAG(F("LCN IN %d%c"),id,(char)ch);
|
||||
Turnout * tt = Turnout::get(id);
|
||||
if (!tt) tt=Turnout::createLCN(id);
|
||||
if (!Turnout::exists(id)) LCNTurnout::create(id);
|
||||
Turnout::setClosedStateOnly(id,ch=='t');
|
||||
Turnout::turnoutlistHash++; // signals ED update of turnout data
|
||||
id = 0;
|
||||
|
4
LCN.h
4
LCN.h
@@ -1,5 +1,7 @@
|
||||
/*
|
||||
* (c) 2021 Fred Decker. All rights reserved.
|
||||
* © 2021 Harald Barth
|
||||
* © 2021 Fred Decker
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
|
@@ -196,7 +196,7 @@ void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) {
|
||||
outputBuffer[len++] = highnib;
|
||||
outputBuffer[len++] = lownib|En;
|
||||
outputBuffer[len++] = lownib;
|
||||
I2CManager.write(_Addr, outputBuffer, len, &requestBlock);
|
||||
I2CManager.write(_Addr, outputBuffer, len); // Write command synchronously
|
||||
}
|
||||
|
||||
// write 4 data bits to the HD44780 LCD controller.
|
||||
@@ -205,19 +205,15 @@ void LiquidCrystal_I2C::write4bits(uint8_t value) {
|
||||
// Enable must be set/reset for at least 450ns. This is well within the
|
||||
// I2C clock cycle time of 2.5us at 400kHz. Data is clocked in to the
|
||||
// HD44780 on the trailing edge of the Enable pin.
|
||||
// Wait for previous request to complete before writing to outputbuffer.
|
||||
requestBlock.wait();
|
||||
uint8_t len = 0;
|
||||
outputBuffer[len++] = _data|En;
|
||||
outputBuffer[len++] = _data;
|
||||
I2CManager.write(_Addr, outputBuffer, len, &requestBlock);
|
||||
I2CManager.write(_Addr, outputBuffer, len); // Write command synchronously
|
||||
}
|
||||
|
||||
// write a byte to the PCF8574 I2C interface. We don't need to set
|
||||
// the enable pin for this.
|
||||
void LiquidCrystal_I2C::expanderWrite(uint8_t value) {
|
||||
// Wait for previous request to complete before writing to outputbuffer.
|
||||
requestBlock.wait();
|
||||
outputBuffer[0] = value | _backlightval;
|
||||
I2CManager.write(_Addr, outputBuffer, 1, &requestBlock);
|
||||
I2CManager.write(_Addr, outputBuffer, 1); // Write command synchronously
|
||||
}
|
@@ -75,10 +75,8 @@ public:
|
||||
void backlight();
|
||||
|
||||
void command(uint8_t);
|
||||
void init();
|
||||
|
||||
private:
|
||||
void init_priv();
|
||||
void send(uint8_t, uint8_t);
|
||||
void write4bits(uint8_t);
|
||||
void expanderWrite(uint8_t);
|
||||
@@ -88,9 +86,9 @@ private:
|
||||
uint8_t _displaymode;
|
||||
uint8_t _backlightval;
|
||||
|
||||
I2CRB requestBlock;
|
||||
uint8_t outputBuffer[4];
|
||||
bool isBusy() { return requestBlock.isBusy(); }
|
||||
// I/O is synchronous, so if this is called we're not busy!
|
||||
bool isBusy() override { return false; }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -1,7 +1,11 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2022 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -76,7 +80,7 @@ MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8
|
||||
if (currentPin==UNUSED_PIN)
|
||||
DIAG(F("MotorDriver ** WARNING ** No current or short detection"));
|
||||
else
|
||||
DIAG(F("MotorDriver currentPin=A%d, senseOffset=%d, rawCurentTripValue(relative to offset)=%d"),
|
||||
DIAG(F("MotorDriver currentPin=A%d, senseOffset=%d, rawCurrentTripValue(relative to offset)=%d"),
|
||||
currentPin-A0, senseOffset,rawCurrentTripValue);
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2020 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
|
@@ -1,4 +1,6 @@
|
||||
/*
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2022 Harald Barth
|
||||
* (c) 2020 Chris Harlow. All rights reserved.
|
||||
* (c) 2021 Fred Decker. All rights reserved.
|
||||
* (c) 2020 Harald Barth. All rights reserved.
|
||||
@@ -82,5 +84,20 @@
|
||||
#define IBT_2_WITH_ARDUINO F("IBT_2_WITH_ARDUINO_SHIELD"), \
|
||||
new MotorDriver(4, 5, 6, UNUSED_PIN, A5, 41.54, 5000, UNUSED_PIN), \
|
||||
new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, 2000, UNUSED_PIN)
|
||||
// YFROBOT Motor Shield (V3.1)
|
||||
#define YFROBOT_MOTOR_SHIELD F("YFROBOT_MOTOR_SHIELD"), \
|
||||
new MotorDriver(5, 4, UNUSED_PIN, UNUSED_PIN, A0, 2.99, 2000, UNUSED_PIN), \
|
||||
new MotorDriver(6, 7, UNUSED_PIN, UNUSED_PIN, A1, 2.99, 2000, UNUSED_PIN)
|
||||
|
||||
// Makeblock ORION UNO like sized board with integrated motor driver
|
||||
// This is like an Uno with H-bridge and RJ12 contacts instead of pin rows.
|
||||
// No current sense. Barrel connector max 12V, Vmotor max 15V. 1.1A polyfuse as output protection.
|
||||
// Main is marked M1 and near RJ12 #5
|
||||
// Prog is marked M2 and near RJ12 #4
|
||||
// For details see
|
||||
// http://docs.makeblock.com/diy-platform/en/electronic-modules/main-control-boards/makeblock-orion.html
|
||||
#define ORION_UNO_INTEGRATED_SHIELD F("ORION_UNO_INTEGRATED_SHIELD"), \
|
||||
new MotorDriver(6, 7, UNUSED_PIN, UNUSED_PIN, UNUSED_PIN, 1.0, 1100, UNUSED_PIN), \
|
||||
new MotorDriver(5, 4, UNUSED_PIN, UNUSED_PIN, UNUSED_PIN, 1.0, 1100, UNUSED_PIN)
|
||||
|
||||
#endif
|
||||
|
14
Outputs.cpp
14
Outputs.cpp
@@ -1,5 +1,9 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Harald Barth
|
||||
* © 2020-2021 Fred Decker
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
@@ -82,7 +86,9 @@ the state of any outputs being monitored or controlled by a separate interface o
|
||||
**********************************************************************/
|
||||
|
||||
#include "Outputs.h"
|
||||
#ifndef DISABLE_EEPROM
|
||||
#include "EEStore.h"
|
||||
#endif
|
||||
#include "StringFormatter.h"
|
||||
#include "IODevice.h"
|
||||
|
||||
@@ -102,10 +108,11 @@ void Output::activate(uint16_t s){
|
||||
data.active = s; // if s>0, set status to active, else inactive
|
||||
// set state of output pin to HIGH or LOW depending on whether bit zero of iFlag is set to 0 (ACTIVE=HIGH) or 1 (ACTIVE=LOW)
|
||||
IODevice::write(data.pin, s ^ data.invert);
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Update EEPROM if output has been stored.
|
||||
if(EEStore::eeStore->data.nOutputs > 0 && num > 0)
|
||||
EEPROM.put(num, data.oStatus);
|
||||
#endif
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -141,7 +148,7 @@ bool Output::remove(uint16_t n){
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to load configuration and state of all Outputs from EEPROM
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
void Output::load(){
|
||||
struct OutputData data;
|
||||
Output *tt;
|
||||
@@ -176,6 +183,7 @@ void Output::store(){
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to create an Output object
|
||||
|
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Harald Barth
|
||||
* © 2021 Fred Decker
|
||||
* © 2020 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
@@ -48,8 +51,10 @@ public:
|
||||
bool isActive();
|
||||
static Output* get(uint16_t);
|
||||
static bool remove(uint16_t);
|
||||
#ifndef DISABLE_EEPROM
|
||||
static void load();
|
||||
static void store();
|
||||
#endif
|
||||
static Output *create(uint16_t, VPIN, int, int=0);
|
||||
static Output *firstOutput;
|
||||
struct OutputData data;
|
||||
|
@@ -17,7 +17,7 @@ Both CommandStation-EX and BaseStation-Classic support much of the NMRA Digital
|
||||
* Control of all cab functions F0-F28 and F29-F68
|
||||
* Main Track: Write configuration variable bytes and set/clear specific configuration variable (CV) bits (aka Programming on Main or POM)
|
||||
* Programming Track: Same as the main track with the addition of reading configuration variable bytes
|
||||
* And manu more custom features. see [What's new in CommandStation-EX?](#whats-new-in-commandstation-ex)
|
||||
* And many more custom features. see [What's new in CommandStation-EX?](#whats-new-in-commandstation-ex)
|
||||
|
||||
|
||||
# What’s in this Repository?
|
||||
|
86
RMFT2.h
86
RMFT2.h
@@ -1,5 +1,7 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2020-2022 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -22,7 +24,7 @@
|
||||
#include "IODevice.h"
|
||||
|
||||
// The following are the operation codes (or instructions) for a kind of virtual machine.
|
||||
// Each instruction is normally 2 bytes long with an operation code followed by a parameter.
|
||||
// Each instruction is normally 3 bytes long with an operation code followed by a parameter.
|
||||
// In cases where more than one parameter is required, the first parameter is followed by one
|
||||
// or more OPCODE_PAD instructions with the subsequent parameters. This wastes a byte but makes
|
||||
// searching easier as a parameter can never be confused with an opcode.
|
||||
@@ -30,34 +32,52 @@
|
||||
enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE,
|
||||
OPCODE_FWD,OPCODE_REV,OPCODE_SPEED,OPCODE_INVERT_DIRECTION,
|
||||
OPCODE_RESERVE,OPCODE_FREE,
|
||||
OPCODE_AT,OPCODE_AFTER,
|
||||
OPCODE_AT,OPCODE_AFTER,OPCODE_AUTOSTART,
|
||||
OPCODE_ATTIMEOUT1,OPCODE_ATTIMEOUT2,OPCODE_IFTIMEOUT,
|
||||
OPCODE_LATCH,OPCODE_UNLATCH,OPCODE_SET,OPCODE_RESET,
|
||||
OPCODE_IF,OPCODE_IFNOT,OPCODE_ENDIF,OPCODE_IFRANDOM,OPCODE_IFRESERVE,
|
||||
OPCODE_DELAY,OPCODE_DELAYMINS,OPCODE_RANDWAIT,
|
||||
OPCODE_FON,OPCODE_FOFF,
|
||||
OPCODE_RED,OPCODE_GREEN,OPCODE_AMBER,
|
||||
OPCODE_SERVO,OPCODE_SIGNAL,OPCODE_TURNOUT,
|
||||
OPCODE_IFCLOSED, OPCODE_IFTHROWN,OPCODE_ELSE,
|
||||
OPCODE_DELAY,OPCODE_DELAYMINS,OPCODE_DELAYMS,OPCODE_RANDWAIT,
|
||||
OPCODE_FON,OPCODE_FOFF,OPCODE_XFON,OPCODE_XFOFF,
|
||||
OPCODE_RED,OPCODE_GREEN,OPCODE_AMBER,OPCODE_DRIVE,
|
||||
OPCODE_SERVO,OPCODE_SIGNAL,OPCODE_TURNOUT,OPCODE_WAITFOR,
|
||||
OPCODE_PAD,OPCODE_FOLLOW,OPCODE_CALL,OPCODE_RETURN,
|
||||
OPCODE_JOIN,OPCODE_UNJOIN,OPCODE_READ_LOCO1,OPCODE_READ_LOCO2,OPCODE_POM,
|
||||
OPCODE_START,OPCODE_SETLOCO,OPCODE_SENDLOCO,
|
||||
OPCODE_PAUSE, OPCODE_RESUME,
|
||||
OPCODE_PAUSE, OPCODE_RESUME,OPCODE_POWEROFF,
|
||||
OPCODE_ONCLOSE, OPCODE_ONTHROW, OPCODE_SERVOTURNOUT, OPCODE_PINTURNOUT,
|
||||
OPCODE_PRINT,
|
||||
OPCODE_PRINT,OPCODE_DCCACTIVATE,
|
||||
OPCODE_ONACTIVATE,OPCODE_ONDEACTIVATE,OPCODE_IFGTE,OPCODE_IFLT,
|
||||
OPCODE_ROSTER,
|
||||
OPCODE_ROUTE,OPCODE_AUTOMATION,OPCODE_SEQUENCE,OPCODE_ENDTASK,OPCODE_ENDEXRAIL
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Flag bits for status of hardware and TPL
|
||||
static const byte SECTION_FLAG = 0x01;
|
||||
static const byte LATCH_FLAG = 0x02;
|
||||
static const byte TASK_FLAG = 0x04;
|
||||
static const byte SECTION_FLAG = 0x80;
|
||||
static const byte LATCH_FLAG = 0x40;
|
||||
static const byte TASK_FLAG = 0x20;
|
||||
static const byte SPARE_FLAG = 0x10;
|
||||
static const byte COUNTER_MASK= 0x0F;
|
||||
|
||||
static const byte MAX_STACK_DEPTH=4;
|
||||
|
||||
static const short MAX_FLAGS=256;
|
||||
#define FLAGOVERFLOW(x) x>=MAX_FLAGS
|
||||
|
||||
class LookList {
|
||||
public:
|
||||
LookList(int16_t size);
|
||||
void add(int16_t lookup, int16_t result);
|
||||
int16_t find(int16_t value);
|
||||
private:
|
||||
int16_t m_size;
|
||||
int16_t m_loaded;
|
||||
int16_t * m_lookupArray;
|
||||
int16_t * m_resultArray;
|
||||
};
|
||||
|
||||
class RMFT2 {
|
||||
public:
|
||||
static void begin();
|
||||
@@ -65,18 +85,22 @@ enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE,
|
||||
RMFT2(int progCounter);
|
||||
RMFT2(int route, uint16_t cab);
|
||||
~RMFT2();
|
||||
static void readLocoCallback(int cv);
|
||||
static void readLocoCallback(int16_t cv);
|
||||
static void emitWithrottleRouteList(Print* stream);
|
||||
static void createNewTask(int route, uint16_t cab);
|
||||
static void turnoutEvent(VPIN id, bool closed);
|
||||
static void turnoutEvent(int16_t id, bool closed);
|
||||
static void activateEvent(int16_t addr, bool active);
|
||||
static void emitTurnoutDescription(Print* stream,int16_t id);
|
||||
static const byte rosterNameCount;
|
||||
static void emitWithrottleRoster(Print * stream);
|
||||
static const FSH * getRosterFunctions(int16_t cabid);
|
||||
private:
|
||||
static void ComandFilter(Print * stream, byte & opcode, byte & paramCount, int p[]);
|
||||
static bool parseSlash(Print * stream, byte & paramCount, int p[]) ;
|
||||
static void ComandFilter(Print * stream, byte & opcode, byte & paramCount, int16_t p[]);
|
||||
static bool parseSlash(Print * stream, byte & paramCount, int16_t p[]) ;
|
||||
static void streamFlags(Print* stream);
|
||||
static void setFlag(VPIN id,byte onMask, byte OffMask=0);
|
||||
static bool getFlag(VPIN id,byte mask);
|
||||
static int locateRouteStart(int16_t _route);
|
||||
static int progtrackLocoId;
|
||||
static bool getFlag(VPIN id,byte mask);
|
||||
static int16_t progtrackLocoId;
|
||||
static void doSignal(VPIN id,bool red, bool amber, bool green);
|
||||
static void emitRouteDescription(Print * stream, char type, int id, const FSH * description);
|
||||
static void emitWithrottleDescriptions(Print * stream);
|
||||
@@ -85,31 +109,43 @@ private:
|
||||
static RMFT2 * pausingTask;
|
||||
void delayMe(long millisecs);
|
||||
void driveLoco(byte speedo);
|
||||
bool readSensor(int16_t sensorId);
|
||||
bool readSensor(uint16_t sensorId);
|
||||
bool skipIfBlock();
|
||||
bool readLoco();
|
||||
void loop2();
|
||||
void kill(const FSH * reason=NULL,int operand=0);
|
||||
void printMessage(uint16_t id); // Built by RMFTMacros.h
|
||||
void printMessage2(const FSH * msg);
|
||||
|
||||
|
||||
|
||||
static bool diag;
|
||||
static const FLASH byte RouteCode[];
|
||||
static const FLASH int16_t SignalDefinitions[];
|
||||
static byte flags[MAX_FLAGS];
|
||||
|
||||
static LookList * sequenceLookup;
|
||||
static LookList * onThrowLookup;
|
||||
static LookList * onCloseLookup;
|
||||
static LookList * onActivateLookup;
|
||||
static LookList * onDeactivateLookup;
|
||||
|
||||
// Local variables - exist for each instance/task
|
||||
RMFT2 *next; // loop chain
|
||||
int progCounter; // Byte offset of next route opcode in ROUTES table
|
||||
unsigned long delayStart; // Used by opcodes that must be recalled before completing
|
||||
unsigned long waitAfter; // Used by OPCODE_AFTER
|
||||
unsigned long delayTime;
|
||||
union {
|
||||
unsigned long waitAfter; // Used by OPCODE_AFTER
|
||||
unsigned long timeoutStart; // Used by OPCODE_ATTIMEOUT
|
||||
};
|
||||
bool timeoutFlag;
|
||||
byte taskId;
|
||||
|
||||
int loco;
|
||||
uint16_t loco;
|
||||
bool forward;
|
||||
bool invert;
|
||||
int speedo;
|
||||
byte speedo;
|
||||
int16_t onTurnoutId;
|
||||
int16_t onActivateAddr;
|
||||
byte stackDepth;
|
||||
int callStack[MAX_STACK_DEPTH];
|
||||
};
|
||||
|
202
RMFT2MacroReset.h
Normal file
202
RMFT2MacroReset.h
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* © 2021-2022 Chris Harlow
|
||||
* © 2020,2021 Chris Harlow. All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// This file cleans and resets the RMFT2 Macros.
|
||||
// It is used between passes to reduce complexity in RMFT2Macros.h
|
||||
// DO NOT add an include guard to this file.
|
||||
|
||||
// Undefine all RMFT macros
|
||||
#undef ACTIVATE
|
||||
#undef ACTIVATEL
|
||||
#undef AFTER
|
||||
#undef ALIAS
|
||||
#undef AMBER
|
||||
#undef AT
|
||||
#undef ATTIMEOUT
|
||||
#undef AUTOMATION
|
||||
#undef AUTOSTART
|
||||
#undef CALL
|
||||
#undef CLOSE
|
||||
#undef DEACTIVATE
|
||||
#undef DEACTIVATEL
|
||||
#undef DELAY
|
||||
#undef DELAYMINS
|
||||
#undef DELAYRANDOM
|
||||
#undef DONE
|
||||
#undef DRIVE
|
||||
#undef ELSE
|
||||
#undef ENDEXRAIL
|
||||
#undef ENDIF
|
||||
#undef ENDTASK
|
||||
#undef ESTOP
|
||||
#undef EXRAIL
|
||||
#undef FADE
|
||||
#undef FOFF
|
||||
#undef FOLLOW
|
||||
#undef FON
|
||||
#undef FREE
|
||||
#undef FWD
|
||||
#undef GREEN
|
||||
#undef IF
|
||||
#undef IFCLOSED
|
||||
#undef IFGTE
|
||||
#undef IFLT
|
||||
#undef IFNOT
|
||||
#undef IFRANDOM
|
||||
#undef IFRESERVE
|
||||
#undef IFTHROWN
|
||||
#undef IFTIMEOUT
|
||||
#undef INVERT_DIRECTION
|
||||
#undef JOIN
|
||||
#undef LATCH
|
||||
#undef LCD
|
||||
#undef LCN
|
||||
#undef ONACTIVATE
|
||||
#undef ONACTIVATEL
|
||||
#undef ONDEACTIVATE
|
||||
#undef ONDEACTIVATEL
|
||||
#undef ONCLOSE
|
||||
#undef ONTHROW
|
||||
#undef PAUSE
|
||||
#undef PIN_TURNOUT
|
||||
#undef PRINT
|
||||
#undef POM
|
||||
#undef POWEROFF
|
||||
#undef READ_LOCO
|
||||
#undef RED
|
||||
#undef RESERVE
|
||||
#undef RESET
|
||||
#undef RESUME
|
||||
#undef RETURN
|
||||
#undef REV
|
||||
#undef ROSTER
|
||||
#undef ROUTE
|
||||
#undef SENDLOCO
|
||||
#undef SEQUENCE
|
||||
#undef SERIAL
|
||||
#undef SERIAL1
|
||||
#undef SERIAL2
|
||||
#undef SERIAL3
|
||||
#undef SERVO
|
||||
#undef SERVO2
|
||||
#undef SERVO_TURNOUT
|
||||
#undef SET
|
||||
#undef SETLOCO
|
||||
#undef SIGNAL
|
||||
#undef SPEED
|
||||
#undef START
|
||||
#undef STOP
|
||||
#undef THROW
|
||||
#undef TURNOUT
|
||||
#undef UNJOIN
|
||||
#undef UNLATCH
|
||||
#undef WAITFOR
|
||||
#undef XFOFF
|
||||
#undef XFON
|
||||
|
||||
#ifndef RMFT2_UNDEF_ONLY
|
||||
#define ACTIVATE(addr,subaddr)
|
||||
#define ACTIVATEL(addr)
|
||||
#define AFTER(sensor_id)
|
||||
#define ALIAS(name,value)
|
||||
#define AMBER(signal_id)
|
||||
#define AT(sensor_id)
|
||||
#define ATTIMEOUT(sensor_id,timeout_ms)
|
||||
#define AUTOMATION(id, description)
|
||||
#define AUTOSTART
|
||||
#define CALL(route)
|
||||
#define CLOSE(id)
|
||||
#define DEACTIVATE(addr,subaddr)
|
||||
#define DEACTIVATEL(addr)
|
||||
#define DELAY(mindelay)
|
||||
#define DELAYMINS(mindelay)
|
||||
#define DELAYRANDOM(mindelay,maxdelay)
|
||||
#define DONE
|
||||
#define DRIVE(analogpin)
|
||||
#define ELSE
|
||||
#define ENDEXRAIL
|
||||
#define ENDIF
|
||||
#define ENDTASK
|
||||
#define ESTOP
|
||||
#define EXRAIL
|
||||
#define FADE(pin,value,ms)
|
||||
#define FOFF(func)
|
||||
#define FOLLOW(route)
|
||||
#define FON(func)
|
||||
#define FREE(blockid)
|
||||
#define FWD(speed)
|
||||
#define GREEN(signal_id)
|
||||
#define IF(sensor_id)
|
||||
#define IFCLOSED(turnout_id)
|
||||
#define IFGTE(sensor_id,value)
|
||||
#define IFLT(sensor_id,value)
|
||||
#define IFNOT(sensor_id)
|
||||
#define IFRANDOM(percent)
|
||||
#define IFTHROWN(turnout_id)
|
||||
#define IFRESERVE(block)
|
||||
#define IFTIMEOUT
|
||||
#define INVERT_DIRECTION
|
||||
#define JOIN
|
||||
#define LATCH(sensor_id)
|
||||
#define LCD(row,msg)
|
||||
#define LCN(msg)
|
||||
#define ONACTIVATE(addr,subaddr)
|
||||
#define ONACTIVATEL(linear)
|
||||
#define ONDEACTIVATE(addr,subaddr)
|
||||
#define ONDEACTIVATEL(linear)
|
||||
#define ONCLOSE(turnout_id)
|
||||
#define ONTHROW(turnout_id)
|
||||
#define PAUSE
|
||||
#define PIN_TURNOUT(id,pin,description...)
|
||||
#define PRINT(msg)
|
||||
#define POM(cv,value)
|
||||
#define POWEROFF
|
||||
#define READ_LOCO
|
||||
#define RED(signal_id)
|
||||
#define RESERVE(blockid)
|
||||
#define RESET(pin)
|
||||
#define RESUME
|
||||
#define RETURN
|
||||
#define REV(speed)
|
||||
#define ROUTE(id, description)
|
||||
#define ROSTER(cab,name,funcmap...)
|
||||
#define SENDLOCO(cab,route)
|
||||
#define SEQUENCE(id)
|
||||
#define SERIAL(msg)
|
||||
#define SERIAL1(msg)
|
||||
#define SERIAL2(msg)
|
||||
#define SERIAL3(msg)
|
||||
#define SERVO(id,position,profile)
|
||||
#define SERVO2(id,position,duration)
|
||||
#define SERVO_TURNOUT(id,pin,activeAngle,inactiveAngle,profile,description...)
|
||||
#define SET(pin)
|
||||
#define SETLOCO(loco)
|
||||
#define SIGNAL(redpin,amberpin,greenpin)
|
||||
#define SPEED(speed)
|
||||
#define START(route)
|
||||
#define STOP
|
||||
#define THROW(id)
|
||||
#define TURNOUT(id,addr,subaddr,description...)
|
||||
#define UNJOIN
|
||||
#define UNLATCH(sensor_id)
|
||||
#define WAITFOR(pin)
|
||||
#define XFOFF(cab,func)
|
||||
#define XFON(cab,func)
|
||||
#endif
|
351
RMFTMacros.h
351
RMFTMacros.h
@@ -1,5 +1,7 @@
|
||||
/*
|
||||
* © 2020,2021 Chris Harlow. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2020-2022 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -16,15 +18,17 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef RMFTMacros_H
|
||||
#define RMFTMacros_H
|
||||
|
||||
// remove normal code LCD macro (will be restored later)
|
||||
// remove normal code LCD & SERIAL macros (will be restored later)
|
||||
#undef LCD
|
||||
#undef SERIAL
|
||||
|
||||
|
||||
// This file will include and build the EXRAIL script and associated helper tricks.
|
||||
// It does this by incliding myAutomation.h several times, each with a set of macros to
|
||||
// It does this by including myAutomation.h several times, each with a set of macros to
|
||||
// extract the relevant parts.
|
||||
|
||||
// The entire automation script is contained within a byte array RMFT2::RouteCode[]
|
||||
@@ -45,175 +49,143 @@
|
||||
|
||||
// CAUTION: The macros below are multiple passed over myAutomation.h
|
||||
|
||||
// Pass 1 Implements aliases and
|
||||
// converts descriptions to withrottle format emitter function
|
||||
// Most macros are simply ignored in this pass.
|
||||
|
||||
|
||||
#define ALIAS(name,value) const int name=value;
|
||||
#define EXRAIL void RMFT2::emitWithrottleDescriptions(Print * stream) {
|
||||
#define ROUTE(id, description) emitRouteDescription(stream,'R',id,F(description));
|
||||
#define AUTOMATION(id, description) emitRouteDescription(stream,'A',id,F(description));
|
||||
#define ENDEXRAIL }
|
||||
|
||||
#define AFTER(sensor_id)
|
||||
#define AMBER(signal_id)
|
||||
#define AT(sensor_id)
|
||||
#define CALL(route)
|
||||
#define CLOSE(id)
|
||||
#define DELAY(mindelay)
|
||||
#define DELAYMINS(mindelay)
|
||||
#define DELAYRANDOM(mindelay,maxdelay)
|
||||
#define DONE
|
||||
#define ENDIF
|
||||
#define ENDTASK
|
||||
#define ESTOP
|
||||
#define FOFF(func)
|
||||
#define FOLLOW(route)
|
||||
#define FON(func)
|
||||
#define FREE(blockid)
|
||||
#define FWD(speed)
|
||||
#define GREEN(signal_id)
|
||||
#define IF(sensor_id)
|
||||
#define IFNOT(sensor_id)
|
||||
#define IFRANDOM(percent)
|
||||
#define IFRESERVE(block)
|
||||
#define INVERT_DIRECTION
|
||||
#define JOIN
|
||||
#define LATCH(sensor_id)
|
||||
#define LCD(row,msg)
|
||||
#define ONCLOSE(turnout_id)
|
||||
#define ONTHROW(turnout_id)
|
||||
#define PAUSE
|
||||
#define PRINT(msg)
|
||||
#define POM(cv,value)
|
||||
#define READ_LOCO
|
||||
#define RED(signal_id)
|
||||
#define RESERVE(blockid)
|
||||
#define RESET(sensor_id)
|
||||
#define RESUME
|
||||
#define RETURN
|
||||
#define REV(speed)
|
||||
#define START(route)
|
||||
#define SENDLOCO(cab,route)
|
||||
#define SERVO(id,position,profile)
|
||||
#define SETLOCO(loco)
|
||||
#define SET(sensor_id)
|
||||
#define SEQUENCE(id)
|
||||
#define SPEED(speed)
|
||||
#define STOP
|
||||
#undef SIGNAL
|
||||
#define SIGNAL(redpin,amberpin,greenpin)
|
||||
#define SERVO_TURNOUT(id,pin,activeAngle,inactiveAngle,profile)
|
||||
#define PIN_TURNOUT(id,pin)
|
||||
#define THROW(id)
|
||||
#define TURNOUT(id,addr,subaddr)
|
||||
#define UNJOIN
|
||||
#define UNLATCH(sensor_id)
|
||||
|
||||
#include "myAutomation.h"
|
||||
|
||||
// setup for pass 2... Create getMessageText function
|
||||
// Pass 1 Implements aliases
|
||||
#include "RMFT2MacroReset.h"
|
||||
#undef ALIAS
|
||||
#undef ROUTE
|
||||
#undef AUTOMATION
|
||||
#define ROUTE(id, description)
|
||||
#define AUTOMATION(id, description)
|
||||
|
||||
#undef EXRAIL
|
||||
#undef PRINT
|
||||
#undef ENDEXRAIL
|
||||
#undef LCD
|
||||
const int StringMacroTracker1=__COUNTER__;
|
||||
#define ALIAS(name,value)
|
||||
#define EXRAIL void RMFT2::printMessage(uint16_t id) { switch(id) {
|
||||
#define ENDEXRAIL default: DIAG(F("printMessage error %d %d"),id,StringMacroTracker1); return ; }}
|
||||
#define PRINT(msg) case (__COUNTER__ - StringMacroTracker1) : printMessage2(F(msg));break;
|
||||
#define LCD(id,msg) case (__COUNTER__ - StringMacroTracker1) : StringFormatter::lcd(id,F(msg));break;
|
||||
#define ALIAS(name,value) const int name=value;
|
||||
#include "myAutomation.h"
|
||||
|
||||
// Setup for Pass 3: create main routes table
|
||||
#undef AFTER
|
||||
#undef AMBER
|
||||
#undef AT
|
||||
#undef AUTOMATION
|
||||
#undef CALL
|
||||
#undef CLOSE
|
||||
#undef DELAY
|
||||
#undef DELAYMINS
|
||||
#undef DELAYRANDOM
|
||||
#undef DONE
|
||||
#undef ENDIF
|
||||
#undef ENDEXRAIL
|
||||
#undef ENDTASK
|
||||
#undef ESTOP
|
||||
#undef EXRAIL
|
||||
#undef FOFF
|
||||
#undef FOLLOW
|
||||
#undef FON
|
||||
#undef FREE
|
||||
#undef FWD
|
||||
#undef GREEN
|
||||
#undef IF
|
||||
#undef IFNOT
|
||||
#undef IFRANDOM
|
||||
#undef IFRESERVE
|
||||
#undef INVERT_DIRECTION
|
||||
#undef JOIN
|
||||
#undef LATCH
|
||||
#undef LCD
|
||||
#undef ONCLOSE
|
||||
#undef ONTHROW
|
||||
#undef PAUSE
|
||||
#undef POM
|
||||
// Pass 2 convert descriptions to withrottle format emitter function
|
||||
#include "RMFT2MacroReset.h"
|
||||
#undef ROUTE
|
||||
#define ROUTE(id, description) emitRouteDescription(stream,'R',id,F(description));
|
||||
#undef AUTOMATION
|
||||
#define AUTOMATION(id, description) emitRouteDescription(stream,'A',id,F(description));
|
||||
void RMFT2::emitWithrottleDescriptions(Print * stream) {
|
||||
(void)stream;
|
||||
#include "myAutomation.h"
|
||||
}
|
||||
|
||||
// Pass 3... Create Text sending functions
|
||||
#include "RMFT2MacroReset.h"
|
||||
const int StringMacroTracker1=__COUNTER__;
|
||||
#undef PRINT
|
||||
#undef READ_LOCO
|
||||
#undef RED
|
||||
#undef RESERVE
|
||||
#undef RESET
|
||||
#undef RESUME
|
||||
#undef RETURN
|
||||
#undef REV
|
||||
#undef ROUTE
|
||||
#undef START
|
||||
#undef SEQUENCE
|
||||
#undef SERVO
|
||||
#undef SENDLOCO
|
||||
#undef SETLOCO
|
||||
#undef SET
|
||||
#undef SPEED
|
||||
#undef STOP
|
||||
#undef SIGNAL
|
||||
#undef SERVO_TURNOUT
|
||||
#undef PIN_TURNOUT
|
||||
#undef THROW
|
||||
#define PRINT(msg) case (__COUNTER__ - StringMacroTracker1) : printMessage2(F(msg));break;
|
||||
#undef LCN
|
||||
#define LCN(msg) case (__COUNTER__ - StringMacroTracker1) : StringFormatter::send(&LCN_SERIAL,F(msg));break;
|
||||
#undef SERIAL
|
||||
#define SERIAL(msg) case (__COUNTER__ - StringMacroTracker1) : StringFormatter::send(&Serial,F(msg));break;
|
||||
#undef SERIAL1
|
||||
#define SERIAL1(msg) case (__COUNTER__ - StringMacroTracker1) : StringFormatter::send(&Serial1,F(msg));break;
|
||||
#undef SERIAL2
|
||||
#define SERIAL2(msg) case (__COUNTER__ - StringMacroTracker1) : StringFormatter::send(&Serial2,F(msg));break;
|
||||
#undef SERIAL3
|
||||
#define SERIAL3(msg) case (__COUNTER__ - StringMacroTracker1) : StringFormatter::send(&Serial3,F(msg));break;
|
||||
#undef LCD
|
||||
#define LCD(id,msg) case (__COUNTER__ - StringMacroTracker1) : StringFormatter::lcd(id,F(msg));break;
|
||||
|
||||
void RMFT2::printMessage(uint16_t id) {
|
||||
switch(id) {
|
||||
#include "myAutomation.h"
|
||||
default: break ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Pass 4: Turnout descriptions (optional)
|
||||
#include "RMFT2MacroReset.h"
|
||||
#undef TURNOUT
|
||||
#undef UNJOIN
|
||||
#undef UNLATCH
|
||||
#define TURNOUT(id,addr,subaddr,description...) case id: desc=F("" description); break;
|
||||
#undef PIN_TURNOUT
|
||||
#define PIN_TURNOUT(id,pin,description...) case id: desc=F("" description); break;
|
||||
#undef SERVO_TURNOUT
|
||||
#define SERVO_TURNOUT(id,pin,activeAngle,inactiveAngle,profile,description...) case id: desc=F("" description); break;
|
||||
|
||||
void RMFT2::emitTurnoutDescription(Print* stream,int16_t turnoutid) {
|
||||
const FSH * desc=F("");
|
||||
switch (turnoutid) {
|
||||
#include "myAutomation.h"
|
||||
default: break;
|
||||
}
|
||||
if (GETFLASH(desc)=='\0') desc=F("%d");
|
||||
StringFormatter::send(stream,desc,turnoutid);
|
||||
}
|
||||
|
||||
// Pass 5: Roster names (count)
|
||||
#include "RMFT2MacroReset.h"
|
||||
#undef ROSTER
|
||||
#define ROSTER(cabid,name,funcmap...) +1
|
||||
|
||||
const byte RMFT2::rosterNameCount=0
|
||||
#include "myAutomation.h"
|
||||
;
|
||||
|
||||
// Pass 6: Roster names emitter
|
||||
#include "RMFT2MacroReset.h"
|
||||
#undef ROSTER
|
||||
#define ROSTER(cabid,name,funcmap...) StringFormatter::send(stream,(FSH *)format,F(name),cabid,cabid<128?'S':'L');
|
||||
void RMFT2::emitWithrottleRoster(Print * stream) {
|
||||
static const char format[] FLASH ="]\\[%S}|{%d}|{%c";
|
||||
(void)format;
|
||||
StringFormatter::send(stream,F("RL%d"), rosterNameCount);
|
||||
#include "myAutomation.h"
|
||||
stream->write('\n');
|
||||
}
|
||||
|
||||
// Pass 7: functions getter
|
||||
#include "RMFT2MacroReset.h"
|
||||
#undef ROSTER
|
||||
#define ROSTER(cabid,name,funcmap...) case cabid: return F("" funcmap);
|
||||
const FSH * RMFT2::getRosterFunctions(int16_t cabid) {
|
||||
switch(cabid) {
|
||||
#include "myAutomation.h"
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 8 Signal definitions
|
||||
#include "RMFT2MacroReset.h"
|
||||
#undef SIGNAL
|
||||
#define SIGNAL(redpin,amberpin,greenpin) redpin,amberpin,greenpin,
|
||||
const FLASH int16_t RMFT2::SignalDefinitions[] = {
|
||||
#include "myAutomation.h"
|
||||
0,0,0 };
|
||||
|
||||
// Last Pass : create main routes table
|
||||
// Only undef the macros, not dummy them.
|
||||
#define RMFT2_UNDEF_ONLY
|
||||
#include "RMFT2MacroReset.h"
|
||||
// Define internal helper macros.
|
||||
// Everything we generate here has to be compile-time evaluated to
|
||||
// a constant.
|
||||
#define V(val) (byte)(((int16_t)(val))&0x00FF),(byte)(((int16_t)(val)>>8)&0x00FF)
|
||||
// Define macros for route code creation
|
||||
#define V(val) ((int16_t)(val))&0x00FF,((int16_t)(val)>>8)&0x00FF
|
||||
#define NOP 0,0
|
||||
|
||||
#define ALIAS(name,value)
|
||||
#define EXRAIL const FLASH byte RMFT2::RouteCode[] = {
|
||||
#define AUTOMATION(id, description) OPCODE_AUTOMATION, V(id),
|
||||
#define ROUTE(id, description) OPCODE_ROUTE, V(id),
|
||||
#define SEQUENCE(id) OPCODE_SEQUENCE, V(id),
|
||||
#define ENDTASK OPCODE_ENDTASK,NOP,
|
||||
#define DONE OPCODE_ENDTASK,NOP,
|
||||
#define ENDEXRAIL OPCODE_ENDTASK,NOP,OPCODE_ENDEXRAIL,NOP };
|
||||
|
||||
#define ACTIVATE(addr,subaddr) OPCODE_DCCACTIVATE,V(addr<<3 | subaddr<<1 | 1),
|
||||
#define ACTIVATEL(addr) OPCODE_DCCACTIVATE,V((addr+3)<<1 | 1),
|
||||
#define AFTER(sensor_id) OPCODE_AT,V(sensor_id),OPCODE_AFTER,V(sensor_id),
|
||||
#define ALIAS(name,value)
|
||||
#define AMBER(signal_id) OPCODE_AMBER,V(signal_id),
|
||||
#define AT(sensor_id) OPCODE_AT,V(sensor_id),
|
||||
#define ATTIMEOUT(sensor_id,timeout) OPCODE_ATTIMEOUT1,0,0,OPCODE_ATTIMEOUT2,V(sensor_id),OPCODE_PAD,V(timeout/100L),
|
||||
#define AUTOMATION(id, description) OPCODE_AUTOMATION, V(id),
|
||||
#define AUTOSTART OPCODE_AUTOSTART,0,0,
|
||||
#define CALL(route) OPCODE_CALL,V(route),
|
||||
#define CLOSE(id) OPCODE_CLOSE,V(id),
|
||||
#define DELAY(ms) OPCODE_DELAY,V(ms/100),
|
||||
#define DEACTIVATE(addr,subaddr) OPCODE_DCCACTIVATE,V(addr<<3 | subaddr<<1),
|
||||
#define DEACTIVATEL(addr) OPCODE_DCCACTIVATE,V((addr+3)<<1),
|
||||
#define DELAY(ms) ms<30000?OPCODE_DELAYMS:OPCODE_DELAY,V(ms/(ms<30000?1L:100L)),
|
||||
#define DELAYMINS(mindelay) OPCODE_DELAYMINS,V(mindelay),
|
||||
#define DELAYRANDOM(mindelay,maxdelay) OPCODE_DELAY,V(mindelay/100),OPCODE_RANDWAIT,V((maxdelay-mindelay)/100),
|
||||
#define ENDIF OPCODE_ENDIF,NOP,
|
||||
#define DELAYRANDOM(mindelay,maxdelay) DELAY(mindelay) OPCODE_RANDWAIT,V((maxdelay-mindelay)/100L),
|
||||
#define DONE OPCODE_ENDTASK,0,0,
|
||||
#define DRIVE(analogpin) OPCODE_DRIVE,V(analogpin),
|
||||
#define ELSE OPCODE_ELSE,0,0,
|
||||
#define ENDEXRAIL
|
||||
#define ENDIF OPCODE_ENDIF,0,0,
|
||||
#define ENDTASK OPCODE_ENDTASK,0,0,
|
||||
#define ESTOP OPCODE_SPEED,V(1),
|
||||
#define EXRAIL
|
||||
#define FADE(pin,value,ms) OPCODE_SERVO,V(pin),OPCODE_PAD,V(value),OPCODE_PAD,V(PCA9685::ProfileType::UseDuration|PCA9685::NoPowerOff),OPCODE_PAD,V(ms/100L),
|
||||
#define FOFF(func) OPCODE_FOFF,V(func),
|
||||
#define FOLLOW(route) OPCODE_FOLLOW,V(route),
|
||||
#define FON(func) OPCODE_FON,V(func),
|
||||
@@ -221,46 +193,71 @@ const int StringMacroTracker1=__COUNTER__;
|
||||
#define FWD(speed) OPCODE_FWD,V(speed),
|
||||
#define GREEN(signal_id) OPCODE_GREEN,V(signal_id),
|
||||
#define IF(sensor_id) OPCODE_IF,V(sensor_id),
|
||||
#define IFCLOSED(turnout_id) OPCODE_IFCLOSED,V(turnout_id),
|
||||
#define IFGTE(sensor_id,value) OPCODE_IFGTE,V(sensor_id),OPCODE_PAD,V(value),
|
||||
#define IFLT(sensor_id,value) OPCODE_IFLT,V(sensor_id),OPCODE_PAD,V(value),
|
||||
#define IFNOT(sensor_id) OPCODE_IFNOT,V(sensor_id),
|
||||
#define IFRANDOM(percent) OPCODE_IFRANDOM,V(percent),
|
||||
#define IFRESERVE(block) OPCODE_IFRESERVE,V(block),
|
||||
#define INVERT_DIRECTION OPCODE_INVERT_DIRECTION,NOP,
|
||||
#define JOIN OPCODE_JOIN,NOP,
|
||||
#define IFTHROWN(turnout_id) OPCODE_IFTHROWN,V(turnout_id),
|
||||
#define IFTIMEOUT OPCODE_IFTIMEOUT,0,0,
|
||||
#define INVERT_DIRECTION OPCODE_INVERT_DIRECTION,0,0,
|
||||
#define JOIN OPCODE_JOIN,0,0,
|
||||
#define LATCH(sensor_id) OPCODE_LATCH,V(sensor_id),
|
||||
#define LCD(id,msg) OPCODE_PRINT,V(__COUNTER__ - StringMacroTracker2),
|
||||
#define LCD(id,msg) PRINT(msg)
|
||||
#define LCN(msg) PRINT(msg)
|
||||
#define ONACTIVATE(addr,subaddr) OPCODE_ONACTIVATE,V(addr<<2|subaddr),
|
||||
#define ONACTIVATEL(linear) OPCODE_ONACTIVATE,V(linear+3),
|
||||
#define ONCLOSE(turnout_id) OPCODE_ONCLOSE,V(turnout_id),
|
||||
#define ONDEACTIVATE(addr,subaddr) OPCODE_ONDEACTIVATE,V(addr<<2|subaddr),
|
||||
#define ONDEACTIVATEL(linear) OPCODE_ONDEACTIVATE,V(linear+3),
|
||||
#define ONTHROW(turnout_id) OPCODE_ONTHROW,V(turnout_id),
|
||||
#define PAUSE OPCODE_PAUSE,NOP,
|
||||
#define PAUSE OPCODE_PAUSE,0,0,
|
||||
#define PIN_TURNOUT(id,pin,description...) OPCODE_PINTURNOUT,V(id),OPCODE_PAD,V(pin),
|
||||
#define POM(cv,value) OPCODE_POM,V(cv),OPCODE_PAD,V(value),
|
||||
#define POWEROFF OPCODE_POWEROFF,0,0,
|
||||
#define PRINT(msg) OPCODE_PRINT,V(__COUNTER__ - StringMacroTracker2),
|
||||
#define READ_LOCO OPCODE_READ_LOCO1,NOP,OPCODE_READ_LOCO2,NOP,
|
||||
#define READ_LOCO OPCODE_READ_LOCO1,0,0,OPCODE_READ_LOCO2,0,0,
|
||||
#define RED(signal_id) OPCODE_RED,V(signal_id),
|
||||
#define RESERVE(blockid) OPCODE_RESERVE,V(blockid),
|
||||
#define RESET(sensor_id) OPCODE_RESET,V(sensor_id),
|
||||
#define RESUME OPCODE_RESUME,NOP,
|
||||
#define RETURN OPCODE_RETURN,NOP,
|
||||
#define RESET(pin) OPCODE_RESET,V(pin),
|
||||
#define RESUME OPCODE_RESUME,0,0,
|
||||
#define RETURN OPCODE_RETURN,0,0,
|
||||
#define REV(speed) OPCODE_REV,V(speed),
|
||||
#define ROSTER(cabid,name,funcmap...)
|
||||
#define ROUTE(id, description) OPCODE_ROUTE, V(id),
|
||||
#define SENDLOCO(cab,route) OPCODE_SENDLOCO,V(cab),OPCODE_PAD,V(route),
|
||||
#define START(route) OPCODE_START,V(route),
|
||||
#define SERVO(id,position,profile) OPCODE_SERVO,V(id),OPCODE_PAD,V(position),OPCODE_PAD,V(PCA9685::ProfileType::profile),
|
||||
#define SEQUENCE(id) OPCODE_SEQUENCE, V(id),
|
||||
#define SERIAL(msg) PRINT(msg)
|
||||
#define SERIAL1(msg) PRINT(msg)
|
||||
#define SERIAL2(msg) PRINT(msg)
|
||||
#define SERIAL3(msg) PRINT(msg)
|
||||
#define SERVO(id,position,profile) OPCODE_SERVO,V(id),OPCODE_PAD,V(position),OPCODE_PAD,V(PCA9685::profile),OPCODE_PAD,V(0),
|
||||
#define SERVO2(id,position,ms) OPCODE_SERVO,V(id),OPCODE_PAD,V(position),OPCODE_PAD,V(PCA9685::Instant),OPCODE_PAD,V(ms/100L),
|
||||
#define SERVO_TURNOUT(id,pin,activeAngle,inactiveAngle,profile,description...) OPCODE_SERVOTURNOUT,V(id),OPCODE_PAD,V(pin),OPCODE_PAD,V(activeAngle),OPCODE_PAD,V(inactiveAngle),OPCODE_PAD,V(PCA9685::ProfileType::profile),
|
||||
#define SET(pin) OPCODE_SET,V(pin),
|
||||
#define SETLOCO(loco) OPCODE_SETLOCO,V(loco),
|
||||
#define SET(sensor_id) OPCODE_SET,V(sensor_id),
|
||||
#define SIGNAL(redpin,amberpin,greenpin)
|
||||
#define SPEED(speed) OPCODE_SPEED,V(speed),
|
||||
#define START(route) OPCODE_START,V(route),
|
||||
#define STOP OPCODE_SPEED,V(0),
|
||||
#define SIGNAL(redpin,amberpin,greenpin) OPCODE_SIGNAL,V(redpin),OPCODE_PAD,V(amberpin),OPCODE_PAD,V(greenpin),
|
||||
#define SERVO_TURNOUT(id,pin,activeAngle,inactiveAngle,profile) OPCODE_SERVOTURNOUT,V(id),OPCODE_PAD,V(pin),OPCODE_PAD,V(activeAngle),OPCODE_PAD,V(inactiveAngle),OPCODE_PAD,V(PCA9685::ProfileType::profile),
|
||||
#define PIN_TURNOUT(id,pin) OPCODE_PINTURNOUT,V(id),OPCODE_PAD,V(pin),
|
||||
#define THROW(id) OPCODE_THROW,V(id),
|
||||
#define TURNOUT(id,addr,subaddr) OPCODE_TURNOUT,V(id),OPCODE_PAD,V(addr),OPCODE_PAD,V(subaddr),
|
||||
#define UNJOIN OPCODE_UNJOIN,NOP,
|
||||
#define TURNOUT(id,addr,subaddr,description...) OPCODE_TURNOUT,V(id),OPCODE_PAD,V(addr),OPCODE_PAD,V(subaddr),
|
||||
#define UNJOIN OPCODE_UNJOIN,0,0,
|
||||
#define UNLATCH(sensor_id) OPCODE_UNLATCH,V(sensor_id),
|
||||
#define WAITFOR(pin) OPCODE_WAITFOR,V(pin),
|
||||
#define XFOFF(cab,func) OPCODE_XFOFF,V(cab),OPCODE_PAD,V(func),
|
||||
#define XFON(cab,func) OPCODE_XFON,V(cab),OPCODE_PAD,V(func),
|
||||
|
||||
// PASS2 Build RouteCode
|
||||
// Build RouteCode
|
||||
const int StringMacroTracker2=__COUNTER__;
|
||||
#include "myAutomation.h"
|
||||
const FLASH byte RMFT2::RouteCode[] = {
|
||||
#include "myAutomation.h"
|
||||
OPCODE_ENDTASK,0,0,OPCODE_ENDEXRAIL,0,0 };
|
||||
|
||||
// Restore normal code LCD macro
|
||||
// Restore normal code LCD & SERIAL macro
|
||||
#undef LCD
|
||||
#define LCD StringFormatter::lcd
|
||||
|
||||
#undef SERIAL
|
||||
#define SERIAL 0x0
|
||||
#endif
|
||||
|
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of DCC-EX CommandStation-EX
|
||||
*
|
||||
@@ -103,3 +104,12 @@ bool RingStream::commit() {
|
||||
_buffer[_mark]=lowByte(_count);
|
||||
return true; // commit worked
|
||||
}
|
||||
void RingStream::flush() {
|
||||
_pos_write=0;
|
||||
_pos_read=0;
|
||||
_buffer[0]=0;
|
||||
}
|
||||
void RingStream::printBuffer(Print * stream) {
|
||||
_buffer[_pos_write]='\0';
|
||||
stream->print((char *)_buffer);
|
||||
}
|
||||
|
@@ -1,7 +1,8 @@
|
||||
#ifndef RingStream_h
|
||||
#define RingStream_h
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of DCC-EX CommandStation-EX
|
||||
*
|
||||
@@ -34,7 +35,8 @@ class RingStream : public Print {
|
||||
void mark(uint8_t b);
|
||||
bool commit();
|
||||
uint8_t peekTargetMark();
|
||||
|
||||
void printBuffer(Print * streamer);
|
||||
void flush();
|
||||
private:
|
||||
int _len;
|
||||
int _pos_write;
|
||||
|
173
SSD1306Ascii.cpp
173
SSD1306Ascii.cpp
@@ -89,31 +89,93 @@ const uint8_t FLASH SSD1306AsciiWire::blankPixels[30] =
|
||||
{0x40, // First byte specifies data mode
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
// this section is based on https://github.com/adafruit/Adafruit_SSD1306
|
||||
|
||||
/** Initialization commands for a 128x32 or 128x64 SSD1306 oled display. */
|
||||
const uint8_t FLASH SSD1306AsciiWire::Adafruit128xXXinit[] = {
|
||||
// Init sequence for Adafruit 128x32/64 OLED module
|
||||
0x00, // Set to command mode
|
||||
SSD1306_DISPLAYOFF,
|
||||
SSD1306_SETDISPLAYCLOCKDIV, 0x80, // the suggested ratio 0x80
|
||||
SSD1306_SETMULTIPLEX, 0x3F, // ratio 64 (initially)
|
||||
SSD1306_SETDISPLAYOFFSET, 0x0, // no offset
|
||||
SSD1306_SETSTARTLINE | 0x0, // line #0
|
||||
SSD1306_CHARGEPUMP, 0x14, // internal vcc
|
||||
SSD1306_MEMORYMODE, 0x02, // page mode
|
||||
SSD1306_SEGREMAP | 0x1, // column 127 mapped to SEG0
|
||||
SSD1306_COMSCANDEC, // column scan direction reversed
|
||||
SSD1306_SETCOMPINS, 0X12, // set COM pins
|
||||
SSD1306_SETCONTRAST, 0x7F, // contrast level 127
|
||||
SSD1306_SETPRECHARGE, 0xF1, // pre-charge period (1, 15)
|
||||
SSD1306_SETVCOMDETECT, 0x40, // vcomh regulator level
|
||||
SSD1306_DISPLAYALLON_RESUME,
|
||||
SSD1306_NORMALDISPLAY,
|
||||
SSD1306_DISPLAYON
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// This section is based on https://github.com/stanleyhuangyc/MultiLCD
|
||||
|
||||
/** Initialization commands for a 128x64 SH1106 oled display. */
|
||||
const uint8_t FLASH SSD1306AsciiWire::SH1106_132x64init[] = {
|
||||
0x00, // Set to command mode
|
||||
SSD1306_DISPLAYOFF,
|
||||
SSD1306_SETDISPLAYCLOCKDIV, 0X80, // set osc division
|
||||
SSD1306_SETMULTIPLEX, 0x3F, // ratio 64
|
||||
SSD1306_SETDISPLAYOFFSET, 0X00, // set display offset
|
||||
SSD1306_SETSTARTPAGE | 0X0, // set page address
|
||||
SSD1306_SETSTARTLINE | 0x0, // set start line
|
||||
SH1106_SET_PUMP_MODE, SH1106_PUMP_ON, // set charge pump enable
|
||||
SSD1306_SEGREMAP | 0X1, // set segment remap
|
||||
SSD1306_COMSCANDEC, // Com scan direction
|
||||
SSD1306_SETCOMPINS, 0X12, // set COM pins
|
||||
SSD1306_SETCONTRAST, 0x80, // 128
|
||||
SSD1306_SETPRECHARGE, 0X1F, // set pre-charge period
|
||||
SSD1306_SETVCOMDETECT, 0x40, // set vcomh
|
||||
SH1106_SET_PUMP_VOLTAGE | 0X2, // 8.0 volts
|
||||
SSD1306_NORMALDISPLAY, // normal / reverse
|
||||
SSD1306_DISPLAYON
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
// SSD1306AsciiWire Method Definitions
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Constructor
|
||||
SSD1306AsciiWire::SSD1306AsciiWire(int width, int height) {
|
||||
m_displayWidth = width;
|
||||
m_displayHeight = height;
|
||||
// Set size in characters in base class
|
||||
lcdRows = height / 8;
|
||||
lcdCols = width / 6;
|
||||
|
||||
// Initialise request block for I2C
|
||||
requestBlock.init();
|
||||
m_col = 0;
|
||||
m_row = 0;
|
||||
m_colOffset = 0;
|
||||
|
||||
I2CManager.begin();
|
||||
I2CManager.setClock(400000L); // Set max supported I2C speed
|
||||
for (byte address = 0x3c; address <= 0x3d; address++) {
|
||||
if (I2CManager.exists(address)) {
|
||||
m_i2cAddr = address;
|
||||
if (m_displayWidth==132 && m_displayHeight==64) {
|
||||
// SH1106 display. This uses 128x64 centered within a 132x64 OLED.
|
||||
m_colOffset = 2;
|
||||
I2CManager.write_P(address, SH1106_132x64init, sizeof(SH1106_132x64init));
|
||||
} else if (m_displayWidth==128 && (m_displayHeight==64 || m_displayHeight==32)) {
|
||||
// SSD1306 128x64 or 128x32
|
||||
I2CManager.write_P(address, Adafruit128xXXinit, sizeof(Adafruit128xXXinit));
|
||||
if (m_displayHeight == 32)
|
||||
I2CManager.write(address, 5, 0, // Set command mode
|
||||
SSD1306_SETMULTIPLEX, 0x1F, // ratio 32
|
||||
SSD1306_SETCOMPINS, 0x02); // sequential COM pins, disable remap
|
||||
} else {
|
||||
DIAG(F("OLED configuration option not recognised"));
|
||||
return;
|
||||
}
|
||||
// Device found
|
||||
DIAG(F("%dx%d OLED display configured on I2C:x%x"), width, height, address);
|
||||
if (width == 132)
|
||||
begin(&SH1106_132x64, address);
|
||||
else if (height == 32)
|
||||
begin(&Adafruit128x32, address);
|
||||
else
|
||||
begin(&Adafruit128x64, address);
|
||||
// Set singleton address
|
||||
lcdDisplay = this;
|
||||
clear();
|
||||
@@ -135,23 +197,6 @@ void SSD1306AsciiWire::clearNative() {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise device
|
||||
void SSD1306AsciiWire::begin(const DevType* dev, uint8_t i2cAddr) {
|
||||
m_i2cAddr = i2cAddr;
|
||||
m_col = 0;
|
||||
m_row = 0;
|
||||
const uint8_t* table = (const uint8_t*)GETFLASHW(&dev->initcmds);
|
||||
uint8_t size = GETFLASH(&dev->initSize);
|
||||
m_displayWidth = GETFLASH(&dev->lcdWidth);
|
||||
m_displayHeight = GETFLASH(&dev->lcdHeight);
|
||||
m_colOffset = GETFLASH(&dev->colOffset);
|
||||
I2CManager.write_P(m_i2cAddr, table, size);
|
||||
if (m_displayHeight == 32)
|
||||
I2CManager.write(m_i2cAddr, 5, 0, // Set command mode
|
||||
SSD1306_SETMULTIPLEX, 0x1F, // ratio 32
|
||||
SSD1306_SETCOMPINS, 0x02); // sequential COM pins, disable remap
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Set cursor position (by text line)
|
||||
@@ -212,82 +257,6 @@ size_t SSD1306AsciiWire::writeNative(uint8_t ch) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
// this section is based on https://github.com/adafruit/Adafruit_SSD1306
|
||||
|
||||
/** Initialization commands for a 128x32 or 128x64 SSD1306 oled display. */
|
||||
const uint8_t FLASH SSD1306AsciiWire::Adafruit128xXXinit[] = {
|
||||
// Init sequence for Adafruit 128x32/64 OLED module
|
||||
0x00, // Set to command mode
|
||||
SSD1306_DISPLAYOFF,
|
||||
SSD1306_SETDISPLAYCLOCKDIV, 0x80, // the suggested ratio 0x80
|
||||
SSD1306_SETMULTIPLEX, 0x3F, // ratio 64 (initially)
|
||||
SSD1306_SETDISPLAYOFFSET, 0x0, // no offset
|
||||
SSD1306_SETSTARTLINE | 0x0, // line #0
|
||||
SSD1306_CHARGEPUMP, 0x14, // internal vcc
|
||||
SSD1306_MEMORYMODE, 0x02, // page mode
|
||||
SSD1306_SEGREMAP | 0x1, // column 127 mapped to SEG0
|
||||
SSD1306_COMSCANDEC, // column scan direction reversed
|
||||
SSD1306_SETCOMPINS, 0X12, // set COM pins
|
||||
SSD1306_SETCONTRAST, 0x7F, // contrast level 127
|
||||
SSD1306_SETPRECHARGE, 0xF1, // pre-charge period (1, 15)
|
||||
SSD1306_SETVCOMDETECT, 0x40, // vcomh regulator level
|
||||
SSD1306_DISPLAYALLON_RESUME,
|
||||
SSD1306_NORMALDISPLAY,
|
||||
SSD1306_DISPLAYON
|
||||
};
|
||||
|
||||
/** Initialize a 128x32 SSD1306 oled display. */
|
||||
const DevType FLASH SSD1306AsciiWire::Adafruit128x32 = {
|
||||
Adafruit128xXXinit,
|
||||
sizeof(Adafruit128xXXinit),
|
||||
128,
|
||||
32,
|
||||
0
|
||||
};
|
||||
|
||||
/** Initialize a 128x64 oled display. */
|
||||
const DevType FLASH SSD1306AsciiWire::Adafruit128x64 = {
|
||||
Adafruit128xXXinit,
|
||||
sizeof(Adafruit128xXXinit),
|
||||
128,
|
||||
64,
|
||||
0
|
||||
};
|
||||
//------------------------------------------------------------------------------
|
||||
// This section is based on https://github.com/stanleyhuangyc/MultiLCD
|
||||
|
||||
/** Initialization commands for a 128x64 SH1106 oled display. */
|
||||
const uint8_t FLASH SSD1306AsciiWire::SH1106_132x64init[] = {
|
||||
0x00, // Set to command mode
|
||||
SSD1306_DISPLAYOFF,
|
||||
SSD1306_SETDISPLAYCLOCKDIV, 0X80, // set osc division
|
||||
SSD1306_SETMULTIPLEX, 0x3F, // ratio 64
|
||||
SSD1306_SETDISPLAYOFFSET, 0X00, // set display offset
|
||||
SSD1306_SETSTARTPAGE | 0X0, // set page address
|
||||
SSD1306_SETSTARTLINE | 0x0, // set start line
|
||||
SH1106_SET_PUMP_MODE, SH1106_PUMP_ON, // set charge pump enable
|
||||
SSD1306_SEGREMAP | 0X1, // set segment remap
|
||||
SSD1306_COMSCANDEC, // Com scan direction
|
||||
SSD1306_SETCOMPINS, 0X12, // set COM pins
|
||||
SSD1306_SETCONTRAST, 0x80, // 128
|
||||
SSD1306_SETPRECHARGE, 0X1F, // set pre-charge period
|
||||
SSD1306_SETVCOMDETECT, 0x40, // set vcomh
|
||||
SH1106_SET_PUMP_VOLTAGE | 0X2, // 8.0 volts
|
||||
SSD1306_NORMALDISPLAY, // normal / reverse
|
||||
SSD1306_DISPLAYON
|
||||
};
|
||||
|
||||
/** Initialize a 132x64 oled SH1106 display. */
|
||||
const DevType FLASH SSD1306AsciiWire::SH1106_132x64 = {
|
||||
SH1106_132x64init,
|
||||
sizeof(SH1106_132x64init),
|
||||
128,
|
||||
64,
|
||||
2 // SH1106 is a 132x64 controller but most OLEDs are only attached
|
||||
// to columns 2-129.
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
@@ -32,21 +32,6 @@
|
||||
//#define NOLOWERCASE
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Device initialization structure.
|
||||
|
||||
struct DevType {
|
||||
/* Pointer to initialization command bytes. */
|
||||
const uint8_t* initcmds;
|
||||
/* Number of initialization bytes */
|
||||
const uint8_t initSize;
|
||||
/* Width of the display in pixels */
|
||||
const uint8_t lcdWidth;
|
||||
/** Height of the display in pixels. */
|
||||
const uint8_t lcdHeight;
|
||||
/* Column offset RAM to display. Used to pick start column of SH1106. */
|
||||
const uint8_t colOffset;
|
||||
};
|
||||
|
||||
// Constructor
|
||||
class SSD1306AsciiWire : public LCDDisplay {
|
||||
public:
|
||||
@@ -55,25 +40,17 @@ class SSD1306AsciiWire : public LCDDisplay {
|
||||
SSD1306AsciiWire(int width, int height);
|
||||
|
||||
// Initialize the display controller.
|
||||
void begin(const DevType* dev, uint8_t i2cAddr);
|
||||
void begin(uint8_t i2cAddr);
|
||||
|
||||
// Clear the display and set the cursor to (0, 0).
|
||||
void clearNative() override;
|
||||
|
||||
// Set cursor to start of specified text line
|
||||
void setRowNative(byte line) override;
|
||||
|
||||
// Initialize the display controller.
|
||||
void init(const DevType* dev);
|
||||
|
||||
// Write one character to OLED
|
||||
size_t writeNative(uint8_t c) override;
|
||||
|
||||
// Display characteristics / initialisation
|
||||
static const DevType FLASH Adafruit128x32;
|
||||
static const DevType FLASH Adafruit128x64;
|
||||
static const DevType FLASH SH1106_132x64;
|
||||
|
||||
bool isBusy() override { return requestBlock.isBusy(); }
|
||||
|
||||
private:
|
||||
|
95
Sensors.cpp
95
Sensors.cpp
@@ -1,8 +1,10 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021, modified by Neil McKechnie. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -67,8 +69,11 @@ decide to ignore the <q ID> return and only react to <Q ID> triggers.
|
||||
**********************************************************************/
|
||||
|
||||
#include "StringFormatter.h"
|
||||
#include "CommandDistributor.h"
|
||||
#include "Sensors.h"
|
||||
#ifndef DISABLE_EEPROM
|
||||
#include "EEStore.h"
|
||||
#endif
|
||||
#include "IODevice.h"
|
||||
|
||||
|
||||
@@ -85,7 +90,7 @@ decide to ignore the <q ID> return and only react to <Q ID> triggers.
|
||||
// second part of the list is determined from by the 'firstPollSensor' pointer.
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Sensor::checkAll(Print *stream){
|
||||
void Sensor::checkAll(){
|
||||
uint16_t sensorCount = 0;
|
||||
|
||||
#ifdef USE_NOTIFY
|
||||
@@ -103,12 +108,6 @@ void Sensor::checkAll(Print *stream){
|
||||
// Required time elapsed since last read cycle started,
|
||||
// so initiate new scan through the sensor list
|
||||
readingSensor = firstSensor;
|
||||
#ifdef USE_NOTIFY
|
||||
if (firstSensor == firstPollSensor)
|
||||
pollSignalPhase = true;
|
||||
else
|
||||
pollSignalPhase = false;
|
||||
#endif
|
||||
lastReadCycle = thisTime;
|
||||
}
|
||||
}
|
||||
@@ -117,12 +116,6 @@ void Sensor::checkAll(Print *stream){
|
||||
bool pause = false;
|
||||
while (readingSensor != NULL && !pause) {
|
||||
|
||||
#ifdef USE_NOTIFY
|
||||
// Check if we have reached the start of the polled portion of the sensor list.
|
||||
if (readingSensor == firstPollSensor)
|
||||
pollSignalPhase = true;
|
||||
#endif
|
||||
|
||||
// Where the sensor is attached to a pin, read pin status. For sources such as LCN,
|
||||
// which don't have an input pin to read, the LCN class calls setState() to update inputState when
|
||||
// a message is received. The IODevice::read() call returns 1 for active pins (0v) and 0 for inactive (5v).
|
||||
@@ -130,10 +123,8 @@ void Sensor::checkAll(Print *stream){
|
||||
// routine when an input signal change is detected, and this updates the inputState directly,
|
||||
// so these inputs don't need to be polled here.
|
||||
VPIN pin = readingSensor->data.pin;
|
||||
#ifdef USE_NOTIFY
|
||||
if (pollSignalPhase)
|
||||
#endif
|
||||
if (pin!=VPIN_NONE) readingSensor->inputState = IODevice::read(pin);
|
||||
if (readingSensor->pollingRequired && pin != VPIN_NONE)
|
||||
readingSensor->inputState = IODevice::read(pin);
|
||||
|
||||
// Check if changed since last time, and process changes.
|
||||
if (readingSensor->inputState == readingSensor->active) {
|
||||
@@ -147,31 +138,19 @@ void Sensor::checkAll(Print *stream){
|
||||
readingSensor->active = readingSensor->inputState;
|
||||
readingSensor->latchDelay = minReadCount; // Reset counter
|
||||
|
||||
if (stream != NULL) {
|
||||
StringFormatter::send(stream, F("<%c %d>\n"), readingSensor->active ? 'Q' : 'q', readingSensor->data.snum);
|
||||
pause = true; // Don't check any more sensors on this entry
|
||||
}
|
||||
CommandDistributor::broadcastSensor(readingSensor->data.snum,readingSensor->active);
|
||||
pause = true; // Don't check any more sensors on this entry
|
||||
}
|
||||
|
||||
// Move to next sensor in list.
|
||||
readingSensor = readingSensor->nextSensor;
|
||||
|
||||
// Currently process max of 16 sensors per entry for polled sensors, and
|
||||
// 16 per entry for sensors notified by callback.
|
||||
// Performance measurements taken during development indicate that, with 64 sensors configured
|
||||
// on 8x 8-pin PCF8574 GPIO expanders, all inputs can be read within 1.4ms (400Mhz I2C bus speed), and a
|
||||
// full cycle of scanning 64 sensors for changes takes between 1.9 and 3.2 milliseconds.
|
||||
// Currently process max of 16 sensors per entry.
|
||||
// Performance measurements taken during development indicate that, with 128 sensors configured
|
||||
// on 8x 16-pin MCP23017 GPIO expanders with polling (no change notification), all inputs can be read from the devices
|
||||
// within 1.4ms (400Mhz I2C bus speed), and a full cycle of checking 128 sensors for changes takes under a millisecond.
|
||||
sensorCount++;
|
||||
#ifdef USE_NOTIFY
|
||||
if (pollSignalPhase) {
|
||||
#endif
|
||||
if (sensorCount >= 16) pause = true;
|
||||
#ifdef USE_NOTIFY
|
||||
} else
|
||||
{
|
||||
if (sensorCount >= 16) pause = true;
|
||||
}
|
||||
#endif
|
||||
if (sensorCount >= 16) pause = true;
|
||||
}
|
||||
|
||||
} // Sensor::checkAll
|
||||
@@ -223,23 +202,18 @@ Sensor *Sensor::create(int snum, VPIN pin, int pullUp){
|
||||
tt = (Sensor *)calloc(1,sizeof(Sensor));
|
||||
if (!tt) return tt; // memory allocation failure
|
||||
|
||||
#ifdef USE_NOTIFY
|
||||
if (pin == VPIN_NONE || IODevice::hasCallback(pin)) {
|
||||
// Callback available, or no pin to read, so link sensor on to the start of the list
|
||||
tt->nextSensor = firstSensor;
|
||||
firstSensor = tt;
|
||||
if (lastSensor == NULL) lastSensor = tt; // This is only item in list.
|
||||
} else {
|
||||
// No callback, so add to end of list so it's polled.
|
||||
if (lastSensor != NULL) lastSensor->nextSensor = tt;
|
||||
lastSensor = tt;
|
||||
if (!firstSensor) firstSensor = tt;
|
||||
if (!firstPollSensor) firstPollSensor = tt;
|
||||
}
|
||||
#else
|
||||
if (pin == VPIN_NONE)
|
||||
tt->pollingRequired = false;
|
||||
#ifdef USE_NOTIFY
|
||||
else if (IODevice::hasCallback(pin))
|
||||
tt->pollingRequired = false;
|
||||
#endif
|
||||
else
|
||||
tt->pollingRequired = true;
|
||||
|
||||
// Add to the start of the list
|
||||
tt->nextSensor = firstSensor;
|
||||
firstSensor = tt;
|
||||
#endif
|
||||
|
||||
tt->data.snum = snum;
|
||||
tt->data.pin = pin;
|
||||
@@ -248,9 +222,8 @@ Sensor *Sensor::create(int snum, VPIN pin, int pullUp){
|
||||
tt->inputState = 0;
|
||||
tt->latchDelay = minReadCount;
|
||||
|
||||
int params[] = {pullUp};
|
||||
if (pin != VPIN_NONE)
|
||||
IODevice::configure(pin, IODevice::CONFIGURE_INPUT, 1, params);
|
||||
IODevice::configureInput(pin, pullUp);
|
||||
// Generally, internal pull-up resistors are not, on their own, sufficient
|
||||
// for external infrared sensors --- each sensor must have its own 1K external pull-up resistor
|
||||
|
||||
@@ -305,12 +278,13 @@ bool Sensor::remove(int n){
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
void Sensor::load(){
|
||||
struct SensorData data;
|
||||
Sensor *tt;
|
||||
|
||||
for(uint16_t i=0;i<EEStore::eeStore->data.nSensors;i++){
|
||||
uint16_t i=EEStore::eeStore->data.nSensors;
|
||||
while(i--){
|
||||
EEPROM.get(EEStore::pointer(),data);
|
||||
tt=create(data.snum, data.pin, data.pullUp);
|
||||
EEStore::advance(sizeof(tt->data));
|
||||
@@ -332,7 +306,7 @@ void Sensor::store(){
|
||||
EEStore::eeStore->data.nSensors++;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Sensor *Sensor::firstSensor=NULL;
|
||||
@@ -342,6 +316,5 @@ unsigned long Sensor::lastReadCycle=0;
|
||||
#ifdef USE_NOTIFY
|
||||
Sensor *Sensor::firstPollSensor = NULL;
|
||||
Sensor *Sensor::lastSensor = NULL;
|
||||
bool Sensor::pollSignalPhase = false;
|
||||
bool Sensor::inputChangeCallbackRegistered = false;
|
||||
#endif
|
||||
#endif
|
||||
|
20
Sensors.h
20
Sensors.h
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
@@ -45,14 +48,6 @@ struct SensorData {
|
||||
class Sensor{
|
||||
// The sensor list is a linked list where each sensor's 'nextSensor' field points to the next.
|
||||
// The pointer is null in the last on the list.
|
||||
// To partition the sensor into those sensors which require polling through cyclic calls
|
||||
// to 'IODevice::read(vpin)', and those which support callback on change, 'firstSensor'
|
||||
// points to the start of the overall list, and 'lastSensor' points to the end of the list
|
||||
// (the last sensor object). This structure allows sensors to be added to the start or the
|
||||
// end of the list easily. So if an input pin supports change notification, it is placed at the
|
||||
// end of the list. If not, it is placed at the beginning. And the pointer 'firstPollSensor'
|
||||
// is set to the first of the sensor objects that requires scanning. Thus, we can iterate
|
||||
// through the whole list, or just through the part that requires scanning.
|
||||
|
||||
public:
|
||||
SensorData data;
|
||||
@@ -74,13 +69,16 @@ public:
|
||||
// Constructor
|
||||
Sensor();
|
||||
Sensor *nextSensor;
|
||||
|
||||
void setState(int state);
|
||||
#ifndef DISABLE_EEPROM
|
||||
static void load();
|
||||
static void store();
|
||||
#endif
|
||||
static Sensor *create(int id, VPIN vpin, int pullUp);
|
||||
static Sensor* get(int id);
|
||||
static bool remove(int id);
|
||||
static void checkAll(Print *stream);
|
||||
static void checkAll();
|
||||
static void printAll(Print *stream);
|
||||
static unsigned long lastReadCycle; // value of micros at start of last read cycle
|
||||
static const unsigned int cycleInterval = 10000; // min time between consecutive reads of each sensor in microsecs.
|
||||
@@ -88,9 +86,9 @@ public:
|
||||
static const unsigned int minReadCount = 1; // number of additional scans before acting on change
|
||||
// E.g. 1 means that a change is ignored for one scan and actioned on the next.
|
||||
// Max value is 63
|
||||
bool pollingRequired = true;
|
||||
|
||||
#ifdef USE_NOTIFY
|
||||
static bool pollSignalPhase;
|
||||
static void inputChangeCallback(VPIN vpin, int state);
|
||||
static bool inputChangeCallbackRegistered;
|
||||
#endif
|
||||
|
77
SerialManager.cpp
Normal file
77
SerialManager.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* © 2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of DCC++EX
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "SerialManager.h"
|
||||
#include "DCCEXParser.h"
|
||||
SerialManager * SerialManager::first=NULL;
|
||||
|
||||
SerialManager::SerialManager(HardwareSerial * myserial) {
|
||||
serial=myserial;
|
||||
next=first;
|
||||
first=this;
|
||||
bufferLength=0;
|
||||
myserial->begin(115200);
|
||||
inCommandPayload=false;
|
||||
}
|
||||
|
||||
void SerialManager::init() {
|
||||
#ifdef SERIAL3_COMMANDS
|
||||
new SerialManager(&Serial3);
|
||||
#endif
|
||||
#ifdef SERIAL2_COMMANDS
|
||||
new SerialManager(&Serial2);
|
||||
#endif
|
||||
#ifdef SERIAL1_COMMANDS
|
||||
new SerialManager(&Serial1);
|
||||
#endif
|
||||
new SerialManager(&Serial);
|
||||
}
|
||||
|
||||
void SerialManager::broadcast(RingStream * ring) {
|
||||
for (SerialManager * s=first;s;s=s->next) s->broadcast2(ring);
|
||||
}
|
||||
void SerialManager::broadcast2(RingStream * ring) {
|
||||
ring->printBuffer(serial);
|
||||
}
|
||||
|
||||
void SerialManager::loop() {
|
||||
for (SerialManager * s=first;s;s=s->next) s->loop2();
|
||||
}
|
||||
|
||||
void SerialManager::loop2() {
|
||||
while (serial->available()) {
|
||||
char ch = serial->read();
|
||||
if (ch == '<') {
|
||||
inCommandPayload = true;
|
||||
bufferLength = 0;
|
||||
buffer[0] = '\0';
|
||||
}
|
||||
else if (ch == '>') {
|
||||
buffer[bufferLength] = '\0';
|
||||
DCCEXParser::parse(serial, buffer, NULL);
|
||||
inCommandPayload = false;
|
||||
break;
|
||||
}
|
||||
else if (inCommandPayload) {
|
||||
if (bufferLength < (COMMAND_BUFFER_SIZE-1)) buffer[bufferLength++] = ch;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
49
SerialManager.h
Normal file
49
SerialManager.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* © 2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of DCC++EX
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SerialManager_h
|
||||
#define SerialManager_h
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "defines.h"
|
||||
#include "RingStream.h"
|
||||
|
||||
#ifndef COMMAND_BUFFER_SIZE
|
||||
#define COMMAND_BUFFER_SIZE 100
|
||||
#endif
|
||||
|
||||
class SerialManager {
|
||||
public:
|
||||
static void init();
|
||||
static void loop();
|
||||
static void broadcast(RingStream * ring);
|
||||
|
||||
private:
|
||||
static SerialManager * first;
|
||||
SerialManager(HardwareSerial * myserial);
|
||||
void loop2();
|
||||
void broadcast2(RingStream * ring);
|
||||
HardwareSerial * serial;
|
||||
SerialManager * next;
|
||||
byte bufferLength;
|
||||
byte buffer[COMMAND_BUFFER_SIZE];
|
||||
bool inCommandPayload;
|
||||
};
|
||||
#endif
|
859
Turnouts.cpp
859
Turnouts.cpp
@@ -1,9 +1,13 @@
|
||||
/*
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 M Steve Todd
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2013-2016 Gregg E. Berman
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -19,390 +23,517 @@
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// >>>>>> ATTENTION: This class requires major cleaning.
|
||||
// The public interface has been narrowed to avoid the ambuguity of "activated".
|
||||
|
||||
|
||||
|
||||
//#define EESTOREDEBUG
|
||||
#include "defines.h"
|
||||
#include "Turnouts.h"
|
||||
#include "defines.h" // includes config.h
|
||||
#ifndef DISABLE_EEPROM
|
||||
#include "EEStore.h"
|
||||
#endif
|
||||
#include "StringFormatter.h"
|
||||
#include "CommandDistributor.h"
|
||||
#include "RMFT2.h"
|
||||
#include "Turnouts.h"
|
||||
#include "DCC.h"
|
||||
#include "LCN.h"
|
||||
#ifdef EESTOREDEBUG
|
||||
#include "DIAG.h"
|
||||
#endif
|
||||
|
||||
// Keywords used for turnout configuration.
|
||||
const int16_t HASH_KEYWORD_SERVO=27709;
|
||||
const int16_t HASH_KEYWORD_DCC=6436;
|
||||
const int16_t HASH_KEYWORD_VPIN=-415;
|
||||
/*
|
||||
* Protected static data
|
||||
*/
|
||||
|
||||
enum unit8_t {
|
||||
TURNOUT_DCC = 1,
|
||||
TURNOUT_SERVO = 2,
|
||||
TURNOUT_VPIN = 3,
|
||||
TURNOUT_LCN = 4,
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to print all Turnout states to stream in form "<H id state>"
|
||||
|
||||
void Turnout::printAll(Print *stream){
|
||||
for (Turnout *tt = Turnout::firstTurnout; tt != NULL; tt = tt->nextTurnout)
|
||||
StringFormatter::send(stream, F("<H %d %d>\n"), tt->data.id, tt->data.active);
|
||||
} // Turnout::printAll
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Object method to print configuration of one Turnout to stream, in one of the following forms:
|
||||
// <H id SERVO vpin activePos inactivePos profile state>
|
||||
// <H id LCN state>
|
||||
// <H id VPIN vpin state>
|
||||
// <H id DCC address subAddress state>
|
||||
|
||||
void Turnout::print(Print *stream){
|
||||
uint8_t state = ((data.active) != 0);
|
||||
uint8_t type = data.type;
|
||||
switch (type) {
|
||||
case TURNOUT_LCN:
|
||||
// LCN Turnout
|
||||
StringFormatter::send(stream, F("<H %d LCN %d>\n"), data.id, state);
|
||||
break;
|
||||
case TURNOUT_DCC:
|
||||
// DCC Turnout
|
||||
StringFormatter::send(stream, F("<H %d DCC %d %d %d>\n"), data.id,
|
||||
(((data.dccAccessoryData.address-1) >> 2)+1), ((data.dccAccessoryData.address-1) & 3), state);
|
||||
break;
|
||||
case TURNOUT_VPIN:
|
||||
// VPIN Digital output
|
||||
StringFormatter::send(stream, F("<H %d VPIN %d %d>\n"), data.id, data.vpinData.vpin, state);
|
||||
break;
|
||||
case TURNOUT_SERVO:
|
||||
// Servo Turnout
|
||||
StringFormatter::send(stream, F("<H %d SERVO %d %d %d %d %d>\n"), data.id, data.servoData.vpin,
|
||||
data.servoData.activePosition, data.servoData.inactivePosition, data.servoData.profile, state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Public interface to turnout throw/close
|
||||
bool Turnout::setClosed(int id, bool closed) {
|
||||
// hides the internal activate argument to a single place
|
||||
return activate(id, closed? false: true ); /// Needs cleaning up
|
||||
}
|
||||
bool Turnout::isClosed(int id) {
|
||||
// hides the internal activate argument to a single place
|
||||
return !isActive(id); /// Needs cleaning up
|
||||
}
|
||||
int Turnout::getId() {
|
||||
return data.id;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to activate/deactivate Turnout with ID 'n'.
|
||||
// Returns false if turnout not found.
|
||||
/* static */ Turnout *Turnout::_firstTurnout = 0;
|
||||
|
||||
/*
|
||||
* Public static data
|
||||
*/
|
||||
/* static */ int Turnout::turnoutlistHash = 0;
|
||||
|
||||
bool Turnout::activate(int n, bool state){
|
||||
Turnout * tt=get(n);
|
||||
if (!tt) return false;
|
||||
tt->activate(state);
|
||||
turnoutlistHash++;
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* Protected static functions
|
||||
*/
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to check if the Turnout with ID 'n' is activated or not.
|
||||
// Returns false if turnout not found.
|
||||
|
||||
bool Turnout::isActive(int n){
|
||||
Turnout * tt=get(n);
|
||||
if (!tt) return false;
|
||||
return tt->isActive();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Object function to check the status of Turnout is activated or not.
|
||||
|
||||
bool Turnout::isActive() {
|
||||
return data.active;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Object method to activate or deactivate the Turnout.
|
||||
|
||||
// activate is virtual here so that it can be overridden by a non-DCC turnout mechanism
|
||||
void Turnout::activate(bool state) {
|
||||
#ifdef EESTOREDEBUG
|
||||
DIAG(F("Turnout::activate(%d)"),state);
|
||||
#endif
|
||||
if (data.type == TURNOUT_LCN) {
|
||||
// A LCN turnout is transmitted to the LCN master.
|
||||
LCN::send('T', data.id, state);
|
||||
return; // The tStatus will be updated by a message from the LCN master, later.
|
||||
}
|
||||
data.active = state;
|
||||
switch (data.type) {
|
||||
case TURNOUT_DCC:
|
||||
DCC::setAccessory((((data.dccAccessoryData.address-1) >> 2) + 1),
|
||||
((data.dccAccessoryData.address-1) & 3), state);
|
||||
break;
|
||||
case TURNOUT_SERVO:
|
||||
#ifndef IO_NO_HAL
|
||||
IODevice::write(data.servoData.vpin, state);
|
||||
#endif
|
||||
break;
|
||||
case TURNOUT_VPIN:
|
||||
IODevice::write(data.vpinData.vpin, state);
|
||||
break;
|
||||
}
|
||||
// Save state if stored in EEPROM
|
||||
if (EEStore::eeStore->data.nTurnouts > 0 && num > 0)
|
||||
EEPROM.put(num, data.tStatus);
|
||||
|
||||
#if defined(RMFT_ACTIVE)
|
||||
RMFT2::turnoutEvent(data.id, !state);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to find Turnout object specified by ID 'n'. Return NULL if not found.
|
||||
|
||||
Turnout* Turnout::get(int n){
|
||||
Turnout *tt;
|
||||
for(tt=firstTurnout;tt!=NULL && tt->data.id!=n;tt=tt->nextTurnout);
|
||||
return(tt);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to delete Turnout object specified by ID 'n'. Return false if not found.
|
||||
|
||||
bool Turnout::remove(int n){
|
||||
Turnout *tt,*pp=NULL;
|
||||
|
||||
for(tt=firstTurnout;tt!=NULL && tt->data.id!=n;pp=tt,tt=tt->nextTurnout);
|
||||
|
||||
if(tt==NULL) return false;
|
||||
|
||||
if(tt==firstTurnout)
|
||||
firstTurnout=tt->nextTurnout;
|
||||
else
|
||||
pp->nextTurnout=tt->nextTurnout;
|
||||
|
||||
free(tt);
|
||||
turnoutlistHash++;
|
||||
return true;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to load all Turnout definitions from EEPROM
|
||||
// TODO: Consider transmitting the initial state of the DCC/LCN turnout here.
|
||||
// (already done for servo turnouts and VPIN turnouts).
|
||||
|
||||
void Turnout::load(){
|
||||
struct TurnoutData data;
|
||||
Turnout *tt=NULL;
|
||||
|
||||
for(uint16_t i=0;i<EEStore::eeStore->data.nTurnouts;i++){
|
||||
// Retrieve data
|
||||
EEPROM.get(EEStore::pointer(), data);
|
||||
|
||||
int lastKnownState = data.active;
|
||||
switch (data.type) {
|
||||
case TURNOUT_DCC:
|
||||
tt=createDCC(data.id, ((data.dccAccessoryData.address-1)>>2)+1, (data.dccAccessoryData.address-1)&3); // DCC-based turnout
|
||||
break;
|
||||
case TURNOUT_LCN:
|
||||
// LCN turnouts are created when the remote device sends a message.
|
||||
break;
|
||||
case TURNOUT_SERVO:
|
||||
tt=createServo(data.id, data.servoData.vpin,
|
||||
data.servoData.activePosition, data.servoData.inactivePosition, data.servoData.profile, lastKnownState);
|
||||
break;
|
||||
case TURNOUT_VPIN:
|
||||
tt=createVpin(data.id, data.vpinData.vpin, lastKnownState); // VPIN-based turnout
|
||||
break;
|
||||
|
||||
default:
|
||||
tt=NULL;
|
||||
}
|
||||
if (tt) tt->num = EEStore::pointer() + offsetof(TurnoutData, tStatus); // Save pointer to tStatus byte within EEPROM
|
||||
// Advance by the actual size of the individual turnout struct.
|
||||
EEStore::advance(data.size);
|
||||
#ifdef EESTOREDEBUG
|
||||
if (tt) print(tt);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function to store all Turnout definitions to EEPROM
|
||||
|
||||
void Turnout::store(){
|
||||
Turnout *tt;
|
||||
|
||||
tt=firstTurnout;
|
||||
EEStore::eeStore->data.nTurnouts=0;
|
||||
|
||||
while(tt!=NULL){
|
||||
// LCN turnouts aren't saved to EEPROM
|
||||
if (tt->data.type != TURNOUT_LCN) {
|
||||
#ifdef EESTOREDEBUG
|
||||
print(tt);
|
||||
#endif
|
||||
tt->num = EEStore::pointer() + offsetof(TurnoutData, tStatus); // Save pointer to tstatus byte within EEPROM
|
||||
EEPROM.put(EEStore::pointer(),tt->data);
|
||||
EEStore::advance(tt->data.size);
|
||||
EEStore::eeStore->data.nTurnouts++;
|
||||
}
|
||||
tt=tt->nextTurnout;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function for creating a DCC-controlled Turnout.
|
||||
|
||||
Turnout *Turnout::createDCC(int id, uint16_t add, uint8_t subAdd){
|
||||
if (add > 511 || subAdd > 3) return NULL;
|
||||
Turnout *tt=create(id);
|
||||
if (!tt) return(tt);
|
||||
tt->data.type = TURNOUT_DCC;
|
||||
tt->data.size = sizeof(tt->data.header) + sizeof(tt->data.dccAccessoryData);
|
||||
tt->data.active = 0;
|
||||
tt->data.dccAccessoryData.address = ((add-1) << 2) + subAdd + 1;
|
||||
return(tt);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function for creating a LCN-controlled Turnout.
|
||||
|
||||
Turnout *Turnout::createLCN(int id, uint8_t state) {
|
||||
Turnout *tt=create(id);
|
||||
if (!tt) return(tt);
|
||||
tt->data.type = TURNOUT_LCN;
|
||||
tt->data.size = sizeof(tt->data.header) + sizeof(tt->data.lcnData);
|
||||
tt->data.active = (state != 0);
|
||||
return(tt);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Static function for associating a Turnout id with a virtual pin in IODevice space.
|
||||
// The actual creation and configuration of the pin must be done elsewhere,
|
||||
// e.g. in mySetup.cpp during startup of the CS.
|
||||
|
||||
Turnout *Turnout::createVpin(int id, VPIN vpin, uint8_t state){
|
||||
if (vpin > VPIN_MAX) return NULL;
|
||||
Turnout *tt=create(id);
|
||||
if(!tt) return(tt);
|
||||
tt->data.type = TURNOUT_VPIN;;
|
||||
tt->data.size = sizeof(tt->data.header) + sizeof(tt->data.vpinData);
|
||||
tt->data.active = (state != 0);
|
||||
tt->data.vpinData.vpin = vpin;
|
||||
IODevice::write(vpin, state); // Set initial state of output.
|
||||
return(tt);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Method for creating a Servo Turnout, e.g. connected to PCA9685 PWM device.
|
||||
|
||||
Turnout *Turnout::createServo(int id, VPIN vpin, uint16_t activePosition, uint16_t inactivePosition, uint8_t profile, uint8_t state){
|
||||
#ifndef IO_NO_HAL
|
||||
if (activePosition > 511 || inactivePosition > 511 || profile > 4) return NULL;
|
||||
|
||||
Turnout *tt=create(id);
|
||||
if (!tt) return(tt);
|
||||
if (tt->data.type != TURNOUT_SERVO) tt->data.active = (state != 0); // Retain current state if it's an existing servo turnout.
|
||||
tt->data.type = TURNOUT_SERVO;
|
||||
tt->data.size = sizeof(tt->data.header) + sizeof(tt->data.servoData);
|
||||
tt->data.servoData.vpin = vpin;
|
||||
tt->data.servoData.activePosition = activePosition;
|
||||
tt->data.servoData.inactivePosition = inactivePosition;
|
||||
tt->data.servoData.profile = profile;
|
||||
// Configure PWM interface device
|
||||
int deviceParams[] = {(int)activePosition, (int)inactivePosition, profile, tt->data.active};
|
||||
if (!IODevice::configure(vpin, IODevice::CONFIGURE_SERVO, 4, deviceParams)) {
|
||||
remove(id);
|
||||
/* static */ Turnout *Turnout::get(uint16_t id) {
|
||||
// Find turnout object from list.
|
||||
for (Turnout *tt = _firstTurnout; tt != NULL; tt = tt->_nextTurnout)
|
||||
if (tt->_turnoutData.id == id) return tt;
|
||||
return NULL;
|
||||
}
|
||||
return(tt);
|
||||
#else
|
||||
(void)id; (void)vpin; (void)activePosition; (void)inactivePosition; (void)profile; (void)state; // avoid compiler warnings
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Support for <T id SERVO pin activepos inactive pos profile>
|
||||
// and <T id DCC address subaddress>
|
||||
// and <T id VPIN pin>
|
||||
// Add new turnout to end of chain
|
||||
/* static */ void Turnout::add(Turnout *tt) {
|
||||
if (!_firstTurnout)
|
||||
_firstTurnout = tt;
|
||||
else {
|
||||
// Find last object on chain
|
||||
Turnout *ptr = _firstTurnout;
|
||||
for ( ; ptr->_nextTurnout!=0; ptr=ptr->_nextTurnout) {}
|
||||
// Line new object to last object.
|
||||
ptr->_nextTurnout = tt;
|
||||
}
|
||||
turnoutlistHash++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Turnout *Turnout::create(int id, int params, int16_t p[]) {
|
||||
if (p[0] == HASH_KEYWORD_SERVO) { // <T id SERVO n n n n>
|
||||
if (params == 5)
|
||||
return createServo(id, (VPIN)p[1], (uint16_t)p[2], (uint16_t)p[3], (uint8_t)p[4]);
|
||||
else
|
||||
return NULL;
|
||||
} else
|
||||
if (p[0] == HASH_KEYWORD_VPIN) { // <T id VPIN n>
|
||||
if (params==2)
|
||||
return createVpin(id, p[1]);
|
||||
// Remove nominated turnout from turnout linked list and delete the object.
|
||||
/* static */ bool Turnout::remove(uint16_t id) {
|
||||
Turnout *tt,*pp=NULL;
|
||||
|
||||
for(tt=_firstTurnout; tt!=NULL && tt->_turnoutData.id!=id; pp=tt, tt=tt->_nextTurnout) {}
|
||||
if (tt == NULL) return false;
|
||||
|
||||
if (tt == _firstTurnout)
|
||||
_firstTurnout = tt->_nextTurnout;
|
||||
else
|
||||
return NULL;
|
||||
} else
|
||||
if (p[0]==HASH_KEYWORD_DCC) {
|
||||
if (params==3 && p[1]>0 && p[1]<=512 && p[2]>=0 && p[2]<4) // <T id DCC n n>
|
||||
return createDCC(id, p[1], p[2]);
|
||||
else if (params==2 && p[1]>0 && p[1]<=512*4) // <T id DCC nn>
|
||||
return createDCC(id, (p[1]-1)/4+1, (p[1]-1)%4);
|
||||
else
|
||||
return NULL;
|
||||
} else if (params==2) { // <T id n n> for DCC or LCN
|
||||
return createDCC(id, p[0], p[1]);
|
||||
pp->_nextTurnout = tt->_nextTurnout;
|
||||
|
||||
delete (ServoTurnout *)tt;
|
||||
|
||||
turnoutlistHash++;
|
||||
return true;
|
||||
}
|
||||
else if (params==3) { // legacy <T id n n n> for Servo
|
||||
return createServo(id, (VPIN)p[0], (uint16_t)p[1], (uint16_t)p[2]);
|
||||
|
||||
|
||||
/*
|
||||
* Public static functions
|
||||
*/
|
||||
|
||||
/* static */ bool Turnout::isClosed(uint16_t id) {
|
||||
Turnout *tt = get(id);
|
||||
if (tt)
|
||||
return tt->isClosed();
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
/* static */ bool Turnout::setClosedStateOnly(uint16_t id, bool closeFlag) {
|
||||
Turnout *tt = get(id);
|
||||
if (!tt) return false;
|
||||
tt->_turnoutData.closed = closeFlag;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Create basic Turnout object. The details of what sort of object it is
|
||||
// controlling are not set here.
|
||||
// I know it says setClosedStateOnly, but we need to tell others
|
||||
// that the state has changed too.
|
||||
#if defined(RMFT_ACTIVE)
|
||||
RMFT2::turnoutEvent(id, closeFlag);
|
||||
#endif
|
||||
|
||||
Turnout *Turnout::create(int id){
|
||||
Turnout *tt=get(id);
|
||||
if (tt==NULL) {
|
||||
tt=(Turnout *)calloc(1,sizeof(Turnout));
|
||||
if (!tt) return (tt);
|
||||
tt->nextTurnout=firstTurnout;
|
||||
firstTurnout=tt;
|
||||
tt->data.id=id;
|
||||
CommandDistributor::broadcastTurnout(id, closeFlag);
|
||||
return true;
|
||||
}
|
||||
turnoutlistHash++;
|
||||
return tt;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Object method to print debug info about the state of a Turnout object
|
||||
//
|
||||
#ifdef EESTOREDEBUG
|
||||
void Turnout::print(Turnout *tt) {
|
||||
tt->print(StringFormatter::diagSerial);
|
||||
}
|
||||
|
||||
// Static setClosed function is invoked from close(), throw() etc. to perform the
|
||||
// common parts of the turnout operation. Code which is specific to a turnout
|
||||
// type should be placed in the virtual function setClosedInternal(bool) which is
|
||||
// called from here.
|
||||
/* static */ bool Turnout::setClosed(uint16_t id, bool closeFlag) {
|
||||
#if defined(DIAG_IO)
|
||||
if (closeFlag)
|
||||
DIAG(F("Turnout::close(%d)"), id);
|
||||
else
|
||||
DIAG(F("Turnout::throw(%d)"), id);
|
||||
#endif
|
||||
Turnout *tt = Turnout::get(id);
|
||||
if (!tt) return false;
|
||||
bool ok = tt->setClosedInternal(closeFlag);
|
||||
|
||||
if (ok) {
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Write byte containing new closed/thrown state to EEPROM if required. Note that eepromAddress
|
||||
// is always zero for LCN turnouts.
|
||||
if (EEStore::eeStore->data.nTurnouts > 0 && tt->_eepromAddress > 0)
|
||||
EEPROM.put(tt->_eepromAddress, tt->_turnoutData.flags);
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
Turnout *Turnout::firstTurnout=NULL;
|
||||
int Turnout::turnoutlistHash=0; //bump on every change so clients know when to refresh their lists
|
||||
#if defined(RMFT_ACTIVE)
|
||||
RMFT2::turnoutEvent(id, closeFlag);
|
||||
#endif
|
||||
|
||||
// Send message to JMRI etc.
|
||||
CommandDistributor::broadcastTurnout(id, closeFlag);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Load all turnout objects
|
||||
/* static */ void Turnout::load() {
|
||||
for (uint16_t i=0; i<EEStore::eeStore->data.nTurnouts; i++) {
|
||||
Turnout::loadTurnout();
|
||||
}
|
||||
}
|
||||
|
||||
// Save all turnout objects
|
||||
/* static */ void Turnout::store() {
|
||||
EEStore::eeStore->data.nTurnouts=0;
|
||||
for (Turnout *tt = _firstTurnout; tt != 0; tt = tt->_nextTurnout) {
|
||||
tt->save();
|
||||
EEStore::eeStore->data.nTurnouts++;
|
||||
}
|
||||
}
|
||||
|
||||
// Load one turnout from EEPROM
|
||||
/* static */ Turnout *Turnout::loadTurnout () {
|
||||
Turnout *tt = 0;
|
||||
// Read turnout type from EEPROM
|
||||
struct TurnoutData turnoutData;
|
||||
int eepromAddress = EEStore::pointer() + offsetof(struct TurnoutData, flags); // Address of byte containing the closed flag.
|
||||
EEPROM.get(EEStore::pointer(), turnoutData);
|
||||
EEStore::advance(sizeof(turnoutData));
|
||||
|
||||
switch (turnoutData.turnoutType) {
|
||||
case TURNOUT_SERVO:
|
||||
// Servo turnout
|
||||
tt = ServoTurnout::load(&turnoutData);
|
||||
break;
|
||||
case TURNOUT_DCC:
|
||||
// DCC Accessory turnout
|
||||
tt = DCCTurnout::load(&turnoutData);
|
||||
break;
|
||||
case TURNOUT_VPIN:
|
||||
// VPIN turnout
|
||||
tt = VpinTurnout::load(&turnoutData);
|
||||
break;
|
||||
default:
|
||||
// If we find anything else, then we don't know what it is or how long it is,
|
||||
// so we can't go any further through the EEPROM!
|
||||
return NULL;
|
||||
}
|
||||
if (tt) {
|
||||
// Save EEPROM address in object. Note that LCN turnouts always have eepromAddress of zero.
|
||||
tt->_eepromAddress = eepromAddress + offsetof(struct TurnoutData, flags);
|
||||
}
|
||||
|
||||
#ifdef EESTOREDEBUG
|
||||
printAll(&Serial);
|
||||
#endif
|
||||
return tt;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*************************************************************************************
|
||||
* ServoTurnout - Turnout controlled by servo device.
|
||||
*
|
||||
*************************************************************************************/
|
||||
|
||||
// Private Constructor
|
||||
ServoTurnout::ServoTurnout(uint16_t id, VPIN vpin, uint16_t thrownPosition, uint16_t closedPosition, uint8_t profile, bool closed) :
|
||||
Turnout(id, TURNOUT_SERVO, closed)
|
||||
{
|
||||
_servoTurnoutData.vpin = vpin;
|
||||
_servoTurnoutData.thrownPosition = thrownPosition;
|
||||
_servoTurnoutData.closedPosition = closedPosition;
|
||||
_servoTurnoutData.profile = profile;
|
||||
}
|
||||
|
||||
// Create function
|
||||
/* static */ Turnout *ServoTurnout::create(uint16_t id, VPIN vpin, uint16_t thrownPosition, uint16_t closedPosition, uint8_t profile, bool closed) {
|
||||
#ifndef IO_NO_HAL
|
||||
Turnout *tt = get(id);
|
||||
if (tt) {
|
||||
// Object already exists, check if it is usable
|
||||
if (tt->isType(TURNOUT_SERVO)) {
|
||||
// Yes, so set parameters
|
||||
ServoTurnout *st = (ServoTurnout *)tt;
|
||||
st->_servoTurnoutData.vpin = vpin;
|
||||
st->_servoTurnoutData.thrownPosition = thrownPosition;
|
||||
st->_servoTurnoutData.closedPosition = closedPosition;
|
||||
st->_servoTurnoutData.profile = profile;
|
||||
// Don't touch the _closed parameter, retain the original value.
|
||||
|
||||
// We don't really need to do the following, since a call to IODevice::_writeAnalogue
|
||||
// will provide all the data that is required! However, if someone has configured
|
||||
// a Turnout, we should ensure that the SET() RESET() and other commands that use write()
|
||||
// behave consistently with the turnout commands.
|
||||
IODevice::configureServo(vpin, thrownPosition, closedPosition, profile, 0, closed);
|
||||
|
||||
// Set position directly to specified position - we don't know where it is moving from.
|
||||
IODevice::writeAnalogue(vpin, closed ? closedPosition : thrownPosition, PCA9685::Instant);
|
||||
|
||||
return tt;
|
||||
} else {
|
||||
// Incompatible object, delete and recreate
|
||||
remove(id);
|
||||
}
|
||||
}
|
||||
tt = (Turnout *)new ServoTurnout(id, vpin, thrownPosition, closedPosition, profile, closed);
|
||||
IODevice::writeAnalogue(vpin, closed ? closedPosition : thrownPosition, PCA9685::Instant);
|
||||
return tt;
|
||||
#else
|
||||
(void)id; (void)vpin; (void)thrownPosition; (void)closedPosition;
|
||||
(void)profile; (void)closed; // avoid compiler warnings.
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Load a Servo turnout definition from EEPROM. The common Turnout data has already been read at this point.
|
||||
Turnout *ServoTurnout::load(struct TurnoutData *turnoutData) {
|
||||
#ifndef DISABLE_EEPROM
|
||||
ServoTurnoutData servoTurnoutData;
|
||||
// Read class-specific data from EEPROM
|
||||
EEPROM.get(EEStore::pointer(), servoTurnoutData);
|
||||
EEStore::advance(sizeof(servoTurnoutData));
|
||||
|
||||
// Create new object
|
||||
Turnout *tt = ServoTurnout::create(turnoutData->id, servoTurnoutData.vpin, servoTurnoutData.thrownPosition,
|
||||
servoTurnoutData.closedPosition, servoTurnoutData.profile, turnoutData->closed);
|
||||
return tt;
|
||||
#else
|
||||
(void)turnoutData;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
// For DCC++ classic compatibility, state reported to JMRI is 1 for thrown and 0 for closed
|
||||
void ServoTurnout::print(Print *stream) {
|
||||
StringFormatter::send(stream, F("<H %d SERVO %d %d %d %d %d>\n"), _turnoutData.id, _servoTurnoutData.vpin,
|
||||
_servoTurnoutData.thrownPosition, _servoTurnoutData.closedPosition, _servoTurnoutData.profile,
|
||||
!_turnoutData.closed);
|
||||
}
|
||||
|
||||
// ServoTurnout-specific code for throwing or closing a servo turnout.
|
||||
bool ServoTurnout::setClosedInternal(bool close) {
|
||||
#ifndef IO_NO_HAL
|
||||
IODevice::writeAnalogue(_servoTurnoutData.vpin,
|
||||
close ? _servoTurnoutData.closedPosition : _servoTurnoutData.thrownPosition, _servoTurnoutData.profile);
|
||||
_turnoutData.closed = close;
|
||||
#else
|
||||
(void)close; // avoid compiler warnings
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
void ServoTurnout::save() {
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Write turnout definition and current position to EEPROM
|
||||
// First write common servo data, then
|
||||
// write the servo-specific data
|
||||
EEPROM.put(EEStore::pointer(), _turnoutData);
|
||||
EEStore::advance(sizeof(_turnoutData));
|
||||
EEPROM.put(EEStore::pointer(), _servoTurnoutData);
|
||||
EEStore::advance(sizeof(_servoTurnoutData));
|
||||
#endif
|
||||
}
|
||||
|
||||
/*************************************************************************************
|
||||
* DCCTurnout - Turnout controlled by DCC Accessory Controller.
|
||||
*
|
||||
*************************************************************************************/
|
||||
|
||||
#if defined(DCC_TURNOUTS_RCN_213)
|
||||
const bool DCCTurnout::rcn213Compliant = true;
|
||||
#else
|
||||
const bool DCCTurnout::rcn213Compliant = false;
|
||||
#endif
|
||||
|
||||
// DCCTurnoutData contains data specific to this subclass that is
|
||||
// written to EEPROM when the turnout is saved.
|
||||
struct DCCTurnoutData {
|
||||
// DCC address (Address in bits 15-2, subaddress in bits 1-0
|
||||
uint16_t address; // CS currently supports linear address 1-2048
|
||||
// That's DCC accessory address 1-512 and subaddress 0-3.
|
||||
} _dccTurnoutData; // 2 bytes
|
||||
|
||||
// Constructor
|
||||
DCCTurnout::DCCTurnout(uint16_t id, uint16_t address, uint8_t subAdd) :
|
||||
Turnout(id, TURNOUT_DCC, false)
|
||||
{
|
||||
_dccTurnoutData.address = address;
|
||||
_dccTurnoutData.subAddress = subAdd;
|
||||
}
|
||||
|
||||
// Create function
|
||||
/* static */ Turnout *DCCTurnout::create(uint16_t id, uint16_t add, uint8_t subAdd) {
|
||||
Turnout *tt = get(id);
|
||||
if (tt) {
|
||||
// Object already exists, check if it is usable
|
||||
if (tt->isType(TURNOUT_DCC)) {
|
||||
// Yes, so set parameters<T>
|
||||
DCCTurnout *dt = (DCCTurnout *)tt;
|
||||
dt->_dccTurnoutData.address = add;
|
||||
dt->_dccTurnoutData.subAddress = subAdd;
|
||||
// Don't touch the _closed parameter, retain the original value.
|
||||
return tt;
|
||||
} else {
|
||||
// Incompatible object, delete and recreate
|
||||
remove(id);
|
||||
}
|
||||
}
|
||||
tt = (Turnout *)new DCCTurnout(id, add, subAdd);
|
||||
return tt;
|
||||
}
|
||||
|
||||
// Load a DCC turnout definition from EEPROM. The common Turnout data has already been read at this point.
|
||||
/* static */ Turnout *DCCTurnout::load(struct TurnoutData *turnoutData) {
|
||||
#ifndef DISABLE_EEPROM
|
||||
DCCTurnoutData dccTurnoutData;
|
||||
// Read class-specific data from EEPROM
|
||||
EEPROM.get(EEStore::pointer(), dccTurnoutData);
|
||||
EEStore::advance(sizeof(dccTurnoutData));
|
||||
|
||||
// Create new object
|
||||
DCCTurnout *tt = new DCCTurnout(turnoutData->id, dccTurnoutData.address, dccTurnoutData.subAddress);
|
||||
|
||||
return tt;
|
||||
#else
|
||||
(void)turnoutData;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
void DCCTurnout::print(Print *stream) {
|
||||
StringFormatter::send(stream, F("<H %d DCC %d %d %d>\n"), _turnoutData.id,
|
||||
_dccTurnoutData.address, _dccTurnoutData.subAddress, !_turnoutData.closed);
|
||||
// Also report using classic DCC++ syntax for DCC accessory turnouts, since JMRI expects this.
|
||||
StringFormatter::send(stream, F("<H %d %d %d %d>\n"), _turnoutData.id,
|
||||
_dccTurnoutData.address, _dccTurnoutData.subAddress, !_turnoutData.closed);
|
||||
}
|
||||
|
||||
bool DCCTurnout::setClosedInternal(bool close) {
|
||||
// DCC++ Classic behaviour is that Throw writes a 1 in the packet,
|
||||
// and Close writes a 0.
|
||||
// RCN-213 specifies that Throw is 0 and Close is 1.
|
||||
DCC::setAccessory(_dccTurnoutData.address, _dccTurnoutData.subAddress, close ^ !rcn213Compliant);
|
||||
_turnoutData.closed = close;
|
||||
return true;
|
||||
}
|
||||
|
||||
void DCCTurnout::save() {
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Write turnout definition and current position to EEPROM
|
||||
// First write common servo data, then
|
||||
// write the servo-specific data
|
||||
EEPROM.put(EEStore::pointer(), _turnoutData);
|
||||
EEStore::advance(sizeof(_turnoutData));
|
||||
EEPROM.put(EEStore::pointer(), _dccTurnoutData);
|
||||
EEStore::advance(sizeof(_dccTurnoutData));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*************************************************************************************
|
||||
* VpinTurnout - Turnout controlled through a HAL vpin.
|
||||
*
|
||||
*************************************************************************************/
|
||||
|
||||
// Constructor
|
||||
VpinTurnout::VpinTurnout(uint16_t id, VPIN vpin, bool closed) :
|
||||
Turnout(id, TURNOUT_VPIN, closed)
|
||||
{
|
||||
_vpinTurnoutData.vpin = vpin;
|
||||
}
|
||||
|
||||
// Create function
|
||||
/* static */ Turnout *VpinTurnout::create(uint16_t id, VPIN vpin, bool closed) {
|
||||
Turnout *tt = get(id);
|
||||
if (tt) {
|
||||
// Object already exists, check if it is usable
|
||||
if (tt->isType(TURNOUT_VPIN)) {
|
||||
// Yes, so set parameters
|
||||
VpinTurnout *vt = (VpinTurnout *)tt;
|
||||
vt->_vpinTurnoutData.vpin = vpin;
|
||||
// Don't touch the _closed parameter, retain the original value.
|
||||
return tt;
|
||||
} else {
|
||||
// Incompatible object, delete and recreate
|
||||
remove(id);
|
||||
}
|
||||
}
|
||||
tt = (Turnout *)new VpinTurnout(id, vpin, closed);
|
||||
return tt;
|
||||
}
|
||||
|
||||
// Load a VPIN turnout definition from EEPROM. The common Turnout data has already been read at this point.
|
||||
/* static */ Turnout *VpinTurnout::load(struct TurnoutData *turnoutData) {
|
||||
#ifndef DISABLE_EEPROM
|
||||
VpinTurnoutData vpinTurnoutData;
|
||||
// Read class-specific data from EEPROM
|
||||
EEPROM.get(EEStore::pointer(), vpinTurnoutData);
|
||||
EEStore::advance(sizeof(vpinTurnoutData));
|
||||
|
||||
// Create new object
|
||||
VpinTurnout *tt = new VpinTurnout(turnoutData->id, vpinTurnoutData.vpin, turnoutData->closed);
|
||||
|
||||
return tt;
|
||||
#else
|
||||
(void)turnoutData;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Report 1 for thrown, 0 for closed.
|
||||
void VpinTurnout::print(Print *stream) {
|
||||
StringFormatter::send(stream, F("<H %d VPIN %d %d>\n"), _turnoutData.id, _vpinTurnoutData.vpin,
|
||||
!_turnoutData.closed);
|
||||
}
|
||||
|
||||
bool VpinTurnout::setClosedInternal(bool close) {
|
||||
IODevice::write(_vpinTurnoutData.vpin, close);
|
||||
_turnoutData.closed = close;
|
||||
return true;
|
||||
}
|
||||
|
||||
void VpinTurnout::save() {
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Write turnout definition and current position to EEPROM
|
||||
// First write common servo data, then
|
||||
// write the servo-specific data
|
||||
EEPROM.put(EEStore::pointer(), _turnoutData);
|
||||
EEStore::advance(sizeof(_turnoutData));
|
||||
EEPROM.put(EEStore::pointer(), _vpinTurnoutData);
|
||||
EEStore::advance(sizeof(_vpinTurnoutData));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************************
|
||||
* LCNTurnout - Turnout controlled by Loconet
|
||||
*
|
||||
*************************************************************************************/
|
||||
|
||||
// LCNTurnout has no specific data, and in any case is not written to EEPROM!
|
||||
// struct LCNTurnoutData {
|
||||
// } _lcnTurnoutData; // 0 bytes
|
||||
|
||||
// Constructor
|
||||
LCNTurnout::LCNTurnout(uint16_t id, bool closed) :
|
||||
Turnout(id, TURNOUT_LCN, closed)
|
||||
{ }
|
||||
|
||||
// Create function
|
||||
/* static */ Turnout *LCNTurnout::create(uint16_t id, bool closed) {
|
||||
Turnout *tt = get(id);
|
||||
if (tt) {
|
||||
// Object already exists, check if it is usable
|
||||
if (tt->isType(TURNOUT_LCN)) {
|
||||
// Yes, so return this object
|
||||
return tt;
|
||||
} else {
|
||||
// Incompatible object, delete and recreate
|
||||
remove(id);
|
||||
}
|
||||
}
|
||||
tt = (Turnout *)new LCNTurnout(id, closed);
|
||||
return tt;
|
||||
}
|
||||
|
||||
bool LCNTurnout::setClosedInternal(bool close) {
|
||||
// Assume that the LCN command still uses 1 for throw and 0 for close...
|
||||
LCN::send('T', _turnoutData.id, !close);
|
||||
// The _turnoutData.closed flag should be updated by a message from the LCN master, later.
|
||||
return true;
|
||||
}
|
||||
|
||||
// LCN turnouts not saved to EEPROM.
|
||||
//void save() override { }
|
||||
//static Turnout *load(struct TurnoutData *turnoutData) {
|
||||
|
||||
// Report 1 for thrown, 0 for closed.
|
||||
void LCNTurnout::print(Print *stream) {
|
||||
StringFormatter::send(stream, F("<H %d LCN %d>\n"), _turnoutData.id,
|
||||
!_turnoutData.closed);
|
||||
}
|
||||
|
||||
|
380
Turnouts.h
380
Turnouts.h
@@ -1,7 +1,13 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 M Steve Todd
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2013-2016 Gregg E. Berman
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -17,109 +23,283 @@
|
||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Turnout data is stored in a structure whose length depends on the
|
||||
* type of turnout. There is a common header of 3 bytes, followed by
|
||||
* 2 bytes for DCC turnout, 5 bytes for servo turnout, 2 bytes for a
|
||||
* VPIN turnout, or zero bytes for an LCN turnout.
|
||||
* The variable length allows the limited space in EEPROM to be used effectively.
|
||||
*/
|
||||
#ifndef TURNOUTS_H
|
||||
#define TURNOUTS_H
|
||||
|
||||
#ifndef Turnouts_h
|
||||
#define Turnouts_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "DCC.h"
|
||||
#include "LCN.h"
|
||||
//#define EESTOREDEBUG
|
||||
#include "Arduino.h"
|
||||
#include "IODevice.h"
|
||||
#include "StringFormatter.h"
|
||||
|
||||
|
||||
const byte STATUS_ACTIVE=0x80; // Flag as activated in tStatus field
|
||||
const byte STATUS_TYPE = 0x7f; // Mask for turnout type in tStatus field
|
||||
|
||||
// The struct 'header' is used to determine the length of the
|
||||
// overlaid data so must be at least as long as the anonymous fields it
|
||||
// is overlaid with.
|
||||
struct TurnoutData {
|
||||
// Header common to all turnouts
|
||||
union {
|
||||
struct {
|
||||
int id;
|
||||
uint8_t tStatus;
|
||||
uint8_t size;
|
||||
} header;
|
||||
|
||||
struct {
|
||||
int id;
|
||||
union {
|
||||
uint8_t tStatus;
|
||||
struct {
|
||||
uint8_t active: 1;
|
||||
uint8_t type: 5;
|
||||
uint8_t :2;
|
||||
};
|
||||
};
|
||||
uint8_t size; // set to actual total length of used structure
|
||||
};
|
||||
};
|
||||
// Turnout-type-specific structure elements, different length depending
|
||||
// on turnout type. This allows the data to be packed efficiently
|
||||
// in the EEPROM.
|
||||
union {
|
||||
struct {
|
||||
// DCC address (Address in bits 15-2, subaddress in bits 1-0
|
||||
uint16_t address; // CS currently supports linear address 1-2048
|
||||
// That's DCC accessory address 1-512 and subaddress 0-3.
|
||||
} dccAccessoryData;
|
||||
|
||||
struct {
|
||||
VPIN vpin;
|
||||
uint16_t activePosition : 12; // 0-4095
|
||||
uint16_t inactivePosition : 12; // 0-4095
|
||||
uint8_t profile;
|
||||
} servoData;
|
||||
|
||||
struct {
|
||||
} lcnData;
|
||||
|
||||
struct {
|
||||
VPIN vpin;
|
||||
} vpinData;
|
||||
};
|
||||
// Turnout type definitions
|
||||
enum {
|
||||
TURNOUT_DCC = 1,
|
||||
TURNOUT_SERVO = 2,
|
||||
TURNOUT_VPIN = 3,
|
||||
TURNOUT_LCN = 4,
|
||||
};
|
||||
|
||||
/*************************************************************************************
|
||||
* Turnout - Base class for turnouts.
|
||||
*
|
||||
*************************************************************************************/
|
||||
|
||||
class Turnout {
|
||||
public:
|
||||
static Turnout *firstTurnout;
|
||||
static int turnoutlistHash;
|
||||
Turnout *nextTurnout;
|
||||
static Turnout* get(int);
|
||||
static bool remove(int);
|
||||
static bool isClosed(int);
|
||||
static bool setClosed(int n, bool closed); // return false if not found.
|
||||
static void setClosedStateOnly(int n, bool closed);
|
||||
int getId();
|
||||
static void load();
|
||||
static void store();
|
||||
static Turnout *createServo(int id , VPIN vpin , uint16_t activeAngle, uint16_t inactiveAngle, uint8_t profile=1, uint8_t initialState=0);
|
||||
static Turnout *createVpin(int id, VPIN vpin, uint8_t initialState=0);
|
||||
static Turnout *createDCC(int id, uint16_t address, uint8_t subAddress);
|
||||
static Turnout *createLCN(int id, uint8_t initialState=0);
|
||||
static Turnout *create(int id, int params, int16_t p[]);
|
||||
static Turnout *create(int id);
|
||||
static void printAll(Print *);
|
||||
void print(Print *stream);
|
||||
#ifdef EESTOREDEBUG
|
||||
static void print(Turnout *tt);
|
||||
#endif
|
||||
private:
|
||||
int num; // EEPROM address of tStatus in TurnoutData struct, or zero if not stored.
|
||||
TurnoutData data;
|
||||
static bool activate(int n, bool thrown);
|
||||
static bool isActive(int);
|
||||
bool isActive();
|
||||
void activate(bool state);
|
||||
void setActive(bool state);
|
||||
}; // Turnout
|
||||
protected:
|
||||
/*
|
||||
* Object data
|
||||
*/
|
||||
|
||||
// The TurnoutData struct contains data common to all turnout types, that
|
||||
// is written to EEPROM when the turnout is saved.
|
||||
// The first byte of this struct contains the 'closed' flag which is
|
||||
// updated whenever the turnout changes from thrown to closed and
|
||||
// vice versa. If the turnout has been saved, then this byte is rewritten
|
||||
// when changed in RAM. The 'closed' flag must be located in the first byte.
|
||||
struct TurnoutData {
|
||||
union {
|
||||
struct {
|
||||
bool closed : 1;
|
||||
bool _rfu: 2;
|
||||
uint8_t turnoutType : 5;
|
||||
};
|
||||
uint8_t flags;
|
||||
};
|
||||
uint16_t id;
|
||||
} _turnoutData; // 3 bytes
|
||||
|
||||
// Address in eeprom of first byte of the _turnoutData struct (containing the closed flag).
|
||||
// Set to zero if the object has not been saved in EEPROM, e.g. for newly created Turnouts, and
|
||||
// for all LCN turnouts.
|
||||
uint16_t _eepromAddress = 0;
|
||||
|
||||
// Pointer to next turnout on linked list.
|
||||
Turnout *_nextTurnout = 0;
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
Turnout(uint16_t id, uint8_t turnoutType, bool closed) {
|
||||
_turnoutData.id = id;
|
||||
_turnoutData.turnoutType = turnoutType;
|
||||
_turnoutData.closed = closed;
|
||||
add(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Static data
|
||||
*/
|
||||
|
||||
static Turnout *_firstTurnout;
|
||||
static int _turnoutlistHash;
|
||||
|
||||
/*
|
||||
* Virtual functions
|
||||
*/
|
||||
|
||||
virtual bool setClosedInternal(bool close) = 0; // Mandatory in subclass
|
||||
virtual void save() {}
|
||||
|
||||
/*
|
||||
* Static functions
|
||||
*/
|
||||
|
||||
static Turnout *get(uint16_t id);
|
||||
|
||||
static void add(Turnout *tt);
|
||||
|
||||
public:
|
||||
/*
|
||||
* Static data
|
||||
*/
|
||||
static int turnoutlistHash;
|
||||
static const bool useClassicTurnoutCommands;
|
||||
|
||||
/*
|
||||
* Public base class functions
|
||||
*/
|
||||
inline bool isClosed() { return _turnoutData.closed; };
|
||||
inline bool isThrown() { return !_turnoutData.closed; }
|
||||
inline bool isType(uint8_t type) { return _turnoutData.turnoutType == type; }
|
||||
inline uint16_t getId() { return _turnoutData.id; }
|
||||
inline Turnout *next() { return _nextTurnout; }
|
||||
void printState(Print *stream);
|
||||
/*
|
||||
* Virtual functions
|
||||
*/
|
||||
virtual void print(Print *stream) {
|
||||
(void)stream; // avoid compiler warnings.
|
||||
}
|
||||
virtual ~Turnout() {} // Destructor
|
||||
|
||||
/*
|
||||
* Public static functions
|
||||
*/
|
||||
inline static bool exists(uint16_t id) { return get(id) != 0; }
|
||||
|
||||
static bool remove(uint16_t id);
|
||||
|
||||
static bool isClosed(uint16_t id);
|
||||
|
||||
inline static bool isThrown(uint16_t id) {
|
||||
return !isClosed(id);
|
||||
}
|
||||
|
||||
static bool setClosed(uint16_t id, bool closeFlag);
|
||||
|
||||
inline static bool setClosed(uint16_t id) {
|
||||
return setClosed(id, true);
|
||||
}
|
||||
|
||||
inline static bool setThrown(uint16_t id) {
|
||||
return setClosed(id, false);
|
||||
}
|
||||
|
||||
static bool setClosedStateOnly(uint16_t id, bool close);
|
||||
|
||||
inline static Turnout *first() { return _firstTurnout; }
|
||||
|
||||
#ifndef DISABLE_EEPROM
|
||||
// Load all turnout definitions.
|
||||
static void load();
|
||||
// Load one turnout definition
|
||||
static Turnout *loadTurnout();
|
||||
// Save all turnout definitions
|
||||
static void store();
|
||||
#endif
|
||||
static void printAll(Print *stream) {
|
||||
for (Turnout *tt = _firstTurnout; tt != 0; tt = tt->_nextTurnout)
|
||||
StringFormatter::send(stream, F("<H %d %d>\n"),tt->getId(), tt->isThrown());
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*************************************************************************************
|
||||
* ServoTurnout - Turnout controlled by servo device.
|
||||
*
|
||||
*************************************************************************************/
|
||||
class ServoTurnout : public Turnout {
|
||||
private:
|
||||
// ServoTurnoutData contains data specific to this subclass that is
|
||||
// written to EEPROM when the turnout is saved.
|
||||
struct ServoTurnoutData {
|
||||
VPIN vpin;
|
||||
uint16_t closedPosition : 12;
|
||||
uint16_t thrownPosition : 12;
|
||||
uint8_t profile;
|
||||
} _servoTurnoutData; // 6 bytes
|
||||
|
||||
// Constructor
|
||||
ServoTurnout(uint16_t id, VPIN vpin, uint16_t thrownPosition, uint16_t closedPosition, uint8_t profile, bool closed);
|
||||
|
||||
public:
|
||||
// Create function
|
||||
static Turnout *create(uint16_t id, VPIN vpin, uint16_t thrownPosition, uint16_t closedPosition, uint8_t profile, bool closed=true);
|
||||
|
||||
// Load a Servo turnout definition from EEPROM. The common Turnout data has already been read at this point.
|
||||
static Turnout *load(struct TurnoutData *turnoutData);
|
||||
void print(Print *stream) override;
|
||||
|
||||
protected:
|
||||
// ServoTurnout-specific code for throwing or closing a servo turnout.
|
||||
bool setClosedInternal(bool close) override;
|
||||
void save() override;
|
||||
|
||||
};
|
||||
|
||||
/*************************************************************************************
|
||||
* DCCTurnout - Turnout controlled by DCC Accessory Controller.
|
||||
*
|
||||
*************************************************************************************/
|
||||
class DCCTurnout : public Turnout {
|
||||
private:
|
||||
// DCCTurnoutData contains data specific to this subclass that is
|
||||
// written to EEPROM when the turnout is saved.
|
||||
struct DCCTurnoutData {
|
||||
// DCC address (Address in bits 15-2, subaddress in bits 1-0)
|
||||
struct {
|
||||
uint16_t address : 14;
|
||||
uint8_t subAddress : 2;
|
||||
};
|
||||
} _dccTurnoutData; // 2 bytes
|
||||
|
||||
// Constructor
|
||||
DCCTurnout(uint16_t id, uint16_t address, uint8_t subAdd);
|
||||
|
||||
public:
|
||||
// Create function
|
||||
static Turnout *create(uint16_t id, uint16_t add, uint8_t subAdd);
|
||||
// Load a VPIN turnout definition from EEPROM. The common Turnout data has already been read at this point.
|
||||
static Turnout *load(struct TurnoutData *turnoutData);
|
||||
void print(Print *stream) override;
|
||||
// Flag whether DCC Accessory packets are to contain 1=close/0=throw(RCN-213) or 1=throw/0-close (DCC++ Classic)
|
||||
static const bool rcn213Compliant;
|
||||
|
||||
protected:
|
||||
bool setClosedInternal(bool close) override;
|
||||
void save() override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*************************************************************************************
|
||||
* VpinTurnout - Turnout controlled through a HAL vpin.
|
||||
*
|
||||
*************************************************************************************/
|
||||
class VpinTurnout : public Turnout {
|
||||
private:
|
||||
// VpinTurnoutData contains data specific to this subclass that is
|
||||
// written to EEPROM when the turnout is saved.
|
||||
struct VpinTurnoutData {
|
||||
VPIN vpin;
|
||||
} _vpinTurnoutData; // 2 bytes
|
||||
|
||||
// Constructor
|
||||
VpinTurnout(uint16_t id, VPIN vpin, bool closed);
|
||||
|
||||
public:
|
||||
// Create function
|
||||
static Turnout *create(uint16_t id, VPIN vpin, bool closed=true);
|
||||
|
||||
// Load a VPIN turnout definition from EEPROM. The common Turnout data has already been read at this point.
|
||||
static Turnout *load(struct TurnoutData *turnoutData);
|
||||
void print(Print *stream) override;
|
||||
|
||||
protected:
|
||||
bool setClosedInternal(bool close) override;
|
||||
void save() override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*************************************************************************************
|
||||
* LCNTurnout - Turnout controlled by Loconet
|
||||
*
|
||||
*************************************************************************************/
|
||||
class LCNTurnout : public Turnout {
|
||||
private:
|
||||
// LCNTurnout has no specific data, and in any case is not written to EEPROM!
|
||||
// struct LCNTurnoutData {
|
||||
// } _lcnTurnoutData; // 0 bytes
|
||||
|
||||
// Constructor
|
||||
LCNTurnout(uint16_t id, bool closed);
|
||||
|
||||
public:
|
||||
// Create function
|
||||
static Turnout *create(uint16_t id, bool closed=true);
|
||||
|
||||
|
||||
bool setClosedInternal(bool close) override;
|
||||
|
||||
// LCN turnouts not saved to EEPROM.
|
||||
//void save() override { }
|
||||
//static Turnout *load(struct TurnoutData *turnoutData) {
|
||||
|
||||
void print(Print *stream) override;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
599
WiThrottle.cpp
599
WiThrottle.cpp
@@ -1,8 +1,12 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Mike S
|
||||
* © 2020-2022 Harald Barth
|
||||
* © 2020-2021 M Steve Todd
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -50,7 +54,7 @@
|
||||
#include "GITHUB_SHA.h"
|
||||
#include "version.h"
|
||||
#include "RMFT2.h"
|
||||
|
||||
#include "CommandDistributor.h"
|
||||
|
||||
#define LOOPLOCOS(THROTTLECHAR, CAB) for (int loco=0;loco<MAX_MY_LOCO;loco++) \
|
||||
if ((myLocos[loco].throttle==THROTTLECHAR || '*'==THROTTLECHAR) && (CAB<0 || myLocos[loco].cab==CAB))
|
||||
@@ -111,18 +115,18 @@ void WiThrottle::parse(RingStream * stream, byte * cmdx) {
|
||||
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d)<-[%e]"),millis(),clientid,cmd);
|
||||
|
||||
if (initSent) {
|
||||
// Send power state if different than last sent
|
||||
bool currentPowerState = (DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
|
||||
if (lastPowerState != currentPowerState) {
|
||||
StringFormatter::send(stream,F("PPA%x\n"),currentPowerState);
|
||||
lastPowerState = currentPowerState;
|
||||
}
|
||||
// Send turnout list if changed since last sent (will replace list on client)
|
||||
if (turnoutListHash != Turnout::turnoutlistHash) {
|
||||
StringFormatter::send(stream,F("PTL"));
|
||||
for(Turnout *tt=Turnout::firstTurnout;tt!=NULL;tt=tt->nextTurnout){
|
||||
for(Turnout *tt=Turnout::first();tt!=NULL;tt=tt->next()){
|
||||
int id=tt->getId();
|
||||
StringFormatter::send(stream,F("]\\[%d}|{%d}|{%c"), id, id, Turnout::isClosed(id)?'2':'4');
|
||||
StringFormatter::send(stream,F("]\\[%d}|{"), id);
|
||||
#ifdef RMFT_ACTIVE
|
||||
RMFT2::emitTurnoutDescription(stream,id);
|
||||
#else
|
||||
StringFormatter::send(stream,F("%d"), id);
|
||||
#endif
|
||||
StringFormatter::send(stream,F("}|{%c"), Turnout::isClosed(id)?'2':'4');
|
||||
}
|
||||
StringFormatter::send(stream,F("\n"));
|
||||
turnoutListHash = Turnout::turnoutlistHash; // keep a copy of hash for later comparison
|
||||
@@ -133,100 +137,105 @@ void WiThrottle::parse(RingStream * stream, byte * cmdx) {
|
||||
exRailSent=true;
|
||||
#ifdef RMFT_ACTIVE
|
||||
RMFT2::emitWithrottleRouteList(stream);
|
||||
#endif
|
||||
#endif
|
||||
// allow heartbeat to slow down once all metadata sent
|
||||
StringFormatter::send(stream,F("*%d\n"),HEARTBEAT_SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
while (cmd[0]) {
|
||||
switch (cmd[0]) {
|
||||
case '*': // heartbeat control
|
||||
if (cmd[1]=='+') heartBeatEnable=true;
|
||||
else if (cmd[1]=='-') heartBeatEnable=false;
|
||||
break;
|
||||
case 'P':
|
||||
if (cmd[1]=='P' && cmd[2]=='A' ) { //PPA power mode
|
||||
DCCWaveform::mainTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF);
|
||||
if (MotorDriver::commonFaultPin) // commonFaultPin prevents individual track handling
|
||||
DCCWaveform::progTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF);
|
||||
StringFormatter::send(stream,F("PPA%x\n"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
|
||||
lastPowerState = (DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON); //remember power state sent for comparison later
|
||||
}
|
||||
|
||||
while (cmd[0]) {
|
||||
switch (cmd[0]) {
|
||||
case '*': // heartbeat control
|
||||
if (cmd[1]=='+') heartBeatEnable=true;
|
||||
else if (cmd[1]=='-') heartBeatEnable=false;
|
||||
break;
|
||||
case 'P':
|
||||
if (cmd[1]=='P' && cmd[2]=='A' ) { //PPA power mode
|
||||
DCCWaveform::mainTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF);
|
||||
if (MotorDriver::commonFaultPin) // commonFaultPin prevents individual track handling
|
||||
DCCWaveform::progTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF);
|
||||
CommandDistributor::broadcastPower();
|
||||
}
|
||||
#if defined(RMFT_ACTIVE)
|
||||
else if (cmd[1]=='R' && cmd[2]=='A' && cmd[3]=='2' ) { // Route activate
|
||||
// exrail routes are RA2Rn , Animations are RA2An
|
||||
int route=getInt(cmd+5);
|
||||
uint16_t cab=cmd[4]=='A' ? mostRecentCab : 0;
|
||||
RMFT2::createNewTask(route, cab);
|
||||
}
|
||||
else if (cmd[1]=='R' && cmd[2]=='A' && cmd[3]=='2' ) { // Route activate
|
||||
// exrail routes are RA2Rn , Animations are RA2An
|
||||
int route=getInt(cmd+5);
|
||||
uint16_t cab=cmd[4]=='A' ? mostRecentCab : 0;
|
||||
RMFT2::createNewTask(route, cab);
|
||||
}
|
||||
#endif
|
||||
else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle
|
||||
int id=getInt(cmd+4);
|
||||
Turnout * tt=Turnout::get(id);
|
||||
if (!tt) {
|
||||
// If turnout does not exist, create it
|
||||
int addr = ((id - 1) / 4) + 1;
|
||||
int subaddr = (id - 1) % 4;
|
||||
Turnout::createDCC(id,addr,subaddr);
|
||||
StringFormatter::send(stream, F("HmTurnout %d created\n"),id);
|
||||
}
|
||||
switch (cmd[3]) {
|
||||
case 'T':
|
||||
Turnout::setClosed(id,false);
|
||||
break;
|
||||
case 'C':
|
||||
Turnout::setClosed(id,true);
|
||||
break;
|
||||
case '2':
|
||||
Turnout::setClosed(id,!Turnout::isClosed(id));
|
||||
break;
|
||||
default :
|
||||
Turnout::setClosed(id,true);
|
||||
break;
|
||||
}
|
||||
StringFormatter::send(stream, F("PTA%c%d\n"),Turnout::isClosed(id)?'2':'4',id );
|
||||
}
|
||||
break;
|
||||
case 'N': // Heartbeat (2), only send if connection completed by 'HU' message
|
||||
if (initSent) {
|
||||
StringFormatter::send(stream, F("*%d\n"),HEARTBEAT_SECONDS); // return timeout value
|
||||
}
|
||||
break;
|
||||
case 'M': // multithrottle
|
||||
multithrottle(stream, cmd);
|
||||
break;
|
||||
case 'H': // send initial connection info after receiving "HU" message
|
||||
if (cmd[1] == 'U') {
|
||||
StringFormatter::send(stream,F("VN2.0\nHTDCC-EX\nRL0\n"));
|
||||
StringFormatter::send(stream,F("HtDCC-EX v%S, %S, %S, %S\n"), F(VERSION), F(ARDUINO_TYPE), DCC::getMotorShieldName(), F(GITHUB_SHA));
|
||||
StringFormatter::send(stream,F("PTT]\\[Turnouts}|{Turnout]\\[THROW}|{2]\\[CLOSE}|{4\n"));
|
||||
StringFormatter::send(stream,F("PPA%x\n"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
|
||||
lastPowerState = (DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON); //remember power state sent for comparison later
|
||||
StringFormatter::send(stream,F("*%d\n"),HEARTBEAT_SECONDS);
|
||||
initSent = true;
|
||||
}
|
||||
break;
|
||||
case 'Q': //
|
||||
LOOPLOCOS('*', -1) { // tell client to drop any locos still assigned to this WiThrottle
|
||||
if (myLocos[loco].throttle!='\0') {
|
||||
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), myLocos[loco].throttle, LorS(myLocos[loco].cab), myLocos[loco].cab);
|
||||
}
|
||||
}
|
||||
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d) Quit"),millis(),clientid);
|
||||
delete this;
|
||||
break;
|
||||
}
|
||||
// skip over cmd until 0 or past \r or \n
|
||||
while(*cmd !='\0' && *cmd != '\r' && *cmd !='\n') cmd++;
|
||||
if (*cmd!='\0') cmd++; // skip \r or \n
|
||||
}
|
||||
}
|
||||
int WiThrottle::getInt(byte * cmd) {
|
||||
int i=0;
|
||||
while (cmd[0]>='0' && cmd[0]<='9') {
|
||||
i=i*10 + (cmd[0]-'0');
|
||||
cmd++;
|
||||
else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle
|
||||
int id=getInt(cmd+4);
|
||||
if (!Turnout::exists(id)) {
|
||||
// If turnout does not exist, create it
|
||||
int addr = ((id - 1) / 4) + 1;
|
||||
int subaddr = (id - 1) % 4;
|
||||
DCCTurnout::create(id,addr,subaddr);
|
||||
StringFormatter::send(stream, F("HmTurnout %d created\n"),id);
|
||||
}
|
||||
switch (cmd[3]) {
|
||||
// T and C according to RCN-213 where 0 is Stop, Red, Thrown, Diverging.
|
||||
case 'T':
|
||||
Turnout::setClosed(id,false);
|
||||
break;
|
||||
case 'C':
|
||||
Turnout::setClosed(id,true);
|
||||
break;
|
||||
case '2':
|
||||
Turnout::setClosed(id,!Turnout::isClosed(id));
|
||||
break;
|
||||
default :
|
||||
Turnout::setClosed(id,true);
|
||||
break;
|
||||
}
|
||||
StringFormatter::send(stream, F("PTA%c%d\n"),Turnout::isClosed(id)?'2':'4',id );
|
||||
}
|
||||
break;
|
||||
case 'N': // Heartbeat (2), only send if connection completed by 'HU' message
|
||||
if (initSent) {
|
||||
StringFormatter::send(stream, F("*%d\n"),HEARTBEAT_SECONDS); // return timeout value
|
||||
}
|
||||
break;
|
||||
case 'M': // multithrottle
|
||||
multithrottle(stream, cmd);
|
||||
break;
|
||||
case 'H': // send initial connection info after receiving "HU" message
|
||||
if (cmd[1] == 'U') {
|
||||
StringFormatter::send(stream,F("VN2.0\nHTDCC-EX\nRL0\n"));
|
||||
StringFormatter::send(stream,F("HtDCC-EX v%S, %S, %S, %S\n"), F(VERSION), F(ARDUINO_TYPE), DCC::getMotorShieldName(), F(GITHUB_SHA));
|
||||
StringFormatter::send(stream,F("PTT]\\[Turnouts}|{Turnout]\\[THROW}|{2]\\[CLOSE}|{4\n"));
|
||||
StringFormatter::send(stream,F("PPA%x\n"),DCCWaveform::mainTrack.getPowerMode()==POWERMODE::ON);
|
||||
#ifdef RMFT_ACTIVE
|
||||
RMFT2::emitWithrottleRoster(stream);
|
||||
#endif
|
||||
// set heartbeat to 1 second because we need to sync the metadata
|
||||
StringFormatter::send(stream,F("*1\n"));
|
||||
initSent = true;
|
||||
}
|
||||
break;
|
||||
case 'Q': //
|
||||
LOOPLOCOS('*', -1) { // tell client to drop any locos still assigned to this WiThrottle
|
||||
if (myLocos[loco].throttle!='\0') {
|
||||
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), myLocos[loco].throttle, LorS(myLocos[loco].cab), myLocos[loco].cab);
|
||||
}
|
||||
}
|
||||
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d) Quit"),millis(),clientid);
|
||||
delete this;
|
||||
break;
|
||||
}
|
||||
return i;
|
||||
// skip over cmd until 0 or past \r or \n
|
||||
while(*cmd !='\0' && *cmd != '\r' && *cmd !='\n') cmd++;
|
||||
if (*cmd!='\0') cmd++; // skip \r or \n
|
||||
}
|
||||
}
|
||||
|
||||
int WiThrottle::getInt(byte * cmd) {
|
||||
int i=0;
|
||||
while (cmd[0]>='0' && cmd[0]<='9') {
|
||||
i=i*10 + (cmd[0]-'0');
|
||||
cmd++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int WiThrottle::getLocoId(byte * cmd) {
|
||||
@@ -236,143 +245,183 @@ int WiThrottle::getLocoId(byte * cmd) {
|
||||
}
|
||||
|
||||
void WiThrottle::multithrottle(RingStream * stream, byte * cmd){
|
||||
char throttleChar=cmd[1];
|
||||
int locoid=getLocoId(cmd+3); // -1 for *
|
||||
byte * aval=cmd;
|
||||
while(*aval !=';' && *aval !='\0') aval++;
|
||||
if (*aval) aval+=2; // skip ;>
|
||||
|
||||
// DIAG(F("Multithrottle aval=%c cab=%d"), aval[0],locoid);
|
||||
switch(cmd[2]) {
|
||||
case '+': // add loco request
|
||||
if (cmd[3]=='*') {
|
||||
// M+* means get loco from prog track, then join tracks ready to drive away
|
||||
// Stash the things the callback will need later
|
||||
stashStream= stream;
|
||||
stashClient=stream->peekTargetMark();
|
||||
stashThrottleChar=throttleChar;
|
||||
stashInstance=this;
|
||||
// ask DCC to call us back when the loco id has been read
|
||||
DCC::getLocoId(getLocoCallback); // will remove any previous join
|
||||
return; // return nothing in stream as response is sent later in the callback
|
||||
}
|
||||
//return error if address zero requested
|
||||
if (locoid==0) {
|
||||
StringFormatter::send(stream, F("HMAddress '0' not supported!\n"), cmd[3] ,locoid);
|
||||
return;
|
||||
}
|
||||
//return error if L or S from request doesn't match DCC++ assumptions
|
||||
if (cmd[3] != LorS(locoid)) {
|
||||
StringFormatter::send(stream, F("HMLength '%c' not valid for %d!\n"), cmd[3] ,locoid);
|
||||
return;
|
||||
}
|
||||
//use first empty "slot" on this client's list, will be added to DCC registration list
|
||||
for (int loco=0;loco<MAX_MY_LOCO;loco++) {
|
||||
if (myLocos[loco].throttle=='\0') {
|
||||
myLocos[loco].throttle=throttleChar;
|
||||
myLocos[loco].cab=locoid;
|
||||
mostRecentCab=locoid;
|
||||
StringFormatter::send(stream, F("M%c+%c%d<;>\n"), throttleChar, cmd[3] ,locoid); //tell client to add loco
|
||||
//Get known Fn states from DCC
|
||||
for(int fKey=0; fKey<=28; fKey++) {
|
||||
char throttleChar=cmd[1];
|
||||
int locoid=getLocoId(cmd+3); // -1 for *
|
||||
byte * aval=cmd;
|
||||
while(*aval !=';' && *aval !='\0') aval++;
|
||||
if (*aval) aval+=2; // skip ;>
|
||||
|
||||
// DIAG(F("Multithrottle aval=%c cab=%d"), aval[0],locoid);
|
||||
switch(cmd[2]) {
|
||||
case '+': // add loco request
|
||||
if (cmd[3]=='*') {
|
||||
// M+* means get loco from prog track, then join tracks ready to drive away
|
||||
// Stash the things the callback will need later
|
||||
stashStream= stream;
|
||||
stashClient=stream->peekTargetMark();
|
||||
stashThrottleChar=throttleChar;
|
||||
stashInstance=this;
|
||||
// ask DCC to call us back when the loco id has been read
|
||||
DCC::getLocoId(getLocoCallback); // will remove any previous join
|
||||
return; // return nothing in stream as response is sent later in the callback
|
||||
}
|
||||
//return error if address zero requested
|
||||
if (locoid==0) {
|
||||
StringFormatter::send(stream, F("HMAddress '0' not supported!\n"), cmd[3] ,locoid);
|
||||
return;
|
||||
}
|
||||
//return error if L or S from request doesn't match DCC++ assumptions
|
||||
if (cmd[3] != LorS(locoid)) {
|
||||
StringFormatter::send(stream, F("HMLength '%c' not valid for %d!\n"), cmd[3] ,locoid);
|
||||
return;
|
||||
}
|
||||
//use first empty "slot" on this client's list, will be added to DCC registration list
|
||||
for (int loco=0;loco<MAX_MY_LOCO;loco++) {
|
||||
if (myLocos[loco].throttle=='\0') {
|
||||
myLocos[loco].throttle=throttleChar;
|
||||
myLocos[loco].cab=locoid;
|
||||
myLocos[loco].functionMap=DCC::getFunctionMap(locoid);
|
||||
myLocos[loco].broadcastPending=true; // means speed/dir will be sent later
|
||||
mostRecentCab=locoid;
|
||||
StringFormatter::send(stream, F("M%c+%c%d<;>\n"), throttleChar, cmd[3] ,locoid); //tell client to add loco
|
||||
int fkeys=29;
|
||||
myLocos[loco].functionToggles=1<<2; // F2 (HORN) is a non-toggle
|
||||
|
||||
#ifdef RMFT_ACTIVE
|
||||
const char * functionNames=(char *) RMFT2::getRosterFunctions(locoid);
|
||||
if (!functionNames) {
|
||||
// no roster, use presets as above
|
||||
}
|
||||
else if (GETFLASH(functionNames)=='\0') {
|
||||
// "" = Roster but no functions given
|
||||
fkeys=0;
|
||||
}
|
||||
else {
|
||||
// we have function names...
|
||||
// scan names list emitting names, counting functions and
|
||||
// flagging non-toggling things like horn.
|
||||
myLocos[loco].functionToggles =0;
|
||||
StringFormatter::send(stream, F("M%cL%c%d<;>]\\["), throttleChar,cmd[3],locoid);
|
||||
fkeys=0;
|
||||
bool firstchar=true;
|
||||
for (int fx=0;;fx++) {
|
||||
char c=GETFLASH(functionNames+fx);
|
||||
if (c=='\0') {
|
||||
fkeys++;
|
||||
break;
|
||||
}
|
||||
if (c=='/') {
|
||||
fkeys++;
|
||||
StringFormatter::send(stream,F("]\\["));
|
||||
firstchar=true;
|
||||
}
|
||||
else if (firstchar && c=='*') {
|
||||
myLocos[loco].functionToggles |= 1UL<<fkeys;
|
||||
firstchar=false;
|
||||
}
|
||||
else {
|
||||
firstchar=false;
|
||||
stream->write(c);
|
||||
}
|
||||
}
|
||||
StringFormatter::send(stream,F("\n"));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
for(int fKey=0; fKey<fkeys; fKey++) {
|
||||
int fstate=DCC::getFn(locoid,fKey);
|
||||
if (fstate>=0) StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"),throttleChar,cmd[3],locoid,fstate,fKey);
|
||||
}
|
||||
StringFormatter::send(stream, F("M%cA%c%d<;>V%d\n"), throttleChar, cmd[3], locoid, DCCToWiTSpeed(DCC::getThrottleSpeed(locoid)));
|
||||
StringFormatter::send(stream, F("M%cA%c%d<;>R%d\n"), throttleChar, cmd[3], locoid, DCC::getThrottleDirection(locoid));
|
||||
StringFormatter::send(stream, F("M%cA%c%d<;>s1\n"), throttleChar, cmd[3], locoid); //default speed step 128
|
||||
return;
|
||||
}
|
||||
}
|
||||
StringFormatter::send(stream, F("HMMax locos (%d) exceeded, %d not added!\n"), MAX_MY_LOCO ,locoid);
|
||||
break;
|
||||
case '-': // remove loco(s) from this client (leave in DCC registration)
|
||||
LOOPLOCOS(throttleChar, locoid) {
|
||||
myLocos[loco].throttle='\0';
|
||||
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab);
|
||||
}
|
||||
|
||||
break;
|
||||
case 'A':
|
||||
locoAction(stream,aval, throttleChar, locoid);
|
||||
}
|
||||
if (fstate>=0) StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"),throttleChar,cmd[3],locoid,fstate,fKey);
|
||||
}
|
||||
//speed and direction will be published at next broadcast cycle
|
||||
StringFormatter::send(stream, F("M%cA%c%d<;>s1\n"), throttleChar, cmd[3], locoid); //default speed step 128
|
||||
return;
|
||||
}
|
||||
}
|
||||
StringFormatter::send(stream, F("HMMax locos (%d) exceeded, %d not added!\n"), MAX_MY_LOCO ,locoid);
|
||||
break;
|
||||
case '-': // remove loco(s) from this client (leave in DCC registration)
|
||||
LOOPLOCOS(throttleChar, locoid) {
|
||||
myLocos[loco].throttle='\0';
|
||||
StringFormatter::send(stream, F("M%c-%c%d<;>\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab);
|
||||
}
|
||||
|
||||
break;
|
||||
case 'A':
|
||||
locoAction(stream,aval, throttleChar, locoid);
|
||||
}
|
||||
}
|
||||
|
||||
void WiThrottle::locoAction(RingStream * stream, byte* aval, char throttleChar, int cab){
|
||||
// Note cab=-1 for all cabs in the consist called throttleChar.
|
||||
// DIAG(F("Loco Action aval=%c%c throttleChar=%c, cab=%d"), aval[0],aval[1],throttleChar, cab);
|
||||
switch (aval[0]) {
|
||||
case 'V': // Vspeed
|
||||
{
|
||||
int witSpeed=getInt(aval+1);
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
mostRecentCab=myLocos[loco].cab;
|
||||
DCC::setThrottle(myLocos[loco].cab, WiTToDCCSpeed(witSpeed), DCC::getThrottleDirection(myLocos[loco].cab));
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, witSpeed);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'F': //F onOff function
|
||||
{
|
||||
bool funcstate;
|
||||
bool pressed=aval[1]=='1';
|
||||
int fKey = getInt(aval+2);
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
funcstate = DCC::changeFn(myLocos[loco].cab, fKey, pressed);
|
||||
if(funcstate==0 || funcstate==1)
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"), throttleChar, LorS(myLocos[loco].cab),
|
||||
myLocos[loco].cab, funcstate, fKey);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
if (aval[1]=='V') { //qV
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, DCCToWiTSpeed(DCC::getThrottleSpeed(myLocos[loco].cab)));
|
||||
}
|
||||
}
|
||||
else if (aval[1]=='R') { // qR
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>R%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, DCC::getThrottleDirection(myLocos[loco].cab));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'R':
|
||||
{
|
||||
bool forward=aval[1]!='0';
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
mostRecentCab=myLocos[loco].cab;
|
||||
DCC::setThrottle(myLocos[loco].cab, DCC::getThrottleSpeed(myLocos[loco].cab), forward);
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>R%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, forward);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'X':
|
||||
//Emergency Stop (speed code 1)
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab));
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, -1);
|
||||
}
|
||||
break;
|
||||
case 'I': // Idle, set speed to 0
|
||||
case 'Q': // Quit, set speed to 0
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
mostRecentCab=myLocos[loco].cab;
|
||||
DCC::setThrottle(myLocos[loco].cab, 0, DCC::getThrottleDirection(myLocos[loco].cab));
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"), throttleChar, LorS(myLocos[loco].cab), myLocos[loco].cab, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Note cab=-1 for all cabs in the consist called throttleChar.
|
||||
// DIAG(F("Loco Action aval=%c%c throttleChar=%c, cab=%d"), aval[0],aval[1],throttleChar, cab);
|
||||
(void) stream;
|
||||
switch (aval[0]) {
|
||||
case 'V': // Vspeed
|
||||
{
|
||||
int witSpeed=getInt(aval+1);
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
mostRecentCab=myLocos[loco].cab;
|
||||
DCC::setThrottle(myLocos[loco].cab, WiTToDCCSpeed(witSpeed), DCC::getThrottleDirection(myLocos[loco].cab));
|
||||
// SetThrottle will cause speed change broadcast
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'F': // Function key pressed/released
|
||||
{
|
||||
bool pressed=aval[1]=='1';
|
||||
int fKey = getInt(aval+2);
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
bool unsetOnRelease = myLocos[loco].functionToggles & (1L<<fKey);
|
||||
if (unsetOnRelease) DCC::setFn(myLocos[loco].cab,fKey, pressed);
|
||||
else if (pressed) DCC::changeFn(myLocos[loco].cab, fKey);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'q':
|
||||
if (aval[1]=='V' || aval[1]=='R' ) { //qV or qR
|
||||
// just flag the loco for broadcast and it will happen.
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
myLocos[loco].broadcastPending=true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'R':
|
||||
{
|
||||
bool forward=aval[1]!='0';
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
mostRecentCab=myLocos[loco].cab;
|
||||
DCC::setThrottle(myLocos[loco].cab, DCC::getThrottleSpeed(myLocos[loco].cab), forward);
|
||||
// setThrottle will cause a broadcast so notification will be sent
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'X':
|
||||
//Emergency Stop (speed code 1)
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
DCC::setThrottle(myLocos[loco].cab, 1, DCC::getThrottleDirection(myLocos[loco].cab));
|
||||
// setThrottle will cause a broadcast so notification will be sent
|
||||
}
|
||||
break;
|
||||
case 'I': // Idle, set speed to 0
|
||||
case 'Q': // Quit, set speed to 0
|
||||
LOOPLOCOS(throttleChar, cab) {
|
||||
mostRecentCab=myLocos[loco].cab;
|
||||
DCC::setThrottle(myLocos[loco].cab, 0, DCC::getThrottleDirection(myLocos[loco].cab));
|
||||
// setThrottle will cause a broadcast so notification will be sent
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// convert between DCC++ speed values and WiThrottle speed values
|
||||
// convert between DCC++ speed values and WiThrottle speed values
|
||||
int WiThrottle::DCCToWiTSpeed(int DCCSpeed) {
|
||||
if (DCCSpeed == 0) return 0; //stop is stop
|
||||
if (DCCSpeed == 1) return -1; //eStop value
|
||||
return DCCSpeed - 1; //offset others by 1
|
||||
}
|
||||
|
||||
// convert between WiThrottle speed values and DCC++ speed values
|
||||
// convert between WiThrottle speed values and DCC++ speed values
|
||||
int WiThrottle::WiTToDCCSpeed(int WiTSpeed) {
|
||||
if (WiTSpeed == 0) return 0; //stop is stop
|
||||
if (WiTSpeed == -1) return 1; //eStop value
|
||||
@@ -380,24 +429,17 @@ int WiThrottle::WiTToDCCSpeed(int WiTSpeed) {
|
||||
}
|
||||
|
||||
void WiThrottle::loop(RingStream * stream) {
|
||||
// for each WiThrottle, check the heartbeat
|
||||
// for each WiThrottle, check the heartbeat and broadcast needed
|
||||
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle)
|
||||
wt->checkHeartbeat();
|
||||
|
||||
// TODO... any broadcasts to be done
|
||||
(void)stream;
|
||||
/* MUST follow this model in this loop.
|
||||
* stream->mark();
|
||||
* send 1 digit client id, and any data
|
||||
* stream->commit()
|
||||
*/
|
||||
wt->checkHeartbeat(stream);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void WiThrottle::checkHeartbeat() {
|
||||
void WiThrottle::checkHeartbeat(RingStream * stream) {
|
||||
// if eStop time passed... eStop any locos still assigned to this client and then drop the connection
|
||||
if(heartBeatEnable && (millis()-heartBeat > ESTOP_SECONDS*1000)) {
|
||||
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d) eStop(%ds) timeout, drop connection"), millis(), clientid, ESTOP_SECONDS);
|
||||
if (Diag::WITHROTTLE) DIAG(F("%l WiThrottle(%d) eStop(%ds) timeout, drop connection"), millis(), clientid, ESTOP_SECONDS);
|
||||
LOOPLOCOS('*', -1) {
|
||||
if (myLocos[loco].throttle!='\0') {
|
||||
if (Diag::WITHROTTLE) DIAG(F("%l eStopping cab %d"),millis(),myLocos[loco].cab);
|
||||
@@ -405,15 +447,65 @@ void WiThrottle::checkHeartbeat() {
|
||||
}
|
||||
}
|
||||
delete this;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// send any outstanding speed/direction/function changes for this clients locos
|
||||
// Changes may have been caused by this client, or another non-Withrottle or Exrail
|
||||
bool streamHasBeenMarked=false;
|
||||
LOOPLOCOS('*', -1) {
|
||||
if (myLocos[loco].throttle!='\0' && myLocos[loco].broadcastPending) {
|
||||
if (!streamHasBeenMarked) {
|
||||
stream->mark(clientid);
|
||||
streamHasBeenMarked=true;
|
||||
}
|
||||
myLocos[loco].broadcastPending=false;
|
||||
int cab=myLocos[loco].cab;
|
||||
char lors=LorS(cab);
|
||||
char throttle=myLocos[loco].throttle;
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>V%d\n"),
|
||||
throttle, lors , cab, DCCToWiTSpeed(DCC::getThrottleSpeed(cab)));
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>R%d\n"),
|
||||
throttle, lors , cab, DCC::getThrottleDirection(cab));
|
||||
|
||||
// compare the DCC functionmap with the local copy and send changes
|
||||
uint32_t dccFunctionMap=DCC::getFunctionMap(cab);
|
||||
uint32_t myFunctionMap=myLocos[loco].functionMap;
|
||||
myLocos[loco].functionMap=dccFunctionMap;
|
||||
|
||||
// loop the maps sending any bit changed
|
||||
// Loop is terminated as soon as no changes are left
|
||||
for (byte fn=0;dccFunctionMap!=myFunctionMap;fn++) {
|
||||
if ((dccFunctionMap&1) != (myFunctionMap&1)) {
|
||||
StringFormatter::send(stream,F("M%cA%c%d<;>F%c%d\n"),
|
||||
throttle, lors , cab, (dccFunctionMap&1)?'1':'0',fn);
|
||||
}
|
||||
// shift just checked bit off end of both maps
|
||||
dccFunctionMap>>=1;
|
||||
myFunctionMap>>=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (streamHasBeenMarked) stream->commit();
|
||||
}
|
||||
|
||||
void WiThrottle::markForBroadcast(int cab) {
|
||||
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle)
|
||||
wt->markForBroadcast2(cab);
|
||||
}
|
||||
void WiThrottle::markForBroadcast2(int cab) {
|
||||
LOOPLOCOS('*', cab) {
|
||||
myLocos[loco].broadcastPending=true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char WiThrottle::LorS(int cab) {
|
||||
return (cab<127)?'S':'L';
|
||||
return (cab<=HIGHEST_SHORT_ADDR)?'S':'L';
|
||||
}
|
||||
|
||||
// Drive Away feature. Callback handling
|
||||
|
||||
|
||||
RingStream * WiThrottle::stashStream;
|
||||
WiThrottle * WiThrottle::stashInstance;
|
||||
byte WiThrottle::stashClient;
|
||||
@@ -421,13 +513,26 @@ char WiThrottle::stashThrottleChar;
|
||||
|
||||
void WiThrottle::getLocoCallback(int16_t locoid) {
|
||||
stashStream->mark(stashClient);
|
||||
if (locoid<0) StringFormatter::send(stashStream,F("HMNo loco found on prog track\n"));
|
||||
|
||||
if (locoid<=0)
|
||||
StringFormatter::send(stashStream,F("HMNo loco found on prog track\n"));
|
||||
else {
|
||||
char addcmd[20]={'M',stashThrottleChar,'+',LorS(locoid) };
|
||||
itoa(locoid,addcmd+4,10);
|
||||
stashInstance->multithrottle(stashStream, (byte *)addcmd);
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
|
||||
DCC::setProgTrackSyncMain(true); // <1 JOIN> so we can drive loco away
|
||||
// short or long
|
||||
char addrchar;
|
||||
if (locoid & LONG_ADDR_MARKER) { // long addr
|
||||
locoid = locoid ^ LONG_ADDR_MARKER;
|
||||
addrchar = 'L';
|
||||
} else
|
||||
addrchar = 'S';
|
||||
if (addrchar == 'L' && locoid <= HIGHEST_SHORT_ADDR )
|
||||
StringFormatter::send(stashStream,F("HMLong addr %d <= %d not supported\n"), locoid,HIGHEST_SHORT_ADDR);
|
||||
else {
|
||||
char addcmd[20]={'M',stashThrottleChar,'+', addrchar};
|
||||
itoa(locoid,addcmd+4,10);
|
||||
stashInstance->multithrottle(stashStream, (byte *)addcmd);
|
||||
DCCWaveform::progTrack.setPowerMode(POWERMODE::ON);
|
||||
DCC::setProgTrackSyncMain(true); // <1 JOIN> so we can drive loco away
|
||||
}
|
||||
}
|
||||
stashStream->commit();
|
||||
}
|
||||
|
18
WiThrottle.h
18
WiThrottle.h
@@ -1,5 +1,7 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2021 Mike S
|
||||
* © 2020-2021 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of Asbelos DCC API
|
||||
*
|
||||
@@ -24,6 +26,9 @@
|
||||
struct MYLOCO {
|
||||
char throttle; //indicates which throttle letter on client, often '0','1' or '2'
|
||||
int cab; //address of this loco
|
||||
bool broadcastPending;
|
||||
uint32_t functionMap;
|
||||
uint32_t functionToggles;
|
||||
};
|
||||
|
||||
class WiThrottle {
|
||||
@@ -31,14 +36,15 @@ class WiThrottle {
|
||||
static void loop(RingStream * stream);
|
||||
void parse(RingStream * stream, byte * cmd);
|
||||
static WiThrottle* getThrottle( int wifiClient);
|
||||
|
||||
static void markForBroadcast(int cab);
|
||||
|
||||
private:
|
||||
WiThrottle( int wifiClientId);
|
||||
~WiThrottle();
|
||||
|
||||
static const int MAX_MY_LOCO=10; // maximum number of locos assigned to a single client
|
||||
static const int HEARTBEAT_SECONDS=4; // heartbeat at 4secs to provide messaging transport
|
||||
static const int ESTOP_SECONDS=8; // eStop if no incoming messages for more than 8secs
|
||||
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 WiThrottle* firstThrottle;
|
||||
static int getInt(byte * cmd);
|
||||
static int getLocoId(byte * cmd);
|
||||
@@ -62,8 +68,8 @@ class WiThrottle {
|
||||
void multithrottle(RingStream * stream, byte * cmd);
|
||||
void locoAction(RingStream * stream, byte* aval, char throttleChar, int cab);
|
||||
void accessory(RingStream *, byte* cmd);
|
||||
void checkHeartbeat();
|
||||
|
||||
void checkHeartbeat(RingStream * stream);
|
||||
void markForBroadcast2(int cab);
|
||||
// callback stuff to support prog track acquire
|
||||
static RingStream * stashStream;
|
||||
static WiThrottle * stashInstance;
|
||||
|
@@ -1,4 +1,7 @@
|
||||
/*
|
||||
* © 2021 Fred Decker
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020, Harald Barth.
|
||||
*
|
||||
@@ -152,7 +155,7 @@ WifiInboundHandler::INBOUND_STATE WifiInboundHandler::loop2() {
|
||||
if (ch=='E' || ch=='l') { // ERROR or "link is not valid"
|
||||
if (clientPendingCIPSEND>=0) {
|
||||
// A CIPSEND was errored... just toss it away
|
||||
purgeCurrentCIPSEND();
|
||||
purgeCurrentCIPSEND();
|
||||
}
|
||||
loopState=SKIPTOEND;
|
||||
break;
|
||||
@@ -231,6 +234,7 @@ WifiInboundHandler::INBOUND_STATE WifiInboundHandler::loop2() {
|
||||
if (ch=='C') {
|
||||
// got "x C" before CLOSE or CONNECTED, or CONNECT FAILED
|
||||
if (runningClientId==clientPendingCIPSEND) purgeCurrentCIPSEND();
|
||||
else CommandDistributor::forget(runningClientId);
|
||||
}
|
||||
loopState=SKIPTOEND;
|
||||
break;
|
||||
@@ -245,8 +249,9 @@ WifiInboundHandler::INBOUND_STATE WifiInboundHandler::loop2() {
|
||||
|
||||
void WifiInboundHandler::purgeCurrentCIPSEND() {
|
||||
// A CIPSEND was sent but errored... or the client closed just toss it away
|
||||
CommandDistributor::forget(clientPendingCIPSEND);
|
||||
DIAG(F("Wifi: DROPPING CIPSEND=%d,%d"),clientPendingCIPSEND,currentReplySize);
|
||||
for (int i=0;i<=currentReplySize;i++) outboundRing->read();
|
||||
for (int i=0;i<currentReplySize;i++) outboundRing->read();
|
||||
pendingCipsend=false;
|
||||
clientPendingCIPSEND=-1;
|
||||
}
|
||||
|
@@ -1,4 +1,6 @@
|
||||
/*
|
||||
* © 2021 Harald Barth
|
||||
* © 2021 Fred Decker
|
||||
* (c) 2021 Fred Decker. All rights reserved.
|
||||
* (c) 2020 Chris Harlow. All rights reserved.
|
||||
*
|
||||
|
@@ -1,22 +1,24 @@
|
||||
/*
|
||||
© 2020, Chris Harlow. All rights reserved.
|
||||
© 2020, Harald Barth.
|
||||
|
||||
This file is part of CommandStation-EX
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2022 Harald Barth
|
||||
* © 2020-2022 Chris Harlow
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef ARDUINO_AVR_UNO_WIFI_REV2
|
||||
// This code is NOT compiled on a unoWifiRev2 processor which uses a different architecture
|
||||
#include "WifiInterface.h" /* config.h included there */
|
||||
@@ -75,13 +77,13 @@ bool WifiInterface::setup(long serial_link_speed,
|
||||
(void) channel;
|
||||
#endif
|
||||
|
||||
#if NUM_SERIAL > 0
|
||||
#if NUM_SERIAL > 0 && !defined(SERIAL1_COMMANDS)
|
||||
Serial1.begin(serial_link_speed);
|
||||
wifiUp = setup(Serial1, wifiESSID, wifiPassword, hostname, port, channel);
|
||||
#endif
|
||||
|
||||
// Other serials are tried, depending on hardware.
|
||||
#if NUM_SERIAL > 1
|
||||
#if NUM_SERIAL > 1 && !defined(SERIAL2_COMMANDS)
|
||||
if (wifiUp == WIFI_NOAT)
|
||||
{
|
||||
Serial2.begin(serial_link_speed);
|
||||
@@ -89,7 +91,7 @@ bool WifiInterface::setup(long serial_link_speed,
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NUM_SERIAL > 2
|
||||
#if NUM_SERIAL > 2 && !defined(SERIAL3_COMMANDS)
|
||||
if (wifiUp == WIFI_NOAT)
|
||||
{
|
||||
Serial3.begin(serial_link_speed);
|
||||
@@ -277,10 +279,16 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
||||
|
||||
StringFormatter::send(wifiStream, F("AT+CIPSERVER=0\r\n")); // turn off tcp server (to clean connections before CIPMUX=1)
|
||||
checkForOK(1000, true); // ignore result in case it already was off
|
||||
|
||||
|
||||
StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections
|
||||
if (!checkForOK(1000, true)) return WIFI_DISCONNECTED;
|
||||
|
||||
if(!oldCmd) { // no idea to test this on old firmware
|
||||
StringFormatter::send(wifiStream, F("AT+MDNS=1,\"%S\",\"withrottle\",%d\r\n"),
|
||||
hostname, port); // mDNS responder
|
||||
checkForOK(1000, true); // dont care if not supported
|
||||
}
|
||||
|
||||
StringFormatter::send(wifiStream, F("AT+CIPSERVER=1,%d\r\n"), port); // turn on server on port
|
||||
if (!checkForOK(1000, true)) return WIFI_DISCONNECTED;
|
||||
#endif //DONT_TOUCH_WIFI_CONF
|
||||
@@ -316,19 +324,45 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
||||
|
||||
|
||||
// This function is used to allow users to enter <+ commands> through the DCCEXParser
|
||||
// <+command> sends AT+command to the ES and returns to the caller.
|
||||
// Once the user has made whatever changes to the AT commands, a <+X> command can be used
|
||||
// to force on the connectd flag so that the loop will start picking up wifi traffic.
|
||||
// If the settings are corrupted <+RST> will clear this and then you must restart the arduino.
|
||||
|
||||
// Using the <+> command with no command string causes the code to enter an echo loop so that all
|
||||
// input is directed to the ES and all ES output written to the USB Serial.
|
||||
// The sequence "!!!" returns the Arduino to the normal loop mode
|
||||
|
||||
|
||||
void WifiInterface::ATCommand(const byte * command) {
|
||||
void WifiInterface::ATCommand(HardwareSerial * stream,const byte * command) {
|
||||
command++;
|
||||
if (*command=='\0') { // User gave <+> command
|
||||
stream->print(F("\nES AT command passthrough mode, use ! to exit\n"));
|
||||
while(stream->available()) stream->read(); // Drain serial input first
|
||||
bool startOfLine=true;
|
||||
while(true) {
|
||||
while (wifiStream->available()) stream->write(wifiStream->read());
|
||||
if (stream->available()) {
|
||||
int cx=stream->read();
|
||||
// A newline followed by !!! is an exit
|
||||
if (cx=='\n' || cx=='\r') startOfLine=true;
|
||||
else if (startOfLine && cx=='!') break;
|
||||
else startOfLine=false;
|
||||
stream->write(cx);
|
||||
wifiStream->write(cx);
|
||||
}
|
||||
}
|
||||
stream->print(F("Passthrough Ended"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (*command=='X') {
|
||||
connected = true;
|
||||
DIAG(F("++++++ Wifi Connction forced on ++++++++"));
|
||||
connected = true;
|
||||
DIAG(F("++++++ Wifi Connction forced on ++++++++"));
|
||||
}
|
||||
else {
|
||||
StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command);
|
||||
checkForOK(10000, true);
|
||||
StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command);
|
||||
checkForOK(10000, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* © 2020, Chris Harlow. All rights reserved.
|
||||
* © 2020-2021 Chris Harlow
|
||||
* © 2020, Harald Barth.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify
|
||||
@@ -37,7 +38,7 @@ public:
|
||||
const int port,
|
||||
const byte channel);
|
||||
static void loop();
|
||||
static void ATCommand(const byte *command);
|
||||
static void ATCommand(HardwareSerial * stream,const byte *command);
|
||||
|
||||
private:
|
||||
static wifiSerialState setup(Stream &setupStream, const FSH *SSSid, const FSH *password,
|
||||
|
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
* COPYRIGHT (c) 2020 Fred Decker
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Fred Decker
|
||||
* © 2020-2021 Chris Harlow
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
@@ -114,11 +117,11 @@ The configuration file for DCC-EX Command Station
|
||||
// DEFINE LCD SCREEN USAGE BY THE BASE STATION
|
||||
//
|
||||
// Note: This feature requires an I2C enabled LCD screen using a Hitachi HD44780
|
||||
// controller and a PCF8574 based I2C 'backpack'.
|
||||
// controller and a commonly available PCF8574 based I2C 'backpack'.
|
||||
// To enable, uncomment one of the #define lines below
|
||||
|
||||
// define LCD_DRIVER for I2C LCD address 0x3f,16 cols, 2 rows
|
||||
// #define LCD_DRIVER 0x3F,16,2
|
||||
// define LCD_DRIVER for I2C address 0x27, 16 cols, 2 rows
|
||||
// #define LCD_DRIVER 0x27,16,2
|
||||
|
||||
//OR define OLED_DRIVER width,height in pixels (address auto detected)
|
||||
// 128x32 or 128x64 I2C SSD1306-based devices are supported.
|
||||
@@ -128,18 +131,66 @@ The configuration file for DCC-EX Command Station
|
||||
// Define scroll mode as 0, 1 or 2
|
||||
#define SCROLLMODE 1
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
// DISABLE EEPROM
|
||||
//
|
||||
// If you do not need the EEPROM at all, you can disable all the code that saves
|
||||
// data in the EEPROM. You might want to do that if you are in a Arduino UNO
|
||||
// and want to use the EX-RAIL automation. Otherwise you do not have enough RAM
|
||||
// to do that. Of course, then none of the EEPROM related commands works.
|
||||
//
|
||||
// #define DISABLE_EEPROM
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
// REDEFINE WHERE SHORT/LONG ADDR break is. According to NMRA the last short address
|
||||
// is 127 and the first long address is 128. There are manufacturers which have
|
||||
// another view. Lenz CS for example have considered addresses long from 100. If
|
||||
// you want to change to that mode, do
|
||||
//#define HIGHEST_SHORT_ADDR 99
|
||||
// If you want to run all your locos addressed long format, you could even do a
|
||||
//#define HIGHEST_SHORT_ADDR 0
|
||||
// We do not support to use the same address, for example 100(long) and 100(short)
|
||||
// at the same time, there must be a border.
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DEFINE TURNOUTS/ACCESSORIES FOLLOW NORM RCN-213
|
||||
//
|
||||
// According to norm RCN-213 a DCC packet with an 1 is closed/straight
|
||||
// and one with a 0 is thrown/diverging. This is reversed to the
|
||||
// definition from DCC++ in the DCC++ protocol. To make the states
|
||||
// match with the norm, we need to reverse the bit in the DCC packet
|
||||
// on the rails, but we don't want to cause havoc on existent layouts,
|
||||
// so we define this only for new installations. For any new install
|
||||
// there is no reason to not define this.
|
||||
#define TURNOUTS_RCN_213
|
||||
// According to norm RCN-213 a DCC packet with a 1 is closed/straight
|
||||
// and one with a 0 is thrown/diverging. In DCC++ Classic, and in previous
|
||||
// versions of DCC++EX, a turnout throw command was implemented in the packet as
|
||||
// '1' and a close command as '0'. The #define below makes the states
|
||||
// match with the norm. But we don't want to cause havoc on existent layouts,
|
||||
// so we define this only for new installations. If you don't want this,
|
||||
// don't add it to your config.h.
|
||||
//#define DCC_TURNOUTS_RCN_213
|
||||
|
||||
// By default, the driver which defines a DCC accessory decoder
|
||||
// does send out the same state change on the DCC packet as it
|
||||
// receives. This means a VPIN state=1 sends D=1 (close turnout
|
||||
// or signal green) in the DCC packet. This can be reversed if
|
||||
// necessary.
|
||||
//#define HAL_ACCESSORY_COMMAND_REVERSE
|
||||
|
||||
// If you have issues with that the direction of the accessory commands is
|
||||
// reversed (for example when converting from another CS to DCC-EX) then
|
||||
// you can use this to revese the sense of all accessory commmands sent
|
||||
// over DCC++. This #define likewise inverts the behaviour of the <a> command
|
||||
// for triggering DCC Accessory Decoders, so that <a addr subaddr 0> generates a
|
||||
// DCC packet with D=1 (close turnout) and <a addr subaddr 1> generates D=0
|
||||
// (throw turnout).
|
||||
//#define DCC_ACCESSORY_RCN_213
|
||||
//
|
||||
// HANDLING MULTIPLE SERIAL THROTTLES
|
||||
// The command station always operates with the default Serial port.
|
||||
// Diagnostics are only emitted on the default serial port and not broadcast.
|
||||
// Other serial throttles may be added to the Serial1, Serial2, Serial3 ports
|
||||
// which may or may not exist on your CPU. (Mega has all 3)
|
||||
// To monitor a throttle on one or more serial ports, uncomment the defines below.
|
||||
// NOTE: do not define here the WiFi shield serial port or your wifi will not work.
|
||||
//
|
||||
//#define SERIAL1_COMMAND
|
||||
//#define SERIAL2_COMMAND
|
||||
//#define SERIAL3_COMMAND
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
87
defines.h
87
defines.h
@@ -1,22 +1,39 @@
|
||||
/*
|
||||
© 2020, Harald Barth.
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Mike S
|
||||
* © 2021 Fred Decker
|
||||
* © 2020-2021 Harald Barth
|
||||
* © 2020-2021 Chris Harlow
|
||||
*
|
||||
* This file is part of CommandStation-EX
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
This file is part of CommandStation-EX
|
||||
#ifndef DEFINES_H
|
||||
#define DEFINES_H
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
// defines.h relies on macros defined in config.h
|
||||
// but it may have already been included (for cosmetic convenence) by the .ino
|
||||
#ifndef MOTOR_SHIELD_TYPE
|
||||
#if __has_include ( "config.h")
|
||||
#include "config.h"
|
||||
#else
|
||||
#include "config.example.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
@@ -26,19 +43,29 @@
|
||||
#if (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO) || defined(TEENSYDUINO))
|
||||
#define BIG_RAM
|
||||
#endif
|
||||
#if ENABLE_WIFI && defined(BIG_RAM)
|
||||
#define WIFI_ON true
|
||||
#ifndef WIFI_CHANNEL
|
||||
#define WIFI_CHANNEL 1
|
||||
#endif
|
||||
#if ENABLE_WIFI
|
||||
#if defined(BIG_RAM)
|
||||
#define WIFI_ON true
|
||||
#ifndef WIFI_CHANNEL
|
||||
#define WIFI_CHANNEL 1
|
||||
#endif
|
||||
#else
|
||||
#define WIFI_WARNING
|
||||
#define WIFI_ON false
|
||||
#endif
|
||||
#else
|
||||
#define WIFI_ON false
|
||||
#define WIFI_ON false
|
||||
#endif
|
||||
|
||||
#if ENABLE_ETHERNET && defined(BIG_RAM)
|
||||
#define ETHERNET_ON true
|
||||
#if ENABLE_ETHERNET
|
||||
#if defined(BIG_RAM)
|
||||
#define ETHERNET_ON true
|
||||
#else
|
||||
#define ETHERNET_WARNING
|
||||
#define ETHERNET_ON false
|
||||
#endif
|
||||
#else
|
||||
#define ETHERNET_ON false
|
||||
#define ETHERNET_ON false
|
||||
#endif
|
||||
|
||||
#if WIFI_ON && ETHERNET_ON
|
||||
@@ -52,6 +79,12 @@
|
||||
//
|
||||
#define WIFI_SERIAL_LINK_SPEED 115200
|
||||
|
||||
#if __has_include ( "myAutomation.h") && defined(BIG_RAM)
|
||||
#define RMFT_ACTIVE
|
||||
#if __has_include ( "myAutomation.h")
|
||||
#if defined(BIG_RAM) || defined(DISABLE_EEPROM)
|
||||
#define RMFT_ACTIVE
|
||||
#else
|
||||
#define EXRAIL_WARNING
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* © 2020, Harald Barth
|
||||
* © 2021, Neil McKechnie
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2021 Mike S
|
||||
* © 2020 Harald Barth
|
||||
*
|
||||
* This file is part of Asbelos DCC-EX
|
||||
*
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* © 2020, Harald Barth
|
||||
* © 2021, Neil McKechnie
|
||||
* © 2021 Neil McKechnie
|
||||
* © 2020 Harald Barth
|
||||
*
|
||||
* This file is part of DCC-EX
|
||||
*
|
||||
|
192
myAutomation2.h
Normal file
192
myAutomation2.h
Normal file
@@ -0,0 +1,192 @@
|
||||
|
||||
/* This is an automation example file.
|
||||
* The presence of a file calle "myAutomation.h" brings EX-RAIL code into
|
||||
* the command station.
|
||||
* The auotomation may have multiple concurrent tasks.
|
||||
* A task may drive one loco through a ROUTE or may simply
|
||||
* automate some other part of the layout without any loco.
|
||||
*
|
||||
* At startup, a single task is created to execute the first
|
||||
* instruction after ROUTES.
|
||||
* This task may simply follow a route, or may SCHEDULE
|
||||
* further tasks (thats is.. send a loco out along a route).
|
||||
*
|
||||
* Where the loco id is not known at compile time, a new task
|
||||
* can be creatd with the command:
|
||||
* </ SCHEDULE [cab] route>
|
||||
*
|
||||
*/
|
||||
|
||||
// Include the name to pin mappings for my layout
|
||||
#include "myLayout.h"
|
||||
|
||||
ALIAS(ROUTE_1,1)
|
||||
ALIAS(UP_MOUNTAIN,8)
|
||||
ALIAS(UP_MOUNTAIN_FROM_PROG,88)
|
||||
ALIAS(INNER_LOOP,7)
|
||||
ALIAS(INNER_FROM_PROG,77)
|
||||
|
||||
//EXRAIL // myAutomation must start with the EXRAIL instruction
|
||||
// This is the default starting route, AKA ROUTE(0)
|
||||
// START(999) // this is just a diagnostic test cycle
|
||||
PRINT("started")
|
||||
LCD(0,"EXRAIL RULES")
|
||||
SERIAL("I had one of them but the leg fell off!")
|
||||
DONE // This just ends the startup thread
|
||||
|
||||
|
||||
/*AUTOSTART*/ ROUTE(ROUTE_1,"Close All")
|
||||
LCD(1,"Bingo")
|
||||
CLOSE(TOP_TURNOUT) DELAY(10)
|
||||
CLOSE(Y_TURNOUT) DELAY(10)
|
||||
CLOSE(MIDDLE_TURNOUT) DELAY(10)
|
||||
CLOSE(JOIN_TURNOUT) DELAY(10)
|
||||
CLOSE(LOWER_TURNOUT) DELAY(10)
|
||||
CLOSE(CROSSOVER_TURNOUT) DELAY(10)
|
||||
CLOSE(PROG_TURNOUT) DELAY(10)
|
||||
PRINT("Close All completed")
|
||||
|
||||
ENDTASK
|
||||
|
||||
|
||||
SEQUENCE(UP_MOUNTAIN) // starting at the lower closed turnout siding and going up the mountain
|
||||
PRINT("Up Mountain started")
|
||||
DELAY(10000) // wait 10 seconds
|
||||
RESERVE(BLOCK_LOWER_MOUNTAIN)
|
||||
CLOSE(LOWER_TURNOUT) CLOSE(JOIN_TURNOUT)
|
||||
FWD(60) AT(Y_LOWER)
|
||||
RESERVE(BLOCK_X_MOUNTAIN)
|
||||
CLOSE(Y_TURNOUT) CLOSE(MIDDLE_TURNOUT)
|
||||
FWD(40) AT(MIDDLE_C_BUFFER) STOP
|
||||
FREE(BLOCK_X_MOUNTAIN) FREE(BLOCK_LOWER_MOUNTAIN)
|
||||
DELAY(10000)
|
||||
RESERVE(BLOCK_UPPER_MOUNTAIN) RESERVE(BLOCK_X_MOUNTAIN)
|
||||
CLOSE(MIDDLE_TURNOUT) THROW(Y_TURNOUT) THROW(TOP_TURNOUT)
|
||||
REV(55)
|
||||
AFTER(Y_UPPER) FREE(BLOCK_X_MOUNTAIN)
|
||||
REV(55) AT(TOP_T_BUFFER) STOP // At top of mountain
|
||||
FREE(BLOCK_UPPER_MOUNTAIN)
|
||||
DELAY(5000)
|
||||
RESERVE(BLOCK_UPPER_MOUNTAIN)
|
||||
THROW(TOP_TURNOUT)
|
||||
FWD(60) AT(Y_UPPER)
|
||||
RESERVE(BLOCK_X_MOUNTAIN)
|
||||
THROW(Y_TURNOUT) CLOSE(MIDDLE_TURNOUT)
|
||||
FWD(40) AT(MIDDLE_C_BUFFER) STOP
|
||||
FREE(BLOCK_UPPER_MOUNTAIN) FREE(BLOCK_X_MOUNTAIN)
|
||||
DELAY(6000)
|
||||
RESERVE(BLOCK_LOWER_MOUNTAIN) RESERVE(BLOCK_X_MOUNTAIN)
|
||||
CLOSE(MIDDLE_TURNOUT) CLOSE(Y_TURNOUT) CLOSE(JOIN_TURNOUT) CLOSE(LOWER_TURNOUT)
|
||||
REV(60)
|
||||
AFTER(Y_LOWER) FREE(BLOCK_X_MOUNTAIN)
|
||||
AT(LOWER_C_BUFFER) STOP
|
||||
FREE(BLOCK_LOWER_MOUNTAIN)
|
||||
FOLLOW(UP_MOUNTAIN)
|
||||
|
||||
AUTOMATION(UP_MOUNTAIN_FROM_PROG,"Send up mountain from prog")
|
||||
JOIN
|
||||
RESERVE(BLOCK_LOWER_MOUNTAIN)
|
||||
RESERVE(BLOCK_X_INNER)
|
||||
RESERVE(BLOCK_X_OUTER)
|
||||
// safe to cross
|
||||
THROW(PROG_TURNOUT) THROW(CROSSOVER_TURNOUT) THROW(JOIN_TURNOUT)
|
||||
FWD(45)
|
||||
AFTER(JOIN_AFTER) STOP
|
||||
CLOSE(PROG_TURNOUT) CLOSE(CROSSOVER_TURNOUT) CLOSE(JOIN_TURNOUT)
|
||||
FREE(BLOCK_X_OUTER) FREE(BLOCK_X_INNER)
|
||||
CLOSE(LOWER_TURNOUT)
|
||||
REV(40) AT(LOWER_C_BUFFER) STOP
|
||||
FREE(BLOCK_LOWER_MOUNTAIN)
|
||||
FOLLOW(UP_MOUNTAIN)
|
||||
|
||||
SEQUENCE(INNER_LOOP)
|
||||
FWD(50)
|
||||
AT(CROSSOVER_INNER_BEFORE)
|
||||
RESERVE(BLOCK_X_INNER)
|
||||
CLOSE(CROSSOVER_TURNOUT)
|
||||
FWD(50)
|
||||
AFTER(CROSSOVER_INNER_AFTER)
|
||||
FREE(BLOCK_X_INNER)
|
||||
FOLLOW(INNER_LOOP)
|
||||
|
||||
|
||||
// Turnout definitions
|
||||
TURNOUT(TOP_TURNOUT, TOP_TURNOUT,0,"Top Station")
|
||||
TURNOUT(Y_TURNOUT, Y_TURNOUT,0,"Mountain join")
|
||||
TURNOUT(MIDDLE_TURNOUT, MIDDLE_TURNOUT,0,"Middle Station")
|
||||
TURNOUT(JOIN_TURNOUT,JOIN_TURNOUT,0)
|
||||
TURNOUT(LOWER_TURNOUT,LOWER_TURNOUT,0)
|
||||
TURNOUT(CROSSOVER_TURNOUT,CROSSOVER_TURNOUT,0)
|
||||
TURNOUT(PROG_TURNOUT,PROG_TURNOUT,0)
|
||||
|
||||
// Single slip protection
|
||||
ONTHROW(2)
|
||||
THROW(1)
|
||||
DONE
|
||||
ONCLOSE(1)
|
||||
CLOSE(2)
|
||||
DONE
|
||||
|
||||
|
||||
ROUTE(61,"Call return test")
|
||||
PRINT("In 61 test 1")
|
||||
CALL(62)
|
||||
PRINT("In 61 test 2")
|
||||
CALL(62)
|
||||
PRINT("In 61 test 3")
|
||||
ACTIVATE(100,2)
|
||||
DEACTIVATE(100,2)
|
||||
DONE
|
||||
|
||||
SEQUENCE(62)
|
||||
PRINT("In seq 62")
|
||||
RETURN
|
||||
|
||||
ROUTE(63,"Signal test 40,41,42")
|
||||
SIGNAL(40,41,42)
|
||||
DELAY(2000)
|
||||
RED(40)
|
||||
DELAY(2000)
|
||||
AMBER(40)
|
||||
DELAY(2000)
|
||||
GREEN(40)
|
||||
FOLLOW(63)
|
||||
|
||||
|
||||
ROUTE(64,"Func test 6772")
|
||||
XFON(6772,1)
|
||||
DELAY(5000)
|
||||
XFOFF(6772,1)
|
||||
DELAY(5000)
|
||||
FOLLOW(64)
|
||||
|
||||
ROUTE(65,"Negative sensor test")
|
||||
PRINT(" WAIT for -176")
|
||||
AT(-176)
|
||||
PRINT(" WAIT for 176")
|
||||
AT(176)
|
||||
PRINT("done")
|
||||
DONE
|
||||
|
||||
ROUTE(123,"Activate stuff")
|
||||
ACTIVATEL(5)
|
||||
ACTIVATE(7,2)
|
||||
DEACTIVATE(3,2)
|
||||
DEACTIVATEL(6)
|
||||
DONE
|
||||
|
||||
ONACTIVATEL(5)
|
||||
PRINT("ACT 5")
|
||||
DONE
|
||||
ONACTIVATE(7,2)
|
||||
PRINT("ACT 7,2")
|
||||
DONE
|
||||
ONDEACTIVATE(7,2)
|
||||
PRINT("DEACT 7,2")
|
||||
DONE
|
||||
ONDEACTIVATEL(5)
|
||||
PRINT("DEACT 5")
|
||||
DONE
|
||||
|
||||
|
||||
|
165
myHal.cpp_example.txt
Normal file
165
myHal.cpp_example.txt
Normal file
@@ -0,0 +1,165 @@
|
||||
// Sample myHal.cpp file.
|
||||
//
|
||||
// To use this file, copy it to myHal.cpp and uncomment the directives and/or
|
||||
// edit them to satisfy your requirements. If you only want to use up to
|
||||
// two MCP23017 GPIO Expander modules and/or up to two PCA9685 Servo modules,
|
||||
// then you don't need this file as DCC++EX configures these for free!
|
||||
|
||||
// Note that if the file has a .cpp extension it WILL be compiled into the build
|
||||
// and the halSetup() function WILL be invoked.
|
||||
//
|
||||
// To prevent this, temporarily rename the file to myHal.txt or similar.
|
||||
//
|
||||
|
||||
// The #if directive prevent compile errors for Uno and Nano by excluding the
|
||||
// HAL directives from the build.
|
||||
#if !defined(IO_NO_HAL)
|
||||
|
||||
// Include devices you need.
|
||||
#include "IODevice.h"
|
||||
#include "IO_HCSR04.h" // Ultrasonic range sensor
|
||||
#include "IO_VL53L0X.h" // Laser time-of-flight sensor
|
||||
#include "IO_DFPlayer.h" // MP3 sound player
|
||||
|
||||
|
||||
//==========================================================================
|
||||
// The function halSetup() is invoked from CS if it exists within the build.
|
||||
// The setup calls are included between the open and close braces "{ ... }".
|
||||
// Comments (lines preceded by "//") are optional.
|
||||
//==========================================================================
|
||||
|
||||
void halSetup() {
|
||||
|
||||
//=======================================================================
|
||||
// The following directive defines a PCA9685 PWM Servo driver module.
|
||||
//=======================================================================
|
||||
// The parameters are:
|
||||
// First Vpin=100
|
||||
// Number of VPINs=16 (numbered 100-115)
|
||||
// I2C address of module=0x40
|
||||
|
||||
//PCA9685::create(100, 16, 0x40);
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// The following directive defines an MCP23017 16-port I2C GPIO Extender module.
|
||||
//=======================================================================
|
||||
// The parameters are:
|
||||
// First Vpin=196
|
||||
// Number of VPINs=16 (numbered 196-211)
|
||||
// I2C address of module=0x22
|
||||
|
||||
//MCP23017::create(196, 16, 0x22);
|
||||
|
||||
|
||||
// Alternative form, which allows the INT pin of the module to request a scan
|
||||
// by pulling Arduino pin 40 to ground. Means that the I2C isn't being polled
|
||||
// all the time, only when a change takes place. Multiple modules' INT pins
|
||||
// may be connected to the same Arduino pin.
|
||||
|
||||
//MCP23017::create(196, 16, 0x22, 40);
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// The following directive defines an MCP23008 8-port I2C GPIO Extender module.
|
||||
//=======================================================================
|
||||
// The parameters are:
|
||||
// First Vpin=300
|
||||
// Number of VPINs=8 (numbered 300-307)
|
||||
// I2C address of module=0x22
|
||||
|
||||
//MCP23008::create(300, 8, 0x22);
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// The following directive defines a PCF8574 8-port I2C GPIO Extender module.
|
||||
//=======================================================================
|
||||
// The parameters are:
|
||||
// First Vpin=200
|
||||
// Number of VPINs=8 (numbered 200-207)
|
||||
// I2C address of module=0x23
|
||||
|
||||
//PCF8574::create(200, 8, 0x23);
|
||||
|
||||
|
||||
// Alternative form using INT pin (see above)
|
||||
|
||||
//PCF8574::create(200, 8, 0x23, 40);
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// The following directive defines an HCSR04 ultrasonic ranging module.
|
||||
//=======================================================================
|
||||
// The parameters are:
|
||||
// Vpin=2000 (only one VPIN per directive)
|
||||
// Number of VPINs=1
|
||||
// Arduino pin connected to TRIG=30
|
||||
// Arduino pin connected to ECHO=31
|
||||
// Minimum trigger range=20cm (VPIN goes to 1 when <20cm)
|
||||
// Maximum trigger range=25cm (VPIN goes to 0 when >25cm)
|
||||
// Note: Multiple devices can be configured by using a different ECHO pin
|
||||
// for each one. The TRIG pin can be shared between multiple devices.
|
||||
// Be aware that the 'ping' of one device may be received by another
|
||||
// device and position them accordingly!
|
||||
|
||||
//HCSR04::create(2000, 30, 31, 20, 25);
|
||||
//HCSR04::create(2001, 30, 32, 20, 25);
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// The following directive defines a single VL53L0X Time-of-Flight range sensor.
|
||||
//=======================================================================
|
||||
// The parameters are:
|
||||
// VPIN=5000
|
||||
// Number of VPINs=1
|
||||
// I2C address=0x29 (default for this chip)
|
||||
// Minimum trigger range=200mm (VPIN goes to 1 when <20cm)
|
||||
// Maximum trigger range=250mm (VPIN goes to 0 when >25cm)
|
||||
|
||||
//VL53L0X::create(5000, 1, 0x29, 200, 250);
|
||||
|
||||
// For multiple VL53L0X modules, add another parameter which is a VPIN connected to the
|
||||
// module's XSHUT pin. This allows the modules to be configured, at start,
|
||||
// with distinct I2C addresses. In this case, the address 0x29 is only used during
|
||||
// initialisation to configure each device in turn with the desired unique I2C address.
|
||||
// The examples below have the modules' XSHUT pins connected to the first two pins of
|
||||
// the first MCP23017 module (164 and 165), but Arduino pins may be used instead.
|
||||
// The first module here is given I2C address 0x30 and the second is 0x31.
|
||||
|
||||
//VL53L0X::create(5000, 1, 0x30, 200, 250, 164);
|
||||
//VL53L0X::create(5001, 1, 0x31, 200, 250, 165);
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// Play mp3 files from a Micro-SD card, using a DFPlayer MP3 Module.
|
||||
//=======================================================================
|
||||
// Parameters:
|
||||
// 10000 = first VPIN allocated.
|
||||
// 10 = number of VPINs allocated.
|
||||
// Serial1 = name of serial port (usually Serial1 or Serial2).
|
||||
// With these parameters, up to 10 files may be played on pins 10000-10009.
|
||||
// Play is started from EX-RAIL with SET(10000) for first mp3 file, SET(10001)
|
||||
// for second file, etc. Play may also be initiated by writing an analogue
|
||||
// value to the first pin, e.g. SERVO(10000,23,0) will play the 23rd mp3 file.
|
||||
// SERVO(10000,23,30) will do the same thing, as well as setting the volume to
|
||||
// 30 (maximum value).
|
||||
// Play is stopped by RESET(10000) (or any other allocated VPIN).
|
||||
// Volume may also be set by writing an analogue value to the second pin for the player,
|
||||
// e.g. SERVO(10001,30,0) sets volume to maximum (30).
|
||||
// The EX-RAIL script may check for completion of play by calling WAITFOR(pin), which will only proceed to the
|
||||
// following line when the player is no longer busy.
|
||||
// E.g.
|
||||
// SEQUENCE(1)
|
||||
// AT(164) // Wait for sensor attached to pin 164 to activate
|
||||
// SET(10003) // Play fourth MP3 file
|
||||
// LCD(4, "Playing") // Display message on LCD/OLED
|
||||
// WAITFOR(10003) // Wait for playing to finish
|
||||
// LCD(4, " ") // Clear LCD/OLED line
|
||||
// FOLLOW(1) // Go back to start
|
||||
|
||||
// DFPlayer::create(10000, 10, Serial1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@@ -16,6 +16,7 @@ default_envs =
|
||||
unowifiR2
|
||||
nano
|
||||
src_dir = .
|
||||
include_dir = .
|
||||
|
||||
[env]
|
||||
build_flags = -Wall -Wextra
|
||||
@@ -41,7 +42,7 @@ lib_deps =
|
||||
SPI
|
||||
monitor_speed = 115200
|
||||
monitor_flags = --echo
|
||||
build_flags = -DDIAG_IO
|
||||
build_flags = -DDIAG_IO -DDIAG_LOOPTIMES
|
||||
|
||||
[env:mega2560-no-HAL]
|
||||
platform = atmelavr
|
||||
|
26
version.h
26
version.h
@@ -3,8 +3,30 @@
|
||||
|
||||
#include "StringFormatter.h"
|
||||
|
||||
|
||||
#define VERSION "3.1.6"
|
||||
#define VERSION "3.2.0 rc9"
|
||||
// 3.2.0 Major functional and non-functional changes.
|
||||
// New HAL added for I/O (digital and analogue inputs and outputs, servos etc).
|
||||
// Support for MCP23008, MCP23017 and PCF9584 I2C GPIO Extender modules.
|
||||
// Support for PCA9685 PWM (servo) control modules.
|
||||
// Support for analogue inputs on Arduino pins and on ADS111x I2C modules.
|
||||
// Support for MP3 sound playback via DFPlayer module.
|
||||
// Support for HC-SR04 Ultrasonic range sensor module.
|
||||
// Support for VL53L0X Laser range sensor module (Time-Of-Flight).
|
||||
// Native non-blocking I2C drivers for AVR and Nano architectures (fallback
|
||||
// to blocking Wire library for other platforms).
|
||||
// EEPROM layout change - deletes EEPROM contents on first start following upgrade.
|
||||
// New EX-RAIL automation capability.
|
||||
// Turnout class revised to expand turnout capabilities, new commands added.
|
||||
// Output class now allows ID > 255.
|
||||
// Configuration options to globally flip polarity of DCC Accessory states when driven
|
||||
// from <a> command and <T> command.
|
||||
// Increased use of display for showing loco decoder programming information.
|
||||
// Can disable EEPROM code
|
||||
// Can define border between long and short addresses
|
||||
// Turnout and accessory states (thrown/closed = 0/1 or 1/0) can be set to match RCN-213
|
||||
// Bugfix: one-off error in CIPSEND drop
|
||||
// ...
|
||||
// 3.1.7 Bugfix: Unknown locos should have speed forward
|
||||
// 3.1.6 Make output ID two bytes and guess format/size of registered outputs found in EEPROM
|
||||
// 3.1.5 Fix LCD corruption on power-up
|
||||
// 3.1.4 Refactor OLED and LCD drivers and remove unused code
|
||||
|
Reference in New Issue
Block a user