NeoPixcel モジュール化

This commit is contained in:
Koichi Ito 2026-09-03 09:55:45 +09:00
parent 3889336829
commit 48cf4020c3
3 changed files with 88 additions and 57 deletions

View File

@ -0,0 +1,63 @@
#include "NeoPixelController.h"
#include <Adafruit_NeoPixel.h>
namespace {
constexpr uint8_t kPin = 18;
constexpr uint16_t kCount = 1;
constexpr uint32_t kColors[] = {
0xFF0000, 0xFF8000, 0xFFFF00, 0x00FF00,
0x0000FF, 0x4B0082, 0x8F00FF
};
constexpr uint8_t kColorCount = sizeof(kColors) / sizeof(kColors[0]);
Adafruit_NeoPixel pixels(kCount, kPin, NEO_GRB + NEO_KHZ800);
}
void NeoPixelController::begin() {
pixels.begin();
pixels.setBrightness(255);
pixels.setPixelColor(0, kColors[kStoppedColorIndex]);
pixels.show();
}
void NeoPixelController::setPlaying(bool playing) {
isPlaying_ = playing;
if (isPlaying_) {
transitionStart_ = millis();
lastUpdate_ = transitionStart_;
return;
}
colorIndex_ = kStoppedColorIndex;
pixels.setPixelColor(0, kColors[colorIndex_]);
pixels.show();
}
void NeoPixelController::update() {
if (!isPlaying_ || millis() - lastUpdate_ < kUpdateIntervalMs) {
return;
}
unsigned long now = millis();
lastUpdate_ = now;
unsigned long transitionElapsed = now - transitionStart_;
if (transitionElapsed >= kTransitionMs) {
colorIndex_ = (colorIndex_ + transitionElapsed / kTransitionMs) % kColorCount;
transitionStart_ = now;
transitionElapsed = 0;
}
uint8_t nextColorIndex = (colorIndex_ + 1) % kColorCount;
uint32_t currentColor = kColors[colorIndex_];
uint32_t nextColor = kColors[nextColorIndex];
uint8_t red = ((currentColor >> 16) * (kTransitionMs - transitionElapsed)
+ (nextColor >> 16) * transitionElapsed) / kTransitionMs;
uint8_t green = (((currentColor >> 8) & 0xFF) * (kTransitionMs - transitionElapsed)
+ ((nextColor >> 8) & 0xFF) * transitionElapsed) / kTransitionMs;
uint8_t blue = ((currentColor & 0xFF) * (kTransitionMs - transitionElapsed)
+ (nextColor & 0xFF) * transitionElapsed) / kTransitionMs;
pixels.setPixelColor(0, red, green, blue);
pixels.show();
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <Arduino.h>
class NeoPixelController {
public:
void begin();
void setPlaying(bool playing);
void update();
private:
static constexpr uint8_t kStoppedColorIndex = 3;
static constexpr unsigned long kUpdateIntervalMs = 30;
static constexpr unsigned long kTransitionMs = 3000;
bool isPlaying_ = false;
uint8_t colorIndex_ = 0;
unsigned long lastUpdate_ = 0;
unsigned long transitionStart_ = 0;
};

View File

@ -1,28 +1,13 @@
#include "BluetoothA2DPSink.h" #include "BluetoothA2DPSink.h"
#include <Wire.h> #include <Wire.h>
#include <U8g2lib.h> #include <U8g2lib.h>
#include <Adafruit_NeoPixel.h> #include "NeoPixelController.h"
// WS2812B設定
constexpr uint8_t WS2812B_PIN = 18;
constexpr uint16_t WS2812B_COUNT = 1;
Adafruit_NeoPixel ws2812b(WS2812B_COUNT, WS2812B_PIN, NEO_GRB + NEO_KHZ800);
const uint32_t ws2812bColors[] = {
0xFF0000, 0xFF8000, 0xFFFF00, 0x00FF00,
0x0000FF, 0x4B0082, 0x8F00FF
};
constexpr uint8_t WS2812B_COLOR_COUNT = sizeof(ws2812bColors) / sizeof(ws2812bColors[0]);
constexpr unsigned long WS2812B_INTERVAL_MS = 30;
constexpr unsigned long WS2812B_TRANSITION_MS = 3000;
bool isPlaying = false;
uint8_t ws2812bColorIndex = 0;
unsigned long lastWs2812bUpdate = 0;
unsigned long ws2812bTransitionStart = 0;
// I2C接続のOLED設定 (SSD1306 128x64) // I2C接続のOLED設定 (SSD1306 128x64)
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
BluetoothA2DPSink a2dp_sink; BluetoothA2DPSink a2dp_sink;
NeoPixelController neoPixelController;
// 表示用変数 // 表示用変数
String currentTitle = "未接続"; String currentTitle = "未接続";
@ -92,28 +77,13 @@ void volume_changed_callback(int volume) {
} }
void audio_state_changed_callback(esp_a2d_audio_state_t state, void *) { void audio_state_changed_callback(esp_a2d_audio_state_t state, void *) {
isPlaying = (state == ESP_A2D_AUDIO_STATE_STARTED); neoPixelController.setPlaying(state == ESP_A2D_AUDIO_STATE_STARTED);
if (isPlaying) {
ws2812bTransitionStart = millis();
lastWs2812bUpdate = ws2812bTransitionStart;
}
if (!isPlaying) {
ws2812bColorIndex = 3;
ws2812b.setPixelColor(0, ws2812bColors[ws2812bColorIndex]);
ws2812b.show();
}
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
// WS2812Bを緑で点灯 neoPixelController.begin();
ws2812b.begin();
ws2812b.setBrightness(255);
ws2812b.setPixelColor(0, ws2812bColors[3]);
ws2812b.show();
// SDAをGPIO 19、SCLをGPIO 32 に設定 // SDAをGPIO 19、SCLをGPIO 32 に設定
// (配線に合わせて Wire.begin(SDA_PIN, SCL_PIN) の順で指定します) // (配線に合わせて Wire.begin(SDA_PIN, SCL_PIN) の順で指定します)
@ -151,27 +121,5 @@ void setup() {
} }
void loop() { void loop() {
if (isPlaying && millis() - lastWs2812bUpdate >= WS2812B_INTERVAL_MS) { neoPixelController.update();
unsigned long now = millis();
lastWs2812bUpdate = now;
unsigned long transitionElapsed = now - ws2812bTransitionStart;
if (transitionElapsed >= WS2812B_TRANSITION_MS) {
ws2812bColorIndex = (ws2812bColorIndex + transitionElapsed / WS2812B_TRANSITION_MS)
% WS2812B_COLOR_COUNT;
ws2812bTransitionStart = now;
transitionElapsed = 0;
}
uint8_t nextColorIndex = (ws2812bColorIndex + 1) % WS2812B_COLOR_COUNT;
uint32_t currentColor = ws2812bColors[ws2812bColorIndex];
uint32_t nextColor = ws2812bColors[nextColorIndex];
uint8_t red = ((currentColor >> 16) * (WS2812B_TRANSITION_MS - transitionElapsed)
+ (nextColor >> 16) * transitionElapsed) / WS2812B_TRANSITION_MS;
uint8_t green = (((currentColor >> 8) & 0xFF) * (WS2812B_TRANSITION_MS - transitionElapsed)
+ ((nextColor >> 8) & 0xFF) * transitionElapsed) / WS2812B_TRANSITION_MS;
uint8_t blue = ((currentColor & 0xFF) * (WS2812B_TRANSITION_MS - transitionElapsed)
+ (nextColor & 0xFF) * transitionElapsed) / WS2812B_TRANSITION_MS;
ws2812b.setPixelColor(0, red, green, blue);
ws2812b.show();
}
} }