XIAO/M5Atom/Wio-Terminalでneopixelsを制御する(Arduino版)
2020/10/16
初版
PlatformIO XAIO M5ATOM WIOT neopixels
PlatformIO XAIO M5ATOM WIOT neopixels
概要
XIAO/M5Atom/Wio-Terminalでneopixelsを制御する(Arduino版)。
XIAO/M5Atom/Wio-TerminalはplatformがArduinoなので、それを活かして3つのボードでソースを共通化して動かしてみる。
開発ツールplatformioのインストールについては以下を参照のこと:
・開発ツールPlatformIOをcliで使う(Seeeduino-XIAO版)
・M5Atomを開発ツールPlatformIOで使う(M5Atom/Arduino版)
・M5Atomを開発ツールPlatformIOで使う(Windows10版)
・開発ツールPlatformIOをcli(comand line interface)で使う(v2:Seeeduino-Wio-Terminal/Arduino版)
接続
neopixelsはGrove_Connector(GPIO用)に接続する。
(1)XIAO
XIAOのShield経由でGrove_Connectorに接続する。
(2)M5Atom
M5AtomのGrove_Connectorに接続する。
(3)Wio-Terminal
LCD画面に向かって、下部の右のGrove_Connectorに接続する。
platformio.ini
XIAO用:
; 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:seeed_xiao]
platform = atmelsam
board = seeed_xiao
framework = arduino
build_flags = -DXIAO
upload_protocol = sam-ba
monitor_speed = 115200
lib_deps =
# Accept new functionality in a backwards compatible manner and patches
adafruit/Adafruit NeoPixel @ ^1.6.0
lib_ldf_mode = deep+
M5Atom用:
; 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:m5stick-c]
platform = espressif32
board = m5stick-c
framework = arduino
build_flags = -DM5ATOM
monitor_speed = 115200
lib_deps =
# use M5Atom lib
3113
# use "FastLED"
126
# Accept new functionality in a backwards compatible manner and patches
adafruit/Adafruit NeoPixel @ ^1.6.0
lib_ldf_mode = deep+
Wio-Terminal用:
; 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:seeed_wio_terminal]
platform = atmelsam
board = seeed_wio_terminal
framework = arduino
build_flags = -DWIO_TERMINAL
lib_deps =
adafruit/Adafruit Zero DMA Library@^1.0.8
https://github.com/Seeed-Studio/Seeed_Arduino_SFUD/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_USBDISP/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_LCD/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_FS/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_atWiFi/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_FreeRTOS/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_atUnified/archive/master.zip
https://github.com/Seeed-Studio/esp-at-lib/archive/develop.zip
https://github.com/Seeed-Studio/Seeed_Arduino_mbedtls/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_atWiFiClientSecure/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_atHTTPClient/archive/master.zip
https://github.com/Seeed-Studio/Seeed_Arduino_atWebServer/archive/master.zip
#
# Accept new functionality in a backwards compatible manner and patches
adafruit/Adafruit NeoPixel @ ^1.6.0
lib_ldf_mode = deep+
Wio-Terminalにおいて「adafruit/Adafruit NeoPixel @ ^1.6.0」ライブラリ以外のライブラリは標準的なものとして登録してあるが、
実際には、今回のスケッチで使用していない。
ビルド時に毎回コンパイルされるようなので、ビルド時間短縮のために、使っていないライブラリの登録は削除してもかまわない。
デモ・スケッチ
該当スケッチをダウンロードする:
cd src
wget https://raw.githubusercontent.com/adafruit/Adafruit_NeoPixel/master/examples/simple_new_operator/simple_new_operator.ino
以下のように変更して共通化する:
src/simple_new_operator.ino
#ifdef M5ATOM
#include "M5Atom.h"
#endif
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// Released under the GPLv3 license to match the rest of the
// Adafruit NeoPixel library
// This sketch shows use of the "new" operator with Adafruit_NeoPixel.
// It's helpful if you don't know NeoPixel settings at compile time or
// just want to store this settings in EEPROM or a file on an SD card.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// Which pin on the Arduino is connected to the NeoPixels?
#ifdef XIAO
int pin = 2; // XIAO Sheild Connector
#endif
#ifdef M5ATOM
int pin = 26; // Grove Connector of M5Atom
#endif
#ifdef WIO_TERMINAL
int pin = 1; // Grove Connector(lower right) of wio-terminal
#endif
//int pin = 6; // On Trinket or Gemma, suggest changing this to 1
// How many NeoPixels are attached to the Arduino?
int numPixels = 16; // Popular NeoPixel ring size
// NeoPixel color format & data rate. See the strandtest example for
// information on possible values.
int pixelFormat = NEO_GRB + NEO_KHZ800;
// Rather than declaring the whole NeoPixel object here, we just create
// a pointer for one, which we'll then allocate later...
Adafruit_NeoPixel *pixels;
#define DELAYVAL 100 // Time (in milliseconds) to pause between pixels
//#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
void setup() {
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
// Right about here is where we could read 'pin', 'numPixels' and/or
// 'pixelFormat' from EEPROM or a file on SD or whatever. This is a simple
// example and doesn't do that -- those variables are just set to fixed
// values at the top of this code -- but this is where it would happen.
// Then create a new NeoPixel object dynamically with these values:
pixels = new Adafruit_NeoPixel(numPixels, pin, pixelFormat);
// Going forward from here, code works almost identically to any other
// NeoPixel example, but instead of the dot operator on function calls
// (e.g. pixels.begin()), we instead use pointer indirection (->) like so:
pixels->begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
// You'll see more of this in the loop() function below.
}
void loop() {
pixels->clear(); // Set all pixel colors to 'off'
delay(500);
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for(int i=0; i<numPixels; i++) { // For each pixel...
// pixels->Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
pixels->setPixelColor(i, pixels->Color(0, 150, 0));
pixels->show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL); // Pause before next pass through loop
}
}
ビルド・実行
以下の手順でビルド・実行する:
(platformio.iniは、ボードごとに切り替える)
run pio -t upload
スケッチが起動すると、neopixelsが緑色に光って伸び縮みする。
参考情報
neopixel library:
https://github.com/adafruit/Adafruit_NeoPixel
・M5Stack用NeoPixel互換 LEDテープ 20 cm
・HEX RGB LED Board
・NeoPixel Ring - 12連フルカラーシリアルLED
・Seeeduino XIAO用Grove シールド バッテリー管理チップ 搭載
・XIAO/M5Atomで気圧センサー(HP206C)を動かす(XIAO/Arduino版、M5Atom/Arduino版)
・XIAO/M5AtomでLCD240x240(SPI)を制御する((XIAO/Arduino版、M5Atom/Arduino版)
・M5AtomでOLED128x128(SPI)を制御する(Arduino版)
・XIAOでOLED128x128(SPI)を制御する(Arduino版)
・XIAOでLCD160x80(SPI)を制御する(Arduino版)
以上