【M5Stack_Core2】Gesture recognition for Core2, on Arduino.
Gesture recognition for Core2, on micropython. pic.twitter.com/Yopap7v7WS
— M5Stack (@M5Stack) 2020年9月27日
公式のまねっこ
// // touch_gesture // ver.02 2020/09/30 chiyoh // #include <M5Core2.h> #include <utility/Sprite.h> #include <touch.h> #include <stdio.h> #define ESP32 #define CST_DEVICE_ADDR 0x38 #define CST_DEVICE_READ_MAX 4 #define CST_DEVICE_WRITE_MAX 4 #define CST_DEVICE_REG_TD_STATUS 0x02 #define CST_DEVICE_REG_P1 0x03 #define CST_DEVICE_REG_GESTURE_ENABLE 0xd0 #define CST_DEVICE_REG_GESTURE_ID 0xd3 TFT_eSprite stext1 = TFT_eSprite(&M5.Lcd); TFT_eSprite stext2 = TFT_eSprite(&M5.Lcd); // 1byte Write uint8_t touchWrite1(uint8_t reg, int8_t value) { // write mode w:reg w:value s Wire1.beginTransmission(CST_DEVICE_ADDR); Wire1.write(reg); Wire1.write(value); uint8_t result = Wire1.endTransmission(); return result; } // 1byte Read uint8_t touchRead1(uint8_t reg) { // read mode w:reg r:value s uint8_t value; Wire1.beginTransmission(CST_DEVICE_ADDR); Wire1.write(reg); Wire1.endTransmission(false); Wire1.requestFrom((uint16_t)CST_DEVICE_ADDR, (uint8_t)1); value = Wire1.read(); return value; } // burstRead uint8_t touchReadBurst(uint8_t reg, uint8_t size, uint8_t* data) { uint8_t result=0; Wire1.beginTransmission((uint8_t)CST_DEVICE_ADDR); Wire1.write(reg); Wire1.endTransmission(false); result = Wire1.requestFrom((uint16_t)CST_DEVICE_ADDR, size); for(uint8_t i=0; i<size; i++){ data[i] = Wire1.read(); } return result; } uint8_t td_status(){ return touchRead1((uint8_t)CST_DEVICE_REG_TD_STATUS); } void gesture_enable(){ touchWrite1((uint8_t)CST_DEVICE_REG_GESTURE_ENABLE, 0x01); } uint8_t gestureID(){ return touchRead1((uint8_t)CST_DEVICE_REG_GESTURE_ID); } char* gestureName(uint8_t id){ char* gName; switch(id){ case 0x00: gName = "no-gesture"; break; case 0x20: gName = "right-slide"; break; case 0x21: gName = "left-slide"; break; case 0x22: gName = "up-slide"; // 途中で止める?突き抜けると0になる? break; case 0x23: gName = "down-slide"; break; case 0x24: gName = "double-click"; break; case 0x30: gName = "o gesture"; break; case 0x31: gName = "w gesture"; break; case 0x32: gName = "m gesture"; break; case 0x33: gName = "e gesture"; break; case 0x34: gName = "c gesture"; break; case 0x35: gName = "g gesture"; break; case 0x36: gName = "a gesture"; break; case 0x37: gName = "d gesture"; break; case 0x40: gName = "n gesture"; break; case 0x41: gName = "???"; break; case 0x42: gName = "b gesture"; break; case 0x43: gName = "(q) ???"; break; case 0x44: gName = "L gesture"; break; case 0x45: gName = "P gesture"; break; case 0x46: gName = "S gesture"; break; case 0x47: gName = "u gesture"; break; case 0x50: gName = "@ gesture"; break; case 0x51: gName = "(<) ???"; break; case 0x52: gName = "> gesture"; break; case 0x53: gName = "^ gesture"; break; case 0x54: gName = "v gesture"; break; case 0x60: gName = "3 gesture"; break; case 0x61: gName = "6 gesture"; break; case 0x62: gName = "(8) ???"; break; case 0x63: gName = "7 gesture"; break; case 0x64: gName = "(9) ???"; break; case 0x65: gName = "2 gesture"; break; case 0x70: gName = "h gesture"; break; case 0x71: gName = "k gesture"; break; case 0x72: gName = "y gesture"; break; case 0x73: gName = "r gesture"; break; default: gName = "???"; } return gName; } uint8_t touchPoint(uint16_t* x, uint16_t* y){ uint8_t e,dat[4]; dat[0] = touchRead1((uint8_t)(CST_DEVICE_REG_P1 +0)); dat[1] = touchRead1((uint8_t)(CST_DEVICE_REG_P1 +1)); dat[2] = touchRead1((uint8_t)(CST_DEVICE_REG_P1 +2)); dat[3] = touchRead1((uint8_t)(CST_DEVICE_REG_P1 +3)); e = (dat[0] >> 6) & 0x03; *x = ((dat[0]<<8)|dat[1]) & 0x0fff; *y = ((dat[2]<<8)|dat[3]) & 0x0fff; return e; } void setup() { M5.begin(); // 外部I2C off Serial.printf("\n"); Serial.flush(); delay(50); yield(); stext1.setColorDepth(8); stext1.createSprite(320, 32); stext1.fillSprite(TFT_WHITE); stext1.setTextColor(TFT_BLACK); stext2.setColorDepth(8); stext2.createSprite(320, 32); stext2.fillSprite(TFT_WHITE); stext2.setTextColor(TFT_BLACK); M5.Lcd.fillScreen(WHITE); M5.Lcd.setTextColor(BLACK); M5.Lcd.setTextSize(2); M5.Lcd.clear(WHITE); M5.Lcd.setCursor(10, 10); M5.Lcd.printf("touch GESTURE Test!"); gesture_enable(); delay(500); yield(); } uint8_t tds=0; uint8_t tds_old=0; uint8_t id=0; uint8_t id_old=0; void loop() { char buf[80]; char* c; uint16_t x,y; uint8_t e,e_old; tds = td_status(); e = touchPoint(&x, &y); sprintf(buf,"event:%d,%d P1(%d,%d)",tds,e,x,y); stext1.fillSprite(TFT_WHITE); stext1.drawString(buf, 0, 0, 4); // (0,0) font #4 stext1.pushSprite(16, 32); if(tds==1){ if(tds_old==0){ M5.Lcd.fillScreen(WHITE); M5.Lcd.fillCircle(x, y, 8, RED); tds_old=1; }else{ M5.Lcd.fillCircle(x, y, 4, BLUE); } }else if(tds_old==1){ tds_old=0; } id = gestureID(); if(id != id_old){ id_old = id; c = gestureName(id); sprintf(buf,"gesture:%d(%s)",id,c); stext2.fillSprite(TFT_WHITE); stext2.drawString(buf, 0, 0, 4); // (0,0) font #4 stext2.pushSprite(16, 64); } delay(50); yield(); }