Skip to content

Commit

Permalink
- Emulator now uses custom EEPROM when passed as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bcsanches committed Jan 30, 2025
1 parent 5e56e4d commit fae0b9b
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 19 deletions.
18 changes: 17 additions & 1 deletion data/EFMR/Helix.decoders.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,23 @@
"activateDelay":1,
"deactivateDelay":1,
"comment":"unused"
},
},
{
"name": "TC_SIG_6D_HR",
"class": "Output",
"address": "1350",
"pin": 65,
"inverted": true,
"ignoreSavedState": false
},
{
"name": "TC_SIG_6D_HG",
"class": "Output",
"address": "1351",
"pin": 64,
"inverted": true,
"ignoreSavedState": false
},
{
"name": "INV_HELIX_TC_SOL",
"class": "QuadInverter",
Expand Down
35 changes: 35 additions & 0 deletions data/EFMR/SignalController.decoders.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,40 @@
"off":["red", "green"]
}
]
},
{
"name":"TC_SIG_6D",
"class":"VirtualSignal",
"address":"1352",
"heads":
{
"red":"TC_SIG_6D_HR",
"green":"TC_SIG_6D_HG"
},
"aspects":
[
{
"name":"Stop",
"on":["red"]
},
{
"name":"Restricted",
"on":["red"],
"flash":true
},
{
"name":"Aproach",
"on":["green"],
"flash":true
},
{
"name":"Clear",
"on":["green"]
},
{
"name":"Dark",
"off":["red", "green"]
}
]
}
]
6 changes: 3 additions & 3 deletions data/EFMR/TC_02.decoders.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[
{
"name": "TC_PL20A",
"name": "TC_PL20B",
"class": "Output",
"address": "1320",
"pin": 6,
"inverted": false,
"ignoreSavedState": true,
"comment":"Power track L20A - rabicho estação"
"comment":"Power track L20B - rabicho estação"
},
{
"name": "TC_PL22",
Expand Down Expand Up @@ -268,7 +268,7 @@
"powerPin": 61,
"frogPin": 62 ,
"range":25,
"invertedFrog": false,
"invertedFrog": true,
"invertedPower": true,
"operationTime": 3000
},
Expand Down
22 changes: 14 additions & 8 deletions src/ArduinoLib/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "ArduinoLib.h"
#include "Clock.h"
#include "Ethercard.h"

#include "DynamicLibrary.h"

Expand Down Expand Up @@ -134,15 +135,10 @@ namespace ArduinoLib
DynamicLibrary g_ModuleLib;
std::string g_strModuleName;

void Setup(std::string moduleName, dcclite::Logger_t log, const char *deviceName)
bool Setup(std::string moduleName, dcclite::Logger_t log, const char *deviceName)
{
dcclite::LogReplace(log);

#error todo

//
//Check if device name rom exists, if not, load the module, init session, save it... unload module and continue


g_ModuleLib.Load(moduleName);

g_strModuleName = std::move(moduleName);
Expand All @@ -152,15 +148,20 @@ namespace ArduinoLib

g_Clock = dcclite::Clock();

detail::RomSetupModule(deviceName ? deviceName : g_strModuleName);
bool romResult = detail::RomSetupModule(deviceName ? deviceName : g_strModuleName);

//initialize client
g_pfnSetup();

return romResult;
}

void Finalize()
{
detail::RomFinalize();

//hack to reset ehtercard lib
ether.udpServerPauseListenOnPort(0);
}

void Tick()
Expand All @@ -180,6 +181,11 @@ namespace ArduinoLib
Serial.internalSetData(data);
}

void SetSerialInput(std::string data)
{
Serial.internalSetData(data);
}

