@@ -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
183251m5::unit::UnitUnified Units;
184252m5::unit::UnitFoo unit; // *2 使用するユニットのインスタンス
@@ -226,7 +294,9 @@ void loop() {
226294ESP-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 による
0 commit comments