Skip to content

Commit

Permalink
DRAFT RF modules
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Jan 16, 2024
1 parent 831e8fc commit b2ff517
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/AudioConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,6 @@ using WiFiServerSecure = BearSSL::WiFiServerSecure;
#define USE_ANALOG_ARDUINO
#define USE_INT24_FROM_INT

#define ANALOG_BUFFER_SIZE 1024
#define ANALOG_BUFFERS 20
#define PIN_ANALOG_START PA0
#define PIN_PWM_START PA0
#define PWM_DEFAULT_TIMER TIM2
Expand Down
102 changes: 102 additions & 0 deletions src/Communication/LT8920Stream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#pragma once
#include "AudioTools/AudioStreams.h"
#include "LT8920.h"
#include <SPI.h>

namespace audio_tools {

class LT8920Config {
public:
RxTxMode mode;
uint8_t rst_pin = -1;
uint8_t cs_pin = -1;
uint8_t pkt_pin = -1;
// LT8920_1MBPS, LT8920_250KBPS,LT8920_125KBPS,LT8920_62KBPS
LT8920::DataRate rate = LT8920::DataRate::LT8920_1MBPS;
uint8_t channel = 0x20; // 0 - 128 (0x80)
uint16_t default_size = 255 * 4;
uint8_t power = 0; // 0-0xf
uint8_t gain = 0; // 0-0xf
int send_fail_delay_ms = 10;
};

/**
* @brief A communications class which uses the LT8920 2.4G RF Transceiver
* and implements the Arduino Stream API. It depends on the
* https://github.com/mengguang/LT8920 library.
* @ingroup communications
* @author Phil Schatzmann
* @copyright GPLv3
*/

class LT8920Stream : public AudioStream {
public:
bool begin(LT8920Config cfg) {
this->config = cfg;
if (cfg.rst_pin==-1 || cfg.cs_pin==-1 || cfg.pkt_pin==-1){
LOGE("Pins not defined");
return false;
}
if (p_lt == nullptr) {
p_lt = new LT8920(cfg.cs_pin, cfg.pkt_pin, cfg.rst_pin);
}
p_lt->begin();
p_lt->setCurrentControl(cfg.power, cfg.gain);
p_lt->setDataRate(cfg.rate);
p_lt->setChannel(cfg.channel);
switch (cfg.mode) {
case RX_MODE:
p_lt->startListening();
break;
case TX_MODE:
break;
default:
LOGE("Mode not supported");
break;
}
return true;
}

void end() { p_lt->sleep(); }

int availableForWrite() { return config.default_size; }

int available() { return p_lt->available() ? config.default_size : 0; }

size_t write(const uint8_t *data, size_t len) {
int open = len;
int processed = 0;
while (open > 0) {
int len = min(open, max_size);
while (!p_lt->sendPacket((uint8_t *)(data + processed), len))
delay(config.send_fail_delay_ms);
open -= len;
processed += len;
}
return processed;
}

size_t readBytes(uint8_t *data, size_t len) {
int open = len;
int processed = 0;
while (open > 0) {
if (!available())
break;
int len = min(open, max_size);
int packetSize = p_lt->read(data + processed, len);
open -= packetSize;
processed += packetSize;
p_lt->startListening();
}
return processed;
}

LT8920 &lt8920() { return *p_lt; }

protected:
LT8920 *p_lt = nullptr;
LT8920Config config;
const int max_size = 255;
};

} // namespace audio_tools
73 changes: 73 additions & 0 deletions src/Communication/LoRaStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once
#include "AudioTools/AudioStreams.h"
#include <LoRa.h>
#include <SPI.h>

namespace audio_tools {

class LoRaConfig {
public:
RxTxMode mode;
long frequency = 868E6; // 433E6, 868E6, 915E6
int tx_power = 20; // max power
long signal_bandwidth = 500E3; // Supported values
// are 7.8E3, 10.4E3, 15.6E3, 20.8E3, 31.25E3, 41.7E3,
// 62.5E3, 125E3, 250E3, and 500E3.
int spreading_factor = 7; // Supported values are between 6 and 12
bool async = false;
};

/**
* @brief A communications class which uses the Semtech LoRa modules and
* implements the Arduino Stream API. It depends on the
* https://github.com/sandeepmistry/arduino-LoRa library.
* @ingroup communications
* @author Phil Schatzmann
* @copyright GPLv3
*/

class LoRaStream : public AudioStream {
public:
LoRaConfig defaultConfig(RxTxMode mode) {
LoRaConfig cfg;
cfg.mode = mode;
return cfg;
}

bool begin(LoRaConfig cfg) {
this->config = cfg;
bool result = LoRa.begin(cfg.frequency);
LoRa.setSignalBandwidth(cfg.signal_bandwidth);
if (config.mode == TX_MODE) {
LoRa.setTxPower(cfg.tx_power);
}
return result;
}

void end() { LoRa.end(); }

int available() { return LoRa.available(); }

int availableForWrite() { return LoRa.availableForWrite(); }

size_t write(const uint8_t *data, size_t len) {
LoRa.beginPacket();
size_t result = LoRa.write(data, len);
LoRa.endPacket(config.async);
return result;
}

size_t readBytes(uint8_t *data, size_t len) {
int packetSize = LoRa.parsePacket();
if (packetSize == 0)
return 0;
return LoRa.readBytes(data, len);
}

LoRaClass &lora() { return LoRa; }

protected:
LoRaConfig config;
};

} // namespace audio_tools
Loading

0 comments on commit b2ff517

Please sign in to comment.