Skip to content

Commit 131eb71

Browse files
committed
Make UARTConfig_t a member variable struct.
1 parent 1dac74b commit 131eb71

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

src/sfTk/sfTkIUART.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ typedef enum _sfTkUARTParity
2828
kUARTParitySpace = 0x5ul
2929
} sfTkUARTParity_t;
3030

31-
inline const char* parityToString(sfTkUARTParity_t parity) {
31+
inline const char* parityToString(sfTkUARTParity_t parity)
32+
{
3233
static const char* parityArray[] = {"Even", "Odd", "None", "Mark", "Space"};
3334

3435
// if the parity is out of bounds, return "Unknown"
@@ -46,7 +47,8 @@ typedef enum _sfTkUARTStopBits
4647
kUARTStopBitsTwo = 0x30ul
4748
} sfTkUARTStopBits_t;
4849

49-
inline const char* stopBitsToString(sfTkUARTStopBits_t stopBits) {
50+
inline const char* stopBitsToString(sfTkUARTStopBits_t stopBits)
51+
{
5052
static const char* stopBitsArray[] = {"One", "OneAndHalf", "Two"};
5153

5254
// Return "Unknown" if index is out of bounds (less than 0 or greater than 2)
@@ -65,7 +67,8 @@ typedef enum _sfTkUARTDataBits
6567
kUARTDataBitsEight = 0x400ul,
6668
} sfTkUARTDataBits_t;
6769

68-
inline const uint8_t dataBitsToValue(sfTkUARTDataBits_t dataBits) {
70+
inline const uint8_t dataBitsToValue(sfTkUARTDataBits_t dataBits)
71+
{
6972
static const uint8_t dataBitsArray[] = {5, 6, 7, 8};
7073

7174
// Check if data bits are within valid range
@@ -76,17 +79,17 @@ inline const uint8_t dataBitsToValue(sfTkUARTDataBits_t dataBits) {
7679
return dataBitsArray[(((uint16_t)dataBits) >> 8) - 1];
7780
}
7881

79-
typedef struct _sfTkUARTConfig
80-
{
81-
uint32_t baudRate;
82-
sfTkUARTDataBits_t dataBits;
83-
sfTkUARTParity_t parity;
84-
sfTkUARTStopBits_t stopBits;
85-
} sfTkUARTConfig_t;
86-
8782
class sfTkIUART : public sfTkISerial
8883
{
8984
public:
85+
86+
typedef struct _UARTConfig
87+
{
88+
uint32_t baudRate;
89+
sfTkUARTDataBits_t dataBits;
90+
sfTkUARTParity_t parity;
91+
sfTkUARTStopBits_t stopBits;
92+
} UARTConfig_t;
9093
/**
9194
* @brief Default constructor for the UART bus
9295
*
@@ -101,7 +104,7 @@ class sfTkIUART : public sfTkISerial
101104
*
102105
* @param config
103106
*/
104-
sfTkIUART(sfTkUARTConfig_t config) : _config(config)
107+
sfTkIUART(UARTConfig_t config) : _config(config)
105108
{
106109
}
107110

@@ -235,7 +238,7 @@ class sfTkIUART : public sfTkISerial
235238
*
236239
* @return sfTkUARTConfig_t
237240
*/
238-
virtual sfTkUARTConfig_t config(void) const
241+
virtual UARTConfig_t config(void) const
239242
{
240243
return _config;
241244
}
@@ -266,5 +269,5 @@ class sfTkIUART : public sfTkISerial
266269

267270
protected:
268271
/** The internal storage of the UART config */
269-
sfTkUARTConfig_t _config;
272+
UARTConfig_t _config;
270273
};

src/sfTkArdUART.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "sfTk/sfTkISerial.h"
2121
#include "sfTk/sfTkIUART.h"
2222

23-
sfTkError_t sfTkArdUART::init(arduino::HardwareSerial &hwSerial, sfTkUARTConfig_t config, bool bInit)
23+
sfTkError_t sfTkArdUART::init(arduino::HardwareSerial &hwSerial, sfTkIUART::UARTConfig_t config, bool bInit)
2424
{
2525
_hwSerial = &hwSerial; // set the serial port
2626

@@ -54,7 +54,7 @@ sfTkError_t sfTkArdUART::init(uint32_t baudRate, bool bInit)
5454
return setBaudRate(baudRate); // set the baud rate
5555
}
5656

57-
sfTkError_t sfTkArdUART::init(sfTkUARTConfig_t config, bool bInit)
57+
sfTkError_t sfTkArdUART::init(sfTkIUART::UARTConfig_t config, bool bInit)
5858
{
5959
// if we don't have a port already, use the default Arduino Serial.
6060
if(!_hwSerial)

src/sfTkArdUART.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class sfTkArdUART : public sfTkIUART
5858
*
5959
* @param config The UART configuration settings.
6060
*/
61-
sfTkArdUART(sfTkUARTConfig_t config) : sfTkIUART(config), _hwSerial{nullptr}
61+
sfTkArdUART(UARTConfig_t config) : sfTkIUART(config), _hwSerial{nullptr}
6262
{
6363
}
6464

@@ -114,7 +114,7 @@ class sfTkArdUART : public sfTkIUART
114114
*
115115
* @param config The UART configuration settings.
116116
*/
117-
sfTkError_t init(sfTkUARTConfig_t config, bool bInit = false);
117+
sfTkError_t init(UARTConfig_t config, bool bInit = false);
118118

119119
/**
120120
* @brief Method sets up the required UART settings.
@@ -136,7 +136,7 @@ class sfTkArdUART : public sfTkIUART
136136
*
137137
* @retval ksftkErrOk on successful execution.
138138
*/
139-
sfTkError_t init(arduino::HardwareSerial &hwSerial, sfTkUARTConfig_t config, bool bInit = false);
139+
sfTkError_t init(arduino::HardwareSerial &hwSerial, UARTConfig_t config, bool bInit = false);
140140

141141
/**
142142
* @brief Write `len` bytes to the UART TX buffer.
@@ -407,7 +407,7 @@ class sfTkArdUARTBus : public sfTkISerialBus
407407
*
408408
* @param config The configuration to set
409409
*/
410-
sfTkError_t init(sfTkUARTConfig_t config, bool bInit = false)
410+
sfTkError_t init(sfTkIUART::UARTConfig_t config, bool bInit = false)
411411
{
412412
if(!_uartPort)
413413
_uartPort = std::make_unique<sfTkArdUART>();
@@ -436,7 +436,7 @@ class sfTkArdUARTBus : public sfTkISerialBus
436436
* @param bInit This flag tracks whether the bus has been initialized.
437437
* @return sfTkError_t ksfTkErrOk on successful execution.
438438
*/
439-
sfTkError_t init(sfTkArdUART &uartPort, sfTkUARTConfig_t config, bool bInit = false)
439+
sfTkError_t init(sfTkArdUART &uartPort, sfTkIUART::UARTConfig_t config, bool bInit = false)
440440
{
441441
_uartPort = std::make_unique<sfTkArdUART>(uartPort);
442442
return _uartPort->init(config, bInit);
@@ -477,7 +477,7 @@ class sfTkArdUARTBus : public sfTkISerialBus
477477
* @param bInit This flag tracks whether the bus has been initialized.
478478
* @return sfTkError_t ksfTkErrOk on successful execution.
479479
*/
480-
sfTkError_t init(arduino::HardwareSerial &hwSerial, sfTkUARTConfig_t config, bool bInit = false)
480+
sfTkError_t init(arduino::HardwareSerial &hwSerial, sfTkIUART::UARTConfig_t config, bool bInit = false)
481481
{
482482
_uartPort = std::make_unique<sfTkArdUART>(hwSerial);
483483
return _uartPort->init(config, bInit);

tests/test_uart/test_uart.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void test1() {
5959

6060
void test2() {
6161
myUART.println("Test 2: Get UART parameters.");
62-
sfTkUARTConfig_t config = myUART.config();
62+
sfTkIUART::UARTConfig_t config = myUART.config();
6363
myUART.print("Baud rate: ");
6464
myUART.println(config.baudRate);
6565
myUART.print("Data bits: ");
@@ -84,12 +84,12 @@ void test3() {
8484
}
8585

8686
// Get initial configuration
87-
sfTkUARTConfig_t initialConfig = myExtUART.config();
87+
sfTkIUART::UARTConfig_t initialConfig = myExtUART.config();
8888
myUART.println("Initial UART configuration:");
8989
printUARTConfig(initialConfig);
9090

9191
// Test different configurations
92-
sfTkUARTConfig_t testConfigs[] = {
92+
sfTkIUART::UARTConfig_t testConfigs[] = {
9393
// Test config 1: 9600 baud, 7 bits, odd parity, 2 stop bits
9494
{9600, kUARTDataBitsSeven, kUARTParityOdd, kUARTStopBitsTwo},
9595
// Test config 2: 57600 baud, 8 bits, even parity, 1 stop bit
@@ -233,7 +233,7 @@ void waitForButtonPress()
233233
}
234234

235235
// Helper function to print UART configuration
236-
void printUARTConfig(const sfTkUARTConfig_t& config) {
236+
void printUARTConfig(const sfTkIUART::UARTConfig_t& config) {
237237
myUART.print(" Baud Rate: ");
238238
myUART.println(config.baudRate);
239239
myUART.print(" Data Bits: ");

0 commit comments

Comments
 (0)