|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "Table.h" |
| 4 | +#include <WString.h> |
| 5 | + |
| 6 | + |
| 7 | +// This pair of classes defines an Observer pattern for bits and bytes. |
| 8 | +// This would allow us to create "devices" that respond in "real" time |
| 9 | +// to Arduino outputs, in the form of altering the Arduino inputs |
| 10 | +// |
| 11 | +// e.g. replying to a serial output with serial input |
| 12 | +class ObservableDataStream; |
| 13 | + |
| 14 | +// datastream observers handle deliveries of bits and bytes. |
| 15 | +// optionally, they can turn bit events into byte events with a given endianness |
| 16 | +class DataStreamObserver { |
| 17 | + private: |
| 18 | + unsigned int mBitPosition; // for building the byte (mask helper) |
| 19 | + unsigned char mBuildingByte; // for storing incoming bits |
| 20 | + bool mAutoBitPack; // whether to report the packed bits |
| 21 | + bool mBigEndian; // bit order for byte |
| 22 | + |
| 23 | + protected: |
| 24 | + // functions that are up to the implementer to provide. |
| 25 | + virtual void onBit(bool aBit) {} |
| 26 | + virtual void onByte(unsigned char aByte) {} |
| 27 | + virtual String observerName() const = 0; |
| 28 | + |
| 29 | + public: |
| 30 | + DataStreamObserver(bool autoBitPack, bool bigEndian) |
| 31 | + { |
| 32 | + mBitPosition = 0; |
| 33 | + mBuildingByte = 0x00; |
| 34 | + mAutoBitPack = autoBitPack; |
| 35 | + mBigEndian = bigEndian; |
| 36 | + } |
| 37 | + |
| 38 | + virtual ~DataStreamObserver() {} |
| 39 | + |
| 40 | + // entry point for byte-related handler |
| 41 | + void handleByte(unsigned char aByte) { |
| 42 | + onByte(aByte); |
| 43 | + } |
| 44 | + |
| 45 | + // entry poitn for bit-related handler |
| 46 | + void handleBit(bool aBit) { |
| 47 | + onBit(aBit); |
| 48 | + |
| 49 | + if (!mAutoBitPack) return; |
| 50 | + |
| 51 | + // build the next value |
| 52 | + int shift = mBigEndian ? 7 - mBitPosition : mBitPosition; |
| 53 | + unsigned char val = aBit ? 0x1 : 0x0; |
| 54 | + mBuildingByte |= (val << shift); |
| 55 | + |
| 56 | + // if we roll over after incrementing, the byte is ready to ship |
| 57 | + mBitPosition = (mBitPosition + 1) % 8; |
| 58 | + if (mBitPosition == 0) { |
| 59 | + handleByte(mBuildingByte); |
| 60 | + mBuildingByte = 0x00; |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + // inlined after ObservableDataStream definition to fake out the compiler |
| 65 | + bool attach(ObservableDataStream* source); |
| 66 | + bool detach(ObservableDataStream* source); |
| 67 | +}; |
| 68 | + |
| 69 | +// Inheritable interface for things that produce data, like pins or serial ports |
| 70 | +// this class allows others to subscribe for updates on these values and trigger actions |
| 71 | +// e.g. if you "turn on" a motor with one pin and expect to see a change in an analog pin |
| 72 | +class ObservableDataStream |
| 73 | +{ |
| 74 | + private: |
| 75 | + Table<String, DataStreamObserver*> mObservers; |
| 76 | + bool mAdvertisingBit; |
| 77 | + unsigned char mAdvertisingByte; |
| 78 | + |
| 79 | + protected: |
| 80 | + // to allow both member and non-member functions to be called, we need to trick the compiler |
| 81 | + // into getting the (this) of a static function. So the default is a work function signature |
| 82 | + // that takes a second optional argument. in this case, we use the argument. |
| 83 | + |
| 84 | + static void workAdvertiseBit(ObservableDataStream* that, String _, DataStreamObserver* val) { |
| 85 | + val->handleBit(that->mAdvertisingBit); |
| 86 | + } |
| 87 | + |
| 88 | + static void workAdvertiseByte(ObservableDataStream* that, String _, DataStreamObserver* val) { |
| 89 | + val->handleByte(that->mAdvertisingByte); |
| 90 | + } |
| 91 | + |
| 92 | + // advertise functions allow the data stream to publish to observers |
| 93 | + |
| 94 | + // update all observers with a byte value |
| 95 | + void advertiseByte(unsigned char aByte) { |
| 96 | + // save the value to a class variable. then use the static method with this instance |
| 97 | + mAdvertisingByte = aByte; |
| 98 | + mObservers.iterate(workAdvertiseByte, this); |
| 99 | + } |
| 100 | + |
| 101 | + // update all observers with a byte value |
| 102 | + // build up a byte |
| 103 | + // if requested, advertise the byte |
| 104 | + void advertiseBit(bool aBit) { |
| 105 | + // do the named thing first, of course |
| 106 | + mAdvertisingBit = aBit; |
| 107 | + mObservers.iterate(workAdvertiseBit, this); |
| 108 | + } |
| 109 | + |
| 110 | + public: |
| 111 | + ObservableDataStream() : mObservers() { |
| 112 | + mAdvertisingBit = false; // we'll re-init on demand though |
| 113 | + mAdvertisingByte = 0x07; // we'll re-init on demand though |
| 114 | + } |
| 115 | + |
| 116 | + virtual ~ObservableDataStream() {} |
| 117 | + |
| 118 | + bool addObserver(String name, DataStreamObserver* obs) { return mObservers.add(name, obs); } |
| 119 | + bool removeObserver(String name) { return mObservers.remove(name); } |
| 120 | +}; |
| 121 | + |
| 122 | +inline bool DataStreamObserver::attach(ObservableDataStream* source) { return source->addObserver(observerName(), this); } |
| 123 | + |
| 124 | +inline bool DataStreamObserver::detach(ObservableDataStream* source) { return source->removeObserver(observerName()); } |
0 commit comments