69 lines
2.3 KiB
C++
69 lines
2.3 KiB
C++
#include "tt_hal_radio.h"
|
|
|
|
#include "Tactility/Check.h"
|
|
#include "Tactility/hal/Device.h"
|
|
#include "Tactility/hal/display/DisplayDevice.h"
|
|
#include "Tactility/hal/display/DisplayDriver.h"
|
|
|
|
static Modulation fromCpp(tt::hal::radio::RadioDevice::Modulation modulation) {
|
|
switch (modulation) {
|
|
case tt::hal::radio::RadioDevice::Modulation::LoRa:
|
|
return MODULATION_LORA;
|
|
case tt::hal::radio::RadioDevice::Modulation::Fsk:
|
|
return MODULATION_FSK;
|
|
case tt::hal::radio::RadioDevice::Modulation::LrFhss:
|
|
return MODULATION_LRFHSS;
|
|
default:
|
|
tt_crash("Modulation not supported");
|
|
}
|
|
}
|
|
|
|
static tt::hal::radio::RadioDevice::Modulation modulation toCpp(Modulation modulation) {
|
|
switch (modulation) {
|
|
case MODULATION_LORA:
|
|
return tt::hal::radio::RadioDevice::Modulation::LoRa;
|
|
case MODULATION_FSK:
|
|
return tt::hal::radio::RadioDevice::Modulation::Fsk;
|
|
case MODULATION_LRFHSS:
|
|
return tt::hal::radio::RadioDevice::Modulation::LrFhss;
|
|
default:
|
|
tt_crash("Modulation not supported");
|
|
}
|
|
}
|
|
|
|
struct DeviceWrapper {
|
|
std::shared_ptr<tt::hal::radio::RadioDevice> device;
|
|
DeviceWrapper(std::shared_ptr<tt::hal::radio::RadioDevice> device) : device(device) {}
|
|
};
|
|
|
|
static std::shared_ptr<tt::hal::radio::RadioDevice> findValidRadioDevice(tt::hal::Device::Id id) {
|
|
auto device = tt::hal::findDevice(id);
|
|
if (device == nullptr || device->getType() != tt::hal::Device::Type::Radio) {
|
|
return nullptr;
|
|
}
|
|
return std::reinterpret_pointer_cast<tt::hal::radio::RadioDevice>(device);
|
|
}
|
|
|
|
extern "C" {
|
|
|
|
RadioHandle tt_hal_radio_alloc(DeviceId radioId) {
|
|
auto radio = findValidRadioDevice(id);
|
|
return new DeviceWrapper(radio);
|
|
}
|
|
|
|
void tt_hal_radio_free(RadioHandle handle) {
|
|
auto wrapper = static_cast<DeviceWrapper*>(handle);
|
|
delete wrapper;
|
|
}
|
|
|
|
void tt_hal_radio_set_modulation(RadioHandle handle, Modulation modulation) {
|
|
auto wrapper = static_cast<DeviceWrapper*>(handle);
|
|
wrapper->device->setModulation(toCpp(modulation));
|
|
}
|
|
|
|
Modulation tt_hal_radio_get_modulation(RadioHandle handle) {
|
|
auto wrapper = static_cast<DeviceWrapper*>(handle);
|
|
return fromCpp(wrapper->device->getModulation());
|
|
}
|
|
}
|