From 68749d4f91d18424239a0f44a7f9a31309d263dc Mon Sep 17 00:00:00 2001
From: Kirk Benell
Date: Thu, 3 Apr 2025 13:18:26 -0600
Subject: [PATCH 01/10] quick fix for the toolkit UART compile/builds issues on
some platforms
---
library.properties | 2 +-
src/sfTkArdUART.cpp | 19 ++++++++++++++-----
2 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/library.properties b/library.properties
index e52c88b..ccfba8b 100644
--- a/library.properties
+++ b/library.properties
@@ -1,5 +1,5 @@
name=SparkFun Toolkit
-version=1.1.0
+version=1.1.1
author=SparkFun Electronics
maintainer=SparkFun Electronics
sentence=A utility library that other SparkFun libraries can take advantage of.
diff --git a/src/sfTkArdUART.cpp b/src/sfTkArdUART.cpp
index 7e63721..7188f58 100644
--- a/src/sfTkArdUART.cpp
+++ b/src/sfTkArdUART.cpp
@@ -48,16 +48,21 @@ sfTkError_t sfTkArdUART::init(HardwareSerial &hwSerial, uint32_t baudRate, bool
sfTkError_t sfTkArdUART::init(uint32_t baudRate, bool bInit)
{
+#ifdef _THIS__NOT_IS_BROKEN
// if we don't have a port already, use the default Arduino Serial.
if (!_hwSerial)
return init(Serial, baudRate, bInit);
// We already have a UART setup, so it's already initialized. Change the baud rate.
return setBaudRate(baudRate); // set the baud rate
+#else
+ return ksfTkErrFail;
+#endif
}
sfTkError_t sfTkArdUART::init(sfTkIUART::UARTConfig_t config, bool bInit)
{
+#ifdef _THIS__NOT_IS_BROKEN
// if we don't have a port already, use the default Arduino Serial.
if (!_hwSerial)
return init(Serial, config, bInit);
@@ -67,6 +72,9 @@ sfTkError_t sfTkArdUART::init(sfTkIUART::UARTConfig_t config, bool bInit)
// We already have a UART setup, so it's already initialized.
return ksfTkErrOk;
+#else
+ return ksfTkErrFail;
+#endif
}
sfTkError_t sfTkArdUART::init()
@@ -103,11 +111,11 @@ sfTkError_t sfTkArdUART::read(uint8_t *data, size_t length, size_t &bytesRead)
bytesRead = 0; // zero out value
-// #ifdef ARDUINO_ARCH_AVR
+ // #ifdef ARDUINO_ARCH_AVR
bytesRead = _hwSerial->readBytes(data, length);
-// #else
-// bytesRead = readBytes(data, length);
-// #endif
+ // #else
+ // bytesRead = readBytes(data, length);
+ // #endif
if (bytesRead == 0)
return ksfTkErrFail;
@@ -187,7 +195,8 @@ sfTkError_t sfTkArdUART::_start(void)
// ESP8266 does not support setting stop bits, parity, and data bits in a stanard manner.
_hwSerial->begin(_config.baudRate);
#else
- _hwSerial->begin(_config.baudRate, _config.stopBits | _config.parity | _config.dataBits);
+ _hwSerial->begin(_config.baudRate,
+ (uint32_t)_config.stopBits | (uint32_t)_config.parity | (uint32_t)_config.dataBits);
#endif
if (!availableForWrite())
return ksfTkErrSerialNotInit; // check if the port is available
From cc2aa7d0ca05b4d5e804397ae9001cc86be224c5 Mon Sep 17 00:00:00 2001
From: Kirk Benell
Date: Fri, 4 Apr 2025 12:57:41 -0600
Subject: [PATCH 02/10] add c6 compile test - testing
---
.github/workflows/compile-sketch.yml | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/.github/workflows/compile-sketch.yml b/.github/workflows/compile-sketch.yml
index eee9338..33c5e24 100644
--- a/.github/workflows/compile-sketch.yml
+++ b/.github/workflows/compile-sketch.yml
@@ -47,6 +47,13 @@ jobs:
- name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
+ # ESP32-C6
+ # https://github.com/espressif/arduino-esp32/blob/master/boards.txt
+ - fqbn: esp32:esp32:esp32c6
+ platforms: |
+ - name: esp32:esp32
+ source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
+
# Artemis / Apollo3 Feb 25 - there some issue here -- will need to look at
# https://github.com/sparkfun/Arduino_Apollo3/blob/main/boards.txt
# - fqbn: SparkFun:apollo3:sfe_artemis_atp
From 514bed95688503c2a176c6520a6b9aaa391736a0 Mon Sep 17 00:00:00 2001
From: Kirk Benell
Date: Fri, 4 Apr 2025 13:17:11 -0600
Subject: [PATCH 03/10] use actual boards, added more boards, added board name
output
---
.github/workflows/compile-sketch.yml | 55 +++++++++++++++++++++-------
1 file changed, 42 insertions(+), 13 deletions(-)
diff --git a/.github/workflows/compile-sketch.yml b/.github/workflows/compile-sketch.yml
index 33c5e24..88f9023 100644
--- a/.github/workflows/compile-sketch.yml
+++ b/.github/workflows/compile-sketch.yml
@@ -22,48 +22,63 @@ jobs:
# Uno
# https://github.com/arduino/ArduinoCore-avr/blob/master/boards.txt
- fqbn: arduino:avr:mega
+ name: Arduino AVR
platforms: |
- name: arduino:avr
source-url: https://downloads.arduino.cc/packages/package_index.json
# ESP32
# https://github.com/espressif/arduino-esp32/blob/master/boards.txt
- - fqbn: esp32:esp32:esp32
+ - fqbn: esp32:esp32:esp32thing_plus_c
+ name: ESP32 Thing Plus C
platforms: |
- name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# ESP32-S2
# https://github.com/espressif/arduino-esp32/blob/master/boards.txt
- - fqbn: esp32:esp32:esp32s2
+ - fqbn: esp32:esp32:sparkfun_esp32s2_thing_plus
+ name: ESP32-S2 Thing Plus
platforms: |
- - name: esp32:esp32
+ - name: esp32:esp32s2
+ source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
+
+ # ESP32-S3
+ # https://github.com/espressif/arduino-esp32/blob/master/boards.txt
+ - fqbn: esp32:esp32:sparkfun_esp32s3_thing_plus
+ name: ESP32-S3 Thing Plus
+ platforms: |
+ - name: esp32:esp32s3
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# ESP32-C3
# https://github.com/espressif/arduino-esp32/blob/master/boards.txt
- - fqbn: esp32:esp32:esp32c3
+ - fqbn: esp32:esp32:sparkfun_pro_micro_esp32c3
+ name: ESP32-C3 Pro Micro
platforms: |
- - name: esp32:esp32
+ - name: esp32:esp32c3
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# ESP32-C6
# https://github.com/espressif/arduino-esp32/blob/master/boards.txt
- - fqbn: esp32:esp32:esp32c6
+ - fqbn: esp32:esp32:sparkfun_esp32c6_thing_plus
+ name: ESP32-C6 Thing Plus
platforms: |
- - name: esp32:esp32
+ - name: esp32:esp32c6
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# Artemis / Apollo3 Feb 25 - there some issue here -- will need to look at
# https://github.com/sparkfun/Arduino_Apollo3/blob/main/boards.txt
- # - fqbn: SparkFun:apollo3:sfe_artemis_atp
- # platforms: |
- # - name: SparkFun:apollo3
- # source-url: https://raw.githubusercontent.com/sparkfun/Arduino_Apollo3/master/package_sparkfun_apollo3_index.json
+ - fqbn: SparkFun:apollo3:sfe_artemis_atp
+ name: SparkFun Artemis ATP
+ platforms: |
+ - name: SparkFun:apollo3
+ source-url: https://raw.githubusercontent.com/sparkfun/Arduino_Apollo3/master/package_sparkfun_apollo3_index.json
# ESP8266
# https://github.com/esp8266/Arduino/blob/master/boards.txt
- fqbn: esp8266:esp8266:thingdev
+ name: ESP8266 Thing Dev
platforms: |
- name: esp8266:esp8266
source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json
@@ -71,6 +86,7 @@ jobs:
# SAMD21
# https://github.com/arduino/ArduinoCore-samd/blob/master/boards.txt
- fqbn: arduino:samd:mkr1000
+ name: SAMD21 MKR1000
platforms: |
- name: arduino:samd
# source-url: https://downloads.arduino.cc/packages/package_index.json
@@ -78,6 +94,7 @@ jobs:
# Nano BLE 33 / nRF52840
# https://github.com/arduino/ArduinoCore-mbed/blob/master/boards.txt
- fqbn: arduino:mbed:nano33ble
+ name: Nano BLE 33
platforms: |
- name: arduino:mbed
# source-url: https://downloads.arduino.cc/packages/package_index.json
@@ -85,13 +102,23 @@ jobs:
# RP2040
# https://github.com/arduino/ArduinoCore-mbed/blob/master/boards.txt
- fqbn: rp2040:rp2040:sparkfun_promicrorp2040
+ name: SparkFun Pro Micro RP2040
platforms: |
- name: rp2040:rp2040
source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
+ # RP2350
+ # https://github.com/arduino/ArduinoCore-mbed/blob/master/boards.txt
+ - fqbn: rp2040:rp2040:sparkfun_iotredboard_rp2350
+ name: SparkFun IoT RedBoard RP2350
+ platforms: |
+ - name: rp2040:rp2350
+ source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
+
# STM32
# https://github.com/arduino/ArduinoCore-mbed/blob/master/boards.txt
- fqbn: STMicroelectronics:stm32:GenF4
+ name: STM32 GenF4
platforms: |
- name: STMicroelectronics:stm32
source-url: https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
@@ -100,8 +127,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- - name: Branch name
- run: echo running on branch ${GITHUB_REF##*/}
+ - name: Branch name and details
+ run: |
+ echo "running on branch ${{ github.ref_name }}"
+ echo "Board: ${{matrix.board.name}}, fqbn: ${{ matrix.board.fqbn }}"
- name: Compile Sketch
uses: arduino/compile-sketches@v1.1.0
From 7a0930df2770319fa1b1c897493e136aaa832a86 Mon Sep 17 00:00:00 2001
From: Kirk Benell
Date: Fri, 4 Apr 2025 13:20:18 -0600
Subject: [PATCH 04/10] fix issues b/c of AI
---
.github/workflows/compile-sketch.yml | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/.github/workflows/compile-sketch.yml b/.github/workflows/compile-sketch.yml
index 88f9023..adced18 100644
--- a/.github/workflows/compile-sketch.yml
+++ b/.github/workflows/compile-sketch.yml
@@ -40,7 +40,7 @@ jobs:
- fqbn: esp32:esp32:sparkfun_esp32s2_thing_plus
name: ESP32-S2 Thing Plus
platforms: |
- - name: esp32:esp32s2
+ - name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# ESP32-S3
@@ -48,7 +48,7 @@ jobs:
- fqbn: esp32:esp32:sparkfun_esp32s3_thing_plus
name: ESP32-S3 Thing Plus
platforms: |
- - name: esp32:esp32s3
+ - name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# ESP32-C3
@@ -56,7 +56,7 @@ jobs:
- fqbn: esp32:esp32:sparkfun_pro_micro_esp32c3
name: ESP32-C3 Pro Micro
platforms: |
- - name: esp32:esp32c3
+ - name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# ESP32-C6
@@ -64,7 +64,7 @@ jobs:
- fqbn: esp32:esp32:sparkfun_esp32c6_thing_plus
name: ESP32-C6 Thing Plus
platforms: |
- - name: esp32:esp32c6
+ - name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# Artemis / Apollo3 Feb 25 - there some issue here -- will need to look at
@@ -112,7 +112,7 @@ jobs:
- fqbn: rp2040:rp2040:sparkfun_iotredboard_rp2350
name: SparkFun IoT RedBoard RP2350
platforms: |
- - name: rp2040:rp2350
+ - name: rp2040:rp2040
source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
# STM32
From d77e1f74956594b13da1d2ae9da2844e5ec4e8ba Mon Sep 17 00:00:00 2001
From: Kirk Benell
Date: Fri, 4 Apr 2025 13:33:54 -0600
Subject: [PATCH 05/10] HIDE uart compile issues on some esp32 platforms. This
needs a perm fix
---
src/sfTkArdUART.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/sfTkArdUART.cpp b/src/sfTkArdUART.cpp
index 7188f58..6c82262 100644
--- a/src/sfTkArdUART.cpp
+++ b/src/sfTkArdUART.cpp
@@ -48,7 +48,8 @@ sfTkError_t sfTkArdUART::init(HardwareSerial &hwSerial, uint32_t baudRate, bool
sfTkError_t sfTkArdUART::init(uint32_t baudRate, bool bInit)
{
-#ifdef _THIS__NOT_IS_BROKEN
+ // issues here on some devices - $defineing out for now
+#ifdef _THIS_IS_BROKEN
// if we don't have a port already, use the default Arduino Serial.
if (!_hwSerial)
return init(Serial, baudRate, bInit);
@@ -62,7 +63,8 @@ sfTkError_t sfTkArdUART::init(uint32_t baudRate, bool bInit)
sfTkError_t sfTkArdUART::init(sfTkIUART::UARTConfig_t config, bool bInit)
{
-#ifdef _THIS__NOT_IS_BROKEN
+// issues here on some devices - $defineing out for now
+#ifdef _THIS_IS_BROKEN
// if we don't have a port already, use the default Arduino Serial.
if (!_hwSerial)
return init(Serial, config, bInit);
From 8cbd3b1f47af1bd1d1140e0ce3878b3064485787 Mon Sep 17 00:00:00 2001
From: Kirk Benell
Date: Fri, 4 Apr 2025 13:34:24 -0600
Subject: [PATCH 06/10] fix compile warning issue on some platforms
---
src/sfTk/sfTkIUART.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/sfTk/sfTkIUART.h b/src/sfTk/sfTkIUART.h
index 9ce5c15..8d517c4 100644
--- a/src/sfTk/sfTkIUART.h
+++ b/src/sfTk/sfTkIUART.h
@@ -68,7 +68,7 @@ typedef enum _sfTkUARTDataBits
kUARTDataBitsEight = 0x400ul,
} sfTkUARTDataBits_t;
-inline const uint8_t dataBitsToValue(sfTkUARTDataBits_t dataBits)
+inline uint8_t dataBitsToValue(sfTkUARTDataBits_t dataBits)
{
static const uint8_t dataBitsArray[] = {5, 6, 7, 8};
From 494bd5305a7ce2f569bd679bf420b820fda74dda Mon Sep 17 00:00:00 2001
From: Alex Brudner <101155592+SFE-Brudnerd@users.noreply.github.com>
Date: Mon, 14 Apr 2025 15:57:18 -0600
Subject: [PATCH 07/10] Create basic serial read/write class.
---
src/SparkFun_Toolkit.h | 2 +-
src/sfTk/sfTkISerial.h | 39 ++++++++++-
src/sfTk/sfTkISerialBus.h | 2 +-
src/sfTkArdSerial.cpp | 95 ++++++++++++++++++++++++++
src/sfTkArdSerial.h | 136 ++++++++++++++++++++++++++++++++++++++
5 files changed, 271 insertions(+), 3 deletions(-)
create mode 100644 src/sfTkArdSerial.cpp
create mode 100644 src/sfTkArdSerial.h
diff --git a/src/SparkFun_Toolkit.h b/src/SparkFun_Toolkit.h
index 4d88ef4..4e9fcd0 100644
--- a/src/SparkFun_Toolkit.h
+++ b/src/SparkFun_Toolkit.h
@@ -21,5 +21,5 @@
// Just include the toolkit headers
#include "sfTkArdI2C.h"
#include "sfTkArdSPI.h"
-#include "sfTkArdUART.h"
+#include "sfTkArdSerial.h"
#include "sfTkArduino.h"
diff --git a/src/sfTk/sfTkISerial.h b/src/sfTk/sfTkISerial.h
index e84c966..2021bc0 100644
--- a/src/sfTk/sfTkISerial.h
+++ b/src/sfTk/sfTkISerial.h
@@ -59,11 +59,21 @@ const sfTkError_t ksfTkErrSerialNullSettings = ksfTkErrFail * (ksfTkErrBaseSeria
const sfTkError_t ksfTkErrSerialNullBuffer = ksfTkErrFail * (ksfTkErrBaseSerial + 6);
/**
- * @brief Returned when the bus is under read. Warning.
+ * @brief Returned when the serial is under read. Warning.
*
*/
const sfTkError_t ksfTkErrSerialUnderRead = ksfTkErrBaseSerial + 7;
+/**
+ * @brief Returned when the serial is not enabled. Warning
+ */
+const sfTkError_t ksfTkErrSerialNotEnabled = ksfTkErrBaseBus + 8;
+
+/**
+ * @brief Returned when the serial data is bad or corrupted.
+ */
+const sfTkError_t ksfTkErrSerialBadData = ksfTkErrFail * (ksfTkErrBaseBus + 9);
+
class sfTkISerial
{
public:
@@ -112,4 +122,31 @@ class sfTkISerial
return read(&data, sizeof(data), nRead);
}
+
+ /**
+ * @brief Check if there are bytes available to read
+ *
+ * @return int Returns the number of bytes available to read in the read buffer
+ */
+ virtual int available() = 0;
+
+ /**
+ * @brief Check if the serial interface is available for writing
+ *
+ * @return int Returns the number of bytes available to write
+ */
+ virtual int availableForWrite() = 0;
+
+ /**
+ * @brief Peek at the next byte available to read without removing it from the buffer
+ *
+ * @return int Returns the next byte available to read, or -1 if no bytes are available
+ */
+ virtual int peek() = 0;
+
+ /**
+ * @brief Flush the serial interface's write buffer
+ *
+ */
+ virtual void flush() = 0;
};
diff --git a/src/sfTk/sfTkISerialBus.h b/src/sfTk/sfTkISerialBus.h
index 0ece81d..5ec7446 100644
--- a/src/sfTk/sfTkISerialBus.h
+++ b/src/sfTk/sfTkISerialBus.h
@@ -21,7 +21,7 @@
const uint8_t ksfTkBusTypeSerialBus = 0x03;
-class sfTkISerialBus : sfTkIBus
+class sfTkISerialBus : public sfTkIBus
{
public:
/**
diff --git a/src/sfTkArdSerial.cpp b/src/sfTkArdSerial.cpp
new file mode 100644
index 0000000..e1041a6
--- /dev/null
+++ b/src/sfTkArdSerial.cpp
@@ -0,0 +1,95 @@
+/**
+ * @file sfTkArdSerial.cpp
+ * @brief Implementation file for the SparkFun Toolkit Arduino Serial driver.
+ *
+ * This file contains the Arduino Serial driver for the SparkFun Toolkit library.
+ *
+ * @author SparkFun Electronics
+ * @date 2025
+ * @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+#include
+
+// clang-format off
+#include "sfTkArdSerial.h"
+#include "sfTk/sfTkError.h"
+#include "sfTk/sfTkISerial.h"
+// clang-format on
+
+sfTkError_t sfTkArdSerial::init(Stream &hwStream)
+{
+ _hwStream = &hwStream;
+
+ return init();
+}
+
+sfTkError_t sfTkArdSerial::init()
+{
+ if (!_hwStream)
+ return ksfTkErrSerialNotInit;
+
+ if (!_hwStream->availableForWrite())
+ return ksfTkErrSerialNoResponse;
+
+ return ksfTkErrOk;
+}
+
+sfTkError_t sfTkArdSerial::write(const uint8_t *data, size_t length)
+{
+ if (!_hwStream)
+ return ksfTkErrSerialNotInit;
+
+ return (_hwStream->write(data, length) == length ? ksfTkErrOk : ksfTkErrSerialUnderRead);
+}
+
+sfTkError_t sfTkArdSerial::read(uint8_t *data, size_t length, size_t &bytesRead)
+{
+ if (!_hwStream)
+ return ksfTkErrSerialNotInit;
+
+ if (!data)
+ return ksfTkErrSerialNullBuffer;
+
+ if (length == 0)
+ return ksfTkErrSerialDataTooLong; // nothing to read
+
+ bytesRead = 0; // zero out value
+
+ bytesRead = _hwStream->readBytes(data, length);
+
+ if (bytesRead == 0)
+ return ksfTkErrFail;
+
+ // return success if all bytes read
+ return (bytesRead == length) ? ksfTkErrOk : ksfTkErrSerialUnderRead;
+}
+
+int sfTkArdSerial::available()
+{
+ if (!_hwStream)
+ return 0;
+ return _hwStream->available();
+}
+
+int sfTkArdSerial::availableForWrite()
+{
+ if (!_hwStream)
+ return 0;
+ return _hwStream->availableForWrite();
+}
+
+int sfTkArdSerial::peek()
+{
+ if (!_hwStream)
+ return -1;
+ return _hwStream->peek();
+}
+
+void sfTkArdSerial::flush()
+{
+ if (!_hwStream)
+ return;
+ _hwStream->flush();
+}
diff --git a/src/sfTkArdSerial.h b/src/sfTkArdSerial.h
new file mode 100644
index 0000000..9a40ab7
--- /dev/null
+++ b/src/sfTkArdSerial.h
@@ -0,0 +1,136 @@
+/**
+ * @file sfTkArdSerial.h
+ * @brief header file for the SparkFun Toolkit Arduino Serial driver.
+ *
+ * This file contains the Arduino Serial header for the SparkFun Toolkit library.
+ *
+ * @author SparkFun Electronics
+ * @date 2025
+ * @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#pragma once
+
+#include
+
+// clang-format off
+#include "sfTk/sfTkError.h"
+#include "sfTk/sfTkISerial.h"
+#include "sfTkArduino.h"
+// clang-format on
+
+class sfTkArdSerial : public sfTkISerial
+
+{
+ public:
+ /**
+ * @brief Default constructor for the sfTkArdSerial class.
+ * Initializes the object with no hardware stream.
+ */
+ sfTkArdSerial() : sfTkISerial(), _hwStream{nullptr}
+ {
+ }
+
+ /**
+ * @brief Constructor for the sfTkArdSerial class.
+ * Initializes the object with a given hardware stream.
+ *
+ * @param hwStream Reference to a Stream object to be used for communication.
+ */
+ sfTkArdSerial(Stream &hwStream) : sfTkISerial(), _hwStream{&hwStream}
+ {
+ }
+
+ /**
+ * @brief Copy Constructor for the sfTkArdSerial class.
+ *
+ * @param rhs Reference to another sfTkArdSerial object to copy from.
+ */
+ sfTkArdSerial(sfTkArdSerial const &rhs) : sfTkISerial(), _hwStream(rhs._hwStream)
+ {
+ }
+
+ /**
+ * @brief Assignment operator for the sfTkArdSerial class.
+ *
+ * @param rhs Reference to another sfTkArdSerial object to assign from.
+ * @return value of the left hand side of the assignment
+ */
+ sfTkArdSerial &operator=(const sfTkArdSerial &rhs)
+ {
+ if (this != &rhs)
+ {
+ _hwStream = rhs._hwStream;
+ }
+ return *this;
+ }
+
+ /**
+ * @brief Initializes the sfTkArdSerial object with a hardware stream.
+ *
+ * @note hwStream is not initialized here, it must be initialized external to this class.
+ *
+ * @param hwStream Reference to a Stream object to be used for communication.
+ * @return sfTkError_t Error code indicating the result of the initialization.
+ */
+ sfTkError_t init(Stream &hwStream);
+
+ /**
+ * @brief Initializes the sfTkArdSerial object with the default hardware stream.
+ *
+ * @note The stream object is not initialized here, it must be initialized external to this class.
+ *
+ * @return sfTkError_t Error code indicating the result of the initialization.
+ */
+ sfTkError_t init();
+
+ /**
+ * @brief Writes data to the hardware stream.
+ *
+ * @param data Pointer to the data buffer to be written.
+ * @param length Number of bytes to write from the data buffer.
+ * @return sfTkError_t Error code indicating the result of the write operation.
+ */
+ sfTkError_t write(const uint8_t *data, size_t length) override;
+
+ /**
+ * @brief Reads data from the hardware stream.
+ *
+ * @param data Pointer to the buffer where the read data will be stored.
+ * @param length Number of bytes to read.
+ * @param readBytes Reference to a variable where the number of bytes actually read will be stored.
+ * @return sfTkError_t Error code indicating the result of the read operation.
+ */
+ sfTkError_t read(uint8_t *data, size_t length, size_t &readBytes) override;
+
+ /**
+ * @brief Gets the number of bytes available to read from the hardware stream.
+ *
+ * @return int Number of bytes available to read.
+ */
+ int available() override;
+
+ /**
+ * @brief Check if the serial interface is available for writing
+ *
+ * @return int Returns the number of bytes available to write
+ */
+ int availableForWrite() override;
+
+ /**
+ * @brief Peeks at the next byte in the hardware stream without removing it.
+ *
+ * @return int The next byte in the stream, or -1 if no data is available.
+ */
+ int peek() override;
+
+ /**
+ * @brief Flushes the hardware stream, ensuring all data is transmitted.
+ */
+ void flush() override;
+
+ protected:
+ Stream *_hwStream = nullptr;
+};
From 180084cd815b75ee4a314813b858cadebd00eea7 Mon Sep 17 00:00:00 2001
From: Alex Brudner <101155592+SFE-Brudnerd@users.noreply.github.com>
Date: Mon, 14 Apr 2025 15:58:27 -0600
Subject: [PATCH 08/10] Delete old UART code.
---
src/sfTk/sfTkIUART.h | 274 -------------------
src/sfTkArdUART.cpp | 638 -------------------------------------------
src/sfTkArdUART.h | 551 -------------------------------------
3 files changed, 1463 deletions(-)
delete mode 100644 src/sfTk/sfTkIUART.h
delete mode 100644 src/sfTkArdUART.cpp
delete mode 100644 src/sfTkArdUART.h
diff --git a/src/sfTk/sfTkIUART.h b/src/sfTk/sfTkIUART.h
deleted file mode 100644
index 8d517c4..0000000
--- a/src/sfTk/sfTkIUART.h
+++ /dev/null
@@ -1,274 +0,0 @@
-/**
- * @file sfTkIUART.h
- * @brief Header file for the SparkFun Toolkit UART Interface Definition.
- *
- * This file contains the interface declaration for UART configuration.
- *
- * @author SparkFun Electronics
- * @date 2025
- * @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
- *
- * SPDX-License-Identifier: MIT
- */
-
-#pragma once
-
-// clang-format off
-#include "sfTkError.h"
-#include "sfTkISerial.h"
-// clang-format on
-
-// Enums follow the Arduino Serial API constants.
-
-typedef enum _sfTkUARTParity
-{
- kUARTParityEven = 0x1ul,
- kUARTParityOdd = 0x2ul,
- kUARTParityNone = 0x3ul,
- kUARTParityMark = 0x4ul,
- kUARTParitySpace = 0x5ul
-} sfTkUARTParity_t;
-
-inline const char *parityToString(sfTkUARTParity_t parity)
-{
- static const char *parityArray[] = {"Even", "Odd", "None", "Mark", "Space"};
-
- // if the parity is out of bounds, return "Unknown"
- if (parity < kUARTParityEven || parity > kUARTParitySpace)
- return "Unknown";
-
- // return the parity string
- return parityArray[((uint8_t)parity) - 1];
-}
-
-typedef enum _sfTkUARTStopBits
-{
- kUARTStopBitsOne = 0x10ul,
- kUARTStopBitsOneAndHalf = 0x20ul,
- kUARTStopBitsTwo = 0x30ul
-} sfTkUARTStopBits_t;
-
-inline const char *stopBitsToString(sfTkUARTStopBits_t stopBits)
-{
- static const char *stopBitsArray[] = {"One", "OneAndHalf", "Two"};
-
- // Return "Unknown" if index is out of bounds (less than 0 or greater than 2)
- if (stopBits < kUARTStopBitsOne || stopBits > kUARTStopBitsTwo)
- return "Unknown";
-
- // Return the stop bits string
- return stopBitsArray[(((uint8_t)stopBits) >> 4) - 1];
-}
-
-typedef enum _sfTkUARTDataBits
-{
- kUARTDataBitsFive = 0x100ul,
- kUARTDataBitsSix = 0x200ul,
- kUARTDataBitsSeven = 0x300ul,
- kUARTDataBitsEight = 0x400ul,
-} sfTkUARTDataBits_t;
-
-inline uint8_t dataBitsToValue(sfTkUARTDataBits_t dataBits)
-{
- static const uint8_t dataBitsArray[] = {5, 6, 7, 8};
-
- // Check if data bits are within valid range
- if (dataBits < kUARTDataBitsFive || dataBits > kUARTDataBitsEight)
- return 0;
-
- // Extract index using bit shift (removing first 8 bits) and subtract 1
- return dataBitsArray[(((uint16_t)dataBits) >> 8) - 1];
-}
-
-class sfTkIUART : public sfTkISerial
-{
- public:
- typedef struct _UARTConfig
- {
- uint32_t baudRate;
- sfTkUARTDataBits_t dataBits;
- sfTkUARTParity_t parity;
- sfTkUARTStopBits_t stopBits;
- } UARTConfig_t;
- /**
- * @brief Default constructor for the UART bus
- *
- * @param baudRate
- */
- sfTkIUART(uint32_t baudRate = kDefaultBaudRate)
- : _config{kDefaultBaudRate, kUARTDataBitsEight, kUARTParityNone, kUARTStopBitsOne}
- {
- }
-
- /**
- * @brief Constructor for the UART bus with a configuration passed in
- *
- * @param config
- */
- sfTkIUART(UARTConfig_t config) : _config(config)
- {
- }
-
- /**
- * @brief setter for UART baud rate
- *
- * @param baudRate The baud rate to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- virtual sfTkError_t setBaudRate(const uint32_t baudRate)
- {
- if (_config.baudRate != baudRate)
- _config.baudRate = baudRate; // set the baud rate
-
- return ksfTkErrOk;
- }
-
- /**
- * @brief getter for the baud rate
- *
- * @retval uint32_t returns the baud rate
- */
- virtual uint32_t baudRate(void) const
- {
- return _config.baudRate;
- }
-
- /**
- * @brief setter for the stop bits
- *
- * @param stopBits The stop bits to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- virtual sfTkError_t setStopBits(const sfTkUARTStopBits_t stopBits)
- {
- if (_config.stopBits != stopBits)
- _config.stopBits = stopBits;
-
- return ksfTkErrOk;
- }
-
- /**
- * @brief getter for the stop bits
- *
- * @retval sfTkUARTStopBits_t returns the stop bits
- */
- virtual sfTkUARTStopBits_t stopBits(void) const
- {
- return _config.stopBits;
- }
-
- /**
- * @brief setter for the parity
- *
- * @param parity The parity to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- virtual sfTkError_t setParity(const sfTkUARTParity_t parity)
- {
- if (_config.parity != parity)
- _config.parity = parity;
-
- return ksfTkErrOk;
- }
-
- /**
- * @brief getter for the parity
- *
- * @retval sfTkUARTParity_t returns the parity
- */
- virtual sfTkUARTParity_t parity(void) const
- {
- return _config.parity;
- }
-
- /**
- * @brief setter for the data bits
- *
- * @param dataBits The data bits to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- virtual sfTkError_t setDataBits(const sfTkUARTDataBits_t dataBits)
- {
- if (_config.dataBits != dataBits)
- _config.dataBits = dataBits;
-
- return ksfTkErrOk;
- }
-
- /**
- * @brief getter for the data bits
- *
- * @retval uint8_t returns the data bits
- */
- virtual sfTkUARTDataBits_t dataBits(void) const
- {
- return _config.dataBits;
- }
-
- /**
- * @brief setter for the internal config object
- *
- * @param baudRate The baud rate to set
- * @param dataBits The data bits to set
- * @param parity The parity to set
- * @param stopBits The stop bits to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- virtual sfTkError_t setConfig(const uint32_t baudRate = kDefaultBaudRate,
- const sfTkUARTDataBits_t dataBits = kDefaultDataBits,
- const sfTkUARTParity_t parity = kDefaultParity,
- const sfTkUARTStopBits_t stopBits = kDefaultStopBits)
- {
- if (_config.baudRate != baudRate)
- _config.baudRate = baudRate;
-
- if (_config.dataBits != dataBits)
- _config.dataBits = dataBits;
-
- if (_config.parity != parity)
- _config.parity = parity;
-
- if (_config.stopBits != stopBits)
- _config.stopBits = stopBits;
-
- return ksfTkErrOk;
- }
-
- /**
- * @brief getter for the internal config object
- *
- * @return sfTkUARTConfig_t
- */
- virtual UARTConfig_t config(void) const
- {
- return _config;
- }
-
- /**
- * @brief kDefaultBaudRate - Default baud rate for the UART bus
- *
- */
- static constexpr uint32_t kDefaultBaudRate = 115200;
-
- /**
- * @brief kDefaultStopBits - Default stop bits for the UART bus
- *
- */
- static constexpr sfTkUARTStopBits_t kDefaultStopBits = kUARTStopBitsOne;
-
- /**
- * @brief kDefaultParity - Default parity for the UART bus
- *
- */
- static constexpr sfTkUARTParity_t kDefaultParity = kUARTParityNone;
-
- /**
- * @brief kDefaultDataBits - Default data bits for the UART bus
- *
- */
- static constexpr sfTkUARTDataBits_t kDefaultDataBits = kUARTDataBitsEight;
-
- protected:
- /** The internal storage of the UART config */
- UARTConfig_t _config;
-};
diff --git a/src/sfTkArdUART.cpp b/src/sfTkArdUART.cpp
deleted file mode 100644
index 6c82262..0000000
--- a/src/sfTkArdUART.cpp
+++ /dev/null
@@ -1,638 +0,0 @@
-/**
- * @file sfTkArdUART.cpp
- * @brief Implementation file for the SparkFun Toolkit Arduino UART driver.
- *
- * This file contains the Arduino UART driver for the SparkFun Toolkit library.
- *
- * @author SparkFun Electronics
- * @date 2025
- * @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
- *
- * SPDX-License-Identifier: MIT
- */
-#include
-
-#include
-#include
-
-// clang-format off
-#include "sfTkArdUART.h"
-#include "sfTk/sfTkError.h"
-#include "sfTk/sfTkISerial.h"
-#include "sfTk/sfTkIUART.h"
-// clang-format on
-
-sfTkError_t sfTkArdUART::init(HardwareSerial &hwSerial, sfTkIUART::UARTConfig_t &config, bool bInit)
-{
- _hwSerial = &hwSerial; // set the serial port
-
- _config = config; // set the config
-
- if (bInit)
- return _start(); // start the port
-
- return ksfTkErrOk;
-}
-
-sfTkError_t sfTkArdUART::init(HardwareSerial &hwSerial, uint32_t baudRate, bool bInit)
-{
- _hwSerial = &hwSerial; // set the serial port
-
- _config.baudRate = baudRate; // set the baud rate
-
- if (bInit)
- return _start(); // start the port
-
- return ksfTkErrOk;
-}
-
-sfTkError_t sfTkArdUART::init(uint32_t baudRate, bool bInit)
-{
- // issues here on some devices - $defineing out for now
-#ifdef _THIS_IS_BROKEN
- // if we don't have a port already, use the default Arduino Serial.
- if (!_hwSerial)
- return init(Serial, baudRate, bInit);
-
- // We already have a UART setup, so it's already initialized. Change the baud rate.
- return setBaudRate(baudRate); // set the baud rate
-#else
- return ksfTkErrFail;
-#endif
-}
-
-sfTkError_t sfTkArdUART::init(sfTkIUART::UARTConfig_t config, bool bInit)
-{
-// issues here on some devices - $defineing out for now
-#ifdef _THIS_IS_BROKEN
- // if we don't have a port already, use the default Arduino Serial.
- if (!_hwSerial)
- return init(Serial, config, bInit);
-
- if (bInit)
- return _start(); // start the port
-
- // We already have a UART setup, so it's already initialized.
- return ksfTkErrOk;
-#else
- return ksfTkErrFail;
-#endif
-}
-
-sfTkError_t sfTkArdUART::init()
-{
- return init(kDefaultBaudRate, true);
-}
-
-sfTkError_t sfTkArdUART::write(const uint8_t *data, size_t len)
-{
- if (!_hwSerial)
- return ksfTkErrSerialNotInit;
-
- return (_hwSerial->write(data, len) == len ? ksfTkErrOk : ksfTkErrSerialUnderRead);
-}
-
-sfTkError_t sfTkArdUART::write(const uint8_t data)
-{
- if (!_hwSerial)
- return ksfTkErrSerialNotInit;
-
- return (_hwSerial->write(data) ? ksfTkErrOk : ksfTkErrFail);
-}
-
-sfTkError_t sfTkArdUART::read(uint8_t *data, size_t length, size_t &bytesRead)
-{
- if (!_hwSerial)
- return ksfTkErrSerialNotInit;
-
- if (!data)
- return ksfTkErrSerialNullBuffer;
-
- if (length == 0)
- return ksfTkErrSerialDataTooLong; // nothing to read
-
- bytesRead = 0; // zero out value
-
- // #ifdef ARDUINO_ARCH_AVR
- bytesRead = _hwSerial->readBytes(data, length);
- // #else
- // bytesRead = readBytes(data, length);
- // #endif
-
- if (bytesRead == 0)
- return ksfTkErrFail;
-
- return (bytesRead == length) ? ksfTkErrOk : ksfTkErrSerialUnderRead; // return success if all bytes read
-}
-
-sfTkError_t sfTkArdUART::read(uint8_t &data)
-{
- size_t nRead;
- return read(&data, 1, nRead);
-}
-
-sfTkArdUART::operator bool()
-{
- return (bool)*_hwSerial;
-}
-
-sfTkError_t sfTkArdUART::setBaudRate(const uint32_t baudRate)
-{
- if (_config.baudRate != baudRate)
- _config.baudRate = baudRate; // set the baud rate
-
- return _start(); // start the port again
-}
-
-sfTkError_t sfTkArdUART::setStopBits(const sfTkUARTStopBits_t stopBits)
-{
- if (_config.stopBits != stopBits)
- _config.stopBits = stopBits; // set the stop bits
-
- return _start(); // start the port again
-}
-
-sfTkError_t sfTkArdUART::setParity(const sfTkUARTParity_t parity)
-{
- if (_config.parity != parity)
- _config.parity = parity; // set the baud rate
-
- return _start(); // start the port again
-}
-
-sfTkError_t sfTkArdUART::setDataBits(const sfTkUARTDataBits_t dataBits)
-{
- if (_config.dataBits != dataBits)
- _config.dataBits = dataBits; // set the baud rate
-
- return _start(); // start the port again
-}
-
-sfTkError_t sfTkArdUART::setConfig(const uint32_t baudRate, const sfTkUARTDataBits_t dataBits,
- const sfTkUARTParity_t parity, const sfTkUARTStopBits_t stopBits)
-{
- if (_config.baudRate != baudRate)
- _config.baudRate = baudRate;
-
- if (_config.dataBits != dataBits)
- _config.dataBits = dataBits;
-
- if (_config.parity != parity)
- _config.parity = parity;
-
- if (_config.stopBits != stopBits)
- _config.stopBits = stopBits;
-
- return _start(); // start the port again
-}
-
-sfTkError_t sfTkArdUART::_start(void)
-{
- if (!_hwSerial)
- return ksfTkErrSerialNotInit;
- if (_running)
- end(); // close the port if already running
- // set the config
-#ifdef ARDUINO_ARCH_ESP8266
- // ESP8266 does not support setting stop bits, parity, and data bits in a stanard manner.
- _hwSerial->begin(_config.baudRate);
-#else
- _hwSerial->begin(_config.baudRate,
- (uint32_t)_config.stopBits | (uint32_t)_config.parity | (uint32_t)_config.dataBits);
-#endif
- if (!availableForWrite())
- return ksfTkErrSerialNotInit; // check if the port is available
- // set the running flag to true
- _running = true;
-
- return ksfTkErrOk;
-}
-
-void sfTkArdUART::end(void)
-{
- _running = false; // set the running flag to false
- _hwSerial->end();
-}
-
-int sfTkArdUART::available(void)
-{
- if (!_hwSerial)
- return 0;
- return _hwSerial->available();
-}
-
-int sfTkArdUART::availableForWrite(void)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->availableForWrite();
-}
-
-int sfTkArdUART::peek(void)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->peek();
-}
-
-void sfTkArdUART::flush(void)
-{
- if (!_hwSerial)
- return;
-
- _hwSerial->flush();
-}
-
-void sfTkArdUART::setTimeout(unsigned long timeout)
-{
- if (!_hwSerial)
- return;
-
- _hwSerial->setTimeout(timeout);
-}
-
-unsigned long sfTkArdUART::getTimeout()
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->getTimeout();
-}
-
-#if !defined(ARDUINO_ARCH_AVR) && !defined(ARDUINO_ARCH_ESP8266)
-bool sfTkArdUART::find(CONSTVAR char *target)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->find(target);
-}
-
-bool sfTkArdUART::find(CONSTVAR uint8_t *target)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->find(target);
-}
-
-bool sfTkArdUART::find(CONSTVAR char *target, size_t length)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->find(target, length);
-}
-
-bool sfTkArdUART::find(CONSTVAR uint8_t *target, size_t length)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->find(target, length);
-}
-
-bool sfTkArdUART::find(char target)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->find(target);
-}
-
-bool sfTkArdUART::findUntil(CONSTVAR char *target, CONSTVAR char *terminator)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->findUntil(target, terminator);
-}
-
-bool sfTkArdUART::findUntil(CONSTVAR uint8_t *target, CONSTVAR char *terminator)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->findUntil(target, terminator);
-}
-
-bool sfTkArdUART::findUntil(CONSTVAR char *target, size_t targetLen, CONSTVAR char *terminate, size_t termLen)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->findUntil(target, targetLen, terminate, termLen);
-}
-
-bool sfTkArdUART::findUntil(CONSTVAR uint8_t *target, size_t targetLen, CONSTVAR char *terminate, size_t termLen)
-{
- if (!_hwSerial)
- return false;
-
- return _hwSerial->findUntil(target, targetLen, terminate, termLen);
-}
-
-#ifdef ARDUINO_ARCH_ESP8266
-long sfTkArdUART::parseInt()
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->parseInt();
-}
-
-float sfTkArdUART::parseFloat()
-{
- if (!_hwSerial)
- return 0.0f;
-
- return _hwSerial->parseFloat();
-}
-#else
-long sfTkArdUART::parseInt(LookaheadMode lookahead, char ignore)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->parseInt(lookahead, ignore);
-}
-
-float sfTkArdUART::parseFloat(LookaheadMode lookahead, char ignore)
-{
- if (!_hwSerial)
- return 0.0f;
-
- return _hwSerial->parseFloat(lookahead, ignore);
-}
-#endif
-
-size_t sfTkArdUART::readBytes(char *buffer, size_t length)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->readBytes(buffer, length);
-}
-
-size_t sfTkArdUART::readBytes(uint8_t *buffer, size_t length)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->readBytes(buffer, length);
-}
-
-size_t sfTkArdUART::readBytesUntil(char terminator, char *buffer, size_t length)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->readBytesUntil(terminator, buffer, length);
-}
-
-size_t sfTkArdUART::readBytesUntil(char terminator, uint8_t *buffer, size_t length)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->readBytesUntil(terminator, buffer, length);
-}
-
-String sfTkArdUART::readString()
-{
- if (!_hwSerial)
- return String("");
-
- return _hwSerial->readString();
-}
-
-String sfTkArdUART::readStringUntil(char terminator)
-{
- if (!_hwSerial)
- return String("");
-
- return _hwSerial->readStringUntil(terminator);
-}
-
-/**
- * @brief Print mappings
- *
- */
-size_t sfTkArdUART::print(const __FlashStringHelper *ifsh)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(ifsh);
-}
-
-size_t sfTkArdUART::print(const String &s)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(s);
-}
-
-size_t sfTkArdUART::print(const char str[])
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(str);
-}
-
-size_t sfTkArdUART::print(char c)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(c);
-}
-
-size_t sfTkArdUART::print(unsigned char b, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(b, base);
-}
-
-size_t sfTkArdUART::print(int n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(n, base);
-}
-
-size_t sfTkArdUART::print(unsigned int n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(n, base);
-}
-
-size_t sfTkArdUART::print(long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(n, base);
-}
-
-size_t sfTkArdUART::print(unsigned long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(n, base);
-}
-
-size_t sfTkArdUART::print(long long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(n, base);
-}
-
-size_t sfTkArdUART::print(unsigned long long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(n, base);
-}
-
-size_t sfTkArdUART::print(double n, int digits)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(n, digits);
-}
-
-size_t sfTkArdUART::print(const Printable &x)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->print(x);
-}
-
-size_t sfTkArdUART::println(const __FlashStringHelper *ifsh)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(ifsh);
-}
-
-size_t sfTkArdUART::println(const String &s)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(s);
-}
-
-size_t sfTkArdUART::println(const char c[])
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(c);
-}
-
-size_t sfTkArdUART::println(char c)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(c);
-}
-
-size_t sfTkArdUART::println(unsigned char b, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(b, base);
-}
-
-size_t sfTkArdUART::println(int n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(n, base);
-}
-
-size_t sfTkArdUART::println(unsigned int n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(n, base);
-}
-
-size_t sfTkArdUART::println(long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(n, base);
-}
-
-size_t sfTkArdUART::println(unsigned long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(n, base);
-}
-
-size_t sfTkArdUART::println(long long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(n, base);
-}
-
-size_t sfTkArdUART::println(unsigned long long n, int base)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(n, base);
-}
-
-size_t sfTkArdUART::println(double n, int digits)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(n, digits);
-}
-
-size_t sfTkArdUART::println(const Printable &x)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println(x);
-}
-
-size_t sfTkArdUART::println(void)
-{
- if (!_hwSerial)
- return 0;
-
- return _hwSerial->println();
-}
-#endif
diff --git a/src/sfTkArdUART.h b/src/sfTkArdUART.h
deleted file mode 100644
index 833587c..0000000
--- a/src/sfTkArdUART.h
+++ /dev/null
@@ -1,551 +0,0 @@
-/**
- * @file sfTkArdUART.h
- * @brief header file for the SparkFun Toolkit Arduino UART driver.
- *
- * This file contains the Arduino UART header for the SparkFun Toolkit library.
- *
- * @author SparkFun Electronics
- * @date 2025
- * @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License.
- *
- * SPDX-License-Identifier: MIT
- */
-
-#pragma once
-
-#include
-#include
-#include
-#include
-
-// clang-format off
-// Include our platform UART interface definitions.
-#include "sfTk/sfTkError.h"
-#include "sfTk/sfTkISerial.h"
-#include "sfTkArduino.h"
-#include
-#include
-// clang-format on
-
-#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
-
-#ifdef ARDUINO_ARCH_AVR
-#define CONSTVAR
-#else
-#define CONSTVAR const
-#endif
-
-/**
- * @brief The sfTkArdUART implements an sfTkIUART interface, defining the Arduino implementation for UART in the Toolkit
- */
-class sfTkArdUART : public sfTkIUART
-{
- public:
- /**
- * @brief Constructor
- */
- sfTkArdUART(void) : sfTkIUART(), _hwSerial{nullptr}
- {
- }
-
- /**
- * @brief Constructor
- *
- * @param baudRate The baud rate to set
- */
- sfTkArdUART(uint32_t baudRate) : sfTkIUART(baudRate), _hwSerial{nullptr}
- {
- }
-
- /**
- * @brief Constructor
- *
- * @param config The UART configuration settings.
- */
- sfTkArdUART(UARTConfig_t config) : sfTkIUART(config), _hwSerial{nullptr}
- {
- }
-
- /**
- * @brief Constructor
- *
- * @param uartPort Port for UART communication.
- */
- sfTkArdUART(HardwareSerial &hwSerial) : sfTkIUART(), _hwSerial{&hwSerial}
- {
- }
-
- /**
- * @brief Copy Constructor
- */
- sfTkArdUART(sfTkArdUART const &rhs) : sfTkIUART(rhs._config), _hwSerial{rhs._hwSerial}
- {
- }
-
- /**
- * @brief Copy assignment
- *
- * @param rhs right hand side of the assignment
- * @return value of the left hand side of the assignment
- */
- sfTkArdUART &operator=(const sfTkArdUART &rhs)
- {
- if (this != &rhs)
- {
- sfTkIUART::operator=(rhs);
- _hwSerial = rhs._hwSerial;
- }
- return *this;
- }
-
- /**
- * @brief Method sets up the required UART settings.
- * @note This function provides a default UART Port.
- *
- * @retval ksfTkErrOk on successful execution.
- */
- sfTkError_t init(void);
-
- /**
- * @brief - address version of the init method
- *
- * @param baudRate The baud rate to set
- */
- sfTkError_t init(uint32_t baudRate, bool bInit = false);
-
- /**
- * @brief config version of the init method
- *
- * @param config The UART configuration settings.
- */
- sfTkError_t init(UARTConfig_t config, bool bInit = false);
-
- /**
- * @brief Method sets up the required UART settings.
- *
- * @param uartPort Port for UART communication.
- * @param baudRate The baud rate to set.
- * @param bInit This flag tracks whether the bus has been initialized.
- *
- * @retval ksfTkErrOk on successful execution.
- */
- sfTkError_t init(HardwareSerial &hwSerial, uint32_t baudRate, bool bInit = false);
-
- /**
- * @brief Method sets up the required UART settings.
- *
- * @param uartPort Port for UART communication.
- * @param config The UART configuration settings.
- * @param bInit This flag tracks whether the bus has been initialized.
- *
- * @retval ksftkErrOk on successful execution.
- */
- sfTkError_t init(HardwareSerial &hwSerial, sfTkIUART::UARTConfig_t &config, bool bInit = false);
-
- /**
- * @brief Write `len` bytes to the UART TX buffer.
- *
- * @param data Data buffer to write.
- * @param len Number of bytes to write.
- * @return sfTkError_t - Returns ksfTkErrOk on success, or ksfTkErrFail code.
- */
- sfTkError_t write(const uint8_t *data, size_t len) override;
-
- /**
- * @brief Write one byte to the UART TX buffer.
- *
- * @param data Byte to write.
- * @return sfTkError_t - Returns ksfTkErrOk on success, or ksfTkErrFail code.
- */
- sfTkError_t write(const uint8_t data) override;
-
- /**
- * @brief Reads an array of bytes from the serial interface
- *
- * @param data The data buffer to read into
- * @param length The length of the data buffer
- * @param bytesRead[out] The number of bytes read
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t read(uint8_t *data, size_t length, size_t &bytesRead) override;
-
- /**
- * @brief Reads a single byte from the serial interface
- *
- * @param data[out] Byte to be read
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t read(uint8_t &data) override;
-
- /**
- * @brief Checks if UART is running.
- *
- * @return true - UART is running.
- * @return false - UART is not running.
- */
- operator bool();
-
- /**
- * @brief setter for UART baud rate
- *
- * @param baudRate The baud rate to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t setBaudRate(const uint32_t baudRate) override;
-
- /**
- * @brief setter for the stop bits
- *
- * @param stopBits The stop bits to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t setStopBits(const sfTkUARTStopBits_t stopBits) override;
-
- /**
- * @brief setter for the parity
- *
- * @param parity The parity to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t setParity(const sfTkUARTParity_t parity) override;
-
- /**
- * @brief setter for the data bits
- *
- * @param dataBits The data bits to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t setDataBits(const sfTkUARTDataBits_t dataBits) override;
-
- /**
- * @brief setter for the internal config object
- *
- * @param config The config struct to set
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t setConfig(const uint32_t baudRate = kDefaultBaudRate,
- const sfTkUARTDataBits_t dataBits = kDefaultDataBits,
- const sfTkUARTParity_t parity = kDefaultParity,
- const sfTkUARTStopBits_t stopBits = kDefaultStopBits) override;
-
- /**
- * @brief Arduino HardwareSerial functionality mappings.
- *
- */
- void end(void);
- int available(void);
- int availableForWrite(void);
- int peek(void);
- void flush(void);
-
- /**
- * @brief Arduino Stream functionality mappings.
- *
- */
- void setTimeout(unsigned long timeout);
- unsigned long getTimeout(void);
-
- bool find(CONSTVAR char *target);
- bool find(CONSTVAR uint8_t *target);
-
- bool find(CONSTVAR char *target, size_t length);
- bool find(CONSTVAR uint8_t *target, size_t length);
-
- bool find(char target);
-
- bool findUntil(CONSTVAR char *target, CONSTVAR char *terminator);
- bool findUntil(CONSTVAR uint8_t *target, CONSTVAR char *terminator);
-
- bool findUntil(CONSTVAR char *target, size_t targetLen, CONSTVAR char *terminate, size_t termLen);
- bool findUntil(CONSTVAR uint8_t *target, size_t targetLen, CONSTVAR char *terminate, size_t termLen);
-
-#ifdef ARDUINO_ARCH_ESP8266
- long parseInt();
- float parseFloat();
-#else
- long parseInt(LookaheadMode lookahead = LookaheadMode::SKIP_ALL, char ignore = NO_IGNORE_CHAR);
- float parseFloat(LookaheadMode lookahead = LookaheadMode::SKIP_ALL, char ignore = NO_IGNORE_CHAR);
-#endif
-
- size_t readBytes(char *buffer, size_t length);
- size_t readBytes(uint8_t *buffer, size_t length);
-
- size_t readBytesUntil(char terminator, char *buffer, size_t length);
- size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length);
-
- String readString(void);
- String readStringUntil(char terminator);
-
- /**
- * @brief Arduino Print functionality mappings.
- *
- */
- size_t print(const __FlashStringHelper *);
- size_t print(const String &);
- size_t print(const char[]);
- size_t print(char);
- size_t print(unsigned char, int = DEC);
- size_t print(int, int = DEC);
- size_t print(unsigned int, int = DEC);
- size_t print(long, int = DEC);
- size_t print(unsigned long, int = DEC);
- size_t print(long long, int = DEC);
- size_t print(unsigned long long, int = DEC);
- size_t print(double, int = 2);
- size_t print(const Printable &);
-
- size_t println(const __FlashStringHelper *);
- size_t println(const String &s);
- size_t println(const char[]);
- size_t println(char);
- size_t println(unsigned char, int = DEC);
- size_t println(int, int = DEC);
- size_t println(unsigned int, int = DEC);
- size_t println(long, int = DEC);
- size_t println(unsigned long, int = DEC);
- size_t println(long long, int = DEC);
- size_t println(unsigned long long, int = DEC);
- size_t println(double, int = 2);
- size_t println(const Printable &);
- size_t println(void);
-
-
- protected:
- /** The actual Arduino hardware port */
- HardwareSerial *_hwSerial;
-
- private:
- bool _running = false; // Flag to track if the bus is running
- sfTkError_t _start(void); // Start the connection to the UART port
-};
-
-/**
- * @brief The sfTkArdUARTBus class implements an sfTkIUARTBus interface, connecting Arduino UART to the sfTkIBus
- * interface.
- *
- */
-class sfTkArdUARTBus : public sfTkISerialBus
-{
- public:
- /**
- * @brief Constructor for the UART bus
- *
- */
- sfTkArdUARTBus(void) : sfTkISerialBus(), _uartPort{nullptr}
- {
- }
-
- /**
- * @brief Construct a new sfTkArdUARTBus object
- *
- * @param uartPort UART port to use
- */
- sfTkArdUARTBus(sfTkArdUART &uartPort) : sfTkISerialBus(), _uartPort{&uartPort}
- {
- }
-
- /**
- * @brief Construct a new sfTkArdUARTBus object
- *
- * @param hwSerial Pass in an underlying hardware serial port
- */
- sfTkArdUARTBus(HardwareSerial &hwSerial) : sfTkISerialBus()
- {
- _uartPort = new sfTkArdUART(hwSerial);
- }
-
- /**
- * @brief Copy constructor
- *
- * @param rhs Bus object to be copied
- */
- sfTkArdUARTBus(sfTkArdUARTBus const &rhs) : sfTkISerialBus()
- {
- _uartPort = rhs._uartPort ? new sfTkArdUART(*rhs._uartPort) : nullptr;
- }
-
- /**
- * @brief Destructor
- */
- ~sfTkArdUARTBus()
- {
- delete _uartPort;
- }
-
- /**
- * @brief Copy assignment
- *
- * @param rhs right hand side of the assignment
- * @return value of the left hand side of the assignment
- */
- sfTkArdUARTBus &operator=(const sfTkArdUARTBus &rhs)
- {
- if (this != &rhs)
- {
- delete _uartPort;
- _uartPort = rhs._uartPort ? new sfTkArdUART(*rhs._uartPort) : nullptr;
- }
- return *this;
- }
-
- /**
- * @brief Method sets up the required UART settings.
- * @note This function provides a default UART Port.
- *
- * @retval ksfTkErrOk on successful execution.
- */
- sfTkError_t init(void)
- {
- if (!_uartPort)
- _uartPort = new sfTkArdUART();
- return _uartPort->init();
- }
-
- /**
- * @brief - address version of the init method
- *
- * @param baudRate The baud rate to set
- */
- sfTkError_t init(uint32_t baudRate, bool bInit = false)
- {
- if (!_uartPort)
- _uartPort = new sfTkArdUART();
- return _uartPort->init(baudRate, bInit);
- }
-
- /**
- * @brief - config version of the init method
- *
- * @param config The configuration to set
- */
- sfTkError_t init(sfTkIUART::UARTConfig_t config, bool bInit = false)
- {
- if (!_uartPort)
- _uartPort = new sfTkArdUART();
- return _uartPort->init(config, bInit);
- }
-
- /**
- * @brief Method sets up the required UART settings using the provided UART port.
- *
- * @param uartPort Port for communication
- * @param baudRate The baud rate to set
- * @param bInit This flag tracks whether the bus has been initialized.
- * @return sfTkError_t ksfTkErrOk on successful execution.
- */
- sfTkError_t init(sfTkArdUART &uartPort, uint32_t baudRate, bool bInit = false)
- {
- delete _uartPort; // Delete existing reference to port.
- _uartPort = new sfTkArdUART(uartPort);
- return _uartPort->init(baudRate, bInit);
- }
-
- /**
- * @brief Method sets up the required UART settings using the provided UART port.
- *
- * @param uartPort Port for communication
- * @param config The configuration to set
- * @param bInit This flag tracks whether the bus has been initialized.
- * @return sfTkError_t ksfTkErrOk on successful execution.
- */
- sfTkError_t init(sfTkArdUART &uartPort, sfTkIUART::UARTConfig_t config, bool bInit = false)
- {
- delete _uartPort; // Delete existing reference to port.
- _uartPort = new sfTkArdUART(uartPort);
- return _uartPort->init(config, bInit);
- }
-
- /**
- * @brief Method sets up the required UART settings using the provided UART port.
- *
- * @param uartPort Port for communication
- * @param bInit This flag tracks whether the bus has been initialized.
- * @return sfTkError_t ksfTkErrOk on successful execution.
- */
- sfTkError_t init(sfTkArdUART &uartPort, bool bInit = false)
- {
- delete _uartPort; // Delete existing reference to port.
- _uartPort = new sfTkArdUART(uartPort);
- return _uartPort->init(sfTkIUART::kDefaultBaudRate, bInit);
- }
-
- /**
- * @brief Method sets up the required UART settings using the provided HardwareSerial port.
- *
- * @param hwSerial The hardware serial port to use
- * @param baudRate The baud rate to set
- * @param bInit This flag tracks whether the bus has been initialized.
- * @return sfTkError_t ksfTkErrOk on successful execution.
- */
- sfTkError_t init(HardwareSerial &hwSerial, uint32_t baudRate, bool bInit = false)
- {
- delete _uartPort; // Delete existing reference to port.
- _uartPort = new sfTkArdUART(hwSerial);
- return _uartPort->init(baudRate, bInit);
- }
-
- /**
- * @brief Method sets up the required UART settings using the provided HardwareSerial port.
- *
- * @param hwSerial The hardware serial port to use
- * @param config The configuration to set
- * @param bInit This flag tracks whether the bus has been initialized.
- * @return sfTkError_t ksfTkErrOk on successful execution.
- */
- sfTkError_t init(HardwareSerial &hwSerial, sfTkIUART::UARTConfig_t config, bool bInit = false)
- {
- delete _uartPort; // Delete existing reference to port.
- _uartPort = new sfTkArdUART(hwSerial);
- return _uartPort->init(config, bInit);
- }
-
- /**
- * @brief Method sets up the required UART settings using the provided HardwareSerial port.
- *
- * @param hwSerial The hardware serial port to use
- * @param bInit This flag tracks whether the bus has been initialized.
- * @return sfTkError_t ksfTkErrOk on successful execution.
- */
- sfTkError_t init(HardwareSerial &hwSerial, bool bInit = false)
- {
- delete _uartPort; // Delete existing reference to port.
- _uartPort = new sfTkArdUART(hwSerial);
- return _uartPort->init(sfTkIUART::kDefaultBaudRate, bInit);
- }
-
- /**
- * @brief Write `len` bytes to the UART TX buffer.
- *
- * @param data Data buffer to write.
- * @param len Number of bytes to write.
- * @return sfTkError_t - Returns ksfTkErrOk on success, or ksfTkErrFail code.
- */
- sfTkError_t write(const uint8_t *data, size_t length) override
- {
- if (!_uartPort)
- return ksfTkErrBusNotInit;
-
- sfTkError_t retVal = _uartPort->write(data, length);
-
- return retVal;
- }
-
- /**
- * @brief Reads an array of bytes from the serial interface
- *
- * @param data The data buffer to read into
- * @param length The length of the data buffer
- * @param bytesRead[out] The number of bytes read
- * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code
- */
- sfTkError_t read(uint8_t *data, size_t length, size_t &readBytes) override
- {
- if (!_uartPort)
- return ksfTkErrBusNotInit;
- return _uartPort->read(data, length, readBytes);
- }
-
- protected:
- /** The actual UART port */
- sfTkArdUART* _uartPort;
-};
From 7a4b4ae5654dd4ae2dc4761bcc00f44c89a931ec Mon Sep 17 00:00:00 2001
From: Alex Brudner <101155592+SFE-Brudnerd@users.noreply.github.com>
Date: Mon, 14 Apr 2025 16:03:24 -0600
Subject: [PATCH 09/10] update compile test.
---
examples/Example_01_TestCompile/Example_01_TestCompile.ino | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/examples/Example_01_TestCompile/Example_01_TestCompile.ino b/examples/Example_01_TestCompile/Example_01_TestCompile.ino
index f214ffc..5d3f382 100644
--- a/examples/Example_01_TestCompile/Example_01_TestCompile.ino
+++ b/examples/Example_01_TestCompile/Example_01_TestCompile.ino
@@ -6,8 +6,7 @@
sfTkArdI2C myI2C;
sfTkArdSPI mySPI;
-sfTkArdUART myUART;
-sfTkArdUARTBus myUARTBus(myUART);
+sfTkArdSerial mySerial;
void setup()
{
From 934abde82d0ba69775435c3d1887e7c32375dc20 Mon Sep 17 00:00:00 2001
From: Alex Brudner <101155592+SFE-Brudnerd@users.noreply.github.com>
Date: Mon, 14 Apr 2025 16:06:09 -0600
Subject: [PATCH 10/10] was calling `basebuserror` instead of `baseserialerror`
---
src/sfTk/sfTkISerial.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/sfTk/sfTkISerial.h b/src/sfTk/sfTkISerial.h
index 2021bc0..259d998 100644
--- a/src/sfTk/sfTkISerial.h
+++ b/src/sfTk/sfTkISerial.h
@@ -67,12 +67,12 @@ const sfTkError_t ksfTkErrSerialUnderRead = ksfTkErrBaseSerial + 7;
/**
* @brief Returned when the serial is not enabled. Warning
*/
-const sfTkError_t ksfTkErrSerialNotEnabled = ksfTkErrBaseBus + 8;
+const sfTkError_t ksfTkErrSerialNotEnabled = ksfTkErrBaseSerial + 8;
/**
* @brief Returned when the serial data is bad or corrupted.
*/
-const sfTkError_t ksfTkErrSerialBadData = ksfTkErrFail * (ksfTkErrBaseBus + 9);
+const sfTkError_t ksfTkErrSerialBadData = ksfTkErrFail * (ksfTkErrBaseSerial + 9);
class sfTkISerial
{