From cc8c27da2c582a25787413a088e6d3a4fa619bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominic=20H=C3=B6glinger?= Date: Mon, 22 Sep 2025 20:04:52 +0200 Subject: [PATCH] Radio: Start working on TactilityC ... it begins ... --- TactilityC/Include/tt_hal_device.h | 3 +- TactilityC/Include/tt_hal_radio.h | 44 +++++++++++++++++++ TactilityC/Source/tt_hal_device.cpp | 2 + TactilityC/Source/tt_hal_radio.cpp | 68 +++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 TactilityC/Include/tt_hal_radio.h create mode 100644 TactilityC/Source/tt_hal_radio.cpp diff --git a/TactilityC/Include/tt_hal_device.h b/TactilityC/Include/tt_hal_device.h index e08c167c..d71a236c 100644 --- a/TactilityC/Include/tt_hal_device.h +++ b/TactilityC/Include/tt_hal_device.h @@ -13,7 +13,8 @@ enum DeviceType { DEVICE_TYPE_SDCARD, DEVICE_TYPE_KEYBOARD, DEVICE_TYPE_POWER, - DEVICE_TYPE_GPS + DEVICE_TYPE_GPS, + DEVICE_TYPE_RADIO }; typedef uint32_t DeviceId; diff --git a/TactilityC/Include/tt_hal_radio.h b/TactilityC/Include/tt_hal_radio.h new file mode 100644 index 00000000..805a33a3 --- /dev/null +++ b/TactilityC/Include/tt_hal_radio.h @@ -0,0 +1,44 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* RadioHandle; + +enum Modulation { + MODULATION_LORA, + MODULATION_FSK, + MODULATION_LRFHSS +}; + +/** + * Allocate a radio driver object for the specified radioId. + * @param[in] radioId the identifier of the radio device + * @return the radio handle + */ +RadioHandle tt_hal_radio_alloc(DeviceId radioId); + +/** + * Free the memory for the radio driver object. + * @param[in] handle the radio driver handle + */ +void tt_hal_radio_free(RadioHandle handle); + +/** + * Set the modulation for the radio driver object. + * @param[in] modulation the modulation type + * @param[in] handle the radio driver handle + */ +void tt_hal_radio_set_modulation(RadioHandle handle, Modulation modulation); + +/** + * Get the modulation for the radio driver object. + * @param[in] handle the radio driver handle + * @return the modulation type + */ +Modulation tt_hal_radio_get_modulation(RadioHandle handle); + +#ifdef __cplusplus +} +#endif diff --git a/TactilityC/Source/tt_hal_device.cpp b/TactilityC/Source/tt_hal_device.cpp index dbd91730..3d45fb55 100644 --- a/TactilityC/Source/tt_hal_device.cpp +++ b/TactilityC/Source/tt_hal_device.cpp @@ -20,6 +20,8 @@ static tt::hal::Device::Type toTactilityDeviceType(DeviceType type) { return tt::hal::Device::Type::Power; case DEVICE_TYPE_GPS: return tt::hal::Device::Type::Gps; + case DEVICE_TYPE_RADIO: + return tt::hal::Device::Type::Radio; default: tt_crash("Device::Type not supported"); } diff --git a/TactilityC/Source/tt_hal_radio.cpp b/TactilityC/Source/tt_hal_radio.cpp new file mode 100644 index 00000000..687b5e9a --- /dev/null +++ b/TactilityC/Source/tt_hal_radio.cpp @@ -0,0 +1,68 @@ +#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 device; + DeviceWrapper(std::shared_ptr device) : device(device) {} +}; + +static std::shared_ptr 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(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(handle); + delete wrapper; + } + + void tt_hal_radio_set_modulation(RadioHandle handle, Modulation modulation) { + auto wrapper = static_cast(handle); + wrapper->device->setModulation(toCpp(modulation)); + } + + Modulation tt_hal_radio_get_modulation(RadioHandle handle) { + auto wrapper = static_cast(handle); + return fromCpp(wrapper->device->getModulation()); + } +}