From 94bd0ca3e96afa9a0d77133b0a5f8e2a853512fd Mon Sep 17 00:00:00 2001 From: Kawin Pechetratanapanit <39807451+kawinie@users.noreply.github.com> Date: Fri, 15 May 2026 20:51:50 -0700 Subject: [PATCH] refactor(v2): drop -dev suffix, clean up headers, and additional HW envs - add envs support for HW1.0 and 1.1 - state.h folds into pocketpd.h and events.h - move v1 to src/v1/main.cpp --- .clang-format | 12 +++-- include/v2/events.h | 11 ++++- include/v2/input/button_gesture.h | 2 +- include/v2/input/two_buttons_gesture.h | 2 +- include/v2/pocketpd.h | 46 +++++++++++++++++- include/v2/stages/energy/energy_view.h | 3 +- include/v2/stages/energy_stage.h | 2 +- include/v2/stages/normal/normal_view.h | 2 +- include/v2/stages/normal/pps_mode.h | 1 - include/v2/stages/normal_stage.h | 2 +- include/v2/stages/profile_picker_stage.h | 2 +- include/v2/state.h | 62 ------------------------ include/v2/tasks/button_task.h | 3 +- include/v2/tasks/energy_task.h | 2 +- include/v2/tasks/sensor_task.h | 2 +- platformio.ini | 51 +++++++++++++++++-- src/main.cpp | 20 ++------ src/v1/main.cpp | 10 ++++ test/test_v2_inputs/test.cpp | 2 +- 19 files changed, 134 insertions(+), 103 deletions(-) delete mode 100644 include/v2/state.h create mode 100644 src/v1/main.cpp diff --git a/.clang-format b/.clang-format index 159cd64..96fe37c 100644 --- a/.clang-format +++ b/.clang-format @@ -19,17 +19,19 @@ PackConstructorInitializers: NextLine PointerAlignment: Left ReflowComments: true SpaceAfterCStyleCast: true -# Header include ordering — Google C++ Style Guide -# https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes +# Header include ordering # 1. C system headers (, ) # 2. C++ standard library (, , ) -# 3. Other libraries / project headers (Arduino, third-party, local) +# 3. Third-party libraries (Arduino, third-party) +# 4. Local project headers (local) SortIncludes: CaseSensitive IncludeBlocks: Regroup IncludeCategories: - - Regex: '^<[[:lower:]_/]+\.h>$' + - Regex: '^<(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype|unistd|fcntl|dirent|pthread|sched|semaphore|sys/.+|netinet/.+|arpa/.+)\.h>$' Priority: 1 - Regex: '^<[[:alpha:]_]+>$' Priority: 2 - - Regex: '.*' + - Regex: '^<.+\.h>$' Priority: 3 + - Regex: '.*' + Priority: 4 diff --git a/include/v2/events.h b/include/v2/events.h index 4f01ce3..99376ff 100644 --- a/include/v2/events.h +++ b/include/v2/events.h @@ -8,7 +8,7 @@ #include -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { @@ -64,6 +64,15 @@ namespace pocketpd { int delta = 0; }; + /** + * @brief Latest sensor reading from INA226. + */ + struct SensorSnapshot { + uint32_t timestamp_ms = 0; + uint32_t vbus_mv = 0; + uint32_t current_ma = 0; + }; + /** * @brief Published by SensorTask. Carries one bus reading. */ diff --git a/include/v2/input/button_gesture.h b/include/v2/input/button_gesture.h index 59c7c5d..7bee7df 100644 --- a/include/v2/input/button_gesture.h +++ b/include/v2/input/button_gesture.h @@ -10,7 +10,7 @@ #include #include -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/include/v2/input/two_buttons_gesture.h b/include/v2/input/two_buttons_gesture.h index 6b7f04f..beae201 100644 --- a/include/v2/input/two_buttons_gesture.h +++ b/include/v2/input/two_buttons_gesture.h @@ -10,7 +10,7 @@ #include #include "v2/input/button_gesture.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/include/v2/pocketpd.h b/include/v2/pocketpd.h index 88e4312..e5ee831 100644 --- a/include/v2/pocketpd.h +++ b/include/v2/pocketpd.h @@ -1,6 +1,6 @@ /** * @file pocketpd.h - * @brief PocketPD v2 product-level types and constants. + * @brief PocketPD v2 product-level types, configuration constants, etc. * * Header is kept dependency-free so it can be included from hardware adapters, stages, tasks, and * native tests alike. @@ -54,4 +54,48 @@ namespace pocketpd { */ constexpr uint32_t EEPROM_SAVE_DEBOUNCE_MS = 2000; + // —— Input types + + /** + * @brief Identifier for the three physical buttons plus a synthetic L_R combo. + * `L` and `R` denote the left and right side buttons; `ENCODER` is the rotary push. + */ + enum class ButtonId : uint8_t { + ENCODER, + R, + L, + L_R, + }; + + inline const char* to_string(ButtonId btn_id) { + switch (btn_id) { + case ButtonId::ENCODER: + return "ENCODER_BTN"; + case ButtonId::L: + return "L_BTN"; + case ButtonId::R: + return "R_BTN"; + case ButtonId::L_R: + return "L_R_SYNTHETIC"; + default: + return "UNKNOWN"; + } + } + + enum class Gesture : uint8_t { + SHORT, + LONG, + }; + + inline const char* to_string(Gesture g) { + switch (g) { + case Gesture::SHORT: + return "SHORT"; + case Gesture::LONG: + return "LONG"; + default: + return "UNKNOWN"; + } + } + } // namespace pocketpd diff --git a/include/v2/stages/energy/energy_view.h b/include/v2/stages/energy/energy_view.h index 4af375e..7d34db8 100644 --- a/include/v2/stages/energy/energy_view.h +++ b/include/v2/stages/energy/energy_view.h @@ -11,8 +11,9 @@ #include #include +#include "v2/events.h" #include "v2/images.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/include/v2/stages/energy_stage.h b/include/v2/stages/energy_stage.h index ee8706b..31cea2c 100644 --- a/include/v2/stages/energy_stage.h +++ b/include/v2/stages/energy_stage.h @@ -22,7 +22,7 @@ #include "v2/hal/output_gate.h" #include "v2/images.h" #include "v2/stages/energy/energy_view.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/include/v2/stages/normal/normal_view.h b/include/v2/stages/normal/normal_view.h index 07b252b..52590a8 100644 --- a/include/v2/stages/normal/normal_view.h +++ b/include/v2/stages/normal/normal_view.h @@ -10,9 +10,9 @@ #include #include +#include "v2/events.h" #include "v2/images.h" #include "v2/pocketpd.h" -#include "v2/state.h" namespace pocketpd { diff --git a/include/v2/stages/normal/pps_mode.h b/include/v2/stages/normal/pps_mode.h index e0d570c..4304362 100644 --- a/include/v2/stages/normal/pps_mode.h +++ b/include/v2/stages/normal/pps_mode.h @@ -11,7 +11,6 @@ #include "v2/events.h" #include "v2/hal/pd_sink_controller.h" #include "v2/pocketpd.h" -#include "v2/state.h" namespace pocketpd { diff --git a/include/v2/stages/normal_stage.h b/include/v2/stages/normal_stage.h index 95be060..2cfea50 100644 --- a/include/v2/stages/normal_stage.h +++ b/include/v2/stages/normal_stage.h @@ -17,7 +17,7 @@ #include "v2/stages/normal/fixed_mode.h" #include "v2/stages/normal/normal_view.h" #include "v2/stages/normal/pps_mode.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/include/v2/stages/profile_picker_stage.h b/include/v2/stages/profile_picker_stage.h index 732cf82..6056198 100644 --- a/include/v2/stages/profile_picker_stage.h +++ b/include/v2/stages/profile_picker_stage.h @@ -19,7 +19,7 @@ #include "v2/app.h" #include "v2/events.h" #include "v2/hal/pd_sink_controller.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/include/v2/state.h b/include/v2/state.h deleted file mode 100644 index 9ba08af..0000000 --- a/include/v2/state.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * @file state.h - * @brief Shared input enums + sensor reading struct. - */ -#pragma once - -#include - -namespace pocketpd { - - /** - * @brief Identifier for the three physical buttons plus a synthetic L_R combo. - * `L` and `R` denote the left and right side buttons; `ENCODER` is the rotary push. - */ - enum class ButtonId : uint8_t { - ENCODER, - R, - L, - L_R, - }; - - inline const char* to_string(ButtonId btn_id) { - switch (btn_id) { - case ButtonId::ENCODER: - return "ENCODER_BTN"; - case ButtonId::L: - return "L_BTN"; - case ButtonId::R: - return "R_BTN"; - case ButtonId::L_R: - return "L_R_SYNTHETIC"; - default: - return "UNKNOWN"; - } - } - - enum class Gesture : uint8_t { - SHORT, - LONG, - }; - - inline const char* to_string(Gesture g) { - switch (g) { - case Gesture::SHORT: - return "SHORT"; - case Gesture::LONG: - return "LONG"; - default: - return "UNKNOWN"; - } - } - - /** - * @brief Latest sensor reading from INA226. - */ - struct SensorSnapshot { - uint32_t timestamp_ms = 0; - uint32_t vbus_mv = 0; - uint32_t current_ma = 0; - }; - -} // namespace pocketpd diff --git a/include/v2/tasks/button_task.h b/include/v2/tasks/button_task.h index bee32ee..06b4781 100644 --- a/include/v2/tasks/button_task.h +++ b/include/v2/tasks/button_task.h @@ -14,7 +14,7 @@ #include "v2/events.h" #include "v2/input/button_gesture.h" #include "v2/input/two_buttons_gesture.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { @@ -29,6 +29,7 @@ namespace pocketpd { DetectorRef() = delete; }; + std::array m_detector_refs; TwoButtonsGestureDetector m_combo_detector; diff --git a/include/v2/tasks/energy_task.h b/include/v2/tasks/energy_task.h index c5b2290..6432a3e 100644 --- a/include/v2/tasks/energy_task.h +++ b/include/v2/tasks/energy_task.h @@ -11,7 +11,7 @@ #include "v2/app.h" #include "v2/events.h" #include "v2/hal/output_gate.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/include/v2/tasks/sensor_task.h b/include/v2/tasks/sensor_task.h index 1d79704..232d268 100644 --- a/include/v2/tasks/sensor_task.h +++ b/include/v2/tasks/sensor_task.h @@ -10,7 +10,7 @@ #include "v2/app.h" #include "v2/events.h" #include "v2/hal/power_monitor.h" -#include "v2/state.h" +#include "v2/pocketpd.h" namespace pocketpd { diff --git a/platformio.ini b/platformio.ini index 70b2254..6a6ed15 100644 --- a/platformio.ini +++ b/platformio.ini @@ -34,6 +34,17 @@ lib_deps = mathertel/RotaryEncoder@^1.5.3 khoih-prog/RPI_PICO_TimerInterrupt@^1.3.1 +[env:HW1_0] +extends = pico_base +extra_scripts = pre:scripts/configure_fmt_header_only.py +build_flags = + ${pico_base.build_flags} + -DHW1_0 + -DVERSION="\"1.0.0\"" +build_src_filter = + -<*> + + + [env:HW1_3] extends = pico_base extra_scripts = pre:scripts/configure_fmt_header_only.py @@ -41,11 +52,40 @@ build_flags = ${pico_base.build_flags} -DHW1_3 -DVERSION="\"1.0.0\"" -build_src_filter = - -<*> - + +build_src_filter = + -<*> + +[env:HW1_0_V2] +extends = pico_base +extra_scripts = + pre:scripts/configure_fmt_header_only.py + post:scripts/compiledb_inject_headers.py +build_flags = + ${pico_base.build_flags} + -DHW_VERSION_MAJOR=1 + -DHW_VERSION_MINOR=0 + -DVERSION="\"2.0.0\"" +build_src_filter = + -<*> + + + + + +[env:HW1_1_V2] +extends = pico_base +extra_scripts = + pre:scripts/configure_fmt_header_only.py + post:scripts/compiledb_inject_headers.py +build_flags = + ${pico_base.build_flags} + -DHW_VERSION_MAJOR=1 + -DHW_VERSION_MINOR=1 + -DVERSION="\"2.0.0\"" +build_src_filter = + -<*> + + + + + [env:HW1_3_V2] extends = pico_base extra_scripts = @@ -53,8 +93,9 @@ extra_scripts = post:scripts/compiledb_inject_headers.py build_flags = ${pico_base.build_flags} - -DHW1_3_V2 - -DVERSION="\"2.0.0-dev\"" + -DHW_VERSION_MAJOR=1 + -DHW_VERSION_MINOR=3 + -DVERSION="\"2.0.0\"" build_src_filter = -<*> + diff --git a/src/main.cpp b/src/main.cpp index 6e8ef73..9509905 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ +#include #include - -#ifdef HW1_3_V2 #include +#include #include #include #include @@ -21,12 +21,9 @@ #include "v2/tasks/encoder_task.h" #include "v2/tasks/energy_task.h" #include "v2/tasks/sensor_task.h" -#include -#include namespace { - // —— Hardware adapters ArduinoTwoWireDevice i2c_device_ap33772{Wire, ap33772::ADDRESS}; @@ -47,7 +44,7 @@ namespace { pocketpd::ArduinoClock arduino_clock; pocketpd::ArduinoStreamWriter arduino_stream_writer; pocketpd::ArduinoStreamReader arduino_stream_reader; - + pocketpd::App app(arduino_clock, arduino_stream_writer); // —— Stages @@ -99,14 +96,3 @@ void setup() { void loop() { app.tick(); } -#else -#include - -static StateMachine statemachine; - -void setup() {} - -void loop() { - statemachine.update(); -} -#endif diff --git a/src/v1/main.cpp b/src/v1/main.cpp new file mode 100644 index 0000000..1d0f47a --- /dev/null +++ b/src/v1/main.cpp @@ -0,0 +1,10 @@ +#include +#include "v1/StateMachine.h" + +static StateMachine statemachine; + +void setup() {} + +void loop() { + statemachine.update(); +} diff --git a/test/test_v2_inputs/test.cpp b/test/test_v2_inputs/test.cpp index a4e6156..59103d2 100644 --- a/test/test_v2_inputs/test.cpp +++ b/test/test_v2_inputs/test.cpp @@ -19,7 +19,7 @@ #include "v2/events.h" #include "v2/input/button_gesture.h" #include "v2/input/two_buttons_gesture.h" -#include "v2/state.h" +#include "v2/pocketpd.h" #include "v2/tasks/button_task.h" #include "v2/tasks/encoder_task.h"