Skip to content

Commit f735d3f

Browse files
committed
Add reset and play support
1 parent 5dbdb1a commit f735d3f

File tree

7 files changed

+180
-13
lines changed

7 files changed

+180
-13
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
#include "main/ResetAndPlay.cpp"
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
/*
7+
Example: Reset and play using TOF units
8+
*/
9+
#include <M5Unified.h>
10+
#include <M5UnitUnified.h>
11+
#include <M5UnitUnifiedTOF.h>
12+
#include <M5Utility.h>
13+
14+
namespace {
15+
auto& lcd = M5.Display;
16+
m5::unit::UnitUnified Units;
17+
18+
m5::unit::UnitToF unitToF;
19+
m5::unit::UnitToF4M unitToF4M;
20+
21+
m5::unit::UnitToF* pUnitToF;
22+
m5::unit::UnitToF4M* pUnitToF4M;
23+
24+
int16_t lastRange{};
25+
} // namespace
26+
27+
void setup()
28+
{
29+
M5.begin();
30+
31+
if (lcd.height() > lcd.width()) {
32+
lcd.setRotation(1);
33+
}
34+
35+
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
36+
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
37+
Wire.end();
38+
Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U);
39+
40+
// Add all units
41+
if (!Units.add(unitToF, Wire) || !Units.add(unitToF4M, Wire) || !Units.begin(true /* ignore error on begin */)) {
42+
M5_LOGE("Failed to begin");
43+
lcd.fillScreen(TFT_RED);
44+
while (true) m5::utility::delay(1000);
45+
}
46+
M5_LOGI("M5UnitUnified has been begun");
47+
M5_LOGI("%s", Units.debugInfo().c_str());
48+
49+
// Reset and Play!
50+
pUnitToF = Units.get<m5::unit::UnitToF>();
51+
pUnitToF4M = Units.get<m5::unit::UnitToF4M>();
52+
53+
if (pUnitToF) {
54+
M5.Log.printf("Found UnitTOF\n");
55+
lcd.setTextColor(TFT_ORANGE);
56+
} else if (pUnitToF4M) {
57+
M5.Log.printf("Found UnitTOF4M\n");
58+
lcd.setTextColor(TFT_CYAN);
59+
} else {
60+
M5_LOGE("Not exists unit");
61+
lcd.fillScreen(TFT_RED);
62+
while (true) m5::utility::delay(1000);
63+
}
64+
65+
lcd.setFont(&fonts::Orbitron_Light_32);
66+
lcd.setTextDatum(middle_center);
67+
float scale = lcd.width() / (32 * 4.0f);
68+
lcd.setTextSize(scale, scale);
69+
70+
lcd.startWrite();
71+
lcd.clear();
72+
}
73+
74+
void loop()
75+
{
76+
M5.update();
77+
Units.update();
78+
79+
// For UnitToF
80+
if (pUnitToF && pUnitToF->updated()) {
81+
auto range = pUnitToF->range();
82+
if (range >= 0 && range != lastRange) {
83+
lcd.fillRect(0, lcd.height() / 2 - lcd.fontHeight() / 2, lcd.width(), lcd.fontHeight(), TFT_BLACK);
84+
lcd.drawString(m5::utility::formatString("%d", range).c_str(), lcd.width() / 2, lcd.height() / 2);
85+
lastRange = range;
86+
}
87+
}
88+
// For UnitToF4M
89+
if (pUnitToF4M && pUnitToF4M->updated()) {
90+
auto range = pUnitToF4M->range();
91+
if (range >= 0 && range != lastRange) {
92+
lcd.fillRect(0, lcd.height() / 2 - lcd.fontHeight() / 2, lcd.width(), lcd.fontHeight(), TFT_BLACK);
93+
lcd.drawString(m5::utility::formatString("%d", range).c_str(), lcd.width() / 2, lcd.height() / 2);
94+
lastRange = range;
95+
}
96+
}
97+
}

platformio.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,13 @@ build_src_filter = +<*> -<.git/> -<.svn/> +<../examples/demo/MultipleUnits>
351351
extends=CoreS3, option_release, demo
352352
build_src_filter = +<*> -<.git/> -<.svn/> +<../examples/demo/MultipleUnits>
353353

354+
;
355+
[reset_and_play_tof]
356+
lib_deps=${env.lib_deps}
357+
m5stack/M5Unit-TOF
358+
359+
[env:ResetAndPlayTOF_Core]
360+
extends=Core, option_release, reset_and_play_tof
361+
build_src_filter = +<*> -<.git/> -<.svn/> +<../examples/ResetAndPlay>
362+
;lib_deps = ${env.lib_deps}
363+
; https://github.com/m5stack/M5Unit-TOF#develop

