C++ conversion (#80)

Converted project to C++
This commit is contained in:
Ken Van Hoeylandt
2024-11-22 20:26:08 +01:00
committed by GitHub
parent 6d80144e12
commit 85e26636a3
488 changed files with 6017 additions and 39466 deletions
@@ -0,0 +1,48 @@
#pragma once
#include "TactilityCore.h"
#include "Sdcard.h"
#include "Power.h"
namespace tt::hal {
typedef bool (*Bootstrap)();
typedef bool (*InitGraphics)();
typedef void (*SetBacklightDuty)(uint8_t);
typedef struct {
/** Set backlight duty */
SetBacklightDuty set_backlight_duty;
} Display;
typedef struct {
/**
* Optional bootstrapping method (e.g. to turn peripherals on)
* This is called after Tactility core init and before any other inits in the HardwareConfig.
* */
const Bootstrap _Nullable bootstrap;
/**
* Initializes LVGL with all relevant hardware.
* This includes the display and optional pointer devices (such as touch) or a keyboard.
*/
const InitGraphics init_graphics;
/**
* An interface for display features such as setting the backlight.
* This does nothing when a display isn't present.
*/
const Display display;
/**
* An optional SD card interface.
*/
const sdcard::SdCard* _Nullable sdcard;
/**
* An optional power interface for battery or other power delivery.
*/
const Power* _Nullable power;
} Configuration;
} // namespace
+22
View File
@@ -0,0 +1,22 @@
#include "Hal/Hal_i.h"
#define TAG "hardware"
namespace tt::hal {
void init(const Configuration* configuration) {
if (configuration->bootstrap != nullptr) {
TT_LOG_I(TAG, "Bootstrapping");
tt_check(configuration->bootstrap(), "bootstrap failed");
}
if (configuration->sdcard != nullptr) {
TT_LOG_I(TAG, "Mounting sdcard");
sdcard::mount(configuration->sdcard);
}
tt_check(configuration->init_graphics, "Graphics init not set");
tt_check(configuration->init_graphics(), "Graphics init failed");
}
} // namespace
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <cstdint>
namespace tt::hal {
typedef bool (*PowerIsCharging)();
typedef bool (*PowerIsChargingEnabled)();
typedef void (*PowerSetChargingEnabled)(bool enabled);
typedef uint8_t (*PowerGetBatteryCharge)(); // Power value [0, 255] which maps to 0-100% charge
typedef int32_t (*PowerGetCurrent)(); // Consumption or charge current in mAh
typedef struct {
PowerIsCharging is_charging;
PowerIsChargingEnabled is_charging_enabled;
PowerSetChargingEnabled set_charging_enabled;
PowerGetBatteryCharge get_charge_level;
PowerGetCurrent get_current;
} Power;
} // namespace tt
+85
View File
@@ -0,0 +1,85 @@
#include "Sdcard.h"
#include "Mutex.h"
#include "TactilityCore.h"
namespace tt::hal::sdcard {
#define TAG "sdcard"
static Mutex mutex(MutexTypeRecursive);
typedef struct {
const SdCard* sdcard;
void* context;
} MountData;
static MountData data = {
.sdcard = nullptr,
.context = nullptr
};
static bool lock(uint32_t timeout_ticks) {
return mutex.acquire(timeout_ticks) == TtStatusOk;
}
static void unlock() {
mutex.release();
}
bool mount(const SdCard* sdcard) {
TT_LOG_I(TAG, "Mounting");
if (data.sdcard != nullptr) {
TT_LOG_E(TAG, "Failed to mount: already mounted");
return false;
}
if (lock(100)) {
void* context = sdcard->mount(TT_SDCARD_MOUNT_POINT);
data = (MountData) {
.sdcard = sdcard,
.context = context
};
unlock();
return (data.context != nullptr);
} else {
TT_LOG_E(TAG, "Failed to lock");
return false;
}
}
State get_state() {
if (data.context == nullptr) {
return StateUnmounted;
} else if (data.sdcard->is_mounted(data.context)) {
return StateMounted;
} else {
return StateError;
}
}
bool unmount(uint32_t timeout_ticks) {
TT_LOG_I(TAG, "Unmounting");
bool result = false;
if (lock(timeout_ticks)) {
if (data.sdcard != nullptr) {
data.sdcard->unmount(data.context);
data = (MountData) {
.sdcard = nullptr,
.context = nullptr
};
result = true;
} else {
TT_LOG_E(TAG, "Can't unmount: nothing mounted");
}
unlock();
} else {
TT_LOG_E(TAG, "Failed to lock in %lu ticks", timeout_ticks);
}
return result;
}
} // namespace
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include "TactilityCore.h"
namespace tt::hal::sdcard {
#define TT_SDCARD_MOUNT_POINT "/sdcard"
typedef void* (*Mount)(const char* mount_path);
typedef void (*Unmount)(void* context);
typedef bool (*IsMounted)(void* context);
typedef enum {
StateMounted,
StateUnmounted,
StateError,
} State;
typedef enum {
MountBehaviourAtBoot, /** Only mount at boot */
MountBehaviourAnytime /** Mount/dismount any time */
} MountBehaviour;
typedef struct {
Mount mount;
Unmount unmount;
IsMounted is_mounted;
MountBehaviour mount_behaviour;
} SdCard;
bool mount(const SdCard* sdcard);
State get_state();
bool unmount(uint32_t timeout_ticks);
} // namespace