void SetPinDigitalVoltage(int pin, VoltageModes voltage)
{
g_Pins.at(pin).setDigitalVoltage(voltage);
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoLib/ArduinoLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace ArduinoLib
{
typedef void(*ArduinoProc_t)();

ARDUINO_API void Setup(std::string moduleName, dcclite::Logger_t log, const char *projectPath);
ARDUINO_API bool Setup(std::string moduleName, dcclite::Logger_t log, const char *projectPath);

ARDUINO_API void Finalize();

Expand Down
4 changes: 2 additions & 2 deletions src/ArduinoLib/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ namespace ArduinoLib::detail
return true;
}

void RomSetupModule(std::string_view moduleName)
bool RomSetupModule(std::string_view moduleName)
{
//first time?
if (!g_thWorker.joinable())
Expand Down Expand Up @@ -274,7 +274,7 @@ namespace ArduinoLib::detail
g_strRomBackup = g_strRomFileName;
g_strRomBackup += ".bkp";

TryLoadRomState();
return TryLoadRomState();
}

void RomFinalize()
Expand Down
7 changes: 6 additions & 1 deletion src/ArduinoLib/EEPROMLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

namespace ArduinoLib::detail
{
void RomSetupModule(std::string_view moduleName);
/**
*
* Returns: true if rom module was loaded, false if not found or load failed.
*
*/
bool RomSetupModule(std::string_view moduleName);

void RomAfterLoop();

Expand Down
6 changes: 6 additions & 0 deletions src/ArduinoLib/Ethercard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ bool EtherCard::clientWaitIp(const uint8_t *ip)
return false;
}

void EtherCard::udpServerPauseListenOnPort(uint16_t port)
{
g_Socket.Close();
g_vecListeners.clear();
}

void EtherCard::udpServerListenOnPort(UdpServerCallback callback, uint16_t port)
{
if (g_Socket.IsOpen())
Expand Down
1 change: 1 addition & 0 deletions src/ArduinoLib/Ethercard.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ARDUINO_API EtherCard : public Ethernet
static bool dhcpSetup(const char *hname = nullptr, bool fromRam = false);

static void udpServerListenOnPort(UdpServerCallback callback, uint16_t port);
static void udpServerPauseListenOnPort(uint16_t port);

static void sendUdp(const char *data, uint8_t len, uint16_t sport, const uint8_t *dip, uint16_t dport);

Expand Down
6 changes: 6 additions & 0 deletions src/ArduinoLib/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ struct ARDUINO_API SerialImpl
m_strData.assign(data);
}

inline void internalSetData(std::string str)
{
m_uPos = 0;
m_strData = std::move(str);
}

private:
#pragma warning(disable:4251)
std::string m_strData;
Expand Down
8 changes: 7 additions & 1 deletion src/BrokerLib/dcc/VirtualDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

namespace dcclite::broker
{

/*
* This is used as the name implies as a Virtual Device, a device without a physical representation.
*
* Useful for storing Virtual decoders to be used to trigger actions, like, for example:
* - Making a button available at JMRI for triggering a lua script.
*
*/
class VirtualDevice : public Device
{
public:
Expand Down
7 changes: 6 additions & 1 deletion src/BrokerLib/dcc/VirtualTurnoutDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

namespace dcclite::broker
{

/*
* Useful decode for exposing a virtual button on JMRI.
*
* Can be used to trigger Lua scripts, for example.
*
*/
class VirtualTurnoutDecoder : public TurnoutDecoder
{
public:
Expand Down
27 changes: 26 additions & 1 deletion src/Emulator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,32 @@ int main(int argc, char **argv)

dcclite::PathUtils::InitAppFolders("Emulator");

ArduinoLib::Setup("LiteDecoderLib.dll", dcclite::LogGetDefault(), argc > 1 ? argv[1] : nullptr);
const char *deviceName = argc > 1 ? argv[1] : nullptr;
if (!ArduinoLib::Setup("LiteDecoderLib.dll", dcclite::LogGetDefault(), deviceName) && deviceName)
{
//If setup returned false, rom failed to load, also if we have a device name, we need to configure it....
dcclite::Log::Info("[main] Initializing module to create rom");

std::stringstream stream;
stream << "/cfg " << deviceName << ';';
ArduinoLib::SetSerialInput(stream.str().c_str());

ArduinoLib::Tick();

dcclite::Log::Info("[main] Killing module");
ArduinoLib::Finalize();

dcclite::Log::Info("[main] Reloading module");
//try again...
if (!ArduinoLib::Setup("LiteDecoderLib.dll", dcclite::LogGetDefault(), deviceName))
{
dcclite::Log::Critical("[main] Failed to reload arduino lib to use new rom");

return EXIT_FAILURE;
}
}

dcclite::Log::Info("[main] Setup complete, starting main loop");

TerminalService terminalService;

Expand Down
10 changes: 10 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
DCCLite
-------

- Modify sync behavior: use DCCLite server data for outputs, not device. DCCLite with rule the device state
- Scripts does not need to monitor device reload after script has run
- states changes when devices are offline will be sent down to the device when it becomes online

- Expose signals to scripts

- Parser: use std::string_view

- Better emulator:
- Make sharpConsole able to start emulator as is
- Make sharpconsole start emulator using a existing device name (auto generate eprom?)
Expand Down Expand Up @@ -30,6 +38,8 @@ DCCLite

- update route section to know next section and avoid re-ocupying the block

- Make a EngineDriver Server, redirect to DCCex Directly and goodbye JMRI

LitePanelSDL
------------

Expand Down

0 comments on commit fae0b9b

Please sign in to comment.