Skip to content

Commit 9e1ffe1

Browse files
committed
Merge branch 'develop'
2 parents f48ea06 + d35d0fe commit 9e1ffe1

17 files changed

+474
-17
lines changed

README.ja.md

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ lib_deps=m5stack/M5Unit-foo ;使用したいユニットのライブラリ
4444

4545
## 使い方
4646

47-
各ユニットのレポジトリの例も参照のこと
47+
各ユニットのリポジトリの例も参照のこと
4848

4949
### Unit コンポーネントを UnitUnified とともに使用する (標準的な使用法)
5050

51+
#### Wire 使用のユニット
52+
5153
```cpp
5254
// 他のユニットを使用する場合、インクルードファイル (*1)、インスタンス (*2)、値の取得 (*3) を変更する
5355
#include <M5Unified.h>
@@ -74,15 +76,107 @@ void setup() {
7476
}
7577

7678
void loop() {
79+
M5.update();
80+
Units.update();
81+
if (unit.updated()) {
82+
// *3 ユニット固有の計測値の取得
83+
M5.Log.printf("CO2:%u Temp:%f Hum:%f\n", unit.co2(), unit.temperature(), unit.humidity());
84+
}
85+
}
86+
```
87+
88+
#### GPIO 使用のユニット
89+
90+
```cpp
91+
// 他のユニットを使用する場合、インクルードファイル (*1)、インスタンス (*2)、値の取得 (*3) を変更する
92+
#include <M5Unified.h>
93+
#include <M5UnitUnified.h>
94+
#include <M5UnitUnifiedTUBE.h> // *1 使用するユニットのヘッダ
95+
96+
m5::unit::UnitUnified Units;
97+
m5::unit::UnitTubePressure unit; // *2 使用するユニットのインスタンス
98+
99+
void setup()
100+
{
77101
M5.begin();
102+
103+
// PortB if available, PortA if not
104+
auto pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_b_in);
105+
auto pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_b_out);
106+
if (pin_num_gpio_in < 0 || pin_num_gpio_out < 0) {
107+
M5_LOGW("PortB is not available");
108+
Wire.end();
109+
pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_a_pin1);
110+
pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_a_pin2);
111+
}
112+
113+
if (!Units.add(unit, pin_num_gpio_in, pin_num_gpio_out) // Add unit to UnitUnified manager
114+
|| !Units.begin()) { // Begin each unit
115+
M5_LOGE("Failed to add/begin");
116+
}
117+
}
118+
119+
void loop()
120+
{
121+
M5.update();
78122
Units.update();
79123
if (unit.updated()) {
80124
// *3 ユニット固有の計測値の取得
81-
M5_LOGI("CO2:%u Temp:%f Hum:%f", unit.co2(), unit.temperature(), unit.humidity());
125+
M5.Log.printf("Pressure:%.2f\n", unit.pressure());
82126
}
83127
}
84128
```
85129

130+
#### UART(Serial) 使用のユニット
131+
132+
```cpp
133+
// 他のユニットを使用する場合、インクルードファイル (*1)、インスタンス (*2)、API呼び出し (*3) を変更する
134+
#include <M5Unified.h>
135+
#include <M5UnitUnified.h>
136+
#include <M5UnitUnifiedFINGER.h> // *1 使用するユニットのヘッダ
137+
138+
m5::unit::UnitUnified Units;
139+
m5::unit::UnitFinger unit; // *2 使用するユニットのインスタンス
140+
141+
void setup()
142+
{
143+
M5.begin();
144+
145+
// PortC if available, PortA if not
146+
auto pin_num_in = M5.getPin(m5::pin_name_t::port_c_rxd);
147+
auto pin_num_out = M5.getPin(m5::pin_name_t::port_c_txd);
148+
if (pin_num_in < 0 || pin_num_out < 0) {
149+
M5_LOGW("PortC is not available");
150+
Wire.end();
151+
pin_num_in = M5.getPin(m5::pin_name_t::port_a_pin1);
152+
pin_num_out = M5.getPin(m5::pin_name_t::port_a_pin2);
153+
}
154+
155+
#if SOC_UART_NUM > 2
156+
auto& s = Serial2;
157+
#elif SOC_UART_NUM > 1
158+
auto& s = Serial1;
159+
#else
160+
#error "Not enough Serial"
161+
#endif
162+
s.end();
163+
// 備考: ユニットによって初期化パラメータは異なる
164+
s.begin(19200, SERIAL_8N1, pin_num_in, pin_num_out);
165+
166+
if (!Units.add(unit, s) // Add unit to UnitUnified manager
167+
|| !Units.begin()) { // Begin each unit
168+
M5_LOGE("Failed to begin");
169+
}
170+
}
171+
172+
void loop() {
173+
M5.update();
174+
Units.update();
175+
// *3 任意の API 呼び出し...
176+
}
177+
178+
```
179+
86180
- 標準外の使い方
87181
- [自分でユニットの更新を行う例](examples/Basic/SelfUpdate)
88182
- [UnitUnified マネージャを使用せず、コンポーネントのみでの例](examples/Basic/ComponentOnly)

