Added I2C settings app (#85)

This commit is contained in:
Ken Van Hoeylandt
2024-11-24 21:31:15 +01:00
committed by GitHub
parent d8731eaa17
commit c8f63adba4
19 changed files with 186 additions and 56 deletions
+2 -2
View File
@@ -14,7 +14,7 @@ static uint8_t backlight_duty = 255;
static void slider_event_cb(lv_event_t* event) {
auto* slider = static_cast<lv_obj_t*>(lv_event_get_target(event));
const Configuration* config = get_config();
const Configuration* config = getConfiguration();
hal::SetBacklightDuty set_backlight_duty = config->hardware->display.setBacklightDuty;
if (set_backlight_duty != nullptr) {
@@ -89,7 +89,7 @@ static void app_show(App app, lv_obj_t* parent) {
lv_obj_add_event_cb(brightness_slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_set_pos(brightness_slider, 0, 30);
const Configuration* config = get_config();
const Configuration* config = getConfiguration();
hal::SetBacklightDuty set_backlight_duty = config->hardware->display.setBacklightDuty;
if (set_backlight_duty == nullptr) {
lv_slider_set_value(brightness_slider, 255, LV_ANIM_OFF);
@@ -0,0 +1,94 @@
#include "Tactility.h"
#include "Ui/Style.h"
#include "Ui/Toolbar.h"
#include "Hal/I2c/I2c.h"
#include "lvgl.h"
namespace tt::app::settings::i2c {
static void on_switch_toggle(lv_event_t* event) {
lv_event_code_t code = lv_event_get_code(event);
auto* state_switch = static_cast<lv_obj_t*>(lv_event_get_target(event));
const hal::i2c::Configuration* configuration = static_cast<hal::i2c::Configuration*>(lv_event_get_user_data(event));
if (code == LV_EVENT_VALUE_CHANGED) {
bool should_enable = lv_obj_get_state(state_switch) & LV_STATE_CHECKED;
bool is_enabled = hal::i2c::isStarted(configuration->port);
if (is_enabled && !should_enable) {
if (!hal::i2c::stop(configuration->port)) {
lv_obj_add_state(state_switch, LV_STATE_CHECKED);
}
} else if (!is_enabled && should_enable) {
if (!hal::i2c::start(configuration->port)) {
lv_obj_remove_state(state_switch, LV_STATE_CHECKED);
}
}
}
}
static void show(lv_obj_t* parent, const hal::i2c::Configuration& configuration) {
lv_obj_t* card = lv_obj_create(parent);
lv_obj_set_height(card, LV_SIZE_CONTENT);
lv_obj_set_style_margin_hor(card, 16, 0);
lv_obj_set_style_margin_bottom(card, 16, 0);
lv_obj_set_width(card, LV_PCT(100));
lv_obj_t* port_label = lv_label_create(card);
const char* name = configuration.name.empty() ? "Unnamed" : configuration.name.c_str();
lv_label_set_text_fmt(port_label, "%s (port %d)", name, configuration.port);
// On-off switch
if (configuration.canReinit) {
lv_obj_t* state_switch = lv_switch_create(card);
lv_obj_align(state_switch, LV_ALIGN_TOP_RIGHT, 0, 0);
if (hal::i2c::isStarted(configuration.port)) {
lv_obj_add_state(state_switch, LV_STATE_CHECKED);
}
lv_obj_add_event_cb(state_switch, on_switch_toggle, LV_EVENT_VALUE_CHANGED, (void*)&configuration);
}
// SDA label
lv_obj_t* sda_pin_label = lv_label_create(card);
const char* sda_pullup_text = configuration.config.sda_pullup_en ? "yes" : "no";
lv_label_set_text_fmt(sda_pin_label, "SDA: pin %d, pullup: %s", configuration.config.sda_io_num, sda_pullup_text );
lv_obj_align_to(sda_pin_label, port_label, LV_ALIGN_OUT_BOTTOM_LEFT, 16, 8);
// SCL label
lv_obj_t* scl_pin_label = lv_label_create(card);
const char* scl_pullup_text = configuration.config.scl_pullup_en ? "yes" : "no";
lv_label_set_text_fmt(scl_pin_label, "SCL: pin %d, pullup: %s", configuration.config.scl_io_num, scl_pullup_text);
lv_obj_align_to(scl_pin_label, sda_pin_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
// Frequency:
if (configuration.config.mode == I2C_MODE_MASTER) {
lv_obj_t* frequency_label = lv_label_create(card);
lv_label_set_text_fmt(frequency_label, "Frequency: %lu Hz", configuration.config.master.clk_speed);
lv_obj_align_to(frequency_label, scl_pin_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
}
}
static void on_show(App app, lv_obj_t* parent) {
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
lvgl::toolbar_create(parent, app);
lv_obj_t* wrapper = lv_obj_create(parent);
lv_obj_set_width(wrapper, LV_PCT(100));
lv_obj_set_flex_grow(wrapper, 1);
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
lvgl::obj_set_style_no_padding(wrapper);
lvgl::obj_set_style_bg_invisible(wrapper);
for (const auto& configuration: getConfiguration()->hardware->i2c) {
show(wrapper, configuration);
}
}
extern const AppManifest manifest = {
.id = "I2cSettings",
.name = "I2C",
.type = AppTypeSettings,
.on_show = &on_show
};
} // namespace
+1 -1
View File
@@ -98,7 +98,7 @@ static void app_hide(TT_UNUSED App app) {
static void app_start(App app) {
auto* data = new AppData();
data->update_timer = new Timer(Timer::TypePeriodic, &app_update_ui, data);
data->power = get_config()->hardware->power;
data->power = getConfiguration()->hardware->power;
assert(data->power != nullptr); // The Power app only shows up on supported devices
tt_app_set_data(app, data);
}
@@ -100,7 +100,7 @@ static void update_sdcard_icon(ServiceData* data) {
// region power
static _Nullable const char* power_get_status_icon() {
_Nullable const hal::Power* power = get_config()->hardware->power;
_Nullable const hal::Power* power = getConfiguration()->hardware->power;
if (power != nullptr) {
uint8_t charge = power->get_charge_level();
if (charge >= 230) {
+5 -3
View File
@@ -42,6 +42,7 @@ namespace app {
namespace screenshot { extern const AppManifest manifest; }
namespace settings { extern const AppManifest manifest; }
namespace settings::display { extern const AppManifest manifest; }
namespace settings::i2c { extern const AppManifest manifest; }
namespace settings::power { extern const AppManifest manifest; }
namespace system_info { extern const AppManifest manifest; }
namespace text_viewer { extern const AppManifest manifest; }
@@ -60,6 +61,7 @@ static const AppManifest* const system_apps[] = {
&app::image_viewer::manifest,
&app::settings::manifest,
&app::settings::display::manifest,
&app::settings::i2c::manifest,
&app::system_info::manifest,
&app::text_viewer::manifest,
&app::wifi_connect::manifest,
@@ -79,7 +81,7 @@ static void register_system_apps() {
app_manifest_registry_add(system_apps[i]);
}
if (get_config()->hardware->power != nullptr) {
if (getConfiguration()->hardware->power != nullptr) {
app_manifest_registry_add(&app::settings::power::manifest);
}
}
@@ -127,7 +129,7 @@ void init(const Configuration* config) {
// Assign early so starting services can use it
config_instance = config;
init(*config->hardware);
initHeadless(*config->hardware);
lvgl_init(config->hardware);
@@ -155,7 +157,7 @@ void init(const Configuration* config) {
TT_LOG_I(TAG, "init complete");
}
const Configuration* _Nullable get_config() {
const Configuration* _Nullable getConfiguration() {
return config_instance;
}
+1 -1
View File
@@ -25,6 +25,6 @@ void init(const Configuration* config);
* While technically nullable, this instance is always set if tt_init() succeeds.
* @return the Configuration instance that was passed on to tt_init() if init is successful
*/
const Configuration* _Nullable get_config();
const Configuration* _Nullable getConfiguration();
} // namespace