Create hal-device module, fix GPIO, fix tests, fix TactilityC (#471)
* **New Features** * Added a HAL device module providing device enumeration, type queries and a HAL↔kernel bridge. * **Improvements** * Integrated HAL module into startup; standardized module names, includes and lifecycle logging; safer file append behavior; broader build support. * **API Changes** * Driver lifecycle now uses explicit add/remove semantics; C and HAL device type/lookup APIs clarified. * **Chores** * Added module README and Apache‑2.0 license; updated build configuration to include the new module. * **Fixes** * Updated tests and object file handling to behave correctly.
This commit is contained in:
committed by
GitHub
parent
5993ceb232
commit
3fe1dc0312
@@ -8,6 +8,7 @@ if (DEFINED ENV{ESP_IDF_VERSION})
|
||||
PlatformEsp32
|
||||
TactilityCore
|
||||
TactilityFreeRtos
|
||||
hal-device
|
||||
lvgl
|
||||
driver
|
||||
elf_loader
|
||||
@@ -71,6 +72,7 @@ else()
|
||||
TactilityCore
|
||||
TactilityKernel
|
||||
PlatformPosix
|
||||
hal-device
|
||||
freertos_kernel
|
||||
lvgl
|
||||
lv_screenshot
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
namespace tt::hal {
|
||||
/** Base class for HAL-related devices. */
|
||||
class Device {
|
||||
|
||||
public:
|
||||
|
||||
enum class Type {
|
||||
I2c,
|
||||
Display,
|
||||
Touch,
|
||||
SdCard,
|
||||
Keyboard,
|
||||
Encoder,
|
||||
Power,
|
||||
Gps,
|
||||
Other
|
||||
};
|
||||
|
||||
typedef uint32_t Id;
|
||||
|
||||
private:
|
||||
|
||||
Id id;
|
||||
|
||||
public:
|
||||
|
||||
Device();
|
||||
virtual ~Device() = default;
|
||||
|
||||
/** Unique identifier */
|
||||
Id getId() const { return id; }
|
||||
|
||||
/** The type of device */
|
||||
virtual Type getType() const = 0;
|
||||
|
||||
/** The part number or hardware name e.g. TdeckTouch, TdeckDisplay, BQ24295, etc. */
|
||||
virtual std::string getName() const = 0;
|
||||
|
||||
/** A short description of what this device does.
|
||||
* e.g. "USB charging controller with I2C interface."
|
||||
*/
|
||||
virtual std::string getDescription() const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a device to the registry.
|
||||
* @warning This will leak memory if you want to destroy a device and don't call deregisterDevice()!
|
||||
*/
|
||||
void registerDevice(const std::shared_ptr<Device>& device);
|
||||
|
||||
/** Remove a device from the registry. */
|
||||
void deregisterDevice(const std::shared_ptr<Device>& device);
|
||||
|
||||
/** Find a single device with a custom filter */
|
||||
std::shared_ptr<Device> _Nullable findDevice(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction);
|
||||
|
||||
/** Find devices with a custom filter */
|
||||
std::vector<std::shared_ptr<Device>> findDevices(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction);
|
||||
|
||||
/** Find a device in the registry by its name. */
|
||||
std::shared_ptr<Device> _Nullable findDevice(std::string name);
|
||||
|
||||
/** Find a device in the registry by its identifier. */
|
||||
std::shared_ptr<Device> _Nullable findDevice(Device::Id id);
|
||||
|
||||
/** Find 0, 1 or more devices in the registry by type. */
|
||||
std::vector<std::shared_ptr<Device>> findDevices(Device::Type type);
|
||||
|
||||
/** Get a copy of the entire device registry in its current state. */
|
||||
std::vector<std::shared_ptr<Device>> getDevices();
|
||||
|
||||
/** Find devices of a certain type and cast them to the specified class */
|
||||
template<class DeviceType>
|
||||
std::vector<std::shared_ptr<DeviceType>> findDevices(Device::Type type) {
|
||||
auto devices = findDevices(type);
|
||||
if (devices.empty()) {
|
||||
return {};
|
||||
} else {
|
||||
std::vector<std::shared_ptr<DeviceType>> result;
|
||||
result.reserve(devices.size());
|
||||
for (auto& device : devices) {
|
||||
auto target_device = std::static_pointer_cast<DeviceType>(device);
|
||||
assert(target_device != nullptr);
|
||||
result.push_back(target_device);
|
||||
}
|
||||
return std::move(result);
|
||||
}
|
||||
}
|
||||
|
||||
template<class DeviceType>
|
||||
void findDevices(Device::Type type, std::function<bool(const std::shared_ptr<DeviceType>&)> onDeviceFound) {
|
||||
auto devices_view = findDevices(type);
|
||||
for (auto& device : devices_view) {
|
||||
auto typed_device = std::static_pointer_cast<DeviceType>(device);
|
||||
if (!onDeviceFound(typed_device)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Find the first device of the specified type and cast it to the specified class */
|
||||
template<class DeviceType>
|
||||
std::shared_ptr<DeviceType> findFirstDevice(Device::Type type) {
|
||||
auto devices = findDevices(type);
|
||||
if (devices.empty()) {
|
||||
return {};
|
||||
} else {
|
||||
auto& first = devices[0];
|
||||
return std::static_pointer_cast<DeviceType>(first);
|
||||
}
|
||||
}
|
||||
|
||||
/** @return true if there are 1 or more devices of the specified type */
|
||||
bool hasDevice(Device::Type type);
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
#include "GpsConfiguration.h"
|
||||
#include "Satellites.h"
|
||||
|
||||
@@ -110,7 +110,7 @@ public:
|
||||
return lastRmcSubscriptionId;
|
||||
}
|
||||
|
||||
void unsubscribeRmc(GgaSubscriptionId subscriptionId) {
|
||||
void unsubscribeRmc(RmcSubscriptionId subscriptionId) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
std::erase_if(rmcSubscriptions, [subscriptionId](auto& subscription) { return subscription.id == subscriptionId; });
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
#include "I2c.h"
|
||||
|
||||
namespace tt::hal::i2c {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <cstdint>
|
||||
|
||||
namespace tt::hal::power {
|
||||
@@ -44,4 +44,4 @@ public:
|
||||
virtual void powerOff() { /* NO-OP*/ }
|
||||
};
|
||||
|
||||
} // namespace tt
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
|
||||
#include <Tactility/TactilityCore.h>
|
||||
#include <Tactility/Lock.h>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "SdCardDevice.h"
|
||||
|
||||
#include <Tactility/RecursiveMutex.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/hal/spi/Spi.h>
|
||||
#include <sd_protocol_types.h>
|
||||
#include <soc/gpio_num.h>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
#include "TouchDriver.h"
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "Tactility/MountPoints.h"
|
||||
|
||||
#include "Tactility/TactilityConfig.h"
|
||||
#include "Tactility/hal/Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
#include "Tactility/hal/sdcard/SdCardDevice.h"
|
||||
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <Tactility/settings/TimePrivate.h>
|
||||
|
||||
#include <tactility/kernel_init.h>
|
||||
#include <tactility/hal_device_module.h>
|
||||
|
||||
#include <map>
|
||||
#include <format>
|
||||
@@ -38,6 +39,11 @@ static auto LOGGER = Logger("Tactility");
|
||||
static const Configuration* config_instance = nullptr;
|
||||
static Dispatcher mainDispatcher;
|
||||
|
||||
struct ModuleParent tactility_module_parent {
|
||||
"tactility",
|
||||
nullptr
|
||||
};
|
||||
|
||||
// region Default services
|
||||
namespace service {
|
||||
// Primary
|
||||
@@ -330,6 +336,11 @@ void run(const Configuration& config, Module* platformModule, Module* deviceModu
|
||||
return;
|
||||
}
|
||||
|
||||
// HAL compatibility module: it creates kernel driver wrappers for tt::hal::Device
|
||||
check(module_parent_construct(&tactility_module_parent) == ERROR_NONE);
|
||||
check(module_set_parent(&hal_device_module, &tactility_module_parent) == ERROR_NONE);
|
||||
check(module_start(&hal_device_module) == ERROR_NONE);
|
||||
|
||||
const hal::Configuration& hardware = *config.hardware;
|
||||
|
||||
// Assign early so starting services can use it
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <Tactility/file/File.h>
|
||||
#include <Tactility/file/FileLock.h>
|
||||
#include <Tactility/file/PropertiesFile.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/Paths.h>
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <Tactility/app/crashdiagnostics/QrHelpers.h>
|
||||
#include <Tactility/app/crashdiagnostics/QrUrl.h>
|
||||
#include <Tactility/app/launcher/Launcher.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/lvgl/Statusbar.h>
|
||||
#include <Tactility/service/loader/Loader.h>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <Tactility/hal/power/PowerDevice.h>
|
||||
#include <Tactility/Timer.h>
|
||||
#include <Tactility/Assets.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <Tactility/lvgl/LvglSync.h>
|
||||
|
||||
#include <Tactility/Assets.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/Tactility.h>
|
||||
#include <Tactility/Timer.h>
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ bool ObjectFileWriter::open() {
|
||||
}
|
||||
|
||||
// Edit existing or create a new file
|
||||
auto opening_file = std::unique_ptr<FILE, FileCloser>(std::fopen(filePath.c_str(), "wb"));
|
||||
auto opening_file = std::unique_ptr<FILE, FileCloser>(std::fopen(filePath.c_str(), edit_existing ? "rb+" : "wb"));
|
||||
if (opening_file == nullptr) {
|
||||
LOGGER.error("Failed to open file {}", filePath);
|
||||
return false;
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
#include <Tactility/hal/Device.h>
|
||||
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/RecursiveMutex.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
std::vector<std::shared_ptr<Device>> devices;
|
||||
RecursiveMutex mutex;
|
||||
static Device::Id nextId = 0;
|
||||
|
||||
static const auto LOGGER = Logger("Devices");
|
||||
|
||||
Device::Device() : id(nextId++) {}
|
||||
|
||||
template <std::ranges::range RangeType>
|
||||
auto toVector(RangeType&& range) {
|
||||
auto view = range | std::views::common;
|
||||
return std::vector(view.begin(), view.end());
|
||||
}
|
||||
|
||||
void registerDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
if (findDevice(device->getId()) == nullptr) {
|
||||
devices.push_back(device);
|
||||
LOGGER.info("Registered {} with id {}", device->getName(), device->getId());
|
||||
} else {
|
||||
LOGGER.warn("Device {} with id {} was already registered", device->getName(), device->getId());
|
||||
}
|
||||
}
|
||||
|
||||
void deregisterDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
auto id_to_remove = device->getId();
|
||||
auto remove_iterator = std::remove_if(devices.begin(), devices.end(), [id_to_remove](const auto& device) {
|
||||
return device->getId() == id_to_remove;
|
||||
});
|
||||
if (remove_iterator != devices.end()) {
|
||||
LOGGER.info("Deregistering {} with id {}", device->getName(), device->getId());
|
||||
devices.erase(remove_iterator);
|
||||
} else {
|
||||
LOGGER.warn("Deregistering {} with id {} failed: not found", device->getName(), device->getId());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> findDevices(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
auto devices_view = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
return toVector(devices_view);
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> _Nullable findDevice(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
auto result_set = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
if (!result_set.empty()) {
|
||||
return result_set.front();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> _Nullable findDevice(std::string name) {
|
||||
return findDevice([&name](auto& device){
|
||||
return device->getName() == name;
|
||||
});
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> _Nullable findDevice(Device::Id id) {
|
||||
return findDevice([id](auto& device){
|
||||
return device->getId() == id;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> findDevices(Device::Type type) {
|
||||
return findDevices([type](auto& device) {
|
||||
return device->getType() == type;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> getDevices() {
|
||||
return devices;
|
||||
}
|
||||
|
||||
bool hasDevice(Device::Type type) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
auto result_set = devices | std::views::filter([&type](auto& device) {
|
||||
return device->getType() == type;
|
||||
});
|
||||
return !result_set.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <Tactility/Tactility.h>
|
||||
#include <tactility/check.h>
|
||||
#include <Tactility/hal/Configuration.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/hal/power/PowerDevice.h>
|
||||
#include <Tactility/hal/spi/SpiInit.h>
|
||||
#include <Tactility/hal/uart/UartInit.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Tactility/hal/Device.h"
|
||||
#include <tactility/hal/Device.h>
|
||||
#include "Tactility/hal/sdcard/SdCardDevice.h"
|
||||
|
||||
namespace tt::hal::sdcard {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#include <Tactility/Tactility.h>
|
||||
#include <Tactility/TactilityConfig.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/app/AppRegistration.h>
|
||||
#include <Tactility/app/AppManifest.h>
|
||||
#include <Tactility/app/App.h>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <Tactility/settings/DisplaySettings.h>
|
||||
|
||||
#include <Tactility/file/PropertiesFile.h>
|
||||
#include <Tactility/hal/Device.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <Tactility/hal/display/DisplayDevice.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
Reference in New Issue
Block a user