Skip to content

Commit cb8c468

Browse files
committed
Merge branch 'develop'
2 parents 5172084 + 501ead4 commit cb8c468

12 files changed

Lines changed: 662 additions & 143 deletions

README.ja.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,81 @@ void setup() {
6060
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
6161
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
6262
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
63+
Wire.end();
6364
Wire.begin(pin_num_sda, pin_num_scl, 400 * 1000U);
6465

65-
M5.Display.clear(TFT_DARKGREEN);
6666
if (!Units.add(unit, Wire) // unit を UnitUnified マネージャへ追加
6767
|| !Units.begin()) { // ユニットの始動
6868
M5_LOGE("Failed to add/begin");
69-
M5.Display.clear(TFT_RED);
69+
}
70+
}
71+
72+
void loop() {
73+
M5.update();
74+
Units.update();
75+
if (unit.updated()) {
76+
// *3 ユニット固有の計測値の取得
77+
M5.Log.printf("CO2:%u Temp:%f Hum:%f\n", unit.co2(), unit.temperature(), unit.humidity());
78+
}
79+
}
80+
```
81+
82+
#### I2C_Class (M5Unified 内部 I2C) 使用のユニット
83+
```cpp
84+
// M5Paper 内蔵 SHT30 センサを M5Unified の In_I2C 経由で読み取る例
85+
#include <M5Unified.h>
86+
#include <M5UnitUnified.h>
87+
#include <M5UnitUnifiedENV.h> // *1 使用するユニットのヘッダ
88+
89+
m5::unit::UnitUnified Units;
90+
m5::unit::UnitSHT30 unit; // *2 使用するユニットのインスタンス
91+
92+
void setup() {
93+
M5.begin();
94+
95+
// M5Unified の In_I2C(内部 I2C バス)を使用
96+
// ピンや周波数の手動設定は不要
97+
if (!Units.add(unit, M5.In_I2C) // I2C_Class を使用してユニットを追加
98+
|| !Units.begin()) {
99+
M5_LOGE("Failed to add/begin");
100+
}
101+
}
102+
103+
void loop() {
104+
M5.update();
105+
Units.update();
106+
if (unit.updated()) {
107+
// *3 ユニット固有の計測値の取得
108+
M5.Log.printf("Temp:%f Hum:%f\n", unit.temperature(), unit.humidity());
109+
}
110+
}
111+
```
112+
113+
#### M5HAL Bus (SoftwareI2C) 使用のユニット
114+
```cpp
115+
// 他のユニットを使用する場合、インクルードファイル (*1)、インスタンス (*2)、値の取得 (*3) を変更する
116+
#include <M5Unified.h>
117+
#include <M5UnitUnified.h>
118+
#include <M5UnitUnifiedENV.h> // *1 使用するユニットのヘッダ
119+
#include <M5HAL.hpp>
120+
121+
m5::unit::UnitUnified Units;
122+
m5::unit::UnitCO2 unit; // *2 使用するユニットのインスタンス
123+
124+
void setup() {
125+
M5.begin();
126+
127+
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
128+
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
129+
130+
m5::hal::bus::I2CBusConfig i2c_cfg;
131+
i2c_cfg.pin_sda = m5::hal::gpio::getPin(pin_num_sda);
132+
i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
133+
auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
134+
135+
if (!Units.add(unit, i2c_bus ? i2c_bus.value() : nullptr) // M5HAL Bus を使用してユニットを追加
136+
|| !Units.begin()) {
137+
M5_LOGE("Failed to add/begin");
70138
}
71139
}
72140