README.md

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ See also examples for each unit repositry too.
4848

4949
### UnitComponent with UnitUnified (Standard usage)
5050

51-
Simple example of the UnitCO2
52-
UnitCO2 is started with default settings in Units.begin(), and loop() print logs measurement data.
53-
51+
#### Unit using Wire
5452
```cpp
5553
// If you use other units, change include files(*1), instances(*2), and get values(*3)
5654
#include <M5Unified.h>
@@ -66,24 +64,115 @@ void setup() {
6664
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
6765
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
6866
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
67+
Wire.end();
6968
Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U);
7069

71-
M5.Display.clear(TFT_DARKGREEN);
7270
if (!Units.add(unit, Wire) // Add unit to UnitUnified manager
7371
|| !Units.begin()) { // Begin each unit
7472
M5_LOGE("Failed to add/begin");
75-
M5.Display.clear(TFT_RED);
7673
}
7774
}
7875

7976
void loop() {
77+
M5.update();
78+
Units.update();
79+
if (unit.updated()) {
80+
// *3 Obtaining unit-specific measurements
81+
M5.Log.printf("CO2:%u Temp:%f Hum:%f\n", unit.co2(), unit.temperature(), unit.humidity());
82+
}
83+
}
84+
```
85+
86+
#### Unit using GPIO
87+
88+
```cpp
89+
// If you use other units, change include files(*1), instances(*2), and get values(*3)
90+
#include <M5Unified.h>
91+
#include <M5UnitUnified.h>
92+
#include <M5UnitUnifiedTUBE.h> // *1 Include the header of the unit to be used
93+
94+
m5::unit::UnitUnified Units;
95+
m5::unit::UnitTubePressure unit; // *2 Instance of the unit
96+
97+
void setup()
98+
{
8099
M5.begin();
100+
101+
// PortB if available, PortA if not
102+
auto pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_b_in);
103+
auto pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_b_out);
104+
if (pin_num_gpio_in < 0 || pin_num_gpio_out < 0) {
105+
M5_LOGW("PortB is not available");
106+
Wire.end();
107+
pin_num_gpio_in = M5.getPin(m5::pin_name_t::port_a_pin1);
108+
pin_num_gpio_out = M5.getPin(m5::pin_name_t::port_a_pin2);
109+
}
110+
111+
if (!Units.add(unit, pin_num_gpio_in, pin_num_gpio_out) // Add unit to UnitUnified manager
112+
|| !Units.begin()) { // Begin each unit
113+
M5_LOGE("Failed to add/begin");
114+
}
115+
}
116+
117+
void loop()
118+
{
119+
M5.update();
81120
Units.update();
82121
if (unit.updated()) {
83122
// *3 Obtaining unit-specific measurements
84-
M5_LOGI("CO2:%u Temp:%f Hum:%f", unit.co2(), unit.temperature(), unit.humidity());
123+
M5.Log.printf("Pressure:%.2f\n", unit.pressure());
124+
}
125+
}
126+
```
127+
128+
#### Unit using UART(Serial)
129+
130+
```cpp
131+
// If you use other units, change include files(*1), instances(*2), and call any API(*3)
132+
#include <M5Unified.h>
133+
#include <M5UnitUnified.h>
134+
#include <M5UnitUnifiedFINGER.h> // *1 Include the header of the unit to be used
135+
136+
m5::unit::UnitUnified Units;
137+
m5::unit::UnitFinger unit; // *2 Instance of the unit
138+
139+
void setup()
140+
{
141+
M5.begin();
142+
143+
// PortC if available, PortA if not
144+
auto pin_num_in = M5.getPin(m5::pin_name_t::port_c_rxd);
145+
auto pin_num_out = M5.getPin(m5::pin_name_t::port_c_txd);
146+
if (pin_num_in < 0 || pin_num_out < 0) {
147+
M5_LOGW("PortC is not available");
148+
Wire.end();
149+
pin_num_in = M5.getPin(m5::pin_name_t::port_a_pin1);
150+
pin_num_out = M5.getPin(m5::pin_name_t::port_a_pin2);
151+
}
152+
153+
#if SOC_UART_NUM > 2
154+
auto& s = Serial2;
155+
#elif SOC_UART_NUM > 1
156+
auto& s = Serial1;
157+
#else
158+
#error "Not enough Serial"
159+
#endif
160+
s.end();
161+
// Note that the argument varies depending on the target unit
162+
s.begin(19200, SERIAL_8N1, pin_num_in, pin_num_out);
163+
164+
if (!Units.add(unit, s) // Add unit to UnitUnified manager
165+
|| !Units.begin()) { // Begin each unit
166+
M5_LOGE("Failed to begin");
85167
}
86168
}
169+
170+
void loop() {
171+
M5.update();
172+
Units.update();
173+
// *3 Arbitrary API calls to the unit...
174+
}
175+
87176
```
88177

