Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow decoupling of hardware transport and LocoNet protocol #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Jueff
Copy link

@Jueff Jueff commented Mar 17, 2022

By setting the new define LOCONET_NO_SW_UART the library internal serial communication is turned off.
Instead an integrator may now use a communication channel of his choice.

A new method addToBuffer(uint8_t newByte) allows the integrator so forward data from the transport channel to LocoNet instance, and an external callback function bool sendRawLocoNet(uint8_t val) is called by the library to output data to the transport channel.

I also added an example called LocoNetMonitorESP32TCP which runs on an ESP32. Its a clone of the Monitor example. It turns on Wifi and creates a TCP server socket at LocoNetServer.local:1001. Application like Rocrail can directly connect to the device via network. In Rocrail configure Interface "LocoNet", Device "LocoNetServer.local:1001", Typ LocoBuffer, Uncheck CTS Flow.

With an ESP32 also a Bluetooth serial could be a communication channel, or more easy just the usage of e.g. Serial2 for LocoNet communication.

How could Serial2 be used?

  • set the preprocessor definiton LOCONET_NO_SW_UART
  • in Setup() add Serial2.begin(19200)
  • in Loop() forward all data received via Serial2 to the LocoNet instance.
 while (Serial2.available()>0) {
      LocoNet.addToBuffer(Serial2.read());
      // Check for any received LocoNet packets
      lnMsg* LnPacket = LocoNet.receive() ;
      if ( LnPacket ) { 
            // first mirror the message
            LocoNet.send(LnPacket);
            // then handle the message
            ...
            LocoFCClass.processMessage( LnPacket );
            LocoNet.processSwitchSensorMessage(LnPacket);
            ...
            ...
    }
}
  • implement the callback function
bool sendRawLocoNet(uint8_t val)
{
  return Serial2.write(val)==1;
}

Your done.


Another change in this commit is the possibility to disable EEPROM support.
This is done by setting the preprocessor definiton LOCONET_NO_EEPROM

Jueff added 2 commits March 17, 2022 23:21
- LOCONET_NO_SW_UART allows complete decoupling of hardware transport and LocoNet protocol
- LOCONET_NO_EEPROM turns off access of EERPOM
@Hardi-St
Copy link

By setting the new define LOCONET_NO_SW_UART the library internal serial communication is turned off. Instead an integrator may now use a communication channel of his choice.

A new method addToBuffer(uint8_t newByte) allows the integrator so forward data from the transport channel to LocoNet instance, and an external callback function bool sendRawLocoNet(uint8_t val) is called by the library to output data to the transport channel.

I also added an example called LocoNetMonitorESP32TCP which runs on an ESP32. Its a clone of the Monitor example. It turns on Wifi and creates a TCP server socket at LocoNetServer.local:1001. Application like Rocrail can directly connect to the device via network. In Rocrail configure Interface "LocoNet", Device "LocoNetServer.local:1001", Typ LocoBuffer, Uncheck CTS Flow.

With an ESP32 also a Bluetooth serial could be a communication channel, or more easy just the usage of e.g. Serial2 for LocoNet communication.

How could Serial2 be used?

* set the preprocessor definiton LOCONET_NO_SW_UART

* in Setup() add Serial2.begin(19200)

* in Loop() forward all data received via Serial2 to the LocoNet instance.
 while (Serial2.available()>0) {
      LocoNet.addToBuffer(Serial2.read());
      // Check for any received LocoNet packets
      lnMsg* LnPacket = LocoNet.receive() ;
      if ( LnPacket ) { 
            // first mirror the message
            LocoNet.send(LnPacket);
            // then handle the message
            ...
            LocoFCClass.processMessage( LnPacket );
            LocoNet.processSwitchSensorMessage(LnPacket);
            ...
            ...
    }
}
* implement the callback function
bool sendRawLocoNet(uint8_t val)
{
  return Serial2.write(val)==1;
}

Your done.

Another change in this commit is the possibility to disable EEPROM support. This is done by setting the preprocessor definiton LOCONET_NO_EEPROM

Great change, I have immediately tested, works fine

@MobaNick-Dominik
Copy link

This is the change I have been waiting for a long time

@Jueff
Copy link
Author

Jueff commented Aug 3, 2022

@kiwi64ajs: would be nice to integrate this code change. Thank you!

@@ -55,6 +55,21 @@
// figure out what board we are building

// Common defines

// turn off EEPROM for platforms not currenty implemented
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you doing a #define LOCONET_NO_EEPROM by default for each of these platforms when they have a EEPROM emulation in FLASH already?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because EEPROM support is only implemented for STM32F1, ESP8266 and AVR.
EEPROM is by default off for all other platforms, and may additionally turned off for ESP8266, STM and AVR

#endif
#endif

// turn off Software UART for platforms not currenty implemented
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems wrong to #define LOCONET_NO_SW_UART by default when there clearly is a Bit-Bashed Software UART for these platforms. I don't understand why you want to do this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, as software UART is only implemented for STM32F1, ESP8266 and AVR.

Why I want to do this: e.g. when using the library on an ESP32 or Raspberry PICO you may implement the transport channel on your own.
I'm using it on an ESP32 with LOCONET via a bluetooth serial port (works great) and also via TCP/IP socket (virtual serial port on PC forwarding communication to a socket).

@Jueff
Copy link
Author

Jueff commented Sep 3, 2022

I'll do a refinement of the pre-processor defaults with another suggestion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants