MLR_Modem
 
Loading...
Searching...
No Matches
MLR_Modem.h
1//
2// MLR_Modem.h
3//
4// The original program
5// (c) 2019 Reimesch Kommunikationssysteme
6// Authors: aj, cl
7// Created on: 13.03.2019
8// Released under the MIT license
9//
10// (c) 2026 CircuitDesign,Inc.
11// Interface driver to Circuit Design SLR/MLR modems.
12//
13
14#pragma once
15#include <Arduino.h>
16#include "common/SerialModemBase.h"
17
21static constexpr uint32_t MLR_DEFAULT_BAUDRATE = 19200;
22
26enum class MLR_Modem_Response
27{
28 // internal state of modem
29 Idle,
30 ParseError,
31 Timeout,
32
33 // serial commands
34 ShowMode,
35 SaveValue,
36 Channel,
37 SerialNumber,
38 MLR_Modem_DtIr,
39 DataReceived,
40 RssiLastRx,
41 RssiCurrentChannel,
42 UserID,
43 CarrierSenseRssi,
44 FactoryReset,
45 BaudRate,
46 GenericResponse
47};
48
49// Use common ModemError for compatibility
50using MLR_Modem_Error = ModemError;
51
55enum class MLR_ModemMode : uint8_t
56{
57 FskBin = 0,
58 FskCmd = 1,
59 LoRaBin = 2,
60 LoRaCmd = 3,
61};
62
66enum class MLR_ModemSpreadFactor : uint8_t
67{
68 Chips128 = 0,
69 Chips256 = 1,
70 Chips512 = 2,
71 Chips1024 = 3,
72 Chips2048 = 4,
73 Chips4096 = 5,
74};
75
77enum class MLR_ModemParserState
78{
79 Start = 0,
80
81 ReadCmdFirstLetter,
82 ReadCmdSecondLetter,
83 ReadCmdParam,
84
85 ReadRawString,
86
87 RadioDrSize,
88 RadioDrPayload,
89
90 ReadCmdUntilCR,
91 ReadCmdUntilLF,
92};
93
98{
99 MLR_Modem_Error error;
100 MLR_Modem_Response type;
101 int32_t value;
102 const uint8_t *pPayload;
103 uint16_t payloadLen;
104
105 // --- Constructors ---
106 // 1. Default
107 MLR_Modem_Event() : error(ModemError::Ok), type(MLR_Modem_Response::Idle), value(0), pPayload(nullptr), payloadLen(0) {}
108
109 // 2. Helper for simple status events
110 MLR_Modem_Event(ModemError err, MLR_Modem_Response t)
111 : error(err), type(t), value(0), pPayload(nullptr), payloadLen(0) {}
112
113 // 3. Helper for events with a value (RSSI, SN, etc.)
114 MLR_Modem_Event(ModemError err, MLR_Modem_Response t, int32_t val)
115 : error(err), type(t), value(val), pPayload(nullptr), payloadLen(0) {}
116
117 // 4. Helper for data reception
118 MLR_Modem_Event(ModemError err, MLR_Modem_Response t, int32_t val, const uint8_t *p, uint16_t l)
119 : error(err), type(t), value(val), pPayload(p), payloadLen(l) {}
120};
121
125typedef void (*MLR_Modem_AsyncCallback)(const MLR_Modem_Event &event);
126
131{
132public: // methods
139 MLR_Modem_Error begin(Stream &pUart, MLR_Modem_AsyncCallback pCallback = nullptr);
140
148 MLR_Modem_Error SetChannel(uint8_t channel, bool saveValue);
149
156 MLR_Modem_Error GetChannel(uint8_t *pChannel);
157
165 MLR_Modem_Error SetMode(MLR_ModemMode mode, bool saveValue);
166
173 MLR_Modem_Error GetMode(MLR_ModemMode *pMode);
174
182 MLR_Modem_Error SetSpreadFactor(MLR_ModemSpreadFactor sf, bool saveValue);
183
190 MLR_Modem_Error GetSpreadFactor(MLR_ModemSpreadFactor *pSf);
191
199 MLR_Modem_Error SetEquipmentID(uint8_t ei, bool saveValue);
200
207 MLR_Modem_Error GetEquipmentID(uint8_t *pEI);
208
216 MLR_Modem_Error SetDestinationID(uint8_t di, bool saveValue);
217
224 MLR_Modem_Error GetDestinationID(uint8_t *pDI);
225
233 MLR_Modem_Error SetGroupID(uint8_t gi, bool saveValue);
234
241 MLR_Modem_Error GetGroupID(uint8_t *pGI);
242
249 MLR_Modem_Error GetUserID(uint16_t *pUserID);
250
257 MLR_Modem_Error GetRssiLastRx(int16_t *pRssi);
258
265 MLR_Modem_Error GetRssiCurrentChannel(int16_t *pRssi);
266
274 MLR_Modem_Error SetCarrierSenseRssiOutput(uint8_t ciValue, bool saveValue);
275
282 MLR_Modem_Error GetCarrierSenseRssiOutput(uint8_t *pCiValue);
283
290 MLR_Modem_Error GetSerialNumber(uint32_t *pSn);
291
297 MLR_Modem_Error FactoryReset();
298
305 MLR_Modem_Error GetBaudRate(uint8_t *pBaudRate);
306
314 MLR_Modem_Error SetBaudRate(uint32_t baudRate, bool saveValue);
315
324 MLR_Modem_Error SendRawCommand(const char *command, char *responseBuffer, size_t bufferSize, uint32_t timeoutMs = 500);
325
333 MLR_Modem_Error SendRawCommandAsync(const char *command, uint32_t timeoutMs = 500);
334
342 MLR_Modem_Error TransmitData(const uint8_t *pMsg, uint8_t len);
343
351 MLR_Modem_Error TransmitDataAsync(const uint8_t *pMsg, uint8_t len);
352
359 MLR_Modem_Error GetRssiCurrentChannelAsync();
360
361 // /**
362 // * \brief Gets the contact function for DIO1..DIO8.
363 // */
364 // MLR_Modem_Error GetContactFunction(uint8_t *pContactFunction);
365
366 // /**
367 // * \brief Sets the contact function for DIO1..DIO8.
368 // */
369 // MLR_Modem_Error SetContactFunction(uint8_t contactFunction, bool saveValue);
370
377 MLR_Modem_Error GetSerialNumberAsync();
378
387 MLR_Modem_Error GetPacket(const uint8_t **ppData, uint8_t *len);
388
393 void SetAsyncCallback(MLR_Modem_AsyncCallback pCallback) { m_pCallback = pCallback; }
394
399 bool HasPacket() { return m_drMessagePresent; }
400
404 void DeletePacket() { m_drMessagePresent = false; }
405
411 MLR_Modem_Error SoftReset();
412
419 void Work() { update(); }
420
421protected:
422 // --- SerialModemBase Virtual Overrides ---
423 ModemParseResult parse() override;
424 void onRxDataReceived() override;
425 void onCommandComplete(ModemError result) override;
426 const char *getLogPrefix() const override { return "[MLR"; }
427
428private: // methods
429 // Internal parser state machine handlers
430 ModemParseResult m_HandleReadStart();
431 ModemParseResult m_HandleReadCmdFirstLetter();
432 ModemParseResult m_HandleReadCmdSecondLetter();
433 ModemParseResult m_HandleReadCmdParam();
434 ModemParseResult m_HandleRadioDrSize();
435 ModemParseResult m_HandleRadioDrPayload();
436 ModemParseResult m_HandleReadCmdUntilCR();
437 ModemParseResult m_HandleReadCmdUntilLF();
438
439 // Internal: Dispatches an event to the async callback
440 void dispatchAsyncEvent(ModemError error, MLR_Modem_Response responseType, int32_t value = 0, const uint8_t *pPayload = nullptr, uint16_t len = 0);
441
442 // Internal helpers using Base methods
444 ModemError m_HandleMessageHexByte(uint8_t *pValue, uint32_t responseLen, const char *responsePrefix);
446 ModemError m_HandleMessageHexWord(uint16_t *pValue, uint32_t responseLen, const char *responsePrefix);
447
449 ModemError m_HandleMessage_RS(int16_t *pRssi);
451 ModemError m_HandleMessage_RA(int16_t *pRssi);
452
454 ModemError m_HandleMessage_SN(uint32_t *pSerialNumber);
455 // check if the received message is "*IZ=OK"
456 ModemError m_HandleMessage_IZ();
457
458private: // data
459 MLR_Modem_Response m_asyncExpectedResponse;
460 MLR_ModemParserState m_parserState;
461
462 // special receive buffer and data for '@DR' command
463 bool m_drMessagePresent;
464 uint8_t m_drMessageLen;
465 uint8_t m_drMessage[300];
466
467 // information response (*IR=...)
468 bool m_irMessagePresent;
469 uint8_t m_irValue;
470
471 MLR_ModemMode m_mode;
472 MLR_Modem_AsyncCallback m_pCallback;
473};
Main class for interfacing with the MLR Modem.
Definition: MLR_Modem.h:131
void onCommandComplete(ModemError result) override
Optional hook called when a queued command finishes.
Definition: MLR_Modem.cpp:460
void DeletePacket()
Deletes the currently stored received packet.
Definition: MLR_Modem.h:404
MLR_Modem_Error SetGroupID(uint8_t gi, bool saveValue)
Sets the Group ID.
Definition: MLR_Modem.cpp:198
const char * getLogPrefix() const override
Definition: MLR_Modem.h:426
MLR_Modem_Error GetRssiLastRx(int16_t *pRssi)
Gets the RSSI (Received Signal Strength) of the last successfully received packet.
Definition: MLR_Modem.cpp:225
MLR_Modem_Error GetEquipmentID(uint8_t *pEI)
Gets the Equipment ID (self ID).
Definition: MLR_Modem.cpp:183
MLR_Modem_Error SetSpreadFactor(MLR_ModemSpreadFactor sf, bool saveValue)
Sets the LoRa spreading factor.
Definition: MLR_Modem.cpp:163
MLR_Modem_Error SetCarrierSenseRssiOutput(uint8_t ciValue, bool saveValue)
Sets the Carrier Sense RSSI Output setting.
Definition: MLR_Modem.cpp:259
ModemParseResult parse() override
Main parser state machine step. Must use readByte() and update _rxBuffer / _rxIndex.
Definition: MLR_Modem.cpp:503
void Work()
Main processing loop for the driver. This function must be called regularly (e.g.,...
Definition: MLR_Modem.h:419
MLR_Modem_Error SetMode(MLR_ModemMode mode, bool saveValue)
Sets the wireless communication mode (e.g., FSK or LoRa).
Definition: MLR_Modem.cpp:142
MLR_Modem_Error TransmitData(const uint8_t *pMsg, uint8_t len)
Transmits data over the wireless link.
Definition: MLR_Modem.cpp:341
MLR_Modem_Error GetCarrierSenseRssiOutput(uint8_t *pCiValue)
Gets the Carrier Sense RSSI Output setting.
Definition: MLR_Modem.cpp:264
MLR_Modem_Error GetRssiCurrentChannelAsync()
Asynchronously requests the current RSSI of the configured channel. The result will be delivered via ...
Definition: MLR_Modem.cpp:398
MLR_Modem_Error SetEquipmentID(uint8_t ei, bool saveValue)
Sets the Equipment ID (self ID).
Definition: MLR_Modem.cpp:178
void SetAsyncCallback(MLR_Modem_AsyncCallback pCallback)
Sets the asynchronous callback function.
Definition: MLR_Modem.h:393
MLR_Modem_Error GetChannel(uint8_t *pChannel)
Gets the current frequency channel.
Definition: MLR_Modem.cpp:137
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.
Definition: MLR_Modem.cpp:325
MLR_Modem_Error GetMode(MLR_ModemMode *pMode)
Gets the current wireless communication mode.
Definition: MLR_Modem.cpp:158
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 ML...
Definition: MLR_Modem.cpp:330
MLR_Modem_Error GetUserID(uint16_t *pUserID)
Gets the User ID.
Definition: MLR_Modem.cpp:208
MLR_Modem_Error begin(Stream &pUart, MLR_Modem_AsyncCallback pCallback=nullptr)
Initializes the modem driver.
Definition: MLR_Modem.cpp:103
MLR_Modem_Error SetDestinationID(uint8_t di, bool saveValue)
Sets the Destination ID.
Definition: MLR_Modem.cpp:188
MLR_Modem_Error GetRssiCurrentChannel(int16_t *pRssi)
Gets the current RSSI (noise floor) of the configured channel.
Definition: MLR_Modem.cpp:242
MLR_Modem_Error GetDestinationID(uint8_t *pDI)
Gets the Destination ID.
Definition: MLR_Modem.cpp:193
MLR_Modem_Error SoftReset()
Performs a software reset of the modem.
Definition: MLR_Modem.cpp:320
MLR_Modem_Error SetChannel(uint8_t channel, bool saveValue)
Sets the frequency channel.
Definition: MLR_Modem.cpp:128
MLR_Modem_Error GetBaudRate(uint8_t *pBaudRate)
Gets the UART Baud Rate setting.
Definition: MLR_Modem.cpp:299
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 AsyncCallb...
Definition: MLR_Modem.cpp:385
void onRxDataReceived() override
Definition: MLR_Modem.cpp:435
MLR_Modem_Error FactoryReset()
Resets the modem to factory settings.
Definition: MLR_Modem.cpp:286
MLR_Modem_Error SetBaudRate(uint32_t baudRate, bool saveValue)
Sets the UART Baud Rate.
Definition: MLR_Modem.cpp:304
MLR_Modem_Error GetSerialNumberAsync()
Asynchronously requests the modem's serial number. The result will be delivered via the AsyncCallback...
Definition: MLR_Modem.cpp:410
bool HasPacket()
Checks if a new radio packet has been received.
Definition: MLR_Modem.h:399
MLR_Modem_Error GetSpreadFactor(MLR_ModemSpreadFactor *pSf)
Gets the current LoRa spreading factor.
Definition: MLR_Modem.cpp:173
MLR_Modem_Error GetGroupID(uint8_t *pGI)
Gets the Group ID.
Definition: MLR_Modem.cpp:203
MLR_Modem_Error GetSerialNumber(uint32_t *pSn)
Gets the modem's serial number.
Definition: MLR_Modem.cpp:269
MLR_Modem_Error GetPacket(const uint8_t **ppData, uint8_t *len)
Retrieves the last received packet.
Definition: MLR_Modem.cpp:422
Base class handling low-level serial I/O, debugging, and async transaction logic.
Definition: SerialModemBase.h:92
void update()
Main processing loop. Must be called frequently. Handles command queue, transmission,...
Definition: SerialModemBase.cpp:50
Represents an event from the modem.
Definition: MLR_Modem.h:98
MLR_Modem_Event()
Definition: MLR_Modem.h:107
MLR_Modem_Event(ModemError err, MLR_Modem_Response t)
Definition: MLR_Modem.h:110
MLR_Modem_Event(ModemError err, MLR_Modem_Response t, int32_t val, const uint8_t *p, uint16_t l)
Definition: MLR_Modem.h:118
const uint8_t * pPayload
Pointer to payload data (e.g., for DataReceived)
Definition: MLR_Modem.h:102
MLR_Modem_Event(ModemError err, MLR_Modem_Response t, int32_t val)
Definition: MLR_Modem.h:114
int32_t value
Numerical value associated with the response.
Definition: MLR_Modem.h:101
uint16_t payloadLen
Length of payload data.
Definition: MLR_Modem.h:103
MLR_Modem_Error error
Error code.
Definition: MLR_Modem.h:99
MLR_Modem_Response type
Type of response.
Definition: MLR_Modem.h:100