Skip to content

Commit 88768e6

Browse files
committed
Merge branch 'develop'
2 parents 6b3818b + 8acc346 commit 88768e6

7 files changed

Lines changed: 70 additions & 32 deletions

File tree

.github/workflows/clang-format-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
ref: ${{ github.event.pull_request.head.sha }}
6161

6262
- name: Run clang-format style check for C/C++/Protobuf programs.
63-
uses: jidicula/clang-format-action@v4.10.2 # Using include-regex 10.x or later
63+
uses: jidicula/clang-format-action@v4.16.0 # Using include-regex 10.x or later
6464
with:
6565
clang-format-version: '13'
6666
check-path: ${{ matrix.path['check'] }}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"m5stack/M5Utility": ">=0.0.9",
1515
"m5stack/M5HAL": "*"
1616
},
17-
"version": "0.3.0",
17+
"version": "0.3.1",
1818
"frameworks": [
1919
"arduino"
2020
],

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5UnitUnified
2-
version=0.3.0
2+
version=0.3.1
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=M5UnitUnified is a library for unified handling of various M5 units products. (Alpha version)

src/M5UnitUnified.cpp

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,35 @@ bool UnitUnified::add(Component& u, SPIClass& spi, const SPISettings& settings)
114114
return false;
115115
}
116116

117-
// Add children if exists
117+
// Add children if exists (iterative to avoid stack overflow)
118118
bool UnitUnified::add_children(Component& u)
119119
{
120-
auto it = u.childBegin();
121-
while (it != u.childEnd()) {
122-
auto ch = it->channel();
123-
124-
M5_LIB_LOGV("%s child:%s channel:%u", u.deviceName(), it->deviceName(), ch);
125-
if (it->isRegistered()) {
126-
M5_LIB_LOGE("Already registered %s", it->deviceName());
127-
return false;
120+
std::vector<Component*> stack;
121+
stack.reserve(8);
122+
stack.push_back(&u);
123+
124+
while (!stack.empty()) {
125+
Component* parent = stack.back();
126+
stack.pop_back();
127+
128+
for (auto it = parent->childBegin(); it != parent->childEnd(); ++it) {
129+
auto ch = it->channel();
130+
131+
M5_LIB_LOGV("%s child:%s channel:%u", parent->deviceName(), it->deviceName(), ch);
132+
if (it->isRegistered()) {
133+
M5_LIB_LOGE("Already registered %s", it->deviceName());
134+
return false;
135+
}
136+
it->_manager = this;
137+
it->_adapter = parent->ensure_adapter(ch);
138+
M5_LIB_LOGD(" Shared:%u %u", parent->_adapter.use_count(), it->_adapter.use_count());
139+
it->_order = ++_registerCount;
140+
_units.emplace_back(&*it);
141+
142+
if (it->hasChildren()) {
143+
stack.push_back(&*it);
144+
}
128145
}
129-
it->_manager = this;
130-
it->_adapter = u.ensure_adapter(ch);
131-
M5_LIB_LOGD(" Shared:%u %u", u._adapter.use_count(), it->_adapter.use_count());
132-
it->_order = ++_registerCount;
133-
_units.emplace_back(&*it);
134-
135-
if (!add_children(*it)) {
136-
return false;
137-
}
138-
++it;
139146
}
140147
return true;
141148
}
@@ -175,13 +182,33 @@ std::string UnitUnified::debugInfo() const
175182

176183
std::string UnitUnified::make_unit_info(const Component* u, const uint8_t indent) const
177184
{
178-
std::string s = m5::utility::formatString("%*c%s\n", indent * 4, ' ', u->debugInfo().c_str());
179-
180-
if (u->hasChildren()) {
181-
s += make_unit_info(u->_child, indent + 1);
185+
std::string s;
186+
s.reserve(256);
187+
188+
struct StackEntry {
189+
const Component* node;
190+
uint8_t indent;
191+
};
192+
std::vector<StackEntry> stack;
193+
stack.reserve(16);
194+
stack.push_back({u, indent});
195+
196+
while (!stack.empty()) {
197+
auto entry = stack.back();
198+
stack.pop_back();
199+
200+
s += m5::utility::formatString("%*c%s\n", entry.indent * 4, ' ', entry.node->debugInfo().c_str());
201+
202+
// Push sibling first (processed later = output later)
203+
if (entry.node->_next) {
204+
stack.push_back({entry.node->_next, entry.indent});
205+
}
206+
// Push child second (processed first = output first)
207+
if (entry.node->hasChildren()) {
208+
stack.push_back({entry.node->_child, static_cast<uint8_t>(entry.indent + 1)});
209+
}
182210
}
183-
u = u->_next;
184-
return u ? s += make_unit_info(u, indent) : s;
211+
return s;
185212
}
186213

187214
} // namespace unit

src/m5_unit_component/adapter_gpio.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
#include "adapter_gpio.hpp"
1212
#include <driver/gpio.h>
13+
#include <esp_idf_version.h>
1314

1415
#if defined(M5_UNIT_UNIFIED_USING_RMT_V2)
1516
#pragma message "Using RMT v2,Oneshot"
@@ -19,6 +20,16 @@
1920
#include <driver/adc.h>
2021
#endif
2122

23+
// ADC_ATTEN_DB_12 was introduced in ESP-IDF v4.4.7 / v5.1.3
24+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3) \
25+
|| (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 7) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0))
26+
#pragma message "Exists ADC_ATTEN_DB_12"
27+
constexpr auto M5_ADC_ATTEN_DB = ADC_ATTEN_DB_12;
28+
#else
29+
#pragma message "Not exists ADC_ATTEN_DB_12"
30+
constexpr auto M5_ADC_ATTEN_DB = ADC_ATTEN_DB_11;
31+
#endif
32+
2233
#if defined(SOC_DAC_SUPPORTED) && SOC_DAC_SUPPORTED
2334
#pragma message "DAC supported"
2435

