MLR_Modem
 
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
MLR_Modem Class Reference

Main class for interfacing with the MLR Modem. More...

#include <MLR_Modem.h>

Inheritance diagram for MLR_Modem:
SerialModemBase

Public Member Functions

MLR_Modem_Error begin (Stream &pUart, MLR_Modem_AsyncCallback pCallback=nullptr)
 Initializes the modem driver.
 
MLR_Modem_Error SetChannel (uint8_t channel, bool saveValue)
 Sets the frequency channel.
 
MLR_Modem_Error GetChannel (uint8_t *pChannel)
 Gets the current frequency channel.
 
MLR_Modem_Error SetMode (MLR_ModemMode mode, bool saveValue)
 Sets the wireless communication mode (e.g., FSK or LoRa).
 
MLR_Modem_Error GetMode (MLR_ModemMode *pMode)
 Gets the current wireless communication mode.
 
MLR_Modem_Error SetSpreadFactor (MLR_ModemSpreadFactor sf, bool saveValue)
 Sets the LoRa spreading factor.
 
MLR_Modem_Error GetSpreadFactor (MLR_ModemSpreadFactor *pSf)
 Gets the current LoRa spreading factor.
 
MLR_Modem_Error SetEquipmentID (uint8_t ei, bool saveValue)
 Sets the Equipment ID (self ID).
 
MLR_Modem_Error GetEquipmentID (uint8_t *pEI)
 Gets the Equipment ID (self ID).
 
MLR_Modem_Error SetDestinationID (uint8_t di, bool saveValue)
 Sets the Destination ID.
 
MLR_Modem_Error GetDestinationID (uint8_t *pDI)
 Gets the Destination ID.
 
MLR_Modem_Error SetGroupID (uint8_t gi, bool saveValue)
 Sets the Group ID.
 
MLR_Modem_Error GetGroupID (uint8_t *pGI)
 Gets the Group ID.
 
MLR_Modem_Error GetUserID (uint16_t *pUserID)
 Gets the User ID.
 
MLR_Modem_Error GetRssiLastRx (int16_t *pRssi)
 Gets the RSSI (Received Signal Strength) of the last successfully received packet.
 
MLR_Modem_Error GetRssiCurrentChannel (int16_t *pRssi)
 Gets the current RSSI (noise floor) of the configured channel.
 
MLR_Modem_Error SetCarrierSenseRssiOutput (uint8_t ciValue, bool saveValue)
 Sets the Carrier Sense RSSI Output setting.
 
MLR_Modem_Error GetCarrierSenseRssiOutput (uint8_t *pCiValue)
 Gets the Carrier Sense RSSI Output setting.
 
MLR_Modem_Error GetSerialNumber (uint32_t *pSn)
 Gets the modem's serial number.
 
MLR_Modem_Error FactoryReset ()
 Resets the modem to factory settings.
 
MLR_Modem_Error GetBaudRate (uint8_t *pBaudRate)
 Gets the UART Baud Rate setting.
 
MLR_Modem_Error SetBaudRate (uint32_t baudRate, bool saveValue)
 Sets the UART Baud Rate.
 
MLR_Modem_Error SendRawCommand (const char *command, char *responseBuffer, size_t bufferSize, uint32_t timeoutMs=500)
 Sends a raw command string and waits synchronously for a response.
 
MLR_Modem_Error SendRawCommandAsync (const char *command, uint32_t timeoutMs=500)
 Sends a raw command string asynchronously. The response will be delivered via the AsyncCallback as MLR_Modem_Response::GenericResponse.
 
MLR_Modem_Error TransmitData (const uint8_t *pMsg, uint8_t len)
 Transmits data over the wireless link.
 
MLR_Modem_Error TransmitDataAsync (const uint8_t *pMsg, uint8_t len)
 Transmits data over the wireless link asynchronously. The result will be delivered via the AsyncCallback as MLR_Modem_Response::MLR_Modem_DtIr.
 
MLR_Modem_Error GetRssiCurrentChannelAsync ()
 Asynchronously requests the current RSSI of the configured channel. The result will be delivered via the AsyncCallback.
 
MLR_Modem_Error GetSerialNumberAsync ()
 Asynchronously requests the modem's serial number. The result will be delivered via the AsyncCallback.
 
MLR_Modem_Error GetPacket (const uint8_t **ppData, uint8_t *len)
 Retrieves the last received packet.
 
void SetAsyncCallback (MLR_Modem_AsyncCallback pCallback)
 Sets the asynchronous callback function.
 
bool HasPacket ()
 Checks if a new radio packet has been received.
 
void DeletePacket ()
 Deletes the currently stored received packet.
 
MLR_Modem_Error SoftReset ()
 Performs a software reset of the modem.
 
void Work ()
 Main processing loop for the driver. This function must be called regularly (e.g., in the Arduino loop()) to parse incoming serial data from the modem. Now calls SerialModemBase::update() internally.
 
- Public Member Functions inherited from SerialModemBase
 SerialModemBase ()
 
virtual ~SerialModemBase ()=default
 
void setDebugStream (Stream *debugStream)
 Initializes the debug stream.
 
void update ()
 Main processing loop. Must be called frequently. Handles command queue, transmission, and response parsing.
 
bool isIdle () const
 Checks if the internal engine is currently idle (no command processing).
 
bool isLastCommandComplete () const
 Checks if the last processed command has finished. Useful for implementing synchronous wrappers.
 
ModemError getLastCommandResult () const
 Gets the result of the last processed command.
 
const uint8_t * getRxBuffer () const
 Accessor for the shared RX buffer.
 
uint16_t getRxIndex () const
 
bool isQueueFull () const
 Checks if the command queue is full.
 
bool isQueueEmpty () const
 Checks if the command queue is empty.
 
uint8_t getQueueCount () const
 Gets the number of commands currently in the queue.
 

Protected Member Functions

ModemParseResult parse () override
 Main parser state machine step. Must use readByte() and update _rxBuffer / _rxIndex.
 
void onRxDataReceived () override
 
void onCommandComplete (ModemError result) override
 Optional hook called when a queued command finishes.
 
const char * getLogPrefix () const override
 
- Protected Member Functions inherited from SerialModemBase
void initSerial (Stream &stream)
 Sets the UART stream to use for modem communication.
 
ModemError enqueueCommand (const char *cmd, CommandType type, uint32_t timeoutMs=1000)
 Enqueues a command for processing. Non-blocking.
 
ModemError enqueueTxCommand (const char *cmdHeader, const uint8_t *payload, uint8_t len, const char *suffix=nullptr, uint32_t timeoutMs=2000)
 Enqueues a data transmission command with payload.
 
ModemError setByteValue (const char *cmd, uint8_t value, bool save, const char *respPrefix, size_t respLen)
 Helper to set a 1-byte value (e.g., @CH0E) and verify the response.
 
ModemError getByteValue (const char *cmd, uint8_t *pValue, const char *respPrefix, size_t respLen)
 Helper to get a 1-byte value.
 
ModemError setBoolValue (const char *baseCmd, bool enabled, bool save, const char *respPrefix)
 Helper to set a boolean value (ON/OF).
 
ModemError getBoolValue (const char *cmd, bool *pValue, const char *respPrefix)
 Helper to get a boolean value (ON/OF).
 
ModemError sendRawCommand (const char *command, char *responseBuffer, size_t bufferSize, uint32_t timeoutMs)
 Sends a raw command string and fills the provided buffer with the response.
 
ModemError waitForSyncComplete (uint32_t timeoutMs)
 Helper to wait for the current command to finish (Pseudo-blocking). Calls update() internally.
 
void writeString (const char *str, bool printPrefix=true)
 
void writeData (const uint8_t *data, size_t len)
 
int readByte ()
 
void unreadByte (uint8_t c)
 
void clearUnreadByte ()
 
void flushGarbage (char keepChar=' *')
 
void startTimeout (uint32_t ms)
 
bool isTimeout ()
 
ModemError parseResponseHex (const uint8_t *buffer, size_t length, const char *prefix, uint8_t hexDigits, uint32_t *pResult)
 
ModemError parseResponseDec (const uint8_t *buffer, size_t length, const char *prefix, const char *suffix, size_t suffixLen, int32_t *pResult)
 
virtual ModemParseResult parse ()=0
 Main parser state machine step. Must use readByte() and update _rxBuffer / _rxIndex.
 
virtual void onRxDataReceived ()=0
 
virtual const char * getLogPrefix () const =0
 
virtual void onCommandComplete (ModemError result)
 Optional hook called when a queued command finishes.
 

Additional Inherited Members

- Static Protected Member Functions inherited from SerialModemBase
static bool parseHex (const uint8_t *pData, size_t len, uint32_t *pResult)
 
static bool parseDec (const uint8_t *pData, size_t len, uint32_t *pResult)
 
static char * appendStr (char *dest, const char *src, const char *destEnd)
 
static char * appendHex2 (char *dest, uint8_t val, const char *destEnd)
 
template<size_t N>
static char * appendStr (char(&destBuf)[N], char *currentPtr, const char *src)
 
template<size_t N>
static char * appendHex2 (char(&destBuf)[N], char *currentPtr, uint8_t val)
 
- Protected Attributes inherited from SerialModemBase
Stream * _uart = nullptr
 
Stream * _debugStream = nullptr
 
uint8_t _rxBuffer [RX_BUFFER_SIZE]
 
uint16_t _rxIndex = 0
 
int16_t _oneByteBuf = -1
 
bool _debugRxNewLine = true
 
- Static Protected Attributes inherited from SerialModemBase
static constexpr char CD_WRITE_OK_RESPONSE [] = "*WR=PS"
 
static constexpr size_t CD_WRITE_OK_RESPONSE_LEN = 6
 
static constexpr char CD_VAL_ON [] = "ON"
 
static constexpr char CD_VAL_OFF [] = "OF"
 
static constexpr char CD_CMD_WRITE_SUFFIX [] = "/W"
 
static constexpr size_t RX_BUFFER_SIZE = 300
 

Detailed Description

Main class for interfacing with the MLR Modem.

Member Function Documentation

◆ begin()

MLR_Modem_Error MLR_Modem::begin ( Stream &  pUart,
MLR_Modem_AsyncCallback  pCallback = nullptr 
)

Initializes the modem driver.

Parameters
pUartThe Serial port connected to the modem.
pCallbackThe function to call for async responses and received data.
Returns
MLR_Modem_Error::Ok on success.

◆ DeletePacket()

void MLR_Modem::DeletePacket ( )
inline

Deletes the currently stored received packet.

◆ FactoryReset()

MLR_Modem_Error MLR_Modem::FactoryReset ( )

Resets the modem to factory settings.

Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@IZ" command.

◆ GetBaudRate()

MLR_Modem_Error MLR_Modem::GetBaudRate ( uint8_t *  pBaudRate)

Gets the UART Baud Rate setting.

Parameters
pBaudRatePointer to store the current baud rate code (e.g., '19' for 19200).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@BR" command.

◆ GetCarrierSenseRssiOutput()

MLR_Modem_Error MLR_Modem::GetCarrierSenseRssiOutput ( uint8_t *  pCiValue)

Gets the Carrier Sense RSSI Output setting.

Parameters
pCiValuePointer to store the current setting ('00' = OFF, '01' = ON).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@CI" command.

◆ GetChannel()

MLR_Modem_Error MLR_Modem::GetChannel ( uint8_t *  pChannel)

Gets the current frequency channel.

Parameters
pChannelPointer to store the current channel (0x07 - 0x2E).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@CH" command.

◆ GetDestinationID()

MLR_Modem_Error MLR_Modem::GetDestinationID ( uint8_t *  pDI)

Gets the Destination ID.

Parameters
pDIPointer to store the current Destination ID (0x00 - 0xFF).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@DI" command.

◆ GetEquipmentID()

MLR_Modem_Error MLR_Modem::GetEquipmentID ( uint8_t *  pEI)

Gets the Equipment ID (self ID).

Parameters
pEIPointer to store the current Equipment ID (0x00 - 0xFF).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@EI" command.

◆ GetGroupID()

MLR_Modem_Error MLR_Modem::GetGroupID ( uint8_t *  pGI)

Gets the Group ID.

Parameters
pGIPointer to store the current Group ID (0x00 - 0xFF).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@GI" command.

◆ getLogPrefix()

const char * MLR_Modem::getLogPrefix ( ) const
inlineoverrideprotectedvirtual

Implements SerialModemBase.

◆ GetMode()

MLR_Modem_Error MLR_Modem::GetMode ( MLR_ModemMode *  pMode)

Gets the current wireless communication mode.

Parameters
pModePointer to store the current mode.
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@MO" command.

◆ GetPacket()

MLR_Modem_Error MLR_Modem::GetPacket ( const uint8_t **  ppData,
uint8_t *  len 
)

