2021/1/9
PlatformIO M5ATOM v2
概要
以下のM5Atomを開発ツールPlatformIOで使う(v2)(M5Atom/Arduino版)
本記事は「
M5Atomを開発ツールPlatformIOで使う(M5Atom/Arduino版)
」を見直して第2版としたものである。
(ホストPCとしてはubuntuを想定している)
ATOM Lite
ATOM Matrix
Peripherals Pin Map
Lite:
Func |
GPIO |
RGB Led(Neo) |
G27 |
Btn |
G39 |
IR |
G12 |
Matrix:
Func |
GPIO |
Neo |
G27 |
Btn |
G39 |
IR |
G12 |
CLK(MPU6886) |
G21 |
SDA(MPU6886) |
GP25 |
Grove Interface
GND |
5V |
G26 |
G32 |
GND |
5V |
SDA |
SCL |
python3 -m venv pio_env
source pio_env/bin/activate
pip3 install platformio
インストール後も、本ツールを使用する場合
同じディレクトリで以下を実行する:
source pio_env/bin/activate
準備
以下を実行して、udevのrulesを登録する:
curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
sudo udevadm control --reload-rules
sudo usermod -a -G dialout $USER
sudo usermod -a -G plugdev $USER
過去に設定してあったとしても、rulesが更新されている場合があるので、再設定したほうが良い。
テスト用プロジェクト sample を作成/実行する
# プロジェクト sample のディレクトリを作成する
mkdir sample
cd sample
# 以下を実行して必要なファイルを作成する
pio init --board m5stick-c
# platformをupdateする
pio platform update
nano platformio.ini
以下にように編集する:
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = m5stick-c
framework = arduino
monitor_speed = 115200
lib_ldf_mode = deep+
lib_deps =
m5stack/M5Atom @ ^0.0.1
fastled/FastLED @ ^3.4.0
jorgen-vikinggod/FastLED GFX Library @ ^0.1.0
nano src/button.ino
以下のように編集する:
#include "M5Atom.h"
uint8_t DisBuff[2 + 5 * 5 * 3];
void setBuff(uint8_t Rdata, uint8_t Gdata, uint8_t Bdata)
{
DisBuff[0] = 0x05;
DisBuff[1] = 0x05;
for (int i = 0; i < 25; i++)
{
DisBuff[2 + i * 3 + 0] = Rdata;
DisBuff[2 + i * 3 + 1] = Gdata;
DisBuff[2 + i * 3 + 2] = Bdata;
}
}
void setup()
{
M5.begin(true, false, true);
delay(10);
setBuff(0xff, 0x00, 0x00);
M5.dis.displaybuff(DisBuff);
}
uint8_t FSM = 0;
void loop()
{
if (M5.Btn.wasPressed())
{
switch (FSM)
{
case 0:
setBuff(0x40, 0x00, 0x00);
break;
case 1:
setBuff(0x00, 0x40, 0x00);
break;
case 2:
setBuff(0x00, 0x00, 0x40);
break;
case 3:
setBuff(0x20, 0x20, 0x20);
break;
default:
break;
}
M5.dis.displaybuff(DisBuff);
FSM++;
if (FSM >= 4)
{
FSM = 0;
}
}
delay(50);
M5.update();
}
続き:
pio run
pio run -t upload
pio run -t nobuild -t upload -v
書き込み後、ボタンを押す度に、LEDの色が変化する。
(本スケッチは、Matrix/Lite兼用になっていて、Matrixの場合、5x5のLEDが同じ色で光る。Liteの場合、一つのLEDが光る)
これ以降、別のプログラムを動かすときは
sampleのディレクトリをまるごと
コピーして別のプロジェクトのディレクトリを作り
そこにプログラム(.ino)を置く。
例:
cp sample m5a_proj01
cd m5a_proj01
...
MPU6886を利用するスケッチ#1(Matrix限定)
src/mpu6886_2.ino
#include "M5Atom.h"
#define SIG_MAX (4096)
uint8_t DisBuff[2 + 5 * 5 * 3];
int16_t adX,adY,adZ;
void setBuffP(uint8_t posData, uint8_t Rdata, uint8_t Gdata, uint8_t Bdata)
{
DisBuff[2 + posData * 3 + 0] = Rdata;
DisBuff[2 + posData * 3 + 1] = Gdata;
DisBuff[2 + posData * 3 + 2] = Bdata;
}
void setBuff(uint8_t Rdata, uint8_t Gdata, uint8_t Bdata)
{
for (uint8_t i = 0; i < 25; i++)
setBuffP(i, Rdata, Gdata, Bdata);
}
void shftBuff()
{
for (uint8_t i = 24; i > 0; i--)
{
for (uint8_t j = 0; j < 3; j++)
DisBuff[2 + i * 3 + j] = DisBuff[2 + (i-1) * 3 + j];
}
}
void setup()
{
DisBuff[0] = 0x05;
DisBuff[1] = 0x05;
M5.begin(false, true, true);
delay(10);
M5.IMU.Init();
setBuff(0x20, 0x20, 0x20);
M5.dis.displaybuff(DisBuff);
Serial.begin(115200);
}
void loop()
{
M5.update();
if (M5.Btn.read()==0)
{
M5.IMU.getAccelAdc(&adX, &adY, &adZ);
int r = min(max((int)map(adX,-SIG_MAX,SIG_MAX,0,255),0),255);
int g = min(max((int)map(adY,-SIG_MAX,SIG_MAX,0,255),0),255);
int b = min(max((int)map(adZ,-SIG_MAX,SIG_MAX,0,255),0),255);
shftBuff();
setBuffP(0,r,g,b);
M5.dis.displaybuff(DisBuff);
Serial.printf("x,y,z: %d,%d,%d\r\n", adX, adY, adZ);
}
delay(20);
}
書き込み実行し、M5Atomを傾けるとそれに応じて5x5のLEDの色が変化する。
MPU6886を利用するスケッチ#2(Matrix限定)
src/mpu6886_3.ino
#include "M5Atom.h"
float accX = 0, accY = 0, accZ = 0;
float gyroX = 0, gyroY = 0, gyroZ = 0;
float temp = 0;
bool IMU6886Flag = false;
void setup()
{
M5.begin(true, false, true);
if (M5.IMU.Init() != 0)
IMU6886Flag = false;
else
IMU6886Flag = true;
}
void loop()
{
if (IMU6886Flag == true)
{
M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ);
M5.IMU.getAccelData(&accX, &accY, &accZ);
M5.IMU.getTempData(&temp);
Serial.printf("%.2f,%.2f,%.2f o/s \r\n", gyroX, gyroY, gyroZ);
Serial.printf("%.2f,%.2f,%.2f mg\r\n", accX * 1000, accY * 1000, accZ * 1000);
Serial.printf("Temperature : %.2f C \r\n", temp);
}
delay(500);
M5.update();
}
スケッチ#3(Matrix限定)
src/Test_GFX.ino
#include "M5Atom.h"
#include <FastLED.h>
#include <FastLED_GFX.h>
#define LED_PIN 27
#define COLOR_ORDER GRB
#define CHIPSET WS2811
#define BRIGHTNESS 64
#define CANVAS_WIDTH 5
#define CANVAS_HEIGHT 5
#define NUM_LEDS (CANVAS_WIDTH * CANVAS_HEIGHT)
GFXcanvas canvas(CANVAS_WIDTH, CANVAS_HEIGHT);
void setup() {
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(canvas.getBuffer(), NUM_LEDS).setCorrection(TypicalSMD5050);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
EVERY_N_SECONDS(5) {
canvas.fillScreen(CRGB::Red);
canvas.drawLine(0, 0, CANVAS_WIDTH-1, CANVAS_HEIGHT-1, CRGB::White);
canvas.drawCircle(2, 2, 2, CRGB(255,255,0));
FastLED.show();
}
}
Adafruit_GFXのライブラリをLED_Matrix(5x5)に適用したデモ・スケッチであり、直線と円を描画している。
スケッチ#4(Matrix限定)
src/Test_DispBuff.ino
#include "M5Atom.h"
#include "hello_img.c"
int ofs = 0;
void setup() {
M5.begin(true, false, true);
}
void loop() {
M5.dis.displaybuff((uint8_t *) image_hello, ofs, 0);
ofs--;
ofs = (ofs == -1) ? image_hello[0] - 1 : ofs;
delay(100);
}
src/hello_img.c
const unsigned char image_hello[977]=
{
/* width 065 */ 0x41,
/* height 005 */ 0x05,
/* Line 000 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, //
/* Line 001 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, //
/* Line 002 */ 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, //
/* Line 003 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, //
/* Line 004 */ 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0xff,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0xff, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0xff,0xff,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, //
};
M5.dis.displaybuffのデモ・スケッチでメッセージをLED_Matrixに表示する。
WiFi対応スケッチ
ESP32のWiFi対応スケッチと互換性があるので、以下をソースの先頭に入れると動作するようだ。
#ifdef M5ATOM
#include "M5Atom.h"
#define ESP32
#endif
したがって、以下のスケッチの先頭に上のコードを入れるとM5Atomでも動作するようになる:
・Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチでWorld Time APIを使う(WorldTimeAPI編)
・Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(HTTP-ACCESS編)
・Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(NTP-CLIENT編)
・Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(REST-API2編)
・Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(OSC編)
・Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(MQTT編)
参考情報
Atom pixel tool
wget https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/software/AtomPixTool.exe
windowsのプログラムだがlinuxのwineでも動作するようだ。
マトリックスのデザインを作成でき、それを保存すると、そのデータのC言語ソースができあがる
Display API Document:
https://github.com/m5stack/M5Atom
サンプル・スケッチ:
git clone https://github.com/m5stack/M5Atom.git
git clone https://github.com/hajimef/m5atom-matrix-samples.git
M5AtomをPlatformIOで動かす-ライブラリインストールから加速度取得まで
PlatformIO Core (CLI)
以上