Skip to content

Commit 5290cc8

Browse files
committed
Add I2C_Class and M5HAL Bus usage example
1 parent fe14610 commit 5290cc8

2 files changed

Lines changed: 145 additions & 4 deletions

File tree

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

0 commit comments

Comments
 (0)