Retrieves the last received packet.

Parameters
ppDataPointer to a const uint8_t* that will be set to the packet data.
Note
The pointer *ppData will point to an internal library buffer. This pointer is only valid until the next call to Work() or DeletePacket(). If you need to access the data later, you must copy it to your own buffer.
Parameters
lenPointer to a uint8_t that will be set to the packet length.
Returns
MLR_Modem_Error::Ok on success, MLR_Modem_Error::Fail if no packet is available.
Note
This function does not remove the packet. Use DeletePacket() to clear it.

◆ GetRssiCurrentChannel()

MLR_Modem_Error MLR_Modem::GetRssiCurrentChannel ( int16_t *  pRssi)

Gets the current RSSI (noise floor) of the configured channel.

Parameters
pRssiPointer to store the RSSI value in dBm.
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@RA" command.

◆ GetRssiCurrentChannelAsync()

MLR_Modem_Error MLR_Modem::GetRssiCurrentChannelAsync ( )

Asynchronously requests the current RSSI of the configured channel. The result will be delivered via the AsyncCallback.

Returns
MLR_Modem_Error::Ok if the request was sent, MLR_Modem_Error::Busy if another async operation is pending.
Note
Uses the "@RA" command.

◆ GetRssiLastRx()

MLR_Modem_Error MLR_Modem::GetRssiLastRx ( int16_t *  pRssi)

Gets the RSSI (Received Signal Strength) of the last successfully received packet.

Parameters
pRssiPointer to store the RSSI value in dBm.
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@RS" command.

◆ GetSerialNumber()

MLR_Modem_Error MLR_Modem::GetSerialNumber ( uint32_t *  pSn)

Gets the modem's serial number.

Parameters
pSnPointer to store the serial number.
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@SN" command.

◆ GetSerialNumberAsync()

MLR_Modem_Error MLR_Modem::GetSerialNumberAsync ( )

Asynchronously requests the modem's serial number. The result will be delivered via the AsyncCallback.

Returns
MLR_Modem_Error::Ok if the request was sent, MLR_Modem_Error::Busy if another async operation is pending.
Note
Uses the "@SN" command.

◆ GetSpreadFactor()

MLR_Modem_Error MLR_Modem::GetSpreadFactor ( MLR_ModemSpreadFactor *  pSf)

Gets the current LoRa spreading factor.

Parameters
pSfPointer to store the current spreading factor.
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@SF" command.

◆ GetUserID()

MLR_Modem_Error MLR_Modem::GetUserID ( uint16_t *  pUserID)

Gets the User ID.

Parameters
pUserIDPointer to store the current User ID (0x0000 - 0xFFFF).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@UI" command.

◆ HasPacket()

bool MLR_Modem::HasPacket ( )
inline

Checks if a new radio packet has been received.

Returns
true if a packet is available, false otherwise.

◆ onCommandComplete()

void MLR_Modem::onCommandComplete ( ModemError  result)
overrideprotectedvirtual

Optional hook called when a queued command finishes.

Reimplemented from SerialModemBase.

◆ onRxDataReceived()

void MLR_Modem::onRxDataReceived ( )
overrideprotectedvirtual

Implements SerialModemBase.

◆ parse()

ModemParseResult MLR_Modem::parse ( )
overrideprotectedvirtual

Main parser state machine step. Must use readByte() and update _rxBuffer / _rxIndex.

Returns
Current status of parsing.

Implements SerialModemBase.

◆ SendRawCommand()

MLR_Modem_Error MLR_Modem::SendRawCommand ( const char *  command,
char *  responseBuffer,
size_t  bufferSize,
uint32_t  timeoutMs = 500 
)

Sends a raw command string and waits synchronously for a response.

Parameters
commandThe null-terminated command string (e.g., "@FV\r\n").
responseBufferBuffer to store the raw response line (excluding CRLF).
bufferSizeSize of the responseBuffer.
timeoutMsTimeout in milliseconds to wait for the response.
Returns
MLR_Modem_Error::Ok on success, or an error code on failure.

◆ SendRawCommandAsync()

MLR_Modem_Error MLR_Modem::SendRawCommandAsync ( const char *  command,
uint32_t  timeoutMs = 500 
)

Sends a raw command string asynchronously. The response will be delivered via the AsyncCallback as MLR_Modem_Response::GenericResponse.