89178
- Nonstandard usage
@@ -99,8 +188,7 @@ Support ESP-IDF with M5HAL in the future.
99188
### Supported connection
100189
- I2C with TwoWire
101190
- GPIO (Currently only functions required for the units are included)
102-
103-
Support UART in the future.
191+
- UART with HardwareSerial
104192

105193
### Supported devices, units
106194
See also [Wiki](https://github.com/m5stack/M5UnitUnified/wiki/)

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"m5stack/M5Utility": "*",
1515
"m5stack/M5HAL": "*"
1616
},
17-
"version": "0.1.6",
17+
"version": "0.2.0",
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.1.6
2+
version=0.2.0
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=M5UnitUnified is a library for unified handling of various M5 units products. (Alpha version)

src/M5UnitComponent.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ bool Component::canAccessGPIO() const
5454
return attribute() & attribute::AccessGPIO;
5555
}
5656

57+
bool Component::canAccessUART() const
58+
{
59+
return attribute() & attribute::AccessUART;
60+
}
61+
5762
bool Component::add(Component& c, const int16_t ch)
5863
{
5964
if (childrenSize() >= _component_cfg.max_children) {
@@ -141,6 +146,14 @@ bool Component::assign(const int8_t rx_pin, const int8_t tx_pin)
141146
return static_cast<bool>(_adapter);
142147
}
143148

149+
bool Component::assign(HardwareSerial& serial)
150+
{
151+
if (canAccessUART()) {
152+
_adapter = std::make_shared<AdapterUART>(serial);
153+
}
154+
return static_cast<bool>(_adapter);
155+
}
156+
144157
bool Component::selectChannel(const uint8_t ch)
145158
{
146159
bool ret{true};

src/M5UnitComponent.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <memory>
2121

2222
class TwoWire;
23+
class HardwareSerial;
2324

2425
namespace m5 {
2526
namespace unit {
@@ -183,6 +184,7 @@ class Component {
183184
///@{
184185
bool canAccessI2C() const;
185186
bool canAccessGPIO() const;
187+
bool canAccessUART() const;
186188
///@}
187189

188190
///@name Periodic measurement
@@ -223,6 +225,8 @@ class Component {
223225
virtual bool assign(TwoWire& wire);
224226
/*! @brief Assgin GPIO */
225227
virtual bool assign(const int8_t rx_pin, const int8_t tx_pin);
228+
/*! @brief Assgin UART */
229+
virtual bool assign(HardwareSerial& serial);
226230
///@}
227231

228232
///@note For daisy-chaining units such as hubs

src/M5UnitUnified.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ bool UnitUnified::add(Component& u, const int8_t rx_pin, const int8_t tx_pin)
7676
return false;
7777
}
7878

79+
bool UnitUnified::add(Component& u, HardwareSerial& serial)
80+
{
81+
if (u.isRegistered()) {
82+
M5_LIB_LOGW("Already added");
83+
return false;
84+
}
85+
86+
M5_LIB_LOGD("Add [%s] addr:%02x children:%zu", u.deviceName(), u.address(), u.childrenSize());
87+
88+
u._manager = this;
89+
if (u.assign(serial)) {
90+
u._order = ++_registerCount;
91+
_units.emplace_back(&u);
92+
return add_children(u);
93+
}
94+
M5_LIB_LOGE("Failed to assign %s:%u", u.deviceName(), u.canAccessI2C());
95+
return false;
96+
}
97+
7998
// Add children if exists
8099
bool UnitUnified::add_children(Component& u)
81100
{

src/M5UnitUnified.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string>
2727

2828
class TwoWire;
29+
class HardwareSerial;
2930

3031
/*!
3132
@namespace m5
@@ -80,6 +81,13 @@ class UnitUnified {
8081
@return True if successful
8182
*/
8283
bool add(Component& u, const int8_t rx_pin, const int8_t tx_pin);
84+
/*!
85+
@brief Adding unit to be managed (UART)
86+
@param u Unit Component
87+
@param serial HardwareSerial to be used
88+
@return True if successful
89+
*/
90+
bool add(Component& u, HardwareSerial& wire);
8391
/*!
8492
@brief Adding unit to be managed (M5HAL)
8593
@param u Unit Component

src/googletest/test_helper.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ uint32_t test_periodic_measurement(U* unit, const uint32_t times, const uint32_t
5050
std::this_thread::yield();
5151
}
5252

53+
// M5_LOGI("AVG:%u avgCnt:%u", avg, avgCnt);
54+
5355
if (!skip_after_test) {
5456
EXPECT_EQ(cnt, 0U);
5557
EXPECT_EQ(avgCnt, times - 1);

0 commit comments

Comments
 (0)