Ken Van Hoeylandt 7ea8c346b0
Various improvements (#673)
Graphics & rendering

- Implement `binfont-module` to generate bitmap fonts on the device itself
- Remove lodepng support from LVGL library and create `Modules/lodepng-module` to make the feature re-usable in non-LVGL apps too. Add binding functions for LVGL support.
- Implement large icons for boot screen (Tactility and USB logos)
- Remove unused asset files in `Data/`

Boot

- Split up `Tactility.cpp` and its `run()` function into various `Init*.cpp` files.
- Re-implement `Boot.cpp` app as non-LVGL app
- Boot app now has a `BootSequence.cpp` for the init code.
- LVGL is now started when the Boot app finishes
- Move more init logic into Boot app

BLE

- Don't allocate scan result buffers unless BLE is on
- Allocate scan results in external memory if possible

UX & UI

- USB Settings app: remove border from the button container
2026-10-04 18:09:21 +02:00

190 lines
4.7 KiB
C++

// SPDX-License-Identifier: Apache-2.0
#include <cstring>
#include <ranges>
#include <vector>
#include <tactility/driver.h>
#include <tactility/concurrent/mutex.h>
#include <tactility/device.h>
#include <tactility/error.h>
#include <tactility/log.h>
#include <tactility/time.h>
#define TAG "driver"
constexpr TickType_t SLOW_START_WARNING_TICKS = pdMS_TO_TICKS(100);
struct DriverInternal {
int use_count = 0;
bool destroying = false;
};
struct DriverLedger {
std::vector<Driver*> drivers;
Mutex mutex {};
DriverLedger() { mutex_construct(&mutex); }
~DriverLedger() { mutex_destruct(&mutex); }
void lock() { mutex_lock(&mutex); }
void unlock() { mutex_unlock(&mutex); }
};
static DriverLedger ledger;
extern "C" {
error_t driver_construct(Driver* driver) {
driver->internal = new(std::nothrow) DriverInternal;
if (driver->internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
return ERROR_NONE;
}
error_t driver_destruct(Driver* driver) {
auto* internal = driver->internal;
if (driver->owner == nullptr) {
return ERROR_NOT_ALLOWED;
}
if (internal->use_count != 0 || internal->destroying) {
return ERROR_INVALID_STATE;
}
internal->destroying = true;
// Nullify internal reference before deletion to prevent use-after-free
driver->internal = nullptr;
delete internal;
return ERROR_NONE;
}
error_t driver_add(Driver* driver) {
LOG_D(TAG, "add %s", driver->name);
ledger.lock();
ledger.drivers.push_back(driver);
ledger.unlock();
return ERROR_NONE;
}
error_t driver_remove(Driver* driver) {
LOG_D(TAG, "remove %s", driver->name);
if (driver->owner == nullptr) return ERROR_NOT_ALLOWED;
ledger.lock();
const auto iterator = std::ranges::find(ledger.drivers, driver);
if (iterator == ledger.drivers.end()) {
ledger.unlock();
return ERROR_NOT_FOUND;
}
ledger.drivers.erase(iterator);
ledger.unlock();
return ERROR_NONE;
}
error_t driver_construct_add(struct Driver* driver) {
if (driver_construct(driver) != ERROR_NONE) return ERROR_RESOURCE;
if (driver_add(driver) != ERROR_NONE) return ERROR_RESOURCE;
return ERROR_NONE;
}
error_t driver_remove_destruct(struct Driver* driver) {
if (driver_remove(driver) != ERROR_NONE) return ERROR_RESOURCE;
if (driver_destruct(driver) != ERROR_NONE) return ERROR_RESOURCE;
return ERROR_NONE;
}
bool driver_is_compatible(Driver* driver, const char* compatible) {
if (compatible == nullptr || driver->compatible == nullptr) {
return false;
}
const char** compatible_iterator = driver->compatible;
while (*compatible_iterator != nullptr) {
if (strcmp(*compatible_iterator, compatible) == 0) {
return true;
}
compatible_iterator++;
}
return false;
}
Driver* driver_find_compatible(const char* compatible) {
ledger.lock();
Driver* result = nullptr;
for (auto* driver : ledger.drivers) {
if (driver_is_compatible(driver, compatible)) {
result = driver;
break;
}
}
ledger.unlock();
return result;
}
error_t driver_bind(Driver* driver, Device* device) {
error_t error = ERROR_NONE;
if (driver->internal->destroying || !device_is_added(device)) {
error = ERROR_INVALID_STATE;
goto error;
}
if (driver->probe != nullptr && driver->probe(device) != ERROR_NONE) {
error = ERROR_NOT_FOUND;
goto error;
}
if (driver->start_device != nullptr) {
const TickType_t start_ticks = get_ticks();
error = driver->start_device(device);
const TickType_t elapsed_ticks = get_ticks() - start_ticks;
if (elapsed_ticks > SLOW_START_WARNING_TICKS) {
LOG_W(TAG, "starting %s with %s took %u ms", device->name, driver->name, static_cast<unsigned>(elapsed_ticks * portTICK_PERIOD_MS));
}
if (error != ERROR_NONE) {
goto error;
}
}
driver->internal->use_count++;
LOG_I(TAG, "bound %s to %s", driver->name, device->name);
return ERROR_NONE;
error:
return error;
}
error_t driver_unbind(Driver* driver, Device* device) {
error_t error = ERROR_NONE;
if (driver->internal->destroying || !device_is_added(device)) {
error = ERROR_INVALID_STATE;
goto error;
}
if (driver->stop_device != nullptr) {
error = driver->stop_device(device);
if (error != ERROR_NONE) {
goto error;
}
}
driver->internal->use_count--;
LOG_I(TAG, "unbound %s from %s", driver->name, device->name);
return ERROR_NONE;
error:
return error;
}
const struct DeviceType* driver_get_device_type(struct Driver* driver) {
return driver->device_type;
}
} // extern "C"