Skip to content

Commit e6a1e13

Browse files
committed
add generic live stream implementation
1 parent 53409f4 commit e6a1e13

File tree

13 files changed

+70
-61
lines changed

13 files changed

+70
-61
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.vscode/c_cpp_properties.json
44
.vscode/launch.json
55
.vscode/ipch
6+
examples/*/.vscode
7+
examples/*/.gitignore

src/BlenderServoAnimation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "animation/Animation.h"
22
#include "servo/Servo.h"
33
#include "show/Show.h"
4+
#include "live/Live.h"
45
#include <Arduino.h>

src/animation/Animation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ void Animation::handleStopMode() {
111111
}
112112

113113
void Animation::handleLiveMode() {
114-
while (this->serial->available() > 0) {
115-
this->command.read(this->serial);
114+
while (this->liveStream->available() > 0) {
115+
this->command.read(this->liveStream);
116116

117117
if (!this->command.isComplete() || !this->command.isValid()) {
118118
continue;
@@ -168,12 +168,12 @@ void Animation::stop(byte stepDelay) {
168168
this->changeMode(MODE_STOP);
169169
}
170170

171-
void Animation::live(Stream &serial) {
171+
void Animation::live(Stream &liveStream) {
172172
if (this->mode != MODE_DEFAULT) {
173173
return;
174174
}
175175

176-
this->serial = &serial;
176+
this->liveStream = &liveStream;
177177
this->changeMode(MODE_LIVE);
178178
}
179179

src/animation/Animation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Animation {
2929
unsigned long lastMicros;
3030

3131
Servo *servos[MAX_SERVO_COUNT] = {};
32-
Stream *serial;
32+
Stream *liveStream;
3333
Command command;
3434
mcb modeCallback = nullptr;
3535

@@ -57,7 +57,7 @@ class Animation {
5757
void pause();
5858
void loop(unsigned long currentMicros = micros());
5959
void stop(byte stepDelay = 20);
60-
void live(Stream &serial);
60+
void live(Stream &liveStream);
6161

6262
byte getFPS();
6363
byte getMode();

src/command/Command.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
using namespace BlenderServoAnimation;
55

6-
void Command::read(Stream *serial) {
7-
byte receivedByte = serial->read();
6+
void Command::read(Stream *liveStream) {
7+
byte receivedByte = liveStream->read();
88

99
if (!this->receiving && receivedByte != START_MARKER) {
1010
return;

src/command/Command.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Command {
2525
bool complete = false;
2626

2727
public:
28-
void read(Stream *serial);
28+
void read(Stream *liveStream);
2929

3030
bool isValid();
3131
bool isComplete();

src/live/Live.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "Live.h"
2+
#include <Arduino.h>
3+
4+
using namespace BlenderServoAnimation;
5+
6+
int Live::available() {
7+
return this->readIndex != this->writeIndex;
8+
}
9+
10+
int Live::peek() {
11+
return this->buffer[this->readIndex];
12+
}
13+
14+
int Live::read() {
15+
int value = this->buffer[this->readIndex++];
16+
17+
if (this->readIndex >= BUFFER_SIZE) {
18+
this->readIndex = 0;
19+
}
20+
21+
return value;
22+
}
23+
24+
size_t Live::write(uint8_t value) {
25+
this->buffer[this->writeIndex++] = value;
26+
27+
if (this->writeIndex >= BUFFER_SIZE) {
28+
this->writeIndex = 0;
29+
}
30+
31+
return 1;
32+
}
33+
34+
void Live::flush() {
35+
for (int i = 0; i < BUFFER_SIZE; i++) {
36+
this->buffer[i] = 0;
37+
}
38+
39+
this->readIndex = 0;
40+
this->writeIndex = 0;
41+
}

test/SerialMock.h renamed to src/live/Live.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#include <Arduino.h>
22

3-
#ifndef BlenderServoAnimation_Serial_Mock_H
4-
#define BlenderServoAnimation_Serial_Mock_H
3+
#ifndef BlenderServoAnimation_Live_H
4+
#define BlenderServoAnimation_Live_H
55

6-
class SerialMock : public Stream {
6+
namespace BlenderServoAnimation {
7+
8+
class Live : public Stream {
79
private:
810
static const byte BUFFER_SIZE = 20;
911

1012
byte buffer[BUFFER_SIZE];
11-
byte readIndex = 0;
1213
byte writeIndex = 0;
14+
byte readIndex = 0;
1315

1416
public:
1517
int available();
@@ -23,4 +25,6 @@ class SerialMock : public Stream {
2325
void flush();
2426
};
2527

28+
} // namespace BlenderServoAnimation
29+
2630
#endif

src/show/Show.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void Show::stop(byte stepDelay) {
113113
this->changeMode(MODE_STOP);
114114
}
115115

116-
void Show::live(Stream &serial) {
116+
void Show::live(Stream &liveStream) {
117117
if (!this->hasAnimations() || this->mode != MODE_DEFAULT) {
118118
return;
119119
}
@@ -122,7 +122,7 @@ void Show::live(Stream &serial) {
122122
this->animation = this->animations[this->playIndex];
123123
}
124124

125-
this->animation->live(serial);
125+
this->animation->live(liveStream);
126126
this->changeMode(MODE_LIVE);
127127
}
128128

src/show/Show.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Show {
5252
void loop(unsigned long currentMicros = micros());
5353
void pause();
5454
void stop(byte stepDelay = 20);
55-
void live(Stream &serial);
55+
void live(Stream &liveStream);
5656
void reset();
5757

5858
bool hasAnimations();

0 commit comments

Comments
 (0)