Skip to content

Commit 728a56b

Browse files
committed
chore: Template NoteSerial
Allows for SoftwareSerial
1 parent 4f1a24e commit 728a56b

9 files changed

+323
-53
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: 62 additions & 17 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+
27+
NoteSerial *
28+
make_note_serial (
29+
nullptr_t
30+
) {
31+
NoteSerial* & note_serial = instance::note_serial();
32+
if (note_serial) {
33+
delete note_serial;
34+
note_serial = nullptr;
35+
}
36+
return note_serial;
37+
}
38+
39+
template <typename T>
340
NoteSerial *
441
make_note_serial (
5-
NoteSerial::param_t serial_parameters_
42+
T & serial_parameters_
643
)
744
{
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);
45+
NoteSerial* & note_serial = instance::note_serial();
46+
if (!note_serial) {
47+
using serial_type = typename ExtractNestedTemplateType<T>::type;
48+
note_serial = new NoteSerial_Arduino<serial_type>(serial_parameters_.hw_serial, serial_parameters_.baud_rate);
1749
}
1850
return note_serial;
1951
}
2052

21-
NoteSerial_Arduino::NoteSerial_Arduino
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/Notecard.h

Lines changed: 7 additions & 2 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));

0 commit comments

Comments
 (0)