@@ -178,7 +246,7 @@ void loop() {
178246
// 他のユニットを使用する場合、インクルードファイル (*1)、インスタンス (*2)、API呼び出し (*3) を変更する
179247
#include <M5Unified.h>
180248
#include <M5UnitUnified.h>
181-
#include <M5UnitUnifiedFoo.h> // *1 Include the header of the unit to be used
249+
#include <M5UnitUnifiedFoo.h> // *1 使用するユニットのヘッダ
182250

183251
m5::unit::UnitUnified Units;
184252
m5::unit::UnitFoo unit; // *2 使用するユニットのインスタンス
@@ -226,7 +294,9 @@ void loop() {
226294
ESP-IDF は将来対応予定です。
227295

228296
### サポートされる通信
229-
- Wire TwoWire class による
297+
- I2C TwoWire class による
298+
- I2C I2C_Class (M5Unified In_I2C/Ex_I2C) による
299+
- I2C M5HAL Bus (SoftwareI2C を含む) による
230300
- GPIO (現在は各ユニットに必要な機能のみ搭載)
231301
- UART HardwareSerial class による
232302
- SPI SPI class による

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,75 @@ void loop() {
8080
}
8181
```
8282

83+
#### Unit using I2C_Class (M5Unified internal I2C)
84+
```cpp
85+
// Example: Reading the SHT30 sensor built into M5Paper via M5Unified's In_I2C
86+
#include <M5Unified.h>
87+
#include <M5UnitUnified.h>
88+
#include <M5UnitUnifiedENV.h> // *1 Include the header of the unit to be used
89+
90+
m5::unit::UnitUnified Units;
91+
m5::unit::UnitSHT30 unit; // *2 Instance of the unit
92+
93+
void setup() {
94+
M5.begin();
95+
96+
// Use M5Unified's In_I2C (internal I2C bus)
97+
// No manual pin/frequency configuration needed
98+
if (!Units.add(unit, M5.In_I2C) // Add unit using I2C_Class
99+
|| !Units.begin()) {
100+
M5_LOGE("Failed to add/begin");
101+
}
102+
}
103+
104+
void loop() {
105+
M5.update();
106+
Units.update();
107+
if (unit.updated()) {
108+
// *3 Obtaining unit-specific measurements
109+
M5.Log.printf("Temp:%f Hum:%f\n", unit.temperature(), unit.humidity());
110+
}
111+
}
112+
```
113+
114+
#### Unit using M5HAL Bus (SoftwareI2C)
115+
```cpp
116+
// If you use other units, change include files(*1), instances(*2), and get values(*3)
117+
#include <M5Unified.h>
118+
#include <M5UnitUnified.h>
119+
#include <M5UnitUnifiedENV.h> // *1 Include the header of the unit to be used
120+
#include <M5HAL.hpp>
121+
122+
m5::unit::UnitUnified Units;
123+
m5::unit::UnitCO2 unit; // *2 Instance of the unit
124+
125+
void setup() {
126+
M5.begin();
127+
128+
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
129+
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
130+
131+
m5::hal::bus::I2CBusConfig i2c_cfg;
132+
i2c_cfg.pin_sda = m5::hal::gpio::getPin(pin_num_sda);
133+
i2c_cfg.pin_scl = m5::hal::gpio::getPin(pin_num_scl);
134+
auto i2c_bus = m5::hal::bus::i2c::getBus(i2c_cfg);
135+
136+
if (!Units.add(unit, i2c_bus ? i2c_bus.value() : nullptr) // Add unit using M5HAL Bus
137+
|| !Units.begin()) {
138+
M5_LOGE("Failed to add/begin");
139+
}
140+
}
141+
142+
void loop() {
143+
M5.update();
144+
Units.update();
145+
if (unit.updated()) {
146+
// *3 Obtaining unit-specific measurements
147+
M5.Log.printf("CO2:%u Temp:%f Hum:%f\n", unit.co2(), unit.temperature(), unit.humidity());
148+
}
149+
}
150+
```
151+
83152
#### Unit using GPIO
84153

85154
```cpp
@@ -225,6 +294,8 @@ Support ESP-IDF with M5HAL in the future.
225294

226295
### Supported connection
227296
- I2C with TwoWire class
297+
- I2C with I2C_Class (M5Unified In_I2C/Ex_I2C)
298+
- I2C with M5HAL Bus (including SoftwareI2C)
228299
- GPIO (Currently only functions required for the units are included)
229300
- UART with HardwareSerial class
230301
- SPI with SPI class

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.10",
1515
"m5stack/M5HAL": "*"
1616
},
17-
"version": "0.3.2",
17+
"version": "0.4.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.3.2
2+
version=0.4.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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ bool Component::assign(TwoWire& wire)
150150
return false;
151151
}
152152

153+
bool Component::assign(m5::I2C_Class& i2c)
154+
{
155+
if (canAccessI2C() && _addr) {
156+
_adapter = std::make_shared<AdapterI2C>(i2c, _addr, _component_cfg.clock);
157+
return static_cast<bool>(_adapter);
158+
}
159+
return false;
160+
}
161+
153162
bool Component::assign(const int8_t rx_pin, const int8_t tx_pin)
154163
{
155164
if (canAccessGPIO()) {

src/M5UnitComponent.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SPIClass;
2525
struct SPISettings;
2626

2727
namespace m5 {
28+
class I2C_Class;
2829
namespace unit {
2930

3031
class UnitUnified;
@@ -226,6 +227,8 @@ class Component {
226227
virtual bool assign(m5::hal::bus::Bus* bus);
227228
/*! @brief Assgin TwoWire */
228229
virtual bool assign(TwoWire& wire);
230+
/*! @brief Assign I2C_Class */
231+
virtual bool assign(m5::I2C_Class& i2c);
229232
/*! @brief Assgin GPIO */
230233
virtual bool assign(const int8_t rx_pin, const int8_t tx_pin);
231234
/*! @brief Assgin UART */

src/M5UnitUnified.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ bool UnitUnified::add(Component& u, TwoWire& wire)
5757
return false;
5858
}
5959

60+
bool UnitUnified::add(Component& u, m5::I2C_Class& i2c)
61+
{
62+
if (u.isRegistered()) {
63+
M5_LIB_LOGW("Already added");
64+
return false;
65+
}
66+
67+
M5_LIB_LOGD("Add [%s] addr:%02x children:%zu", u.deviceName(), u.address(), u.childrenSize());
68+
69+
u._manager = this;
70+
if (u.assign(i2c)) {
71+
u._order = ++_registerCount;
72+
_units.emplace_back(&u);
73+
return add_children(u);
74+
}
75+
M5_LIB_LOGE("Failed to assign %s:%u", u.deviceName(), u.canAccessI2C());
76+
return false;
77+
}
78+
6079
bool UnitUnified::add(Component& u, const int8_t rx_pin, const int8_t tx_pin)
6180
{
6281
if (u.isRegistered()) {

src/M5UnitUnified.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct SPISettings;
3535
@brief Top level namespace of M5stack
3636
*/
3737
namespace m5 {
38+
class I2C_Class;
3839
/*!
3940
@namespace unit
4041
@brief Unit-related namespace
@@ -104,6 +105,13 @@ class UnitUnified {
104105
@return True if successful
105106
*/
106107
bool add(Component& u, m5::hal::bus::Bus* bus);
108+
/*!
109+
@brief Adding unit to be managed (I2C_Class)
110+
@param u Unit Component
111+
@param i2c I2C_Class to be used (e.g. M5.In_I2C)
112+
@return True if successful
113+
*/
114+
bool add(Component& u, m5::I2C_Class& i2c);
107115
///@}
108116

109117
//! @brief Begin of all units under management

0 commit comments

Comments
 (0)