@@ -422,7 +433,7 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_analog(uint16_t& value,
422433
}
423434

424435
adc_oneshot_chan_cfg_t chan_config = {
425-
.atten = ADC_ATTEN_DB_12, // 0~3.3V
436+
.atten = M5_ADC_ATTEN_DB, // 0~3.3V
426437
.bitwidth = ADC_BITWIDTH_DEFAULT // 12bit
427438
};
428439

@@ -454,7 +465,7 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_analog(uint16_t& value,
454465
// ADC1
455466
adc1_channel_t channel = static_cast<adc1_channel_t>(ch);
456467
adc1_config_width(ADC_WIDTH_BIT_12);
457-
adc1_config_channel_atten(channel, ADC_ATTEN_DB_12);
468+
adc1_config_channel_atten(channel, M5_ADC_ATTEN_DB);
458469
value = static_cast<uint16_t>(adc1_get_raw(channel));
459470
return m5::hal::error::error_t::OK;
460471
#endif

src/m5_unit_component/adapter_i2c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ m5::hal::error::error_t AdapterI2C::BusImpl::readWithTransaction(uint8_t* data,
192192
if (acc) {
193193
auto trans = acc.value();
194194
auto result = trans->startRead().and_then([&trans, &data, &len]() {
195-
return trans->read(data, len).and_then([&trans](size_t&&) { return trans->stop(); });
195+
return trans->readLastNack(data, len).and_then([&trans](size_t&&) { return trans->stop(); });
196196
});
197197
// Clean-up must be called
198198
auto eresult = this->_bus->endAccess(std::move(trans));

src/m5_unit_component/adapter_uart.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void AdapterUART::SerialImpl::flush()
3434
void AdapterUART::SerialImpl::flushRX()
3535
{
3636
while (_serial->available()) {
37-
_serial->read(); // Discard
37+
(void)_serial->read(); // Discard
3838
}
3939
}
4040

0 commit comments

Comments
 (0)