Skip to content

Commit 494bd53

Browse files
committed
Create basic serial read/write class.
1 parent 286297b commit 494bd53

File tree

5 files changed

+271
-3
lines changed

5 files changed

+271
-3
lines changed

src/SparkFun_Toolkit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
// Just include the toolkit headers
2222
#include "sfTkArdI2C.h"
2323
#include "sfTkArdSPI.h"
24-
#include "sfTkArdUART.h"
24+
#include "sfTkArdSerial.h"
2525
#include "sfTkArduino.h"

src/sfTk/sfTkISerial.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,21 @@ const sfTkError_t ksfTkErrSerialNullSettings = ksfTkErrFail * (ksfTkErrBaseSeria
5959
const sfTkError_t ksfTkErrSerialNullBuffer = ksfTkErrFail * (ksfTkErrBaseSerial + 6);
6060

6161
/**
62-
* @brief Returned when the bus is under read. Warning.
62+
* @brief Returned when the serial is under read. Warning.
6363
*
6464
*/
6565
const sfTkError_t ksfTkErrSerialUnderRead = ksfTkErrBaseSerial + 7;
6666

67+
/**
68+
* @brief Returned when the serial is not enabled. Warning
69+
*/
70+
const sfTkError_t ksfTkErrSerialNotEnabled = ksfTkErrBaseBus + 8;
71+
72+
/**
73+
* @brief Returned when the serial data is bad or corrupted.
74+
*/
75+
const sfTkError_t ksfTkErrSerialBadData = ksfTkErrFail * (ksfTkErrBaseBus + 9);
76+
6777
class sfTkISerial
6878
{
6979
public:
@@ -112,4 +122,31 @@ class sfTkISerial
112122

113123
return read(&data, sizeof(data), nRead);
114124
}
125+
126+
/**
127+
* @brief Check if there are bytes available to read
128+
*
129+
* @return int Returns the number of bytes available to read in the read buffer
130+
*/
131+
virtual int available() = 0;
132+
133+
/**
134+
* @brief Check if the serial interface is available for writing
135+
*
136+
* @return int Returns the number of bytes available to write
137+
*/
138+
virtual int availableForWrite() = 0;
139+
140+
/**
141+
* @brief Peek at the next byte available to read without removing it from the buffer
142+
*
143+
* @return int Returns the next byte available to read, or -1 if no bytes are available
144+
*/
145+
virtual int peek() = 0;
146+
147+
/**
148+
* @brief Flush the serial interface's write buffer
149+
*
150+
*/
151+
virtual void flush() = 0;
115152
};

src/sfTk/sfTkISerialBus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
const uint8_t ksfTkBusTypeSerialBus = 0x03;
2323

