From a1bc738bf1a521606bc59453c25b7189925cf907 Mon Sep 17 00:00:00 2001 From: Fuji Pebri Date: Mon, 23 Feb 2026 11:03:22 +0700 Subject: [PATCH] [add] i2c demo accessing internal os driver for apps --- .gitignore | 1 + Apps/AHT10Sensor/CMakeLists.txt | 16 +++++ Apps/AHT10Sensor/main/CMakeLists.txt | 6 ++ Apps/AHT10Sensor/main/Source/aht10.c | 102 ++++++++++++++++++++++++++ Apps/AHT10Sensor/main/Source/aht10.h | 14 ++++ Apps/AHT10Sensor/main/Source/main.c | 103 +++++++++++++++++++++++++++ Apps/AHT10Sensor/manifest.properties | 10 +++ Apps/AHT10Sensor/tactility.py | 1 + 8 files changed, 253 insertions(+) create mode 100644 Apps/AHT10Sensor/CMakeLists.txt create mode 100644 Apps/AHT10Sensor/main/CMakeLists.txt create mode 100644 Apps/AHT10Sensor/main/Source/aht10.c create mode 100644 Apps/AHT10Sensor/main/Source/aht10.h create mode 100644 Apps/AHT10Sensor/main/Source/main.c create mode 100644 Apps/AHT10Sensor/manifest.properties create mode 120000 Apps/AHT10Sensor/tactility.py diff --git a/.gitignore b/.gitignore index 8704f76..1c8f8fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea/ .DS_Store +.cache build/ cmake-build-*/ diff --git a/Apps/AHT10Sensor/CMakeLists.txt b/Apps/AHT10Sensor/CMakeLists.txt new file mode 100644 index 0000000..e4bbaed --- /dev/null +++ b/Apps/AHT10Sensor/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.20) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +if (DEFINED ENV{TACTILITY_SDK_PATH}) + set(TACTILITY_SDK_PATH $ENV{TACTILITY_SDK_PATH}) +else() + set(TACTILITY_SDK_PATH "../../release/TactilitySDK") + message(WARNING "⚠️ TACTILITY_SDK_PATH environment variable is not set, defaulting to ${TACTILITY_SDK_PATH}") +endif() + +include("${TACTILITY_SDK_PATH}/TactilitySDK.cmake") +set(EXTRA_COMPONENT_DIRS ${TACTILITY_SDK_PATH}) + +project(AHT10Sensor) +tactility_project(AHT10Sensor) diff --git a/Apps/AHT10Sensor/main/CMakeLists.txt b/Apps/AHT10Sensor/main/CMakeLists.txt new file mode 100644 index 0000000..36339a0 --- /dev/null +++ b/Apps/AHT10Sensor/main/CMakeLists.txt @@ -0,0 +1,6 @@ +file(GLOB_RECURSE SOURCE_FILES Source/*.c*) + +idf_component_register( + SRCS ${SOURCE_FILES} + REQUIRES TactilitySDK driver +) diff --git a/Apps/AHT10Sensor/main/Source/aht10.c b/Apps/AHT10Sensor/main/Source/aht10.c new file mode 100644 index 0000000..2cfb0c6 --- /dev/null +++ b/Apps/AHT10Sensor/main/Source/aht10.c @@ -0,0 +1,102 @@ +#include "aht10.h" + +#include +#include +#include + +#define AHT10_ADDR 0x38 + +static struct Device* s_i2c = NULL; + +/* ------------------------------ */ +/* Find I2C controller */ +/* ------------------------------ */ + +static bool find_i2c(struct Device* dev, void* ctx) +{ + s_i2c = dev; + return false; +} + +static bool setup_i2c(void) +{ + s_i2c = NULL; + + device_for_each_of_type( + &I2C_CONTROLLER_TYPE, + NULL, + find_i2c); + + if (!s_i2c) + return false; + + return device_is_ready(s_i2c); +} + +/* ------------------------------ */ +/* Init sensor */ +/* ------------------------------ */ + +bool aht10_init(void) +{ + if (!setup_i2c()) + return false; + + uint8_t init_cmd[3] = {0xE1, 0x08, 0x00}; + + return i2c_controller_write( + s_i2c, + AHT10_ADDR, + init_cmd, + sizeof(init_cmd), + pdMS_TO_TICKS(100)) + == ERROR_NONE; +} + +/* ------------------------------ */ +/* Read measurement */ +/* ------------------------------ */ + +bool aht10_read(float* temperature, float* humidity) +{ + if (!s_i2c) + return false; + + uint8_t cmd[3] = {0xAC, 0x33, 0x00}; + + if (i2c_controller_write( + s_i2c, + AHT10_ADDR, + cmd, + sizeof(cmd), + pdMS_TO_TICKS(100)) != ERROR_NONE) + return false; + + vTaskDelay(pdMS_TO_TICKS(80)); + + uint8_t data[6]; + + if (i2c_controller_read( + s_i2c, + AHT10_ADDR, + data, + sizeof(data), + pdMS_TO_TICKS(100)) != ERROR_NONE) + return false; + + uint32_t raw_h = + ((uint32_t)data[1] << 12) | + ((uint32_t)data[2] << 4) | + (data[3] >> 4); + + uint32_t raw_t = + ((uint32_t)(data[3] & 0x0F) << 16) | + ((uint32_t)data[4] << 8) | + data[5]; + + *humidity = raw_h * 100.0f / 1048576.0f; + *temperature = raw_t * 200.0f / 1048576.0f - 50.0f; + + return true; +} + diff --git a/Apps/AHT10Sensor/main/Source/aht10.h b/Apps/AHT10Sensor/main/Source/aht10.h new file mode 100644 index 0000000..63d7dba --- /dev/null +++ b/Apps/AHT10Sensor/main/Source/aht10.h @@ -0,0 +1,14 @@ +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +bool aht10_init(void); +bool aht10_read(float* temperature, float* humidity); + +#ifdef __cplusplus +} +#endif + diff --git a/Apps/AHT10Sensor/main/Source/main.c b/Apps/AHT10Sensor/main/Source/main.c new file mode 100644 index 0000000..acc84c1 --- /dev/null +++ b/Apps/AHT10Sensor/main/Source/main.c @@ -0,0 +1,103 @@ +#include +#include + +#include "aht10.h" + +#include +#include + +static lv_obj_t* arc_temp; +static lv_obj_t* label_temp; +static lv_obj_t* label_hum; + +static lv_timer_t* sensor_timer = NULL; + +/* ------------------------------ */ +/* Sensor update */ +/* ------------------------------ */ + +static void sensor_update(lv_timer_t* timer) +{ + if (!label_temp || !label_hum) + return; + + float t, h; + + if (!aht10_read(&t, &h)) + return; + + char buf[32]; + + int t_int = (int)t; + lv_arc_set_value(arc_temp, t_int); + + snprintf(buf, sizeof(buf), "%.1f °C", t); + lv_label_set_text(label_temp, buf); + + snprintf(buf, sizeof(buf), "Humidity %.1f %%", h); + lv_label_set_text(label_hum, buf); +} + +/* ------------------------------ */ +/* Show app */ +/* ------------------------------ */ + +static void onShowApp(AppHandle app, void* data, lv_obj_t* parent) +{ + lv_obj_t* toolbar = + tt_lvgl_toolbar_create_for_app(parent, app); + lv_obj_align(toolbar, LV_ALIGN_TOP_MID, 0, 0); + + arc_temp = lv_arc_create(parent); + lv_obj_set_size(arc_temp, 200, 200); + lv_obj_align(arc_temp, LV_ALIGN_CENTER, 0, 30); + + lv_arc_set_range(arc_temp, 0, 50); + lv_arc_set_value(arc_temp, 25); + + lv_arc_set_rotation(arc_temp, 135); + lv_arc_set_bg_angles(arc_temp, 0, 270); + + label_temp = lv_label_create(parent); + lv_label_set_text(label_temp, "--.- °C"); + lv_obj_align(label_temp, LV_ALIGN_CENTER, 0, 10); + + label_hum = lv_label_create(parent); + lv_label_set_text(label_hum, "Humidity --%"); + lv_obj_align(label_hum, LV_ALIGN_CENTER, 0, 60); + + aht10_init(); + + sensor_timer = lv_timer_create(sensor_update, 2000, NULL); +} + +/* ------------------------------ */ +/* Hide app (IMPORTANT FIX) */ +/* ------------------------------ */ + +static void onHideApp(AppHandle app, void* data) +{ + if (sensor_timer) { + lv_timer_del(sensor_timer); + sensor_timer = NULL; + } + + label_temp = NULL; + label_hum = NULL; + arc_temp = NULL; +} + +/* ------------------------------ */ +/* Entry */ +/* ------------------------------ */ + +int main(int argc, char* argv[]) +{ + tt_app_register((AppRegistration) { + .onShow = onShowApp, + .onHide = onHideApp + }); + + return 0; +} + diff --git a/Apps/AHT10Sensor/manifest.properties b/Apps/AHT10Sensor/manifest.properties new file mode 100644 index 0000000..4aa05a0 --- /dev/null +++ b/Apps/AHT10Sensor/manifest.properties @@ -0,0 +1,10 @@ +[manifest] +version=0.1 +[target] +sdk=0.7.0-dev +platforms=esp32 +[app] +id=one.tactility.aht10 +versionName=0.1.0 +versionCode=1 +name=AHT10 Sensor diff --git a/Apps/AHT10Sensor/tactility.py b/Apps/AHT10Sensor/tactility.py new file mode 120000 index 0000000..ca0ddb1 --- /dev/null +++ b/Apps/AHT10Sensor/tactility.py @@ -0,0 +1 @@ +../../tactility.py \ No newline at end of file