phyphox BLE
Plot your Arduino's data in the phyphox app and control the board from the phone
Loading...
Searching...
No Matches
Sink.h
Go to the documentation of this file.
1// Byte sinks the serializer writes into. The serializer never allocates and never assembles
2// the document: it emits bytes into one of these.
3#ifndef PHYPHOX_BLE_CORE_SINK_H
4#define PHYPHOX_BLE_CORE_SINK_H
5
6#include <stdint.h>
7#include <stddef.h>
8#include "Crc32.h"
9
10namespace phyphox {
11
14class Sink {
15public:
16 virtual ~Sink() {}
17 virtual void write(const uint8_t* data, size_t len) = 0;
18 void write(const char* text);
19 void write(char c) { uint8_t b = (uint8_t)c; write(&b, 1); }
20};
21
24class CountingSink : public Sink {
25public:
26 using Sink::write;
27 void write(const uint8_t* data, size_t len) override { count_ += len; crc_.update(data, len); }
28 size_t count() const { return count_; }
29 uint32_t crc() const { return crc_.value(); }
30 void reset() { count_ = 0; crc_.reset(); }
31private:
32 size_t count_ = 0;
33 Crc32 crc_;
34};
35
39class WindowSink : public Sink {
40public:
41 WindowSink(uint8_t* out, size_t capacity, size_t skip) : out_(out), cap_(capacity), skip_(skip) {}
42 using Sink::write;
43 void write(const uint8_t* data, size_t len) override;
44 size_t produced() const { return filled_; }
45 bool full() const { return filled_ >= cap_; }
46private:
47 uint8_t* out_;
48 size_t cap_;
49 size_t skip_;
50 size_t seen_ = 0;
51 size_t filled_ = 0;
52};
53
54} // namespace phyphox
55
56#endif
Counts bytes and accumulates the CRC-32 — the first pass at start(), which yields the transfer header...
Definition Sink.h:24
void write(const uint8_t *data, size_t len) override
Definition Sink.h:27
uint32_t crc() const
Definition Sink.h:29
size_t count() const
Definition Sink.h:28
void reset()
Definition Sink.h:30
Definition Crc32.h:12
void update(const uint8_t *data, size_t len)
void reset()
Definition Crc32.h:15
uint32_t value() const
The finished value (does not alter the running state).
Definition Crc32.h:19
Receives the bytes of a serialization.
Definition Sink.h:14
virtual ~Sink()
Definition Sink.h:16
void write(const char *text)
void write(char c)
Definition Sink.h:19
virtual void write(const uint8_t *data, size_t len)=0
Passes through only the bytes inside [skip, skip + capacity) of what is written to it,...
Definition Sink.h:39
void write(const uint8_t *data, size_t len) override
bool full() const
Definition Sink.h:45
size_t produced() const
Definition Sink.h:44
WindowSink(uint8_t *out, size_t capacity, size_t skip)
Definition Sink.h:41
Definition ChannelStore.h:11