Skip to content

Commit

Permalink
Updated Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gabryelreyes committed Nov 21, 2023
1 parent d4da5b8 commit 4e87162
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 75 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,8 @@ jobs:
python -m pip install --upgrade pip
pip install --upgrade platformio
- name: Run tests on native environment
run: platformio ci examples/cpp/* --lib . -c platformio.ini -e esp32dev
- name: Build example for ServerA
run: platformio ci examples/ServerA/* --lib . --lib examples/SerialMuxChannels.h -c platformio.ini -e esp32dev

- name: Build example for ServerB
run: platformio ci examples/ServerB/* --lib . --lib examples/SerialMuxChannels.h -c platformio.ini -e esp32dev
14 changes: 12 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Example

The following example is written for testing the SerialMuxProt with two microcontroller boards, in this case, the ESP32 was used. Each board shall have an LED on the Pin 13.
The following example is written for testing the SerialMuxProt with two microcontroller boards, in this case, the ESP32 was used. Each board shall have an LED, either in the LED_BUILTIN Pin, or if its none on-board, on the Pin 13. This can be changed in the compiler switches in each of the `main.cpp` files.

## Installation

Expand All @@ -11,4 +11,14 @@ The following example is written for testing the SerialMuxProt with two microcon
- Rx from Board 1 with Tx from Board 2
- Tx from Board 1 with Rx from Board 2
- GND from Board 1 with GND from Board 2
- The LEDs should blink only when the boards are connected
- The LEDs should be synchronized.

## ServerA

- ServerA acts as a host, which toggles its own LED and send a command to ServerB to toggle their LED.
- ServerA creates the channel "LED" defined in SerialMuxChannels.h

## ServerB

- ServerB acts as a client, where its LED mirrors the LED from ServerA
- ServerB subscribes to the channel "LED" defined in SerialMuxChannels.h
30 changes: 9 additions & 21 deletions examples/SerialMuxChannels.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,23 @@
*****************************************************************************/

/** Maximum number of SerialMuxProt Channels. */
#define MAX_CHANNELS (2U)
#define MAX_CHANNELS (10U)

/** Name of Channel to send Timestamp to. */
#define TIMESTAMP_CHANNEL_NAME "TIMESTAMP"
/** Name of Channel to send LED data to. */
#define LED_CHANNEL_NAME "LED"

/** DLC of Timestamp Channel */
#define TIMESTAMP_CHANNEL_DLC (sizeof(Timestamp))

/** Name of Channel to send Counter to. */
#define COUNTER_CHANNEL_NAME "COUNTER"

/** DLC of Counter Channel */
#define COUNTER_CHANNEL_DLC (sizeof(Counter))
/** DLC of LED Channel */
#define LED_CHANNEL_DLC (sizeof(LedData))

/******************************************************************************
* Types and Classes
*****************************************************************************/

/** Struct of the "Timestamp" channel payload. */
typedef struct _Timestamp
{
uint32_t timestamp; /**< Timestamp [ms]. */
} __attribute__((packed)) Timestamp;

/** Struct of the "Counter" channel payload. */
typedef struct _Counter
/** Struct of the LED channel payload. */
typedef struct _LedData
{
uint32_t count; /**< Count [digits]. */
} __attribute__((packed)) Counter;
int state; /**< Enable LED. */
} __attribute__((packed)) LedData; /**< LED Channel payload. */

/******************************************************************************
* Functions
Expand Down
67 changes: 36 additions & 31 deletions examples/ServerA/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
* Compiler Switches
*****************************************************************************/

#ifndef LED_BUILTIN
#define LED_BUILTIN (13U)
#endif /* LED_BUILTIN */

/******************************************************************************
* Macros
*****************************************************************************/
Expand All @@ -53,15 +57,16 @@
* Prototypes
*****************************************************************************/

static void gCounterChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData);

/******************************************************************************
* Local Variables
*****************************************************************************/

/** Serial interface baudrate. */
static const uint32_t SERIAL_BAUDRATE = 115200U;

/** Sending LED Data Period. */
static const uint32_t LED_SEND_PERIOD = 1000U;

/**
* SerialMuxProt Server Instance.
* @note Serial is given as parameter to the constructor. This means, it cannot be used for other purposes.
Expand All @@ -70,8 +75,11 @@ static const uint32_t SERIAL_BAUDRATE = 115200U;
*/
SerialMuxProtServer<MAX_CHANNELS> gSmpServer(Serial);

/** SerialMuxProt Channel id for sending timestamps. */
uint8_t gSerialMuxProtChannelIdTimestamp = 0U;
/** SerialMuxProt Channel id for sending LED data. */
uint8_t gSerialMuxProtChannelIdLedData = 0U;

/** Last timestamp of sent data. */
uint32_t gLastLedSendTimestamp = 0U;

/******************************************************************************
* Public Methods
Expand All @@ -85,11 +93,11 @@ void setup()
/* Initialize Serial */
Serial.begin(SERIAL_BAUDRATE);

/* Create Channel for sending timestamps. */
gSerialMuxProtChannelIdTimestamp = gSmpServer.createChannel(TIMESTAMP_CHANNEL_NAME, TIMESTAMP_CHANNEL_DLC);
/* Initialize LED. */
pinMode(LED_BUILTIN, OUTPUT);

/* Subscribe to channel for receiving a counter. */
gSmpServer.subscribeToChannel(COUNTER_CHANNEL_NAME, gCounterChannelCallback);
/* Create Channel for sending LED data. */
gSerialMuxProtChannelIdLedData = gSmpServer.createChannel(LED_CHANNEL_NAME, LED_CHANNEL_DLC);
}

/**
Expand All @@ -99,6 +107,26 @@ void loop()
{
/* Process SerialMuxProt. */
gSmpServer.process(millis());

/* Send LED data periodically. */
if ((millis() - gLastLedSendTimestamp) > LED_SEND_PERIOD)
{
/* Read LED state. */
int currentLedState = digitalRead(LED_BUILTIN);

/* Create LED data payload. */
LedData payload = {.state = !currentLedState};

/* Send LED data. */
if (true == gSmpServer.sendData(gSerialMuxProtChannelIdLedData, &payload, sizeof(LedData)))
{
/* Toggle LED. */
digitalWrite(LED_BUILTIN, !currentLedState);

/* Update timestamp. */
gLastLedSendTimestamp = millis();
}
}
}

/******************************************************************************
Expand All @@ -116,26 +144,3 @@ void loop()
/******************************************************************************
* Local Functions
*****************************************************************************/

/**
* Receives a counter over SerialMuxProt channel.
*
* @param[in] payload Counter in digits.
* @param[in] payloadSize Size of one counter.
* @param[in] userData User data provided by the application.
*/
void gCounterChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData)
{
if ((nullptr != payload) && (COUNTER_CHANNEL_DLC == payloadSize))
{
const Counter* counterData = reinterpret_cast<const Counter*>(payload);

/* Send as many timestamps as the counter is set. */
for (uint32_t i = 0U; i < counterData->count; i++)
{
Timestamp timestampData;
timestampData.timestamp = millis();
gSmpServer.sendData(gSerialMuxProtChannelIdTimestamp, &timestampData, sizeof(timestampData));
}
}
}
35 changes: 16 additions & 19 deletions examples/ServerB/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
* Compiler Switches
*****************************************************************************/

#ifndef LED_BUILTIN
#define LED_BUILTIN (13U)
#endif /* LED_BUILTIN */

/******************************************************************************
* Macros
*****************************************************************************/
Expand All @@ -53,7 +57,7 @@
* Prototypes
*****************************************************************************/

static void gCounterChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData);
void gLedChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData);

/******************************************************************************
* Local Variables
Expand All @@ -70,9 +74,6 @@ static const uint32_t SERIAL_BAUDRATE = 115200U;
*/
SerialMuxProtServer<MAX_CHANNELS> gSmpServer(Serial);

/** SerialMuxProt Channel id for sending timestamps. */
uint8_t gSerialMuxProtChannelIdTimestamp = 0U;

/******************************************************************************
* Public Methods
*****************************************************************************/
Expand All @@ -85,11 +86,11 @@ void setup()
/* Initialize Serial */
Serial.begin(SERIAL_BAUDRATE);

/* Create Channel for sending timestamps. */
gSerialMuxProtChannelIdTimestamp = gSmpServer.createChannel(TIMESTAMP_CHANNEL_NAME, TIMESTAMP_CHANNEL_DLC);
/* Initialize LED. */
pinMode(LED_BUILTIN, OUTPUT);

/* Subscribe to channel for receiving a counter. */
gSmpServer.subscribeToChannel(COUNTER_CHANNEL_NAME, gCounterChannelCallback);
/* Subscribe to LED data Channel. */
gSmpServer.subscribeToChannel(LED_CHANNEL_NAME, gLedChannelCallback);
}

/**
Expand Down Expand Up @@ -124,18 +125,14 @@ void loop()
* @param[in] payloadSize Size of one counter.
* @param[in] userData User data provided by the application.
*/
void gCounterChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData)
void gLedChannelCallback(const uint8_t* payload, const uint8_t payloadSize, void* userData)
{
if ((nullptr != payload) && (COUNTER_CHANNEL_DLC == payloadSize))
if ((nullptr != payload) && (LED_CHANNEL_DLC == payloadSize))
{
const Counter* counterData = reinterpret_cast<const Counter*>(payload);

/* Send as many timestamps as the counter is set. */
for (uint32_t i = 0U; i < counterData->count; i++)
{
Timestamp timestampData;
timestampData.timestamp = millis();
gSmpServer.sendData(gSerialMuxProtChannelIdTimestamp, &timestampData, sizeof(timestampData));
}
/* Cast payload to Channel Payload Struct. */
const LedData* ledData = reinterpret_cast<const LedData*>(payload);

/* Set LED state. */
digitalWrite(LED_BUILTIN, ledData->state);
}
}

0 comments on commit 4e87162

Please sign in to comment.