Parameters
commandThe null-terminated command string (e.g., "@FV\r\n").
timeoutMsTimeout in milliseconds to wait for the response.
Returns
MLR_Modem_Error::Ok if the command was sent, or an error code.

◆ SetAsyncCallback()

void MLR_Modem::SetAsyncCallback ( MLR_Modem_AsyncCallback  pCallback)
inline

Sets the asynchronous callback function.

Parameters
pCallbackThe callback function. If set to nullptr, no callback will take place.

◆ SetBaudRate()

MLR_Modem_Error MLR_Modem::SetBaudRate ( uint32_t  baudRate,
bool  saveValue 
)

Sets the UART Baud Rate.

Parameters
baudRateThe baud rate to set (e.g., 9600, 19200).
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@BR" command.

◆ SetCarrierSenseRssiOutput()

MLR_Modem_Error MLR_Modem::SetCarrierSenseRssiOutput ( uint8_t  ciValue,
bool  saveValue 
)

Sets the Carrier Sense RSSI Output setting.

Parameters
ciValueThe setting to set ('00' = OFF, '01' = ON).
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@CI" command.

◆ SetChannel()

MLR_Modem_Error MLR_Modem::SetChannel ( uint8_t  channel,
bool  saveValue 
)

Sets the frequency channel.

Parameters
channelThe channel to set (0x07 - 0x2E).
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@CH" command.

◆ SetDestinationID()

MLR_Modem_Error MLR_Modem::SetDestinationID ( uint8_t  di,
bool  saveValue 
)

Sets the Destination ID.

Parameters
diThe Destination ID to set (0x00 - 0xFF). (0x00 is broadcast)
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@DI" command.

◆ SetEquipmentID()

MLR_Modem_Error MLR_Modem::SetEquipmentID ( uint8_t  ei,
bool  saveValue 
)

Sets the Equipment ID (self ID).

Parameters
eiThe Equipment ID to set (0x00 - 0xFF).
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@EI" command.

◆ SetGroupID()

MLR_Modem_Error MLR_Modem::SetGroupID ( uint8_t  gi,
bool  saveValue 
)

Sets the Group ID.

Parameters
giThe Group ID to set (0x00 - 0xFF).
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@GI" command.

◆ SetMode()

MLR_Modem_Error MLR_Modem::SetMode ( MLR_ModemMode  mode,
bool  saveValue 
)

Sets the wireless communication mode (e.g., FSK or LoRa).

Parameters
modeThe mode to set (e.g., LoRaCmd).
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@MO" command.

◆ SetSpreadFactor()

MLR_Modem_Error MLR_Modem::SetSpreadFactor ( MLR_ModemSpreadFactor  sf,
bool  saveValue 
)

Sets the LoRa spreading factor.

Parameters
sfThe spreading factor to set.
saveValueIf true, saves the setting to non-volatile memory (/W option).
Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@SF" command.

◆ SoftReset()

MLR_Modem_Error MLR_Modem::SoftReset ( )

Performs a software reset of the modem.

Returns
MLR_Modem_Error::Ok on success.
Note
Uses the "@SR" command.

◆ TransmitData()

MLR_Modem_Error MLR_Modem::TransmitData ( const uint8_t *  pMsg,
uint8_t  len 
)

Transmits data over the wireless link.

Parameters
pMsgPointer to the data payload to send.
lenLength of the data payload (0-255 bytes).
Returns
MLR_Modem_Error::Ok on success, MLR_Modem_Error::FailLbt if carrier sense fails.
Note
Uses the "@DT" command.

◆ TransmitDataAsync()

MLR_Modem_Error MLR_Modem::TransmitDataAsync ( const uint8_t *  pMsg,
uint8_t  len 
)

Transmits data over the wireless link asynchronously. The result will be delivered via the AsyncCallback as MLR_Modem_Response::MLR_Modem_DtIr.

Parameters
pMsgPointer to the data payload to send.
lenLength of the data payload (0-255 bytes).
Returns
MLR_Modem_Error::Ok if the command was sent, MLR_Modem_Error::Busy if another async operation is pending.

◆ Work()

void MLR_Modem::Work ( )
inline

Main processing loop for the driver. This function must be called regularly (e.g., in the Arduino loop()) to parse incoming serial data from the modem. Now calls SerialModemBase::update() internally.


The documentation for this class was generated from the following files: