Skip to content

Commit 143ef43

Browse files
authored
feat: NoteSerial Interface (#42)
1 parent 0f4ec37 commit 143ef43

13 files changed

+780
-140
lines changed

library.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ paragraph=Supports Serial and I2C for communication from a host MCU.
77
category=Communication
88
url=https://github.com/blues/note-arduino
99
architectures=*
10+
includes=Notecard.h

src/NoteSerial.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef NOTE_SERIAL_HPP
2+
#define NOTE_SERIAL_HPP
3+
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
class NoteSerial {
8+
public:
9+
/**************************************************************************/
10+
/*!
11+
@brief Type used to abstract specific hardware implementation types.
12+
*/
13+
/**************************************************************************/
14+
typedef void * channel_t;
15+
16+
virtual ~NoteSerial(void) {}
17+
18+
/**************************************************************************/
19+
/*!
20+
@brief Determines if the Notecard Serial port has data available.
21+
@return The number of bytes available to read.
22+
*/
23+
/**************************************************************************/
24+
virtual size_t available(void) = 0;
25+
26+
/**************************************************************************/
27+
/*!
28+
@brief Read a byte from the Notecard Serial port.
29+
@return A single character byte.
30+
*/
31+
/**************************************************************************/
32+
virtual char receive(void) = 0;
33+
34+
/**************************************************************************/
35+
/*!
36+
@brief Resets the serial port.
37+
@return `true` if the Serial port is available.
38+
*/
39+
/**************************************************************************/
40+
virtual bool reset(void) = 0;
41+
42+
/**************************************************************************/
43+
/*!
44+
@brief Writes a message to the Notecard Serial port.
45+
@param buffer
46+
The bytes to write.
47+
@param size
48+
The number of bytes to write.
49+
@param flush
50+
Use `true` to flush to Serial.
51+
@return The number of bytes transmitted.
52+
*/
53+
/**************************************************************************/
54+
virtual size_t transmit(uint8_t * buffer, size_t size, bool flush) = 0;
55+
};
56+
57+
/******************************************************************************/
58+
/*!
59+
@brief Helper function to abstract, create and maintain a single instance
60+
of the NoteSerial interface implementation, as required by the underlying
61+
`note-c` library.
62+
@param[in] serial_channel Pointer to the hardware specific UART
63+
implementation.
64+
@param[in] baut_rate The operating baud rate for the UART.
65+
*/
66+
/******************************************************************************/
67+
NoteSerial * make_note_serial (
68+
NoteSerial::channel_t serial_channel,
69+
size_t baud_rate
70+
);
71+
72+
#endif // NOTE_SERIAL_HPP

src/NoteSerial_Arduino.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "NoteSerial_Arduino.hpp"
2+
3+
NoteSerial * make_note_serial (
4+
NoteSerial::channel_t serial_channel_,
5+
size_t baud_rate_
6+
) {
7+
static NoteSerial * note_serial = nullptr;
8+
if (note_serial) {
9+
delete note_serial;
10+
}
11+
note_serial = new NoteSerial_Arduino(*reinterpret_cast<HardwareSerial *>(serial_channel_), baud_rate_);
12+
return note_serial;
13+
}
14+
15+
NoteSerial_Arduino::NoteSerial_Arduino
16+
(
17+
HardwareSerial & hw_serial_,
18+
size_t baud_rate_
19+
) :
20+
_notecardSerial(hw_serial_),
21+
_notecardSerialSpeed(baud_rate_)
22+
{
23+
_notecardSerial.begin(_notecardSerialSpeed);
24+
}
25+
26+
size_t
27+
NoteSerial_Arduino::available (
28+
void
29+
) {
30+
return _notecardSerial.available();
31+
}
32+
33+
char
34+
NoteSerial_Arduino::receive (
35+
void
36+
) {
37+
return _notecardSerial.read();
38+
}
39+
40+
bool
41+
NoteSerial_Arduino::reset (
42+
void
43+
) {
44+
_notecardSerial.begin(_notecardSerialSpeed);
45+
46+
return true;
47+
}
48+
49+
size_t
50+
NoteSerial_Arduino::transmit (
51+
uint8_t *buffer,
52+
size_t size,
53+
bool flush
54+
) {
55+
size_t result;
56+
result = _notecardSerial.write(buffer, size);
57+
if (flush) {
58+
_notecardSerial.flush();
59+
}
60+
return result;
61+
}

src/NoteSerial_Arduino.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef NOTE_SERIAL_ARDUINO_HPP
2+
#define NOTE_SERIAL_ARDUINO_HPP
3+
4+
#include "NoteSerial.hpp"
5+
6+
#ifndef MOCK
7+
#include <Arduino.h>
8+
#else
9+
#include "mock/mock-arduino.hpp"
10+
#endif
11+
12+
class NoteSerial_Arduino final : public NoteSerial {
13+
public:
14+
NoteSerial_Arduino(HardwareSerial & hw_serial_, size_t baud_rate_);
15+
size_t available(void) override;
16+
char receive(void) override;
17+
bool reset(void) override;
18+
size_t transmit(uint8_t * buffer, size_t size, bool flush) override;
19+
20+
private:
21+
HardwareSerial & _notecardSerial;
22+
int _notecardSerialSpeed;
23+
};
24+
25+
#endif // NOTE_SERIAL_ARDUINO_HPP

src/Notecard.cpp

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
#include <stdio.h>
4343
#include <stdlib.h>
4444

45+
#include "NoteSerial.hpp"
46+
4547
TwoWire *Notecard::_i2cPort;
46-
HardwareSerial *Notecard::_notecardSerial;
47-
int Notecard::_notecardSerialSpeed;
4848
Stream *Notecard::_debugSerial;
4949
bool Notecard::_debugSerialInitialized;
5050

@@ -54,6 +54,46 @@ bool Notecard::_debugSerialInitialized;
5454
static const char *i2cerr = "i2c?";
5555
#endif
5656

57+
namespace {
58+
NoteSerial * noteSerial(nullptr);
59+
60+
bool noteSerialAvailable (void) {
61+
bool result;
62+
if (noteSerial) {
63+
result = noteSerial->available();
64+
} else {
65+
result = false;
66+
}
67+
return result;
68+
}
69+
70+
char noteSerialReceive (void) {
71+
char result;
72+
if (noteSerial) {
73+
result = noteSerial->receive();
74+
} else {
75+
result = '\0';
76+
}
77+
return result;
78+
}
79+
80+
bool noteSerialReset (void) {
81+
bool result;
82+
if (noteSerial) {
83+
result = noteSerial->reset();
84+
} else {
85+
result = false;
86+
}
87+
return result;
88+
}
89+
90+
void noteSerialTransmit (uint8_t *text_, size_t len_, bool flush_) {
91+
if (noteSerial) {
92+
noteSerial->transmit(text_, len_, flush_);
93+
}
94+
}
95+
}
96+
5797
// 2018-06 ST Microelectronics has a HAL bug that causes an infinite hang.
5898
// This code enables us to exercise that code path to test the state of the bug.
5999
int _readLengthAdjustment = 0;
@@ -102,12 +142,10 @@ void Notecard::begin(uint32_t i2caddress, uint32_t i2cmax, TwoWire &wirePort)
102142
void Notecard::begin(HardwareSerial &selectedSerialPort, int selectedSpeed)
103143
{
104144
NoteSetFnDefault(malloc, free, delay, millis);
105-
_notecardSerial = &selectedSerialPort;
106-
_notecardSerialSpeed = selectedSpeed;
145+
noteSerial = make_note_serial(&selectedSerialPort, selectedSpeed);
107146

108-
NoteSetFnSerial(Notecard::noteSerialReset, Notecard::noteSerialTransmit,
109-
Notecard::noteSerialAvailable, Notecard::noteSerialReceive);
110-
_notecardSerial->begin(_notecardSerialSpeed);
147+
NoteSetFnSerial(noteSerialReset, noteSerialTransmit,
148+
noteSerialAvailable, noteSerialReceive);
111149
}
112150

113151
/**************************************************************************/
@@ -290,62 +328,6 @@ size_t Notecard::debugSerialOutput(const char *message)
290328
return(_debugSerial->print(message));
291329
}
292330

293-
/**************************************************************************/
294-
/*!
295-
@brief Resets the serial port.
296-
@return `True` if the Serial port is available.
297-
*/
298-
/**************************************************************************/
299-
bool Notecard::noteSerialReset()
300-
{
301-
_notecardSerial->begin(_notecardSerialSpeed);
302-
303-
return (!!_notecardSerial);
304-
}
305-
306-
/**************************************************************************/
307-
/*!
308-
@brief Writes a message to the Notecard Serial port.
309-
@param text
310-
The text to write.
311-
@param len
312-
The number of bytes to write.
313-
@param flush
314-
`True` to flush to Serial.
315-
*/
316-
/**************************************************************************/
317-
void Notecard::noteSerialTransmit(uint8_t *text, size_t len, bool flush)
318-
{
319-
_notecardSerial->write(text, len);
320-
if (flush) {
321-
_notecardSerial->flush();
322-
}
323-
}
324-
325-
/**************************************************************************/
326-
/*!
327-
@brief Determines if the Notecard Serial port has data available.
328-
@return `True` if there are bytes available to read.
329-
*/
330-
/**************************************************************************/
331-
bool Notecard::noteSerialAvailable()
332-
{
333-
return (_notecardSerial->available() > 0);
334-
}
335-
336-
/**************************************************************************/
337-
/*!
338-
@brief Read a byte from the Notecard Serial port. guaranteed only ever to
339-
be called if there is data available.
340-
@return a single character byte.
341-
*/
342-
/**************************************************************************/
343-
char Notecard::noteSerialReceive()
344-
{
345-
return _notecardSerial->read();
346-
}
347-
348-
349331
/**************************************************************************/
350332
/*!
351333
@brief Resets the I2C port. Required by note-c.

src/Notecard.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stddef.h>
2828
#include <stdint.h>
29+
2930
#ifndef MOCK
3031
#include <Arduino.h>
3132
#include <HardwareSerial.h>
@@ -137,15 +138,9 @@ class Notecard
137138

138139
private:
139140
static TwoWire *_i2cPort;
140-
static HardwareSerial *_notecardSerial;
141-
static int _notecardSerialSpeed;
142141
static Stream *_debugSerial;
143142
static bool _debugSerialInitialized;
144143

145-
static bool noteSerialReset(void);
146-
static void noteSerialTransmit(uint8_t *text, size_t len, bool flush);
147-
static bool noteSerialAvailable(void);
148-
static char noteSerialReceive(void);
149144
static bool noteI2CReset(uint16_t DevAddress);
150145
static const char *noteI2CTransmit(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size);
151146
static const char *noteI2CReceive(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size, uint32_t *avail);

0 commit comments

Comments
 (0)