Skip to content

Commit 164c0ed

Browse files
committed
chore: Template NoteSerial
Allows for SoftwareSerial
1 parent 4f1a24e commit 164c0ed

11 files changed

+343
-69
lines changed

src/NoteSerial.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ class NoteSerial
6565
the platform specific UART implementation.
6666
*/
6767
/******************************************************************************/
68-
NoteSerial * make_note_serial (
69-
NoteSerial::param_t serial_parameters
70-
);
68+
template<typename T> NoteSerial * make_note_serial (T & serial_parameters);
69+
NoteSerial * make_note_serial (nullptr_t);
7170

7271
#endif // NOTE_SERIAL_HPP

src/NoteSerial_Arduino.cpp

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,59 @@
11
#include "NoteSerial_Arduino.hpp"
22

3+
#ifndef NOTE_MOCK
4+
#include <SoftwareSerial.h>
5+
#else
6+
#include "mock/mock-arduino.hpp"
7+
#endif
8+
9+
// Template Meta-Programming (TMP) to extract the nested template type
10+
template <typename nested_type>
11+
struct ExtractNestedTemplateType {
12+
// Default case: no extraction
13+
};
14+
template <typename nested_type>
15+
struct ExtractNestedTemplateType<MakeNoteSerial_ArduinoParameters<nested_type>> {
16+
using type = nested_type;
17+
};
18+
19+
// Singleton instance of the NoteSerial_Arduino class
20+
namespace instance {
21+
inline NoteSerial* & note_serial (void) {
22+
static NoteSerial* note_serial = nullptr;
23+
return note_serial;
24+
}
25+
};
26+
327
NoteSerial *
428
make_note_serial (
5-
NoteSerial::param_t serial_parameters_
6-
)
7-
{
8-
static NoteSerial * note_serial = nullptr;
9-
if (!serial_parameters_) {
10-
if (note_serial) {
11-
delete note_serial;
12-
note_serial = nullptr;
13-
}
14-
} else if (!note_serial) {
15-
MakeNoteSerial_ArduinoParameters * arduino_parameters = reinterpret_cast<MakeNoteSerial_ArduinoParameters *>(serial_parameters_);
16-
note_serial = new NoteSerial_Arduino(arduino_parameters->hw_serial, arduino_parameters->baud_rate);
29+
nullptr_t
30+
) {
31+
NoteSerial* & note_serial = instance::note_serial();
32+
if (note_serial) {
33+
delete note_serial;
34+
note_serial = nullptr;
1735
}
1836
return note_serial;
1937
}
2038

21-
NoteSerial_Arduino::NoteSerial_Arduino
39+
template <typename T>
40+
NoteSerial *
41+
make_note_serial (
42+
T & serial_parameters_
43+
) {
44+
NoteSerial* & note_serial = instance::note_serial();
45+
if (!note_serial) {
46+
using serial_type = typename ExtractNestedTemplateType<T>::type;
47+
note_serial = new NoteSerial_Arduino<serial_type>(serial_parameters_.hw_serial, serial_parameters_.baud_rate);
48+
}
49+
50+
return note_serial;
51+
}
52+
53+
template <typename T>
54+
NoteSerial_Arduino<T>::NoteSerial_Arduino
2255
(
23-
HardwareSerial & hw_serial_,
56+
T & hw_serial_,
2457
size_t baud_rate_
2558
) :
2659
_notecardSerial(hw_serial_),
@@ -29,31 +62,35 @@ NoteSerial_Arduino::NoteSerial_Arduino
2962
_notecardSerial.begin(_notecardSerialSpeed);
3063
}
3164

32-
NoteSerial_Arduino::~NoteSerial_Arduino (
65+
template <typename T>
66+
NoteSerial_Arduino<T>::~NoteSerial_Arduino (
3367
void
3468
)
3569
{
3670
_notecardSerial.end();
3771
}
3872

73+
template <typename T>
3974
size_t
40-
NoteSerial_Arduino::available (
75+
NoteSerial_Arduino<T>::available (
4176
void
4277
)
4378
{
4479
return _notecardSerial.available();
4580
}
4681

82+
template <typename T>
4783
char
48-
NoteSerial_Arduino::receive (
84+
NoteSerial_Arduino<T>::receive (
4985
void
5086
)
5187
{
5288
return _notecardSerial.read();
5389
}
5490

91+
template <typename T>
5592
bool
56-
NoteSerial_Arduino::reset (
93+
NoteSerial_Arduino<T>::reset (
5794
void
5895
)
5996
{
@@ -63,8 +100,9 @@ NoteSerial_Arduino::reset (
63100
return true;
64101
}
65102

103+
template <typename T>
66104
size_t
67-
NoteSerial_Arduino::transmit (
105+
NoteSerial_Arduino<T>::transmit (
68106
uint8_t *buffer,
69107
size_t size,
70108
bool flush
@@ -77,3 +115,10 @@ NoteSerial_Arduino::transmit (
77115
}
78116
return result;
79117
}
118+
119+
// Explicitly instantiate the classes and methods for the supported types
120+
template class NoteSerial_Arduino<HardwareSerial>;
121+
template class NoteSerial_Arduino<SoftwareSerial>;
122+
123+
template NoteSerial * make_note_serial<MakeNoteSerial_ArduinoParameters<HardwareSerial>>(MakeNoteSerial_ArduinoParameters<HardwareSerial> &);
124+
template NoteSerial * make_note_serial<MakeNoteSerial_ArduinoParameters<SoftwareSerial>>(MakeNoteSerial_ArduinoParameters<SoftwareSerial> &);

src/NoteSerial_Arduino.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,32 @@
99
#include "mock/mock-arduino.hpp"
1010
#endif
1111

12+
template <typename T>
1213
struct MakeNoteSerial_ArduinoParameters {
1314
MakeNoteSerial_ArduinoParameters (
14-
HardwareSerial & hw_serial_,
15+
T & hw_serial_,
1516
size_t baud_rate_
1617
) :
1718
hw_serial(hw_serial_),
1819
baud_rate(baud_rate_)
1920
{ }
20-
HardwareSerial & hw_serial;
21+
T & hw_serial;
2122
size_t baud_rate;
2223
};
2324

25+
template <typename T>
2426
class NoteSerial_Arduino final : public NoteSerial
2527
{
2628
public:
27-
NoteSerial_Arduino(HardwareSerial & hw_serial_, size_t baud_rate_);
29+
NoteSerial_Arduino(T & hw_serial_, size_t baud_rate_);
2830
~NoteSerial_Arduino(void);
2931
size_t available(void) override;
3032
char receive(void) override;
3133
bool reset(void) override;
3234
size_t transmit(uint8_t * buffer, size_t size, bool flush) override;
3335

3436
private:
35-
HardwareSerial & _notecardSerial;
37+
T & _notecardSerial;
3638
const int _notecardSerialSpeed;
3739
};
3840

src/NoteTxn.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ class NoteTxn
3838
the platform specific transaction implementation.
3939
*/
4040
/******************************************************************************/
41-
template <typename T>
42-
NoteTxn * make_note_txn (
43-
T & txn_parameters
44-
);
45-
46-
NoteTxn * make_note_txn (
47-
nullptr_t
48-
);
41+
template<typename T> NoteTxn * make_note_txn (T & txn_parameters);
42+
NoteTxn * make_note_txn (nullptr_t);
4943

5044
#endif // NOTE_TXN_HPP

src/NoteTxn_Arduino.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@
77
#include "mock/mock-parameters.hpp"
88
#endif
99

10+
// Singleton instance of the NoteSerial_Arduino class
11+
namespace instance {
12+
inline NoteTxn* & note_txn (void) {
13+
static NoteTxn* note_txn = nullptr;
14+
return note_txn;
15+
}
16+
};
17+
1018
NoteTxn *
1119
make_note_txn (
1220
nullptr_t
1321
) {
14-
const uint8_t invoke_deletion[2] = {0, 0}; // Invalid tuple invokes deletion
15-
return make_note_txn(invoke_deletion);
22+
NoteTxn* & note_txn = instance::note_txn();
23+
if (note_txn) {
24+
delete note_txn;
25+
note_txn = nullptr;
26+
}
27+
return note_txn;
1628
}
1729

1830
template <typename T>
1931
NoteTxn *
2032
make_note_txn (
2133
T & txn_pins_
2234
) {
23-
// Singleton
24-
static NoteTxn * note_txn = nullptr;
35+
NoteTxn* & note_txn = instance::note_txn();
2536

2637
if (txn_pins_[0] == txn_pins_[1]) {
2738
// Invalid tuple invokes deletion

src/Notecard.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#ifdef ARDUINO
3636
#include <Arduino.h>
37+
#include <SoftwareSerial.h>
3738
#include <Wire.h>
3839
#include "NoteSerial_Arduino.hpp"
3940
#endif
@@ -77,8 +78,12 @@ class Notecard
7778
begin(make_note_i2c(&wirePort), i2cAddress, i2cMax);
7879
}
7980
inline void begin(HardwareSerial &serial, uint32_t speed = 9600) {
80-
MakeNoteSerial_ArduinoParameters arduino_parameters(serial, speed);
81-
begin(make_note_serial(&arduino_parameters));
81+
MakeNoteSerial_ArduinoParameters<HardwareSerial> arduino_parameters(serial, speed);
82+
begin(make_note_serial<MakeNoteSerial_ArduinoParameters<HardwareSerial>>(&arduino_parameters));
83+
}
84+
inline void begin(SoftwareSerial &serial, uint32_t speed = 9600) {
85+
MakeNoteSerial_ArduinoParameters<SoftwareSerial> arduino_parameters(serial, speed);
86+
begin(make_note_serial<MakeNoteSerial_ArduinoParameters<SoftwareSerial>>(&arduino_parameters));
8287
}
8388
inline void setDebugOutputStream(Stream &dbgserial) {
8489
setDebugOutputStream(make_note_log(&dbgserial));
@@ -96,8 +101,7 @@ class Notecard
96101
setDebugOutputStream(nullptr);
97102
}
98103
inline void clearTransactionPins(void) {
99-
uint8_t txn_pins[2] = {0};
100-
setTransactionPins(make_note_txn(txn_pins));
104+
setTransactionPins(make_note_txn(nullptr));
101105
}
102106
bool debugSyncStatus (int pollFrequencyMs, int maxLevel);
103107
void deleteResponse(J *rsp) const;

0 commit comments

Comments
 (0)