Skip to content

Commit 8411937

Browse files
committed
Merge branch 'develop'
2 parents e8d58d0 + 767418f commit 8411937

13 files changed

Lines changed: 406 additions & 34 deletions

README.ja.md

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
2-
# M5UnitUnified(α リリース)
1+
# M5UnitUnified
32

43
[English](README.md)
54

65
**M5Stack に色々な M5 ユニットをつないで扱う為の新たなアプローチ**
76
M5Stack シリーズ、 M5Unitシリーズの為のライブラリです。
87

9-
**注意: 現在αバージョンです**
10-
ご意見ご要望などは Issue または PR にてお願いします。
11-
128
## 概要
139
M5UnitUnified は、様々な M5 ユニット製品を統一的に扱うためのライブラリです。
1410

@@ -25,9 +21,8 @@ M5UnitUnified は、様々な M5 ユニット製品を統一的に扱うため
2521
各ユニットの外部ライブラリのライセンスは様々な物が混在しています。
2622
すべてのM5UnitUnifiedおよび関連ライブラリは、[MITライセンス](LICENSE)下にあります。
2723

28-
2924
## インストール方法
30-
アルファ版ですが Arduino/PlatformIO のライブラリマネージャーに登録されています
25+
Arduino/PlatformIO のライブラリマネージャーに登録されています
3126

3227
### ArduinoIDE
3328
1. ライブリマネージャから使用したいユニットのライブラリ (M5Unit-Foo) を選択してください
@@ -177,6 +172,48 @@ void loop() {
177172

178173
```
179174

175+
#### SPI 使用のユニット
176+
177+
```cpp
178+
// 他のユニットを使用する場合、インクルードファイル (*1)、インスタンス (*2)、API呼び出し (*3) を変更する
179+
#include <M5Unified.h>
180+
#include <M5UnitUnified.h>
181+
#include <M5UnitUnifiedFoo.h> // *1 Include the header of the unit to be used
182+
183+
m5::unit::UnitUnified Units;
184+
m5::unit::UnitFoo unit; // *2 使用するユニットのインスタンス
185+
186+
void setup()
187+
{
188+
M5.begin();
189+
190+
if (!SPI.bus()) {
191+
auto spi_sclk = M5.getPin(m5::pin_name_t::sd_spi_sclk);
192+
auto spi_mosi = M5.getPin(m5::pin_name_t::sd_spi_mosi);
193+
auto spi_miso = M5.getPin(m5::pin_name_t::sd_spi_miso);
194+
M5_LOGI("getPin: %d,%d,%d", spi_sclk, spi_mosi, spi_miso);
195+
SPI.begin(spi_sclk, spi_miso, spi_mosi);
196+
}
197+
198+
// 備考: ユニットによって初期化パラメータは異なる
199+
SPISettings settings = {10000000, MSBFIRST, SPI_MODE1};
200+
if (!Units.add(cap, SPI, settings) || !Units.begin()) {
201+
M5_LOGE("Failed to begin");
202+
lcd.fillScreen(TFT_RED);
203+
while (true) {
204+
m5::utility::delay(10000);
205+
}
206+
}
207+
}
208+
209+
void loop() {
210+
M5.update();
211+
Units.update();
212+
// *3 任意の API 呼び出し...
213+
}
214+
```
215+
216+
180217
- 標準外の使い方
181218
- [自分でユニットの更新を行う例](examples/Basic/SelfUpdate)
182219
- [UnitUnified マネージャを使用せず、コンポーネントのみでの例](examples/Basic/ComponentOnly)
@@ -189,10 +226,10 @@ void loop() {
189226
ESP-IDF は将来対応予定です。
190227

191228
### サポートされる通信
192-
- TwoWire による I2C 通信
229+
- Wire TwoWire class による
193230
- GPIO (現在は各ユニットに必要な機能のみ搭載)
194-
195-
UART は将来対応予定です。
231+
- UART HardwareSerial class による
232+
- SPI SPI class による
196233

197234
### サポートされるデバイス,ユニット
198235
[Wiki](https://github.com/m5stack/M5UnitUnified/wiki/)を参照

README.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
**A new approach to connect and handle various M5 units in the M5Stack**
66
Library for M5Stack Series and M5Unit Series
77

8-
**Notice: Now α version**
9-
Please send your comments and requests to Issue or PR.
10-
118
## Overview
129
M5UnitUnified is a library for unified handling of various M5 units products.
1310

@@ -26,7 +23,7 @@ All M5UnitUnified and related libraries are under the [MIT license](LICENSE).
2623

2724

2825
## How to install
29-
Alpha version, but registered in the Library Manager
26+
The library is registered with the library manager.
3027

3128
### Arduino IDE
3229

@@ -175,6 +172,47 @@ void loop() {
175172

176173
```
177174

175+
#### Unit using SPI
176+
177+
```cpp
178+
// If you use other units, change include files(*1), instances(*2), and call any API(*3)
179+
#include <M5Unified.h>
180+
#include <M5UnitUnified.h>
181+
#include <M5UnitUnifiedFoo.h> // *1 Include the header of the unit to be used
182+
183+
m5::unit::UnitUnified Units;
184+
m5::unit::UnitFoo unit; // *2 Instance of the unit
185+
186+
void setup()
187+
{
188+
M5.begin();
189+
190+
if (!SPI.bus()) {
191+
auto spi_sclk = M5.getPin(m5::pin_name_t::sd_spi_sclk);
192+
auto spi_mosi = M5.getPin(m5::pin_name_t::sd_spi_mosi);
193+
auto spi_miso = M5.getPin(m5::pin_name_t::sd_spi_miso);
194+
M5_LOGI("getPin: %d,%d,%d", spi_sclk, spi_mosi, spi_miso);
195+
SPI.begin(spi_sclk, spi_miso, spi_mosi);
196+
}
197+
198+
// Note that the depending on the target unit
199+
SPISettings settings = {10000000, MSBFIRST, SPI_MODE1};
200+
if (!Units.add(cap, SPI, settings) || !Units.begin()) {
201+
M5_LOGE("Failed to begin");
202+
lcd.fillScreen(TFT_RED);
203+
while (true) {
204+
m5::utility::delay(10000);
205+
}
206+
}
207+
}
208+
209+
void loop() {
210+
M5.update();
211+
Units.update();
212+
// *3 Arbitrary API calls to the unit...
213+
}
214+
```
215+
178216
- Nonstandard usage
179217
- [To update the unit yourself usage example](examples/Basic/SelfUpdate)
180218
- [Using only unit component without UnitUnified manager](examples/Basic/ComponentOnly)
@@ -186,9 +224,10 @@ void loop() {
186224
Support ESP-IDF with M5HAL in the future.
187225

188226
### Supported connection
189-
- I2C with TwoWire
227+
- I2C with TwoWire class
190228
- GPIO (Currently only functions required for the units are included)
191-
- UART with HardwareSerial
229+
- UART with HardwareSerial class
230+
- SPI with SPI class
192231

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

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
"url": "https://github.com/m5stack/M5UnitUnified.git"
1212
},
1313
"dependencies": {
14-
"m5stack/M5Utility": "*",
14+
"m5stack/M5Utility": ">=0.0.9",
1515
"m5stack/M5HAL": "*"
1616
},
17-
"version": "0.2.0",
17+
"version": "0.3.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.2.0
2+
version=0.3.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: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ bool Component::canAccessUART() const
5959
return attribute() & attribute::AccessUART;
6060
}
6161

62-
bool Component::add(Component& c, const int16_t ch)
62+
bool Component::canAccessSPI() const
63+
{
64+
return attribute() & attribute::AccessSPI;
65+
}
66+
67+
bool Component::add(Component& c, const int16_t ch16)
6368
{
6469
if (childrenSize() >= _component_cfg.max_children) {
6570
M5_LIB_LOGE("Can't connect any more");
6671
return false;
6772
}
73+
if (ch16 < 0 || ch16 > 255) {
74+
M5_LIB_LOGE("Invalid channel %d", ch16);
75+
return false;
76+
}
77+
78+
const uint8_t ch = static_cast<uint8_t>(ch16);
6879
if (existsChild(ch)) {
6980
M5_LIB_LOGE("Already connected an other unit at channel:%u", ch);
7081
return false;
@@ -134,24 +145,36 @@ bool Component::assign(TwoWire& wire)
134145
{
135146
if (canAccessI2C() && _addr) {
136147
_adapter = std::make_shared<AdapterI2C>(wire, _addr, _component_cfg.clock);
148+
return static_cast<bool>(_adapter);
137149
}
138-
return static_cast<bool>(_adapter);
150+
return false;
139151
}
140152

141153
bool Component::assign(const int8_t rx_pin, const int8_t tx_pin)
142154
{
143155
if (canAccessGPIO()) {
144156
_adapter = std::make_shared<AdapterGPIO>(rx_pin, tx_pin);
157+
return static_cast<bool>(_adapter);
145158
}
146-
return static_cast<bool>(_adapter);
159+
return false;
147160
}
148161

149162
bool Component::assign(HardwareSerial& serial)
150163
{
151164
if (canAccessUART()) {
152165
_adapter = std::make_shared<AdapterUART>(serial);
166+
return static_cast<bool>(_adapter);
153167
}
154-
return static_cast<bool>(_adapter);
168+
return false;
169+
}
170+
171+
bool Component::assign(SPIClass& spi, const SPISettings& settings)
172+
{
173+
if (canAccessSPI()) {
174+
_adapter = std::make_shared<AdapterSPI>(spi, settings, address() /* CS */);
175+
return static_cast<bool>(_adapter);
176+
}
177+
return false;
155178
}
156179

157180
bool Component::selectChannel(const uint8_t ch)

src/M5UnitComponent.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
class TwoWire;
2323
class HardwareSerial;
24+
class SPIClass;
25+
struct SPISettings;
2426

2527
namespace m5 {
2628
namespace unit {
@@ -185,6 +187,7 @@ class Component {
185187
bool canAccessI2C() const;
186188
bool canAccessGPIO() const;
187189
bool canAccessUART() const;
190+
bool canAccessSPI() const;
188191
///@}
189192

190193
///@name Periodic measurement
@@ -227,6 +230,8 @@ class Component {
227230
virtual bool assign(const int8_t rx_pin, const int8_t tx_pin);
228231
/*! @brief Assgin UART */
229232
virtual bool assign(HardwareSerial& serial);
233+
/*! @brief Assgin SPI */
234+
virtual bool assign(SPIClass& spi, const SPISettings& settings);
230235
///@}
231236

232237
///@note For daisy-chaining units such as hubs
@@ -256,13 +261,6 @@ class Component {
256261
{
257262
return _parent;
258263
}
259-
#if 0
260-
//! @brief Gets the parent unit
261-
inline const Component* parent() const
262-
{
263-
return _parent;
264-
}
265-
#endif
266264
//! @brief Gets the device connected to the specified channel
267265
Component* child(const uint8_t chhanle) const;
268266
//! @brief Connect the unit to the specified channel
@@ -343,6 +341,8 @@ class Component {
343341
//! @brief Output information for debug
344342
virtual std::string debugInfo() const;
345343

344+
////// TODO : Split interface (I2C, GPIO, UART, SPI)
345+
346346
// I2C R/W
347347
///@cond 0
348348
m5::hal::error::error_t readWithTransaction(uint8_t* data, const size_t len);

src/M5UnitUnified.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ bool UnitUnified::add(Component& u, HardwareSerial& serial)
9595
return false;
9696
}
9797

98+
bool UnitUnified::add(Component& u, SPIClass& spi, const SPISettings& settings)
99+
{
100+
if (u.isRegistered()) {
101+
M5_LIB_LOGW("Already added");
102+
return false;
103+
}
104+
105+
M5_LIB_LOGD("Add [%s] addr:%02x children:%zu", u.deviceName(), u.address(), u.childrenSize());
106+
107+
u._manager = this;
108+
if (u.assign(spi, settings)) {
109+
u._order = ++_registerCount;
110+
_units.emplace_back(&u);
111+
return add_children(u);
112+
}
113+
M5_LIB_LOGE("Failed to assign %s:%u", u.deviceName(), u.canAccessI2C());
114+
return false;
115+
}
116+
98117
// Add children if exists
99118
bool UnitUnified::add_children(Component& u)
100119
{

src/M5UnitUnified.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
class TwoWire;
2929
class HardwareSerial;
30+
class SPIClass;
31+
struct SPISettings;
3032

3133
/*!
3234
@namespace m5
@@ -87,7 +89,14 @@ class UnitUnified {
8789
@param serial HardwareSerial to be used
8890
@return True if successful
8991
*/
90-
bool add(Component& u, HardwareSerial& wire);
92+
bool add(Component& u, HardwareSerial& serial);
93+
/*!
94+
@brief Adding unit to be managed (SPI)
95+
@param u Unit Component
96+
@param spi SPI to be used
97+
@return True if successful
98+
*/
99+
bool add(Component& u, SPIClass& spi, const SPISettings& settings);
91100
/*!
92101
@brief Adding unit to be managed (M5HAL)
93102
@param u Unit Component

src/m5_unit_component/adapter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
#include "adapter_gpio_v1.hpp"
2121
#endif
2222
#include "adapter_uart.hpp"
23+
#include "adapter_spi.hpp"
2324

2425
#endif

0 commit comments

Comments
 (0)