arduino-audio-tools
RadioHeadStream.h
1 #pragma once
2 #include "AudioTools/CoreAudio/BaseStream.h"
3 #include "RHGenericDriver.h"
4 
5 namespace audio_tools {
6 
7 
15 class ReadioHeadStream : public Stream {
16  public:
17  ReadioHeadStream(RHGenericDriver &driver) { setDriver(driver); }
18 
19  void setDriver(RHGenericDriver &driver) { p_driver = &driver; }
20 
21  bool begin() {
22  if (p_driver == nullptr) return false;
23  p_driver->setMode(mode == RX_MODE ? RHGenericDriver::RHMode::RHModeRx
24  : RHGenericDriver::RHMode::RHModeTx);
25  return p_driver->init();
26  }
27 
28  void end() { p_driver->setMode(RHGenericDriver::RHMode::RHModeSleep); }
29 
30  int available() override {
31  if (mode == TX_MODE) return 0;
32  return p_driver->available() ? p_driver->maxMessageLength() : 0;
33  }
34 
35  size_t readBytes(uint8_t *data, size_t len) override {
36  if (mode == TX_MODE) return 0;
37  int open = len;
38  int processed = 0;
39  while (open > 0) {
40  uint8_t av = available();
41  if (av == 0) break;
42  p_driver->recv(data + processed, &av);
43  open -= av;
44  processed += av;
45  }
46  return processed;
47  }
48 
49  int availableForWrite() override {
50  if (mode == RX_MODE) return 0;
51  return p_driver->maxMessageLength();
52  }
53 
54  size_t write(const uint8_t *data, size_t len) override {
55  if (mode == RX_MODE) return 0;
56  int open = len;
57  int processed = 0;
58  while (open > 0) {
59  int av = available();
60  if (av == 0) break;
61  p_driver->send(data + processed, av);
62  open -= av;
63  processed += av;
64  }
65  return processed;
66  }
67 
68  protected:
69  RHGenericDriver *p_driver = nullptr;
70  RxTxMode mode;
71 };
72 
73 } // namespace audio_tools
Arduino Stream which is using the RadioHead library to send and receive data. We use the river API di...
Definition: RadioHeadStream.h:15
Definition: NoArduino.h:125
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823