Simplify app registration with TactilityCpp (#7)
- Simplified app registration with a C++ wrapper for the TactilityC. - Rename the various app classes from `Application` to more specific names like `Calculator` etc.
This commit is contained in:
committed by
GitHub
parent
e1579d3cbd
commit
b39bd6b6f5
@@ -10,6 +10,7 @@ idf_component_register(
|
||||
# Library headers must be included directly,
|
||||
# because all regular dependencies get stripped by elf_loader's cmake script
|
||||
INCLUDE_DIRS ../../../Libraries/Str/Include
|
||||
INCLUDE_DIRS ../../../Libraries/TactilityCpp/Include
|
||||
REQUIRES TactilitySDK
|
||||
)
|
||||
|
||||
|
||||
+13
-13
@@ -1,4 +1,4 @@
|
||||
#include "Application.h"
|
||||
#include "Diceware.h"
|
||||
|
||||
#include <tt_app_alertdialog.h>
|
||||
#include <tt_file.h>
|
||||
@@ -53,8 +53,8 @@ static Str readWordAtLine(const AppHandle handle, const int lineIndex) {
|
||||
return word;
|
||||
}
|
||||
|
||||
int32_t Application::jobMain(void* data) {
|
||||
Application* application = static_cast<Application*>(data);
|
||||
int32_t Diceware::jobMain(void* data) {
|
||||
Diceware* application = static_cast<Diceware*>(data);
|
||||
Str result;
|
||||
for (int i = 0; i < application->wordCount; i++) {
|
||||
constexpr int line_count = 7776;
|
||||
@@ -68,7 +68,7 @@ int32_t Application::jobMain(void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Application::cleanupJob() {
|
||||
void Diceware::cleanupJob() {
|
||||
if (jobThread != nullptr) {
|
||||
tt_thread_join(jobThread, TT_MAX_TICKS);
|
||||
tt_thread_free(jobThread);
|
||||
@@ -76,7 +76,7 @@ void Application::cleanupJob() {
|
||||
}
|
||||
}
|
||||
|
||||
void Application::startJob(uint32_t jobWordCount) {
|
||||
void Diceware::startJob(uint32_t jobWordCount) {
|
||||
cleanupJob();
|
||||
|
||||
wordCount = jobWordCount;
|
||||
@@ -84,14 +84,14 @@ void Application::startJob(uint32_t jobWordCount) {
|
||||
tt_thread_start(jobThread);
|
||||
}
|
||||
|
||||
void Application::onFinishJob(Str result) {
|
||||
void Diceware::onFinishJob(Str result) {
|
||||
tt_lvgl_lock();
|
||||
lv_label_set_text(resultLabel, result.c_str());
|
||||
tt_lvgl_unlock();
|
||||
}
|
||||
|
||||
void Application::onClickGenerate(lv_event_t* e) {
|
||||
auto* application = static_cast<Application*>(lv_event_get_user_data(e));
|
||||
void Diceware::onClickGenerate(lv_event_t* e) {
|
||||
auto* application = static_cast<Diceware*>(lv_event_get_user_data(e));
|
||||
auto* spinbox = application->spinbox;
|
||||
|
||||
lv_label_set_text(application->resultLabel, "Generating...");
|
||||
@@ -100,22 +100,22 @@ void Application::onClickGenerate(lv_event_t* e) {
|
||||
application->startJob(word_count);
|
||||
}
|
||||
|
||||
void Application::onSpinboxDecrement(lv_event_t* e) {
|
||||
void Diceware::onSpinboxDecrement(lv_event_t* e) {
|
||||
auto* spinbox = static_cast<lv_obj_t*>(lv_event_get_user_data(e));
|
||||
lv_spinbox_decrement(spinbox);
|
||||
}
|
||||
|
||||
void Application::onSpinboxIncrement(lv_event_t* e) {
|
||||
void Diceware::onSpinboxIncrement(lv_event_t* e) {
|
||||
auto* spinbox = static_cast<lv_obj_t*>(lv_event_get_user_data(e));
|
||||
lv_spinbox_increment(spinbox);
|
||||
}
|
||||
|
||||
void Application::onHelpClicked(lv_event_t* e) {
|
||||
void Diceware::onHelpClicked(lv_event_t* e) {
|
||||
const char* buttons[] = { "OK" };
|
||||
tt_app_alertdialog_start("Diceware Info", "The hardware random number generator can use the Wi-Fi radio to improve randomness. There's no need to connect to a Wi-Fi network for this to work.", buttons, 1);
|
||||
}
|
||||
|
||||
void Application::onShow(AppHandle appHandle, lv_obj_t* parent) {
|
||||
void Diceware::onShow(AppHandle appHandle, lv_obj_t* parent) {
|
||||
handle = appHandle;
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
@@ -179,6 +179,6 @@ void Application::onShow(AppHandle appHandle, lv_obj_t* parent) {
|
||||
lv_obj_set_style_text_align(resultLabel, LV_TEXT_ALIGN_CENTER, LV_STATE_DEFAULT);
|
||||
}
|
||||
|
||||
void Application::onHide(AppHandle context) {
|
||||
void Diceware::onHide(AppHandle context) {
|
||||
cleanupJob();
|
||||
}
|
||||
@@ -5,8 +5,9 @@
|
||||
|
||||
#include <Str.h>
|
||||
#include <lvgl.h>
|
||||
#include <TactilityCpp/App.h>
|
||||
|
||||
class Application final {
|
||||
class Diceware final : public App {
|
||||
|
||||
AppHandle handle = nullptr;
|
||||
lv_obj_t* spinbox = nullptr;
|
||||
@@ -27,6 +28,6 @@ class Application final {
|
||||
|
||||
public:
|
||||
|
||||
void onShow(AppHandle context, lv_obj_t* parent);
|
||||
void onHide(AppHandle context);
|
||||
void onShow(AppHandle context, lv_obj_t* parent) override;
|
||||
void onHide(AppHandle context) override;
|
||||
};
|
||||
@@ -1,31 +1,10 @@
|
||||
#include <tt_app.h>
|
||||
#include "Application.h"
|
||||
|
||||
static void onShowApp(AppHandle appHandle, void* data, lv_obj_t* parent) {
|
||||
static_cast<Application*>(data)->onShow(appHandle, parent);
|
||||
}
|
||||
|
||||
static void onHideApp(AppHandle appHandle, void* data) {
|
||||
static_cast<Application*>(data)->onHide(appHandle);
|
||||
}
|
||||
|
||||
static void* createAppData() {
|
||||
return new Application();
|
||||
}
|
||||
|
||||
static void destroyAppData(void* app) {
|
||||
delete static_cast<Application*>(app);
|
||||
}
|
||||
#include "Diceware.h"
|
||||
#include <TactilityCpp/App.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
tt_app_register((AppRegistration) {
|
||||
.createData = createAppData,
|
||||
.destroyData = destroyAppData,
|
||||
.onShow = onShowApp,
|
||||
.onHide = onHideApp,
|
||||
});
|
||||
registerApp<Diceware>();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user