Skip to content

Instantly share code, notes, and snippets.

@mongonta0716
Created December 8, 2023 09:51
Show Gist options
  • Save mongonta0716/a6bc6130f6354ca1844c425ef2c91525 to your computer and use it in GitHub Desktop.
Save mongonta0716/a6bc6130f6354ca1844c425ef2c91525 to your computer and use it in GitHub Desktop.
M5Dial + TONE&NOTEの自作キーボード
#include <M5Dial.h>
#include "keyconvination.hpp"
#include "Wire.h"
#include "USB.h"
#include "USBHIDMouse.h"
USBHIDMouse Mouse;
USBHIDKeyboard keyboard;
#include <Avatar.h>
#include "formatString.hpp"
#include <Wire.h>
#define I2C_ADDR 0x55
using namespace m5avatar;
Avatar avatar;
float rotation = 0.0f;
uint8_t layer_no = 0;
enum Layer {
ADOBE_ILLUSTRATOR1,
ADOBE_ILLUSTRATOR2,
MUSIC,
LAYER_END
};
#define LSFT(kc) (KEY_LEFT_SHIFT << 8 | (kc))
uint16_t keycode_layout[LAYER_END][8] = {
{ KEY_ESC << 8, 'v', 'a', 'b', 'p', 't', LSFT('c'), LSFT('+') },
{ KEY_ESC << 8, 'v', 'a', 'b', 'p', 't', LSFT(HID_KEY_C), LSFT(HID_KEY_KEYPAD_ADD) },
{ KEY_ESC << 8, 'v', 'a', 'b', 'p', 't', LSFT(HID_KEY_C), LSFT(HID_KEY_KEYPAD_ADD) },
};
uint16_t convertKeyCode(uint8_t keycode) {
uint8_t char_value;
switch(keycode) {
// TONE&NOTEからのキーコードを変換
case 39:
char_value = 0;
rotation = 0.0f;
break;
case 30:
char_value = 1;
rotation = 90.0f;
break;
case 31:
char_value = 2;
rotation = 180.0f;
break;
case 32:
char_value = 3;
rotation = 270.0f;
break;
case 33:
char_value = 4;
break;
case 34:
char_value = 5;
break;
case 35:
char_value = 6;
break;
case 36:
char_value = 7;
break;
case 37:
char_value = 8;
layer_no--;
return 0x0000;
case 38:
char_value = 9;
layer_no++;
return 0xffff;
default:
char_value = 10;
break;
}
uint16_t keyconvination = keycode_layout[layer_no][char_value];
if ((keyconvination >> 8) >= 0x80) {
keyboard.press(keyconvination >> 8);
}
keyboard.press(keyconvination & 0x00FF);
keyboard.releaseAll();
return keyconvination;
}
void onReceive(int len){
uint8_t buff[20];
M5_LOGI("onReceive[%d]: ", len);
while(Wire.available()){
Wire.readBytes(buff, len);
M5.Display.setCursor(100, 100);
for (int i=0; i< len; i++) {
M5.Log.printf("%d", buff[i]);
if (buff[0] == 37) {
rotation = rotation - 10.0f;
} else if (buff[0] == 38) {
rotation = rotation + 10.0f;
}
avatar.setRotation(rotation);
//char data = convertKeyCode(buff[0]);
std::string s = formatString("%4x", convertKeyCode(buff[0]));
avatar.setSpeechText(s.c_str());
}
}
M5.Log.println();
}
void setup()
{
auto cfg = M5.config();
M5Dial.begin(cfg, true, false);
M5.Display.setRotation(1);
Mouse.begin();
USB.begin();
keyboard.begin();
avatar.init();
avatar.setPosition(0, -40);
M5.Speaker.begin();
M5.Speaker.tone(2000, 500);
delay(500);
Wire.onReceive(onReceive);
Wire.begin((uint8_t)I2C_ADDR, 15, 13, 400000U);
}
long oldPosition = -999;
int prev_x = -1;
int prev_y = -1;
static m5::touch_state_t prev_state;
int alpha_count = 0;
bool touched = false;
bool first_move = false;
void loop()
{
M5Dial.update();
auto t = M5Dial.Touch.getDetail();
if (prev_state != t.state)
{
prev_state = t.state;
static constexpr const char *state_name[16] = {
"none", "touch", "touch_end", "touch_begin",
"___", "hold", "hold_end", "hold_begin",
"___", "flick", "flick_end", "flick_begin",
"___", "drag", "drag_end", "drag_begin"};
Serial.println(state_name[t.state]);
if (t.state == m5::touch_state_t::touch)
{
touched = true;
first_move = true;
prev_x = t.x;
prev_y = t.y;
Serial.println("TOUCH X:" + String(t.x) + " / " + "Y:" + String(t.y));
}
if (t.state == m5::touch_state_t::touch_end)
{
Serial.println("LEFT CLICK!!!");
Mouse.click(MOUSE_LEFT);
}
if (t.state == m5::touch_state_t::none)
{
touched = false;
}
}
if (touched && (prev_x != t.x || prev_y != t.y))
{
int8_t dx = (int16_t)t.x - (int16_t)prev_x;
int8_t dy = (int16_t)t.y - (int16_t)prev_y;
if (first_move)
{
first_move = false;
Serial.println("FIRST MOVE");
}
else
{
Serial.println("MOVE X:" + String(t.x) + " / " + "Y:" + String(t.y) + " / " + "DX:" + String(dx) + " / " + "DY:" + String(dy));
Mouse.move(dx * 5, dy * 5, 0, 0);
}
prev_x = t.x;
prev_y = t.y;
}
if (M5Dial.BtnA.wasPressed())
{
Serial.println("RIGHT CLICK");
Mouse.click(MOUSE_RIGHT);
}
long newPosition = M5Dial.Encoder.read();
if (newPosition != oldPosition)
{
int8_t dw = newPosition - oldPosition;
Serial.println("W:" + String(newPosition) + " / " + "DW:" + String(dw));
oldPosition = newPosition;
Mouse.move(0, 0, dw, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment