Skip to content

Commit f8f8b54

Browse files
committed
add StreamTape class
1 parent 5b0811e commit f8f8b54

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

CHANGELOG.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99
### Added
10-
1110
- Minimal Wire mocks. Will not provide support for unit testing I2C communication yet, but will allow compilation of libraries that use I2C.
11+
- `StreamTape` class now bridges `Stream` and `HardwareSerial` to allow general-purpose stream mocking & history
1212

1313
### Changed
1414
- Arduino command failures (to read preferences) now causes a fatal error, with help for troubleshooting the underlying command
@@ -25,19 +25,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2525

2626
## [0.2.0] - 2019-02-20
2727
### Added
28-
* `release-new-version.sh` script
29-
* outputs for `PinHistory` can now report timestamps
30-
* Fibonacci Clock for clock testing purposes (internal to this library)
28+
- `release-new-version.sh` script
29+
- outputs for `PinHistory` can now report timestamps
30+
- Fibonacci Clock for clock testing purposes (internal to this library)
3131

3232
### Changed
33-
* Shortened `ArduinoQueue` push and pop operations
34-
* `ci/Queue.h` is now `MockEventQueue.h`, with timing data
35-
* `MockEventQueue::Node` now contains struct `MockEventQueue::Event`, which contains both the templated type `T` and a field for a timestamp.
36-
* Construction of `MockEventQueue` now includes a constructor argument for the time-fetching function
37-
* Construction of `PinHistory` now includes a constructor argument for the time-fetching function
38-
* `PinHistory` can now return an array of timestamps for its events
39-
* `GodmodeState` is now a singleton pattern, which is necessary to support the globality of Arduino functions
40-
* `GodmodeState` now uses timestamped PinHistory for Analog and Digital
33+
- Shortened `ArduinoQueue` push and pop operations
34+
- `ci/Queue.h` is now `MockEventQueue.h`, with timing data
35+
- `MockEventQueue::Node` now contains struct `MockEventQueue::Event`, which contains both the templated type `T` and a field for a timestamp.
36+
- Construction of `MockEventQueue` now includes a constructor argument for the time-fetching function
37+
- Construction of `PinHistory` now includes a constructor argument for the time-fetching function
38+
- `PinHistory` can now return an array of timestamps for its events
39+
- `GodmodeState` is now a singleton pattern, which is necessary to support the globality of Arduino functions
40+
- `GodmodeState` now uses timestamped PinHistory for Analog and Digital
4141

4242
### Fixed
4343
* `ArduinoQueue` no longer leaks memory

cpp/arduino/HardwareSerial.h

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
//#include <inttypes.h>
4-
#include "Stream.h"
4+
#include "ci/StreamTape.h"
55

66
// definitions neeeded for Serial.begin's config arg
77
#define SERIAL_5N1 0x00
@@ -29,38 +29,19 @@
2929
#define SERIAL_7O2 0x3C
3030
#define SERIAL_8O2 0x3E
3131

32-
class HardwareSerial : public Stream, public ObservableDataStream
32+
class HardwareSerial : public StreamTape
3333
{
34-
protected:
35-
String* mGodmodeDataOut;
36-
3734
public:
38-
HardwareSerial(String* dataIn, String* dataOut, unsigned long* delay): Stream(), ObservableDataStream() {
39-
mGodmodeDataIn = dataIn;
40-
mGodmodeDataOut = dataOut;
41-
mGodmodeMicrosDelay = delay;
42-
}
35+
HardwareSerial(String* dataIn, String* dataOut, unsigned long* delay): StreamTape(dataIn, dataOut, delay) {}
36+
4337
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
4438
void begin(unsigned long baud, uint8_t config) {
4539
*mGodmodeMicrosDelay = 1000000 / baud;
4640
}
4741
void end() {}
4842

49-
// virtual int available(void);
50-
// virtual int peek(void);
51-
// virtual int read(void);
52-
// virtual int availableForWrite(void);
53-
// virtual void flush(void);
54-
virtual size_t write(uint8_t aChar) {
55-
mGodmodeDataOut->append(String((char)aChar));
56-
advertiseByte((unsigned char)aChar);
57-
return 1;
58-
}
59-
60-
// https://stackoverflow.com/a/4271276
61-
using Print::write; // pull in write(str) and write(buf, size) from Print
43+
// support "if (Serial1) {}" sorts of things
6244
operator bool() { return true; }
63-
6445
};
6546

6647
#if defined(UBRRH) || defined(UBRR0H)

cpp/arduino/ci/StreamTape.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include "../Stream.h"
4+
5+
/**
6+
* Stream with godmode-controlled input and godmode-persisted output
7+
*/
8+
class StreamTape : public Stream, public ObservableDataStream
9+
{
10+
protected:
11+
String* mGodmodeDataOut;
12+
// mGodmodeDataIn is provided by Stream
13+
14+
public:
15+
StreamTape(String* dataIn, String* dataOut, unsigned long* delay): Stream(), ObservableDataStream() {
16+
mGodmodeDataIn = dataIn;
17+
mGodmodeDataOut = dataOut;
18+
mGodmodeMicrosDelay = delay;
19+
}
20+
21+
// virtual int available(void);
22+
// virtual int peek(void);
23+
// virtual int read(void);
24+
// virtual int availableForWrite(void);
25+
// virtual void flush(void);
26+
virtual size_t write(uint8_t aChar) {
27+
mGodmodeDataOut->append(String((char)aChar));
28+
advertiseByte((unsigned char)aChar);
29+
return 1;
30+
}
31+
32+
// https://stackoverflow.com/a/4271276
33+
using Print::write; // pull in write(str) and write(buf, size) from Print
34+
35+
};
36+

0 commit comments

Comments
 (0)