Skip to content

Commit

Permalink
Edit: serial class using template
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonyang-ee committed Jul 4, 2023
1 parent 6821679 commit 58e8709
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Application/Inc/Instances.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ extern CustomDAC dac;
extern CustomADC adc;

extern sml::sm<StreamState> stream_sm;
extern sml::sm<MainState, sml::process_queue<std::queue>, sml::defer_queue<std::deque>> main_sm;
extern sml::sm<MainState, sml::process_queue<std::queue>> main_sm;

#endif /* APPLICATION_INC_INSTANCES */
41 changes: 27 additions & 14 deletions Application/Inc/SerialCOM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CORE_INC_SERIALCOM

#include "algorithm"
#include "array"
#include "main.h"
#include "string"

Expand All @@ -23,26 +24,38 @@ class SerialCOM {

void sendLn();
void sendString(std::string);
void sendNumber(int);
void sendNumber(float);
void sendNumber(double);
void sendNumber(uint8_t);
void sendNumber(uint16_t);
void sendNumber(uint32_t);
void sendNumber(uint64_t);
void sendNumber(int8_t);
void sendNumber(int16_t);
void sendNumber(int32_t);
void sendNumber(int64_t);


void sendNumber(bool);


/**
* @brief Append single number to buffer.
*
* @param value all kinds of numbers.
*/
template <class T>
void sendNumber(T value) {
m_buffer_msg.append(std::to_string(value));
}

/**
* @brief Append array of numbers to buffer.
*
* @param items std::array of numbers.
*/
template <class T, size_t N>
void sendNumber(std::array<T, N> items) {
for (auto &i : items) {
m_buffer_msg.append(std::to_string(i));
if (&i != &items.back()) m_buffer_msg.append(", ");
}
}

bool sendOut();

// Tx Rx Memory for DMA to fetch and push
uint8_t m_tx_data[UART_BUFFER]; // UART_BUFFER may be re-defined in main.h
uint8_t m_rx_data[UART_BUFFER];

private:
// Port
UART_HandleTypeDef *m_port;

Expand Down
21 changes: 7 additions & 14 deletions Application/Src/SerialCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@ void SerialCOM::sendLn() { m_buffer_msg.append("\n"); };
void SerialCOM::sendString(std::string msg) { m_buffer_msg.append(msg); }

/**
* @brief Append single number to buffer.
*
* @param value all kinds of numbers.
* @brief Append bool to buffer as text.
*
* @param value bool value.
*/
void SerialCOM::sendNumber(int value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(float value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(double value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(uint8_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(uint16_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(uint32_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(uint64_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(int8_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(int16_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(int32_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(int64_t value) { m_buffer_msg.append(std::to_string(value)); }
void SerialCOM::sendNumber(bool value) {
value ? m_buffer_msg.append("ON") : m_buffer_msg.append("OFF");
}


/**
* @brief Periodically send uart in DMA mode if buffer !empty.
Expand Down
2 changes: 1 addition & 1 deletion Application/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CustomDAC dac{};
CustomADC adc{};

sml::sm<StreamState> stream_sm{&thread};
sml::sm<MainState, sml::process_queue<std::queue>, sml::defer_queue<std::deque>> main_sm{&thread, &serialCOM};
sml::sm<MainState, sml::process_queue<std::queue>> main_sm{&thread, &serialCOM};

/**
* @brief The application entry point.
Expand Down

0 comments on commit 58e8709

Please sign in to comment.