Skip to content

Commit 6748175

Browse files
SerialStreamer has been moved to codal-core.
And the version in codal-core includes a bug fix required in the audio-refactor branch.
1 parent 8f63538 commit 6748175

File tree

3 files changed

+7
-185
lines changed

3 files changed

+7
-185
lines changed

source/samples/SerialStreamer.cpp

Lines changed: 6 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -21,126 +21,15 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2121
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2222
DEALINGS IN THE SOFTWARE.
2323
*/
24-
24+
#include "MicroBit.h"
2525
#include "SerialStreamer.h"
2626
#include "Tests.h"
2727

28-
/**
29-
* Creates a simple component that logs a stream of signed 16 bit data as signed 8-bit data over serial.
30-
* @param source a DataSource to measure the level of.
31-
* @param mode the format of the serialised data. Valid options are SERIAL_STREAM_MODE_BINARY (default), SERIAL_STREAM_MODE_DECIMAL, SERIAL_STREAM_MODE_HEX.
32-
*/
33-
SerialStreamer::SerialStreamer(DataSource &source, int mode) : upstream(source)
34-
{
35-
this->mode = mode;
36-
37-
// Register with our upstream component
38-
source.connect(*this);
39-
}
40-
41-
/**
42-
* Callback provided when data is ready.
43-
*/
44-
int SerialStreamer::pullRequest()
45-
{
46-
static volatile int pr = 0;
47-
48-
if(!pr)
49-
{
50-
pr++;
51-
while(pr)
52-
{
53-
lastBuffer = upstream.pull();
54-
streamBuffer(lastBuffer);
55-
pr--;
56-
}
57-
}
58-
else
59-
{
60-
pr++;
61-
}
62-
63-
return DEVICE_OK;
64-
}
65-
66-
/**
67-
* returns the last buffer processed by this component
68-
*/
69-
ManagedBuffer SerialStreamer::getLastBuffer()
70-
{
71-
return lastBuffer;
72-
}
73-
74-
/**
75-
* Callback provided when data is ready.
76-
*/
77-
void SerialStreamer::streamBuffer(ManagedBuffer buffer)
78-
{
79-
int CRLF = 0;
80-
int bps = upstream.getFormat();
81-
82-
// If a BINARY mode is requested, simply output all the bytes to the serial port.
83-
if( mode == SERIAL_STREAM_MODE_BINARY )
84-
{
85-
uint8_t *p = &buffer[0];
86-
uint8_t *end = p + buffer.length();
87-
88-
while(p < end)
89-
uBit.serial.putc(*p++);
90-
}
91-
92-
// if a HEX mode is requested, format using printf, framed by sample size..
93-
if( mode == SERIAL_STREAM_MODE_HEX || mode == SERIAL_STREAM_MODE_DECIMAL )
94-
{
95-
uint8_t *d = &buffer[0];
96-
uint8_t *end = d+buffer.length();
97-
uint32_t data;
98-
99-
while(d < end)
100-
{
101-
data = *d++;
102-
103-
if (bps > DATASTREAM_FORMAT_8BIT_SIGNED)
104-
data |= (*d++) << 8;
105-
if (bps > DATASTREAM_FORMAT_16BIT_SIGNED)
106-
data |= (*d++) << 16;
107-
if (bps > DATASTREAM_FORMAT_24BIT_SIGNED)
108-
data |= (*d++) << 24;
109-
110-
if (mode == SERIAL_STREAM_MODE_HEX) {
111-
uBit.serial.printf("%x ", data);
112-
} else {
113-
// SERIAL_STREAM_MODE_DECIMAL
114-
if (bps == DATASTREAM_FORMAT_8BIT_SIGNED) {
115-
uBit.serial.printf("%d ", (int8_t)(data & 0xFF));
116-
} else if (bps == DATASTREAM_FORMAT_16BIT_SIGNED) {
117-
uBit.serial.printf("%d ", (int16_t)(data & 0xFFFF));
118-
} else if (bps == DATASTREAM_FORMAT_24BIT_SIGNED) {
119-
// Move the sign bit to the most significant bit
120-
int32_t signed_data = data & 0x7FFFFF;
121-
if (data & (1 << 23)) {
122-
signed_data |= 1 << 31;
123-
}
124-
uBit.serial.printf("%d ", signed_data);
125-
} else {
126-
// Cannot print uint32_t correctly as serial.printf
127-
// does not support "%u"
128-
uBit.serial.printf("%d ", data);
129-
}
130-
}
131-
132-
CRLF++;
133-
134-
if (CRLF >= 16){
135-
uBit.serial.putc('\r');
136-
uBit.serial.putc('\n');
137-
CRLF = 0;
138-
}
139-
}
28+
void streamer_serial_test() {
29+
static SplitterChannel *splitterChannel = uBit.audio.splitter->createChannel();
30+
SerialStreamer *streamer = new SerialStreamer(*splitterChannel, SERIAL_STREAM_MODE_DECIMAL);
14031

141-
if (CRLF > 0) {
142-
uBit.serial.putc( '\r' );
143-
uBit.serial.putc( '\n' );
144-
}
32+
while (true) {
33+
uBit.sleep(1000);
14534
}
14635
}

source/samples/SerialStreamer.h

Lines changed: 0 additions & 68 deletions
This file was deleted.

source/samples/Tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,6 @@ void stream_test_getValue_interval();
105105
void stream_test_record();
106106
void stream_test_recording_sample_rates();
107107
void stream_test_all();
108+
void streamer_serial_test();
108109

109110
#endif

0 commit comments

Comments
 (0)