arduino-audio-tools
AudioLoRa.h
1 #include "AudioTools/CoreAudio/Buffers.h"
2 #include "LoRa.h"
3 
4 // define the default pins used by the transceiver module
5 #define ss 8
6 #define rst 12
7 #define dio0 14
8 
9 namespace audio_tools {
10 
28 struct AudioLoRaConfig : public AudioInfo {
29  int32_t spi_speed = 8E6;
30  int max_size = 200;
31  int frequency = 868E6; // (433E6, 868E6, 915E6)
32  int sync_word = 0xF3;
33  int tx_power = 20; // 2-20;
34  int spreading_factor = 12; // 6-12
35  int signal_bandwidth =
36  7.8E3; // 7.8E3, 10.4E3, 15.6E3, 20.8E3, 31.25E3, 41.7E3,
37  // 62.5E3, 125E3, 250E3, and 500E3.
38  int pin_ss = ss;
39  int pin_rst = rst;
40  int pin_dio0 = dio0;
41  bool process_audio_info = true;
42 };
43 
49 class AudioLoRa : public AudioStream {
50  public:
51  AudioLoRaConfig defaultConfig() {
52  AudioLoRaConfig rc;
53  return rc;
54  }
55 
56  void setAudioInfo(AudioInfo info) {
57  cfg.sample_rate = info.sample_rate;
58  cfg.channels = info.channels;
61  }
62 
63  bool begin(AudioLoRaConfig config) {
64  cfg = config;
66  return begin();
67  }
68 
69  bool begin() {
70  TRACEI();
71  buffer.resize(cfg.max_size);
72  LoRa.setSignalBandwidth(cfg.signal_bandwidth);
73  LoRa.setSpreadingFactor(cfg.spreading_factor);
74  LoRa.setTxPower(cfg.tx_power);
75  LoRa.setSPIFrequency(cfg.spi_speed);
76  LoRa.setPins(cfg.pin_ss, cfg.pin_rst, cfg.pin_dio0);
77  LoRa.setSyncWord(cfg.sync_word);
78  bool rc = LoRa.begin(cfg.frequency);
79  if (cfg.process_audio_info) {
80  writeAudioInfo();
81  }
82  return rc;
83  }
84 
85  void end() { LoRa.end(); }
86 
87  size_t readBytes(uint8_t* data, size_t len) {
88  TRACEI();
89  size_t packetSize = LoRa.parsePacket();
90  if (cfg.process_audio_info && packetSize == sizeof(AudioInfo)) {
91  readAudioInfo();
92  packetSize = LoRa.parsePacket();
93  }
94  int toRead = min(len, packetSize);
95  int read = LoRa.readBytes(data, toRead);
96  return read;
97  }
98 
99  int available() { return cfg.max_size; }
100 
101  int availableForWrite() { return cfg.max_size; }
102 
103  size_t write(const uint8_t* data, size_t len) {
104  TRACEI();
105  for (int j = 0; j < len; j++) {
106  buffer.write(data[j]);
107  if (buffer.isFull()) {
108  LoRa.beginPacket();
109  LoRa.write(buffer.data(), buffer.available());
110  LoRa.endPacket();
111  buffer.clear();
112  }
113  }
114  return len;
115  }
116 
117  protected:
118  AudioLoRaConfig cfg;
119  SingleBuffer<uint8_t> buffer;
120 
121  void readAudioInfo() {
122  AudioInfo tmp;
123  int read = LoRa.readBytes((uint8_t*)&tmp, sizeof(AudioInfo));
124  setAudioInfo(tmp);
125  }
126 
127  void writeAudioInfo() {
128  LoRa.beginPacket();
129  AudioInfo ai = audioInfo();
130  LoRa.write((uint8_t*)&ai, sizeof(ai));
131  LoRa.endPacket();
132  }
133 };
134 
135 } // namespace audio_tools
LoRa Audio Sending and Receiving.
Definition: AudioLoRa.h:49
void setAudioInfo(AudioInfo info)
Defines the input AudioInfo.
Definition: AudioLoRa.h:56
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition: BaseStream.h:113
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition: BaseStream.h:121
virtual AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: BaseStream.h:144
void clear()
same as reset
Definition: Buffers.h:134
T * data()
Provides address of actual data.
Definition: Buffers.h:252
bool write(T sample) override
write add an entry to the buffer
Definition: Buffers.h:194
int available() override
provides the number of entries that are available to read
Definition: Buffers.h:219
bool isFull() override
checks if the buffer is full
Definition: Buffers.h:226
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:872
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:59
LoRa Audio Configuration with default values maximised for speed.
Definition: AudioLoRa.h:28