
Arduino入門
Stamp-S3Bat 用サンプルプログラムです。
#include <Arduino.h>
#include <M5PM1.h>
#include <Wire.h>
M5PM1 pm1;
#define PIN_SCL 47
#define PIN_SDA 48
static const uint8_t LED_COUNT = 1;
static const uint8_t BRIGHTNESS = 64;
static const m5pm1_rgb_t COLOR_RED = {BRIGHTNESS, 0, 0};
static const m5pm1_rgb_t COLOR_GREEN = {0, BRIGHTNESS, 0};
static const m5pm1_rgb_t COLOR_BLUE = {0, 0, BRIGHTNESS};
void setup()
{
Serial.begin(115200);
delay(2000);
Wire.end();
Wire.begin(PIN_SDA, PIN_SCL, 100000U);
// Initialize PM1
m5pm1_err_t err = pm1.begin(&Wire, M5PM1_DEFAULT_ADDR, PIN_SDA, PIN_SCL, M5PM1_I2C_FREQ_400K);
if (err != M5PM1_OK) {
Serial.printf("[PM1][E] PM1 初期化に失敗しました: %d\r\n", err);
while (true) {
delay(1000);
}
}
Serial.printf("[PM1][I] PM1 初期化に成功しました\r\n");
// 設定プラン1
pm1.gpioSetFunc(M5PM1_GPIO_NUM_0, M5PM1_GPIO_FUNC_OTHER);
pm1.gpioSetDrive(M5PM1_GPIO_NUM_0, M5PM1_GPIO_DRIVE_PUSHPULL);
pm1.gpioSetOutput(M5PM1_GPIO_NUM_0, true);
// 設定プラン2(同等。ライブラリ内部でプラン1に変換されます)
pm1.pinMode(M5PM1_GPIO_NUM_0, M5PM1_OTHER);
// LED出力の設定:出力を有効化 + LED数の設定
m5pm1_err_t err1 = pm1.setLedEnLevel(true);
if (err1 != M5PM1_OK) {
Serial.printf("[PM1][E] LED出力の有効化に失敗しました: %d\r\n", err1);
} else {
Serial.printf("[PM1][I] LED出力を有効化しました\r\n");
}
m5pm1_err_t err2 = pm1.setLedCount(LED_COUNT);
if (err2 != M5PM1_OK) {
Serial.printf("[PM1][E] LED数の設定に失敗しました: %d\r\n", err2);
} else {
Serial.printf("[PM1][I] LED数を %u に設定しました\r\n", LED_COUNT);
Serial.printf("[PM1][I] Fixed LED brightness set to %u/255\r\n", BRIGHTNESS);
}
}
void loop()
{
static uint8_t colorIndex = 0;
m5pm1_rgb_t color = COLOR_RED;
if (colorIndex == 1) {
color = COLOR_GREEN;
} else if (colorIndex == 2) {
color = COLOR_BLUE;
}
m5pm1_err_t err3 = pm1.setLedColor(0, color);
if (err3 != M5PM1_OK) {
Serial.printf("[PM1][E] LED色の設定に失敗しました: %d\r\n", err3);
}
pm1.refreshLeds();
colorIndex = static_cast<uint8_t>((colorIndex + 1) % 3);
delay(500);
}