diff --git a/AppSim/Source/hardware_config.cpp b/AppSim/Source/hardware_config.cpp index 4361f990..e69d2f52 100644 --- a/AppSim/Source/hardware_config.cpp +++ b/AppSim/Source/hardware_config.cpp @@ -30,5 +30,44 @@ extern const tt::hal::Configuration sim_hardware = { }, .sdcard = nullptr, .power = &power, - .i2c = {} + .i2c = { + tt::hal::i2c::Configuration { + .name = "Internal", + .port = I2C_NUM_0, + .initMode = tt::hal::i2c::InitByTactility, + .canReinit = false, + .hasMutableConfiguration = false, + .timeout = 1000, + .config = (i2c_config_t) { + .mode = I2C_MODE_MASTER, + .sda_io_num = 1, + .scl_io_num = 2, + .sda_pullup_en = true, + .scl_pullup_en = true, + .master = { + .clk_speed = 400000 + }, + .clk_flags = 0 + } + }, + tt::hal::i2c::Configuration { + .name = "External", + .port = I2C_NUM_1, + .initMode = tt::hal::i2c::InitByTactility, + .canReinit = true, + .hasMutableConfiguration = true, + .timeout = 1000, + .config = (i2c_config_t) { + .mode = I2C_MODE_MASTER, + .sda_io_num = 1, + .scl_io_num = 2, + .sda_pullup_en = false, + .scl_pullup_en = false, + .master = { + .clk_speed = 400000 + }, + .clk_flags = 0 + } + } + } }; diff --git a/Boards/LilygoTdeck/InitHardware.cpp b/Boards/LilygoTdeck/InitHardware.cpp index c008fc85..e87a7784 100644 --- a/Boards/LilygoTdeck/InitHardware.cpp +++ b/Boards/LilygoTdeck/InitHardware.cpp @@ -6,21 +6,6 @@ #define TAG "tdeck" -static bool init_i2c() { - const i2c_config_t i2c_conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = GPIO_NUM_18, - .scl_io_num = GPIO_NUM_8, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_pullup_en = GPIO_PULLUP_DISABLE, - .master = { - .clk_speed = 400000 - } - }; - - return i2c_param_config(TDECK_I2C_BUS_HANDLE, &i2c_conf) == ESP_OK && i2c_driver_install(TDECK_I2C_BUS_HANDLE, i2c_conf.mode, 0, 0, 0) == ESP_OK; -} - static bool init_spi() { spi_bus_config_t bus_config = { .mosi_io_num = TDECK_SPI_PIN_MOSI, diff --git a/Boards/LilygoTdeck/lilygo_tdeck.cpp b/Boards/LilygoTdeck/lilygo_tdeck.cpp index da389c4c..e601b806 100644 --- a/Boards/LilygoTdeck/lilygo_tdeck.cpp +++ b/Boards/LilygoTdeck/lilygo_tdeck.cpp @@ -27,8 +27,8 @@ extern const tt::hal::Configuration lilygo_tdeck = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_18, .scl_io_num = GPIO_NUM_8, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_pullup_en = GPIO_PULLUP_DISABLE, + .sda_pullup_en = true, + .scl_pullup_en = true, .master = { .clk_speed = 400000 }, @@ -37,7 +37,7 @@ extern const tt::hal::Configuration lilygo_tdeck = { }, tt::hal::i2c::Configuration { .port = I2C_NUM_1, - .initMode = tt::hal::i2c::InitDisabled, + .initMode = tt::hal::i2c::InitByTactility, .canReinit = true, .hasMutableConfiguration = true, .timeout = 1000, @@ -45,8 +45,8 @@ extern const tt::hal::Configuration lilygo_tdeck = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_43, .scl_io_num = GPIO_NUM_44, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_pullup_en = GPIO_PULLUP_DISABLE, + .sda_pullup_en = false, + .scl_pullup_en = false, .master = { .clk_speed = 400000 }, diff --git a/Boards/M5stackCore2/Source/M5stackCore2.cpp b/Boards/M5stackCore2/Source/M5stackCore2.cpp index 61aba450..ace841be 100644 --- a/Boards/M5stackCore2/Source/M5stackCore2.cpp +++ b/Boards/M5stackCore2/Source/M5stackCore2.cpp @@ -18,8 +18,8 @@ extern const tt::hal::Configuration m5stack_core2 = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_21, .scl_io_num = GPIO_NUM_22, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, + .sda_pullup_en = true, + .scl_pullup_en = true, .master = { .clk_speed = 400000 }, @@ -37,8 +37,8 @@ extern const tt::hal::Configuration m5stack_core2 = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_32, .scl_io_num = GPIO_NUM_33, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, + .sda_pullup_en = true, + .scl_pullup_en = true, .master = { .clk_speed = 400000 }, diff --git a/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp b/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp index 1dd40ab9..200289de 100644 --- a/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp +++ b/Boards/M5stackCoreS3/Source/M5stackCoreS3.cpp @@ -20,8 +20,8 @@ const tt::hal::Configuration m5stack_cores3 = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_12, .scl_io_num = GPIO_NUM_11, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, + .sda_pullup_en = true, + .scl_pullup_en = true, .master = { .clk_speed = 400000 }, @@ -39,8 +39,8 @@ const tt::hal::Configuration m5stack_cores3 = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_2, .scl_io_num = GPIO_NUM_1, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, + .sda_pullup_en = true, + .scl_pullup_en = true, .master = { .clk_speed = 400000 }, diff --git a/Boards/WaveshareS3Touch/bootstrap.cpp b/Boards/WaveshareS3Touch/bootstrap.cpp index f0139a9d..f8837be1 100644 --- a/Boards/WaveshareS3Touch/bootstrap.cpp +++ b/Boards/WaveshareS3Touch/bootstrap.cpp @@ -12,8 +12,8 @@ static esp_err_t i2c_init() { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_8, .scl_io_num = GPIO_NUM_9, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_pullup_en = GPIO_PULLUP_DISABLE, + .sda_pullup_en = false, + .scl_pullup_en = false, .master = { .clk_speed = 400000 } diff --git a/Boards/YellowBoard/bootstrap.cpp b/Boards/YellowBoard/bootstrap.cpp index 817dd925..edf34b8e 100644 --- a/Boards/YellowBoard/bootstrap.cpp +++ b/Boards/YellowBoard/bootstrap.cpp @@ -10,8 +10,8 @@ static bool init_i2c() { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_33, .scl_io_num = GPIO_NUM_32, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_pullup_en = GPIO_PULLUP_DISABLE, + .sda_pullup_en = false, + .scl_pullup_en = false, .master = { .clk_speed = 400000 } diff --git a/Boards/YellowBoard/yellow_board.cpp b/Boards/YellowBoard/yellow_board.cpp index 3d01276d..4c37cd12 100644 --- a/Boards/YellowBoard/yellow_board.cpp +++ b/Boards/YellowBoard/yellow_board.cpp @@ -25,8 +25,8 @@ const tt::hal::Configuration yellow_board_24inch_cap = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_NC, .scl_io_num = GPIO_NUM_NC, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_pullup_en = GPIO_PULLUP_DISABLE, + .sda_pullup_en = false, + .scl_pullup_en = false, .master = { .clk_speed = 400000 }, @@ -43,8 +43,8 @@ const tt::hal::Configuration yellow_board_24inch_cap = { .mode = I2C_MODE_MASTER, .sda_io_num = GPIO_NUM_NC, .scl_io_num = GPIO_NUM_NC, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_pullup_en = GPIO_PULLUP_DISABLE, + .sda_pullup_en = false, + .scl_pullup_en = false, .master = { .clk_speed = 400000 }, diff --git a/Tactility/Source/Apps/Display/Display.cpp b/Tactility/Source/Apps/Display/Display.cpp index 438b1c4a..1523588d 100644 --- a/Tactility/Source/Apps/Display/Display.cpp +++ b/Tactility/Source/Apps/Display/Display.cpp @@ -14,7 +14,7 @@ static uint8_t backlight_duty = 255; static void slider_event_cb(lv_event_t* event) { auto* slider = static_cast(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); diff --git a/Tactility/Source/Apps/I2cSettings/I2cSettings.cpp b/Tactility/Source/Apps/I2cSettings/I2cSettings.cpp new file mode 100644 index 00000000..9ba1fdce --- /dev/null +++ b/Tactility/Source/Apps/I2cSettings/I2cSettings.cpp @@ -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_event_get_target(event)); + const hal::i2c::Configuration* configuration = static_cast(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 diff --git a/Tactility/Source/Apps/Power/Power.cpp b/Tactility/Source/Apps/Power/Power.cpp index f0764559..92c2eee1 100644 --- a/Tactility/Source/Apps/Power/Power.cpp +++ b/Tactility/Source/Apps/Power/Power.cpp @@ -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); } diff --git a/Tactility/Source/Services/Statusbar/Statusbar.cpp b/Tactility/Source/Services/Statusbar/Statusbar.cpp index da1508ea..0f0f024b 100644 --- a/Tactility/Source/Services/Statusbar/Statusbar.cpp +++ b/Tactility/Source/Services/Statusbar/Statusbar.cpp @@ -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) { diff --git a/Tactility/Source/Tactility.cpp b/Tactility/Source/Tactility.cpp index 1c6348e4..b6b0cc84 100644 --- a/Tactility/Source/Tactility.cpp +++ b/Tactility/Source/Tactility.cpp @@ -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; } diff --git a/Tactility/Source/Tactility.h b/Tactility/Source/Tactility.h index 8e9f3d2d..e5ed5d8e 100644 --- a/Tactility/Source/Tactility.h +++ b/Tactility/Source/Tactility.h @@ -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 diff --git a/TactilityHeadless/Source/Hal/I2c/I2c.h b/TactilityHeadless/Source/Hal/I2c/I2c.h index 87f0bb34..9bfd847a 100644 --- a/TactilityHeadless/Source/Hal/I2c/I2c.h +++ b/TactilityHeadless/Source/Hal/I2c/I2c.h @@ -1,7 +1,8 @@ #pragma once #include "I2cCompat.h" -#include +#include "CoreTypes.h" +#include #include namespace tt::hal::i2c { @@ -13,6 +14,7 @@ typedef enum { } InitMode; typedef struct { + std::string name; /** The port to operate on */ i2c_port_t port; /** Whether this bus should be initialized when device starts up */ diff --git a/TactilityHeadless/Source/Hal/I2c/I2cMock.cpp b/TactilityHeadless/Source/Hal/I2c/I2cMock.cpp index b93ebd65..5116b72b 100644 --- a/TactilityHeadless/Source/Hal/I2c/I2cMock.cpp +++ b/TactilityHeadless/Source/Hal/I2c/I2cMock.cpp @@ -39,7 +39,7 @@ bool init(const std::vector& configurations) { return true; } -static bool configure_locked(i2c_port_t port, const i2c_config_t& configuration) { +static bool configureLocked(i2c_port_t port, const i2c_config_t& configuration) { Data& data = dataArray[port]; if (data.isStarted) { TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", port); @@ -55,7 +55,7 @@ static bool configure_locked(i2c_port_t port, const i2c_config_t& configuration) esp_err_t configure(i2c_port_t port, const i2c_config_t& configuration) { lock(port); - bool result = configure_locked(port, configuration); + bool result = configureLocked(port, configuration); unlock(port); return result; } @@ -64,7 +64,7 @@ bool start(i2c_port_t port) { lock(port); dataArray[port].isStarted = true; unlock(port); - return false; + return true; } bool stop(i2c_port_t port) { diff --git a/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp b/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp index ab546390..d3f4a142 100644 --- a/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp +++ b/TactilityHeadless/Source/Services/Sdcard/Sdcard.cpp @@ -76,7 +76,7 @@ static int32_t sdcard_task(void* context) { } static void on_start(Service& service) { - if (get_hardware_config().sdcard != nullptr) { + if (hal::getConfiguration().sdcard != nullptr) { ServiceData* data = service_data_alloc(); service.setData(data); data->thread->start(); diff --git a/TactilityHeadless/Source/TactilityHeadless.cpp b/TactilityHeadless/Source/TactilityHeadless.cpp index a1917306..bde0ff4d 100644 --- a/TactilityHeadless/Source/TactilityHeadless.cpp +++ b/TactilityHeadless/Source/TactilityHeadless.cpp @@ -31,7 +31,7 @@ static void register_and_start_system_services() { } } -void init(const hal::Configuration& config) { +void initHeadless(const hal::Configuration& config) { #ifdef ESP_PLATFORM esp_init(); #endif @@ -40,9 +40,13 @@ void init(const hal::Configuration& config) { register_and_start_system_services(); } -const hal::Configuration& get_hardware_config() { +namespace hal { + +const Configuration& getConfiguration() { tt_assert(hardwareConfig != nullptr); return *hardwareConfig; } -} // namespace +} // namespace hal + +} // namespace tt diff --git a/TactilityHeadless/Source/TactilityHeadless.h b/TactilityHeadless/Source/TactilityHeadless.h index bc011c57..1697438e 100644 --- a/TactilityHeadless/Source/TactilityHeadless.h +++ b/TactilityHeadless/Source/TactilityHeadless.h @@ -5,8 +5,12 @@ namespace tt { -void init(const hal::Configuration& config); - -const hal::Configuration& get_hardware_config(); +void initHeadless(const hal::Configuration& config); + +} // namespace + +namespace tt::hal { + +const Configuration& getConfiguration(); } // namespace