24-
class sfTkISerialBus : sfTkIBus
24+
class sfTkISerialBus : public sfTkIBus
2525
{
2626
public:
2727
/**

src/sfTkArdSerial.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @file sfTkArdSerial.cpp
3+
* @brief Implementation file for the SparkFun Toolkit Arduino Serial driver.
4+
*
5+
* This file contains the Arduino Serial driver for the SparkFun Toolkit library.
6+
*
7+
* @author SparkFun Electronics
8+
* @date 2025
9+
* @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
10+
*
11+
* SPDX-License-Identifier: MIT
12+
*/
13+
#include <Arduino.h>
14+
15+
// clang-format off
16+
#include "sfTkArdSerial.h"
17+
#include "sfTk/sfTkError.h"
18+
#include "sfTk/sfTkISerial.h"
19+
// clang-format on
20+
21+
sfTkError_t sfTkArdSerial::init(Stream &hwStream)
22+
{
23+
_hwStream = &hwStream;
24+
25+
return init();
26+
}
27+
28+
sfTkError_t sfTkArdSerial::init()
29+
{
30+
if (!_hwStream)
31+
return ksfTkErrSerialNotInit;
32+
33+
if (!_hwStream->availableForWrite())
34+
return ksfTkErrSerialNoResponse;
35+
36+
return ksfTkErrOk;
37+
}
38+
39+
sfTkError_t sfTkArdSerial::write(const uint8_t *data, size_t length)
40+
{
41+
if (!_hwStream)
42+
return ksfTkErrSerialNotInit;
43+
44+
return (_hwStream->write(data, length) == length ? ksfTkErrOk : ksfTkErrSerialUnderRead);
45+
}
46+
47+
sfTkError_t sfTkArdSerial::read(uint8_t *data, size_t length, size_t &bytesRead)
48+
{
49+
if (!_hwStream)
50+
return ksfTkErrSerialNotInit;
51+
52+
if (!data)
53+
return ksfTkErrSerialNullBuffer;
54+
55+
if (length == 0)
56+
return ksfTkErrSerialDataTooLong; // nothing to read
57+
58+
bytesRead = 0; // zero out value
59+
60+
bytesRead = _hwStream->readBytes(data, length);
61+
62+
if (bytesRead == 0)
63+
return ksfTkErrFail;
64+
65+
// return success if all bytes read
66+
return (bytesRead == length) ? ksfTkErrOk : ksfTkErrSerialUnderRead;
67+
}
68+
69+
int sfTkArdSerial::available()
70+
{
71+
if (!_hwStream)
72+
return 0;
73+
return _hwStream->available();
74+
}
75+
76+
int sfTkArdSerial::availableForWrite()
77+
{
78+
if (!_hwStream)
79+
return 0;
80+
return _hwStream->availableForWrite();
81+
}
82+
83+
int sfTkArdSerial::peek()
84+
{
85+
if (!_hwStream)
86+
return -1;
87+
return _hwStream->peek();
88+
}
89+
90+
void sfTkArdSerial::flush()
91+
{
92+
if (!_hwStream)
93+
return;
94+
_hwStream->flush();
95+
}

src/sfTkArdSerial.h

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* @file sfTkArdSerial.h
3+
* @brief header file for the SparkFun Toolkit Arduino Serial driver.
4+
*
5+
* This file contains the Arduino Serial header for the SparkFun Toolkit library.
6+
*
7+
* @author SparkFun Electronics
8+
* @date 2025
9+
* @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
10+
*
11+
* SPDX-License-Identifier: MIT
12+
*/
13+
14+
#pragma once
15+
16+
#include <Arduino.h>
17+
18+
// clang-format off
19+
#include "sfTk/sfTkError.h"
20+
#include "sfTk/sfTkISerial.h"
21+
#include "sfTkArduino.h"
22+
// clang-format on
23+
24+
class sfTkArdSerial : public sfTkISerial
25+
26+
{
27+
public:
28+
/**
29+
* @brief Default constructor for the sfTkArdSerial class.
30+
* Initializes the object with no hardware stream.
31+
*/
32+
sfTkArdSerial() : sfTkISerial(), _hwStream{nullptr}
33+
{
34+
}
35+
36+
/**
37+
* @brief Constructor for the sfTkArdSerial class.
38+
* Initializes the object with a given hardware stream.
39+
*
40+
* @param hwStream Reference to a Stream object to be used for communication.
41+
*/
42+
sfTkArdSerial(Stream &hwStream) : sfTkISerial(), _hwStream{&hwStream}
43+
{
44+
}
45+
46+
/**
47+
* @brief Copy Constructor for the sfTkArdSerial class.
48+
*
49+
* @param rhs Reference to another sfTkArdSerial object to copy from.
50+
*/
51+
sfTkArdSerial(sfTkArdSerial const &rhs) : sfTkISerial(), _hwStream(rhs._hwStream)
52+
{
53+
}
54+
55+
/**
56+
* @brief Assignment operator for the sfTkArdSerial class.
57+
*
58+
* @param rhs Reference to another sfTkArdSerial object to assign from.
59+
* @return value of the left hand side of the assignment
60+
*/
61+
sfTkArdSerial &operator=(const sfTkArdSerial &rhs)
62+
{
63+
if (this != &rhs)
64+
{
65+
_hwStream = rhs._hwStream;
66+
}
67+
return *this;
68+
}
69+
70+
/**
71+
* @brief Initializes the sfTkArdSerial object with a hardware stream.
72+
*
73+
* @note hwStream is not initialized here, it must be initialized external to this class.
74+
*
75+
* @param hwStream Reference to a Stream object to be used for communication.
76+
* @return sfTkError_t Error code indicating the result of the initialization.
77+
*/
78+
sfTkError_t init(Stream &hwStream);
79+
80+
/**
81+
* @brief Initializes the sfTkArdSerial object with the default hardware stream.
82+
*
83+
* @note The stream object is not initialized here, it must be initialized external to this class.
84+
*
85+
* @return sfTkError_t Error code indicating the result of the initialization.
86+
*/
87+
sfTkError_t init();
88+
89+
/**
90+
* @brief Writes data to the hardware stream.
91+
*
92+
* @param data Pointer to the data buffer to be written.
93+
* @param length Number of bytes to write from the data buffer.
94+
* @return sfTkError_t Error code indicating the result of the write operation.
95+
*/
96+
sfTkError_t write(const uint8_t *data, size_t length) override;
97+
98+
/**
99+
* @brief Reads data from the hardware stream.
100+
*
101+
* @param data Pointer to the buffer where the read data will be stored.
102+
* @param length Number of bytes to read.
103+
* @param readBytes Reference to a variable where the number of bytes actually read will be stored.
104+
* @return sfTkError_t Error code indicating the result of the read operation.
105+
*/
106+
sfTkError_t read(uint8_t *data, size_t length, size_t &readBytes) override;
107+
108+
/**
109+
* @brief Gets the number of bytes available to read from the hardware stream.
110+
*
111+
* @return int Number of bytes available to read.
112+
*/
113+
int available() override;
114+
115+
/**
116+
* @brief Check if the serial interface is available for writing
117+
*
118+
* @return int Returns the number of bytes available to write
119+
*/
120+
int availableForWrite() override;
121+
122+
/**
123+
* @brief Peeks at the next byte in the hardware stream without removing it.
124+
*
125+
* @return int The next byte in the stream, or -1 if no data is available.
126+
*/
127+
int peek() override;
128+
129+
/**
130+
* @brief Flushes the hardware stream, ensuring all data is transmitted.
131+
*/
132+
void flush() override;
133+
134+
protected:
135+
Stream *_hwStream = nullptr;
136+
};

0 commit comments

Comments
 (0)