src/M5UnitComponent.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,9 @@ bool Component::changeAddress(const uint8_t addr)
271271

272272
std::string Component::debugInfo() const
273273
{
274-
return m5::utility::formatString("[%s]:ID{0X%08x}:ADDR{0X%02x/0X%02x} parent:%u children:%zu", deviceName(),
275-
identifier(), address(), _adapter->address(), hasParent(), childrenSize());
274+
return m5::utility::formatString("%s:[%s]ID{0X%08x}:ADDR{0X%02x/0X%02x} parent:%u children:%zu",
275+
_begun ? "OK" : "NG", deviceName(), identifier(), address(), _adapter->address(),
276+
hasParent(), childrenSize());
276277
}
277278

278279
// Explicit template instantiation

src/M5UnitUnified.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,31 @@ bool UnitUnified::add_children(Component& u)
9999
return true;
100100
}
101101

102-
bool UnitUnified::begin()
102+
bool UnitUnified::begin(const bool resetAndPlay)
103103
{
104-
return !std::any_of(_units.begin(), _units.end(), [](Component* c) {
105-
M5_LIB_LOGV("Try begin:%s", c->deviceName());
106-
bool ret = c->_begun = c->begin();
107-
if (!ret) {
108-
M5_LIB_LOGE("Failed to begin: [%s] ID:{0X%08X} ADDR{0X%02X}", c->deviceName(), c->identifier(),
109-
c->address());
110-
}
111-
return !ret;
104+
bool result{true};
105+
M5_LIB_LOGW("Try begin 1st");
106+
std::for_each(_units.begin(), _units.end(), [&result](Component* c) {
107+
c->_begun = c->begin();
108+
M5_LIB_LOGW(" [%s]:%u", c->deviceName(), c->_begun);
109+
result &= c->_begun;
112110
});
111+
112+
// Reinitialize only successful
113+
// If there is a device with the same address, initialize it again because the previous unit may fall into the above
114+
// state on subsequent initialization
115+
if (resetAndPlay) {
116+
result = true;
117+
M5_LIB_LOGW("Try begin 2nd");
118+
std::for_each(_units.begin(), _units.end(), [&result](Component* c) {
119+
if (c->_begun) {
120+
c->_begun = c->begin();
121+
M5_LIB_LOGW(" [%s]:%u", c->deviceName(), c->_begun);
122+
result &= c->_begun;
123+
}
124+
});
125+
}
126+
return result;
113127
}
114128

115129
void UnitUnified::update(const bool force)

src/M5UnitUnified.hpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,47 @@ class UnitUnified {
6868
bool add(Component& u, TwoWire& wire);
6969
///@}
7070

71-
//! @brief Begin of all units under management
72-
bool begin();
71+
/*!
72+
@brief Begin of all units under management
73+
@param resetAndPlay Use the Reset and play function if true
74+
@return True if successful
75+
@note Reset and play is only available for supported units
76+
*/
77+
bool begin(const bool resetAndPlay = false);
7378
//! @brief Update of all units under management
7479
void update(const bool force = false);
7580

81+
///@name Reset and play
82+
///@{
83+
/*!
84+
@brief Retrieve the specified unit that has been successfully initiated
85+
@tparam T Unit class type
86+
@param prev Obtains the corresponding unit located next to the specified unit,
87+
Get first match if nullptr
88+
@retval pointer Unit
89+
@retval nullptr Not exists
90+
*/
91+
template <class T>
92+
T* get(const T* prev = nullptr)
93+
{
94+
static_assert(std::is_base_of<Component, T>::value, "T muse be derived of Component");
95+
auto it = _units.begin();
96+
if (prev) {
97+
it = std::find(it, _units.end(), prev);
98+
if (it == _units.end()) {
99+
return nullptr;
100+
}
101+
++it;
102+
}
103+
for (; it != _units.end(); ++it) {
104+
Component* u = *it;
105+
if (u->_begun && T::uid == u->identifier() && (T::attr & types::ATTRIBUTE_RESET_AND_PLAY)) {
106+
return static_cast<T*>(u);
107+
}
108+
}
109+
return nullptr;
110+
}
111+
76112
//! @brief Output information for debug
77113
std::string debugInfo() const;
78114

src/m5_unit_component/types.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ using uid_t = uint32_t; //!< @brief Component unique identifier
2424
using attr_t = uint32_t; //!< @brief Component attribute bits
2525
using elapsed_time_t = unsigned long; //!< @brief Elapsed time unit (ms)
2626

27+
28+
///@name Attribute
29+
///@{
30+
constexpr attr_t ATTRIBUTE_RESET_AND_PLAY{0x00000001}; //!< Reset & Play support
31+
///@}
32+
2733
} // namespace types
2834
} // namespace unit
2935
} // namespace m5

0 commit comments

Comments
 (0)