Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active December 23, 2022 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsuyoshi/1395d09e156158841df22ea1be50e978 to your computer and use it in GitHub Desktop.
Save katsuyoshi/1395d09e156158841df22ea1be50e978 to your computer and use it in GitHub Desktop.
雪寄せ時間を記録するためのM5StickCのプログラム
#include <Arduino.h>
#include <M5Unified.h>
static float velocity = 0;
static void imu_task(void*)
{
float offset = 0.09;
int tap_size = 9;
int b_ptr = 0;
float z_buf[tap_size] = {0};
float val[3];
float vel = 0;
float sum = 0;
while(true) {
M5.Imu.getAccel(&val[0], &val[1], &val[2]);
vel = sqrt(val[0] * val[0] + val[1] * val[1] + val[2] * val[2]) - offset;
sum -= z_buf[b_ptr];
z_buf[b_ptr] = vel;
sum += z_buf[b_ptr];
b_ptr = (b_ptr + 1) % tap_size;
velocity = sum / tap_size;
delay(10);
}
}
void setup() {
auto cfg = M5.config();
cfg.clear_display = true; // default=true. clear the screen when begin.
cfg.output_power = false; // default=true. use external port 5V output.
cfg.internal_imu = true; // default=true. use internal IMU.
cfg.internal_rtc = false; // default=true. use internal RTC.
cfg.internal_spk = false; // default=true. use internal speaker.
cfg.internal_mic = false; // default=true. use internal microphone.
cfg.external_imu = false; // default=false. use Unit Accel & Gyro.
cfg.external_rtc = false; // default=false. use Unit RTC.
cfg.external_spk = false; // default=false. use SPK_HAT / ATOMIC_SPK
//cfg.external_spk_detail.omit_atomic_spk = true; // omit ATOMIC SPK
//cfg.external_spk_detail.omit_spk_hat = true; // omit SPK HAT
cfg.led_brightness = 50; // default= 0. system LED brightness (0=off / 255=max) (※ not NeoPixel)
M5.begin(cfg);
// Rotate LCD origin
M5.Lcd.setRotation(1);
M5.Lcd.setTextSize(2);
// LED OFF
pinMode(GPIO_NUM_10, OUTPUT);
digitalWrite(GPIO_NUM_10, HIGH);
// IMU task
xTaskCreatePinnedToCore(imu_task, "imu_task", 1024, NULL, 25, NULL, APP_CPU_NUM);
}
static void display() {
int bar_height = 20;
int h = M5.Lcd.height();
int w = M5.Lcd.width();
int y = h - bar_height;
int value = 0;
M5.Lcd.setCursor(1, 1);
M5.Lcd.printf("V: %.4f", velocity);
value = map((int)(velocity * 10000), 0, 20000, 0, w);
Serial.println(value);
value = constrain(value, 0, w);
Serial.println(value);
M5.Lcd.fillRect(0, y, w, bar_height, TFT_BLACK);
M5.Lcd.fillRect(0, y, value, bar_height, TFT_GREEN);
M5.Lcd.fillRect(0, y, 3, bar_height, TFT_WHITE);
}
void loop() {
Serial.print(".");
display();
delay(200);
}
[env:m5stick-c]
platform = espressif32
board = m5stick-c
framework = arduino
lib_deps = m5stack/M5Unified@^0.0.7
monitor_speed = 115200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment