Skip to content

Instantly share code, notes, and snippets.

@mongonta0716
Created October 19, 2024 10:58
Show Gist options
  • Save mongonta0716/9962183b2759fa5ac0d767dfd4e24e8b to your computer and use it in GitHub Desktop.
Save mongonta0716/9962183b2759fa5ac0d767dfd4e24e8b to your computer and use it in GitHub Desktop.
M5DialにUnitKeyとFaderUnitを接続してAvatarの顔等を変更するサンプル
/*
see also https://stackoverflow.com/questions/7315936/which-of-sprintf-snprintf-is-more-secure/35401865#35401865
If you leave memory management to std::string, you don't have to free it yourself.
*/
// Using template
#include <string>
template<typename ...Args> std::string formatString(const char* fmt, Args... args)
{
size_t sz = snprintf(nullptr, 0U, fmt, args...); // calculate length
char buf[sz + 1];
snprintf(buf, sizeof(buf), fmt, args...);
return std::string(buf, buf + sz + 1);
}
// Using vsnprintf
#include <stdio.h>
#include <stdarg.h>
std::string formatString(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
size_t sz = vsnprintf(nullptr, 0U, fmt, args); // calculate length
char buf[sz + 1];
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
return std::string(buf, buf + sz + 1);
}
#include <Arduino.h>
#include <M5Dial.h>
#include <Avatar.h>
#include "formatString.hpp"
#include <FastLED.h>
using namespace m5avatar;
#define NUM_LEDS_FADER 14
#define LED_PIN_FADER 2
#define DATA_PIN_FADER 1
#define NUM_LEDS_KEY 1
#define LED_PIN_KEY 13
#define DATA_PIN_KEY 15
CRGB leds_fader[NUM_LEDS_FADER];
CRGB leds_key[NUM_LEDS_KEY];
CRGB red(255, 0, 0);
CRGB orange(255, 165, 0);
CRGB yellow(255, 255, 0);
CRGB green(0, 255, 0);
CRGB lightgreen(32, 178, 170);
CRGB blue(0, 0, 255);
CRGB darkblue(0, 0, 128);
CRGB color_table[7] {red, orange, yellow, green, lightgreen, blue, darkblue};
uint8_t beginHue = 0;
uint8_t deltaHue = 30;
uint8_t brightness = 100;
uint16_t rawADC = 0;
Avatar avatar;
void setup() {
auto cfg = M5.config();
//M5.begin(cfg);
M5Dial.begin(cfg, true, false);
M5.Log.setLogLevel(m5::log_target_display, ESP_LOG_NONE);
M5.Log.setLogLevel(m5::log_target_serial, ESP_LOG_INFO);
M5.Log.setEnableColor(m5::log_target_serial, false);
M5_LOGI("Avatar Start");
//M5.Log.printf("M5.Log avatar Start\n");
float scale = 0.0f;
int8_t position_top = 0;
int8_t position_left = 0;
uint8_t display_rotation = 1; // ディスプレイの向き(0〜3)
scale = 0.8f;
position_top = 0;
position_left = -40;
display_rotation = 2;
avatar.setScale(scale);
avatar.setPosition(position_top, position_left);
avatar.init(1); // start drawing
avatar.setSpeechFont(&fonts::efontCN_12);
FastLED.addLeds<NEOPIXEL, LED_PIN_FADER>(leds_fader, NUM_LEDS_FADER);
delay(1000);
pinMode(DATA_PIN_FADER, INPUT);
// M5ADial(StampS3)は外部マイク(PDMUnit)なので設定を行う。(Port.A)
//fill_rainbow(leds_fader, NUM_LEDS_FADER, beginHue, deltaHue);
//fill_rainbow(leds, NUM_LEDS_FADER, beginHue, deltaHue);
for(int i=0; i<NUM_LEDS_FADER / 2; i++) {
leds_fader[i] = color_table[i];
leds_fader[i+(NUM_LEDS_FADER/2)] = color_table[NUM_LEDS_FADER / 2 - i - 1];
}
pinMode(DATA_PIN_KEY, INPUT);
}
long oldPosition = -999;
int previousState = HIGH;
int readState = 0;
bool ledState = false;
void loop() {
std::string s;
rawADC = analogRead(DATA_PIN_FADER);
brightness = map(rawADC, 0, 4095, 10,
170); // The mapping ADC value is the brightness value
// range 映射ADC值为亮度值范围
FastLED.setBrightness(brightness); // Adjust the brightness of the FADER
// LED 调整FADER LED灯亮度
FastLED.show();
float mouth_ratio = brightness / 150.0f;
avatar.setMouthOpenRatio(mouth_ratio);
s = formatString("P:%d,A:%d,K:%d", oldPosition, rawADC, readState);
avatar.setSpeechText(s.c_str());
M5_LOGI("Loop");
M5Dial.update();
long newPosition = M5Dial.Encoder.read();
if (newPosition != oldPosition) {
M5Dial.Speaker.tone(8000, 20);
oldPosition = newPosition;
avatar.setRotation(10 * oldPosition);
M5_LOGI("nowPosition%d", newPosition);
}
if (M5Dial.BtnA.wasPressed()) {
//M5Dial.Encoder.readAndReset();
}
if (M5Dial.BtnA.pressedFor(5000)) {
//M5Dial.Encoder.write(100);
}
// スイッチ操作またはノイズによる状態変化検知
readState = digitalRead(DATA_PIN_KEY);
// LED点灯時は消灯、消灯時は点灯
if (readState == 0) {
avatar.setExpression(Expression::Happy);
} else {
avatar.setExpression(Expression::Neutral);
}
}
; 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
[platformio]
default_envs = m5stack-dial
[env]
platform = espressif32 @ 6.5.0
framework = arduino
upload_speed = 1500000
monitor_speed = 115200
board_build.f_flash = 80000000L
board_build.filesystem = spiffs
board_build.partitions = default_16MB.csv
build_flags = -DCORE_DEBUG_LEVEL=4
lib_deps =
meganetaaan/[email protected]
m5stack/[email protected]
m5stack/[email protected]
FastLED/FastLED
lib_ldf_mode = deep
[env:m5stack-dial]
board = esp32-s3-devkitc-1
board_build.partitions = default_8MB.csv
build_flags =
${env.build_flags}
-DARDUINO_M5Stack_StampS3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment