From 99fd55ccb993ba605eef49886195c4f2e948f655 Mon Sep 17 00:00:00 2001 From: GOB Date: Wed, 18 Sep 2024 14:35:31 +0900 Subject: [PATCH] Add some examples --- .../Basic/ComponentOnly/ComponentOnly.ino | 9 +++ .../ComponentOnly/main/ComponentOnly.cpp | 39 +++++++++++++ examples/Basic/SelfUpdate/SelfUpdate.ino | 9 +++ examples/Basic/SelfUpdate/main/SelfUpdate.cpp | 56 +++++++++++++++++++ examples/Basic/Simple/Simple.ino | 13 +++++ examples/Basic/Simple/main/Simple.cpp | 40 +++++++++++++ .../MultipleDevices/main/MultipleDevices.cpp | 4 +- platformio.ini | 29 +++++++++- 8 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 examples/Basic/ComponentOnly/ComponentOnly.ino create mode 100644 examples/Basic/ComponentOnly/main/ComponentOnly.cpp create mode 100644 examples/Basic/SelfUpdate/SelfUpdate.ino create mode 100644 examples/Basic/SelfUpdate/main/SelfUpdate.cpp create mode 100644 examples/Basic/Simple/Simple.ino create mode 100644 examples/Basic/Simple/main/Simple.cpp diff --git a/examples/Basic/ComponentOnly/ComponentOnly.ino b/examples/Basic/ComponentOnly/ComponentOnly.ino new file mode 100644 index 0000000..aba29f6 --- /dev/null +++ b/examples/Basic/ComponentOnly/ComponentOnly.ino @@ -0,0 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +/* + To update the unit yourself usage example (UnitCO2) +*/ +#include "main/ComponentOnly.cpp" diff --git a/examples/Basic/ComponentOnly/main/ComponentOnly.cpp b/examples/Basic/ComponentOnly/main/ComponentOnly.cpp new file mode 100644 index 0000000..43921fa --- /dev/null +++ b/examples/Basic/ComponentOnly/main/ComponentOnly.cpp @@ -0,0 +1,39 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +/* + Example of using only unit component without UnitUnified manager + If you use other units, change include files(*1), instances(*2), and get values(*3) +*/ +#include +#include +#include // *1 Include the header of the unit to be used + +m5::unit::UnitCO2 unit; // *2 Instance of the unit + +void setup() { + M5.begin(); + + auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); + auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); + M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl); + Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U); + + M5.Display.clear(TFT_DARKGREEN); + if (!unit.assign(Wire) // Assign Wire + || unit.begin()) { // Begin unit + M5_LOGE("Failed to assign/begin"); + M5.Display.clear(TFT_RED); + } +} + +void loop() { + M5.begin(); + unit.update(); // Explicitly call unit.update() yourself + if (unit.updated()) { + // *3 Obtaining unit-specific measurements + M5_LOGI("CO2:%u Temp:%f Hum:%f", unit.co2(), unit.temperature(), unit.humidity()); + } +} diff --git a/examples/Basic/SelfUpdate/SelfUpdate.ino b/examples/Basic/SelfUpdate/SelfUpdate.ino new file mode 100644 index 0000000..99f121e --- /dev/null +++ b/examples/Basic/SelfUpdate/SelfUpdate.ino @@ -0,0 +1,9 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +/* + To update the unit yourself usage example (UnitCO2) +*/ +#include "main/SelfUpdate.cpp" diff --git a/examples/Basic/SelfUpdate/main/SelfUpdate.cpp b/examples/Basic/SelfUpdate/main/SelfUpdate.cpp new file mode 100644 index 0000000..4ef3dfe --- /dev/null +++ b/examples/Basic/SelfUpdate/main/SelfUpdate.cpp @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +/* + To update the unit yourself usage example (UnitCO2) + If you use other units, change include files(*1), instances(*2), and get values(*3) +*/ +#include +#include +#include // *1 Include the header of the unit to be used + +m5::unit::UnitUnified Units; +m5::unit::UnitCO2 unit; // *2 Instance of the unit + +void update_task(void*) { + for (;;) { + // If exclusive control is required for access to Wire, insert appropriate controls + unit.update(); // Explicitly call unit.update() yourself + if (unit.updated()) { + // *3 Obtaining unit-specific measurements + M5_LOGI("CO2:%u Temp:%f Hum:%f", unit.co2(), unit.temperature(), unit.humidity()); + } + m5::utility::delay(1); + } +} + +void setup() { + M5.begin(); + + auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); + auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); + M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl); + Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U); + + auto ccfg = unit.component_config(); + ccfg.self_update = true; + unit.component_config(ccfg); + + M5.Display.clear(TFT_DARKGREEN); + if (!Units.add(unit, Wire) // Add unit to UnitUnified manager + || !Units.begin()) { // Begin each unit + M5_LOGE("Failed to add/begin"); + M5.Display.clear(TFT_RED); + return; + } + xTaskCreateUniversal(update_task, "update_task", 8192, nullptr, 1, nullptr, APP_CPU_NUM); +} + +void loop() { + M5.begin(); + Units.update(); // unit.update() is not called within this function + M5_LOGI("loop"); + m5::utility::delay(1000); +} diff --git a/examples/Basic/Simple/Simple.ino b/examples/Basic/Simple/Simple.ino new file mode 100644 index 0000000..ba20efe --- /dev/null +++ b/examples/Basic/Simple/Simple.ino @@ -0,0 +1,13 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +/* + Simple usage example (UnitCO2) +*/ +#include "main/Simple.cpp" + + + + diff --git a/examples/Basic/Simple/main/Simple.cpp b/examples/Basic/Simple/main/Simple.cpp new file mode 100644 index 0000000..f52ebab --- /dev/null +++ b/examples/Basic/Simple/main/Simple.cpp @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +/* + Simple usage example (UnitCO2) + If you use other units, change include files(*1), instances(*2), and get values(*3) +*/ +#include +#include +#include // *1 Include the header of the unit to be used + +m5::unit::UnitUnified Units; +m5::unit::UnitCO2 unit; // *2 Instance of the unit + +void setup() { + M5.begin(); + + auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); + auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); + M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl); + Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U); + + M5.Display.clear(TFT_DARKGREEN); + if (!Units.add(unit, Wire) // Add unit to UnitUnified manager + || !Units.begin()) { // Begin each unit + M5_LOGE("Failed to add/begin"); + M5.Display.clear(TFT_RED); + } +} + +void loop() { + M5.begin(); + Units.update(); + if (unit.updated()) { + // *3 Obtaining unit-specific measurements + M5_LOGI("CO2:%u Temp:%f Hum:%f", unit.co2(), unit.temperature(), unit.humidity()); + } +} diff --git a/examples/demo/MultipleDevices/main/MultipleDevices.cpp b/examples/demo/MultipleDevices/main/MultipleDevices.cpp index 19397f0..0175838 100644 --- a/examples/demo/MultipleDevices/main/MultipleDevices.cpp +++ b/examples/demo/MultipleDevices/main/MultipleDevices.cpp @@ -4,8 +4,7 @@ * SPDX-License-Identifier: MIT */ /* - @brief Demonstration of using M5UnitUnified with multiple devices - @file MultipleDevices.cpp + Demonstration of using M5UnitUnified with multiple devices Required Devices: - Any Core with LCD @@ -353,7 +352,6 @@ void setup() { auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda); auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl); M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl); - Wire.end(); Wire.begin(pin_num_sda, pin_num_scl, 400000U); if (!unitPaHub.add(unitVmeter, 0) /* Connect Vmeter to PaHub2 ch:0 */ diff --git a/platformio.ini b/platformio.ini index 1532a49..5abcbf9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,7 +20,7 @@ lib_deps=m5stack/M5Unified https://github.com/m5stack/M5Unit-GESTURE/#develop https://github.com/m5stack/M5Unit-KMeterISO/#develop https://github.com/m5stack/M5Unit-TVOC/#develop - + ; -------------------------------- [m5base] monitor_speed = 115200 @@ -220,8 +220,7 @@ build_flags = ${env.build_flags} ${option_release.build_flags} lib_deps = ${Paper.lib_deps} ${test_fw.lib_deps} -; - +;examples MultipleDevices [env:MultipleDevices_Core] extends=Core, option_release build_flags = ${env.build_flags} ${option_release.build_flags} @@ -242,3 +241,27 @@ build_flags = ${env.build_flags} ${option_release.build_flags} lib_deps = ${CoreS3.lib_deps} https://github.com/boschsensortec/Bosch-BSEC2-Library.git build_src_filter = +<*> -<.git/> -<.svn/> +<../examples/demo/MultipleDevices> + +;examples Simple +[env:Simple_CoreS3] +extends=CoreS3, option_release +build_flags = ${env.build_flags} ${option_release.build_flags} +lib_deps = ${CoreS3.lib_deps} + https://github.com/boschsensortec/Bosch-BSEC2-Library.git +build_src_filter = +<*> -<.git/> -<.svn/> +<../examples/Basic/Simple> + +;examples SelfUpdate +[env:SelfUpdate_CoreS3] +extends=CoreS3, option_release +build_flags = ${env.build_flags} ${option_release.build_flags} +lib_deps = ${CoreS3.lib_deps} + https://github.com/boschsensortec/Bosch-BSEC2-Library.git +build_src_filter = +<*> -<.git/> -<.svn/> +<../examples/Basic/SelfUpdate> + +;examples ComponentOnly +[env:ComponentOnly_CoreS3] +extends=CoreS3, option_release +build_flags = ${env.build_flags} ${option_release.build_flags} +lib_deps = ${CoreS3.lib_deps} + https://github.com/boschsensortec/Bosch-BSEC2-Library.git +build_src_filter = +<*> -<.git/> -<.svn/> +<../examples/Basic/ComponentOnly>