-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from DCC-EX:fix-ssize_t
Fix-ssize_t
- Loading branch information
Showing
6 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
// DCCEXProtocol_Serial | ||
// | ||
// Shows how to use DCCEXProtocol over serial using a Mega2560 | ||
// This uses a Mega2560 (or other AVR device) that has a second hardware serial port (Serial1) | ||
// Tested with Arduino Mega2560 | ||
// | ||
// Peter Cole (PeteGSX) 2024 | ||
|
||
#include <DCCEXProtocol.h> | ||
|
||
// If we haven't got a custom config.h, use the example | ||
#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 | ||
|
||
// Setup serial macros if not already defined elsewhere | ||
#ifndef CONSOLE | ||
#define CONSOLE Serial // All output should go to this | ||
#endif | ||
#ifndef CLIENT | ||
#define CLIENT Serial1 // All DCCEXProtocol commands/responses/broadcasts use this | ||
#endif | ||
|
||
// Declare functions to call from our delegate | ||
void printRoster(); | ||
void printTurnouts(); | ||
void printRoutes(); | ||
void printTurntables(); | ||
|
||
// Setup the delegate class to process broadcasts/responses | ||
class MyDelegate : public DCCEXProtocolDelegate { | ||
public: | ||
void receivedServerVersion(int major, int minor, int patch) { | ||
CONSOLE.print("\n\nReceived version: "); | ||
CONSOLE.print(major); | ||
CONSOLE.print("."); | ||
CONSOLE.print(minor); | ||
CONSOLE.print("."); | ||
CONSOLE.println(patch); | ||
} | ||
|
||
void receivedTrackPower(TrackPower state) { | ||
CONSOLE.print("\n\nReceived Track Power: "); | ||
CONSOLE.println(state); | ||
CONSOLE.println("\n\n"); | ||
} | ||
|
||
void receivedRosterList() { | ||
CONSOLE.println("\n\nReceived Roster"); | ||
printRoster(); | ||
} | ||
void receivedTurnoutList() { | ||
CONSOLE.print("\n\nReceived Turnouts/Points list"); | ||
printTurnouts(); | ||
CONSOLE.println("\n\n"); | ||
} | ||
void receivedRouteList() { | ||
CONSOLE.print("\n\nReceived Routes List"); | ||
printRoutes(); | ||
CONSOLE.println("\n\n"); | ||
} | ||
void receivedTurntableList() { | ||
CONSOLE.print("\n\nReceived Turntables list"); | ||
printTurntables(); | ||
CONSOLE.println("\n\n"); | ||
} | ||
|
||
void receivedScreenUpdate(int screen, int row, char *message) { | ||
CONSOLE.println("\n\nReceived screen|row|message"); | ||
CONSOLE.print(screen); | ||
CONSOLE.print("|"); | ||
CONSOLE.print(row); | ||
CONSOLE.print("|"); | ||
CONSOLE.println(message); | ||
} | ||
|
||
}; | ||
|
||
// Global objects | ||
DCCEXProtocol dccexProtocol; | ||
MyDelegate myDelegate; | ||
|
||
void printRoster() { | ||
for (Loco *loco = dccexProtocol.roster->getFirst(); loco; loco = loco->getNext()) { | ||
int id = loco->getAddress(); | ||
char *name = loco->getName(); | ||
CONSOLE.print(id); | ||
CONSOLE.print(" ~"); | ||
CONSOLE.print(name); | ||
CONSOLE.println("~"); | ||
for (int i = 0; i < 32; i++) { | ||
char *fName = loco->getFunctionName(i); | ||
if (fName != nullptr) { | ||
CONSOLE.print("loadFunctionLabels() "); | ||
CONSOLE.print(fName); | ||
if (loco->isFunctionMomentary(i)) { | ||
CONSOLE.print(" - Momentary"); | ||
} | ||
CONSOLE.println(); | ||
} | ||
} | ||
} | ||
CONSOLE.println("\n"); | ||
} | ||
|
||
void printTurnouts() { | ||
for (Turnout *turnout = dccexProtocol.turnouts->getFirst(); turnout; turnout = turnout->getNext()) { | ||
int id = turnout->getId(); | ||
char *name = turnout->getName(); | ||
CONSOLE.print(id); | ||
CONSOLE.print(" ~"); | ||
CONSOLE.print(name); | ||
CONSOLE.println("~"); | ||
} | ||
CONSOLE.println("\n"); | ||
} | ||
|
||
void printRoutes() { | ||
for (Route *route = dccexProtocol.routes->getFirst(); route; route = route->getNext()) { | ||
int id = route->getId(); | ||
char *name = route->getName(); | ||
CONSOLE.print(id); | ||
CONSOLE.print(" ~"); | ||
CONSOLE.print(name); | ||
CONSOLE.println("~"); | ||
} | ||
CONSOLE.println("\n"); | ||
} | ||
|
||
void printTurntables() { | ||
for (Turntable *turntable = dccexProtocol.turntables->getFirst(); turntable; turntable = turntable->getNext()) { | ||
int id = turntable->getId(); | ||
char *name = turntable->getName(); | ||
CONSOLE.print(id); | ||
CONSOLE.print(" ~"); | ||
CONSOLE.print(name); | ||
CONSOLE.println("~"); | ||
|
||
int j = 0; | ||
for (TurntableIndex *turntableIndex = turntable->getFirstIndex(); turntableIndex; | ||
turntableIndex = turntableIndex->getNextIndex()) { | ||
char *indexName = turntableIndex->getName(); | ||
CONSOLE.print(" index"); | ||
CONSOLE.print(j); | ||
CONSOLE.print(" ~"); | ||
CONSOLE.print(indexName); | ||
CONSOLE.println("~"); | ||
j++; | ||
} | ||
} | ||
CONSOLE.println("\n"); | ||
} | ||
|
||
void setup() { | ||
CONSOLE.begin(115200); | ||
CLIENT.begin(115200); | ||
CONSOLE.println(F("DCCEXProtocol Serial Connection Demo")); | ||
CONSOLE.println(F("")); | ||
|
||
// Direct logs to CONSOLE | ||
dccexProtocol.setLogStream(&CONSOLE); | ||
|
||
// Set the delegate for broadcasts/responses | ||
dccexProtocol.setDelegate(&myDelegate); | ||
|
||
// Connect to the CS via CLIENT | ||
dccexProtocol.connect(&CLIENT); | ||
CONSOLE.println(F("DCC-EX connected")); | ||
|
||
dccexProtocol.requestServerVersion(); | ||
} | ||
|
||
void loop() { | ||
// Parse incoming messages | ||
dccexProtocol.check(); | ||
|
||
dccexProtocol.getLists(true, true, true, true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Example config file for the DCCEXProtocol_Serial example. | ||
// Copy to config.h if you need to customise these for your setup. | ||
|
||
#define CONSOLE Serial | ||
#define CLIENT Serial1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=DCCEXProtocol | ||
version=0.0.12 | ||
version=0.0.13 | ||
author=Peter Cole, Peter Akers <[email protected]> | ||
maintainer=Peter Cole, Peter Akers <[email protected]> | ||
sentence=DCC-EX Native Protocol implementation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "DCCEXProtocolTest.hpp" | ||
|
||
TEST_F(DCCEXProtocolTest, clearBufferWhenFull) { | ||
// Fill buffer with garbage | ||
for (auto i{0uz}; i < 500uz; ++i) | ||
_stream.write(static_cast<uint8_t>('A' + (random() % 26))); | ||
|
||
// Proceed with normal message | ||
_stream << R"(<m "Hello World">)"; | ||
EXPECT_CALL(_delegate, receivedMessage(StrEq("Hello World"))).Times(Exactly(1)); | ||
_dccexProtocol.check(); | ||
} |