Skip to content

Instantly share code, notes, and snippets.

@mongonta0716
Created October 19, 2024 11:07
Show Gist options
  • Save mongonta0716/ea08134b892212d8953fa9bab85bbf0c to your computer and use it in GitHub Desktop.
Save mongonta0716/ea08134b892212d8953fa9bab85bbf0c to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <M5Dial.h>
#include <Avatar.h>
#include "formatString.hpp"
using namespace m5avatar;
Avatar avatar;
void setup() {
auto cfg = M5.config();
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");
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);
delay(1000);
pinMode(DATA_PIN_FADER, INPUT);
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);
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);
}
// スイッチ操作またはノイズによる状態変化検知
readState = digitalRead(DATA_PIN_KEY);
// LED点灯時は消灯、消灯時は点灯
if (readState == 0) {
avatar.setExpression(Expression::Happy);
} else {
avatar.setExpression(Expression::Neutral);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment