-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1988acc
commit 289bdf5
Showing
4 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* MIT License | ||
* | ||
* Copyright (c) 2023 Gabryel Reyes <gabryelrdiaz@gmail.com> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
/******************************************************************************* | ||
DESCRIPTION | ||
*******************************************************************************/ | ||
/** | ||
* @brief Channel structure definition for the SerialMuxProt. | ||
* @author Gabryel Reyes <gabryelrdiaz@gmail.com> | ||
*/ | ||
#ifndef SERIAL_MUX_CHANNELS_H | ||
#define SERIAL_MUX_CHANNELS_H | ||
|
||
/****************************************************************************** | ||
* Compile Switches | ||
*****************************************************************************/ | ||
|
||
/****************************************************************************** | ||
* Includes | ||
*****************************************************************************/ | ||
|
||
#include <Arduino.h> | ||
|
||
/****************************************************************************** | ||
* Macros | ||
*****************************************************************************/ | ||
|
||
/** Maximum number of SerialMuxProt Channels. */ | ||
#define MAX_CHANNELS (2U) | ||
|
||
/** Name of Channel to send Timestamp to. */ | ||
#define TIMESTAMP_CHANNEL_NAME "TIMESTAMP" | ||
|
||
/** 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)) | ||
|
||
/****************************************************************************** | ||
* 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 | ||
{ | ||
uint32_t count; /**< Count [digits]. */ | ||
} __attribute__((packed)) Counter; | ||
|
||
/****************************************************************************** | ||
* Functions | ||
*****************************************************************************/ | ||
|
||
#endif /* SERIAL_MUX_CHANNELS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* MIT License | ||
* | ||
* Copyright (c) 2023 Gabryel Reyes <gabryelrdiaz@gmail.com> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
/******************************************************************************* | ||
DESCRIPTION | ||
*******************************************************************************/ | ||
/** | ||
* @brief SerialMuxProt Example | ||
* @author Gabryel Reyes <gabryelrdiaz@gmail.com> | ||
*/ | ||
|
||
/****************************************************************************** | ||
* Includes | ||
*****************************************************************************/ | ||
|
||
#include <Arduino.h> | ||
#include <SerialMuxProtServer.hpp> | ||
#include "SerialMuxChannels.h" | ||
|
||
/****************************************************************************** | ||
* Compiler Switches | ||
*****************************************************************************/ | ||
|
||
/****************************************************************************** | ||
* Macros | ||
*****************************************************************************/ | ||
|
||
/****************************************************************************** | ||
* Types and classes | ||
*****************************************************************************/ | ||
|
||
/****************************************************************************** | ||
* Prototypes | ||
*****************************************************************************/ | ||
|
||
static void gCounterChannelCallback(const uint8_t *payload, const uint8_t payloadSize); | ||
|
||
/****************************************************************************** | ||
* Local Variables | ||
*****************************************************************************/ | ||
|
||
/** Serial interface baudrate. */ | ||
static const uint32_t SERIAL_BAUDRATE = 115200U; | ||
|
||
/** | ||
* SerialMuxProt Server Instance. | ||
* @note Serial is given as parameter to the constructor. This means, it cannot be used for other purposes. | ||
* | ||
* @tparam tMaxChannels set to MAX_CHANNELS, defined in SerialMuxChannels.h. | ||
*/ | ||
SerialMuxProtServer<MAX_CHANNELS> gSmpServer(Serial); | ||
|
||
/** SerialMuxProt Channel id for sending timestamps. */ | ||
uint8_t gSerialMuxProtChannelIdTimestamp = 0U; | ||
|
||
/****************************************************************************** | ||
* Public Methods | ||
*****************************************************************************/ | ||
|
||
/** | ||
* Setup the application. | ||
*/ | ||
void setup() | ||
{ | ||
/* Initialize Serial */ | ||
Serial.begin(SERIAL_BAUDRATE); | ||
|
||
/* Create Channel for sending timestamps. */ | ||
gSerialMuxProtChannelIdTimestamp = gSmpServer.createChannel(TIMESTAMP_CHANNEL_NAME, TIMESTAMP_CHANNEL_DLC); | ||
|
||
/* Subscribe to channel for receiving a counter. */ | ||
gSmpServer.subscribeToChannel(COUNTER_CHANNEL_NAME, gCounterChannelCallback); | ||
} | ||
|
||
/** | ||
* Process the application periodically. | ||
*/ | ||
void loop() | ||
{ | ||
/* Process SerialMuxProt. */ | ||
gSmpServer.process(millis()); | ||
} | ||
|
||
/****************************************************************************** | ||
* Protected Methods | ||
*****************************************************************************/ | ||
|
||
/****************************************************************************** | ||
* Private Methods | ||
*****************************************************************************/ | ||
|
||
/****************************************************************************** | ||
* External Functions | ||
*****************************************************************************/ | ||
|
||
/****************************************************************************** | ||
* Local Functions | ||
*****************************************************************************/ | ||
|
||
/** | ||
* Receives a counter over SerialMuxProt channel. | ||
* | ||
* @param[in] payload Counter in digits. | ||
* @param[in] payloadSize Size of one counter. | ||
*/ | ||
void gCounterChannelCallback(const uint8_t *payload, const uint8_t payloadSize) | ||
{ | ||
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, reinterpret_cast<uint8_t *>(×tampData), | ||
sizeof(timestampData)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters