Commit b7431be8 authored by yuichitamiya's avatar yuichitamiya
Browse files

workshop example homma

parent be5fb906
Pipeline #243824 passed with stage
in 46 seconds
#include <Adafruit_FreeTouch.h>
#include <TinyUSB_Mouse_and_Keyboard.h>
Adafruit_FreeTouch qt_0 = Adafruit_FreeTouch(A0, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_7 = Adafruit_FreeTouch(A7, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_8 = Adafruit_FreeTouch(A8, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_9 = Adafruit_FreeTouch(A9, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_10 = Adafruit_FreeTouch(A10, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
uint8_t Key2;//pin2 bike sensor in
/* Xiao
|USB-C|
|A0_qt0| |5V|
|A1_qt1| |GND|
|A2_bike| |3V3|
|A3| |A10_qt10|
|A4| |A9_qt9|
|A5| |A8_qt8|
|A6_qt6| |A7_qt7|
*/
int qt0 = 0; // Enter
int qt1 = 0; // Backspace
int qt7 = 0; // Down
int qt8 = 0; // Left
int qt9 = 0; // Up
int qt10 = 0; // Right
int last_qt0 = 0; // Enter
int last_qt1 = 0; // Backspace
int last_qt7 = 0; // Down
int last_qt8 = 0; // Left
int last_qt9 = 0; // Up
int last_qt10 = 0; // Right
const int num_avg = 10;
//int qt_Threshold = 1010;
//int qt0_Threshold = 1014;
// int qt0_diff_threshold = 30;
int qt_diff_threshold = 20;
int key_press_delay = 100;
int calculate_last_qt(int qt, int last_qt) {
last_qt = (qt + last_qt )/2;
return last_qt;
}
void sense_qtouch(){
qt0 = 0;
qt1 = 0;
qt7 = 0;
qt8 = 0;
qt9 = 0;
qt10 = 0;
// loop num_avg times
for (int i = 1; i <= num_avg; i++){
qt0 = qt0 + qt_0.measure();
qt1 = qt1 + qt_1.measure();
qt7 = qt7 + qt_7.measure();
qt8 = qt8 + qt_8.measure();
qt9 = qt9 + qt_9.measure();
qt10 = qt10 + qt_10.measure();
delay(20);
}
// average
qt0 = qt0/num_avg;
qt1 = qt0/num_avg;
qt7 = qt7/num_avg;
qt8 = qt8/num_avg;
qt9 = qt9/num_avg;
qt10 = qt10/num_avg;
Serial.print("qt0(En): "); Serial.print(qt0);
Serial.print(" qt1(BS): "); Serial.print(qt1);
Serial.print(" qt7(D): "); Serial.print(qt7);
Serial.print(" qt8(L): "); Serial.print(qt8);
Serial.print(" qt9(U): "); Serial.print(qt9);
Serial.print(" qt10(R): "); Serial.println(qt10);
}
void setup() {
Keyboard.begin();
Serial.begin(115200);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
qt_0.begin();
qt_1.begin();
qt_7.begin();
qt_8.begin();
qt_9.begin();
qt_10.begin();
delay(3000);
sense_qtouch();
last_qt0 = qt0;
last_qt1 = qt1;
last_qt7 = qt7;
last_qt8 = qt8;
last_qt9 = qt9;
last_qt10 = qt10;
}
void loop() {
sense_qtouch();
if (qt0 - last_qt0 >= qt_diff_threshold) {
Serial.print("qt0(Enter): "); Serial.print(qt0);
Serial.print(" last_qt0: "); Serial.println(last_qt0);
Key0_act();
}
last_qt0 = qt0;
if (qt1 - last_qt1 >= qt_diff_threshold) {
Serial.print("qt1(BS): "); Serial.print(qt1);
Serial.print(" last_qt1: "); Serial.println(last_qt1);
Key1_act();
}
last_qt1 = qt1;
if (qt7 - last_qt7 >= qt_diff_threshold) {
Serial.print("qt7(D): "); Serial.print(qt7);
Serial.print(" last_qt7: "); Serial.println(last_qt7);
Key7_act();
}
last_qt7 = qt7;
if (qt8 - last_qt8 >= qt_diff_threshold) {
Serial.print("qt8(L): "); Serial.print(qt8);
Serial.print(" last_qt8: "); Serial.println(last_qt8);
Key8_act();
}
last_qt8 = qt8;
if (qt9 - last_qt9 >= qt_diff_threshold) {
Serial.print("qt9(U): "); Serial.print(qt9);
Serial.print(" last_qt9: "); Serial.println(last_qt9);
Key9_act();
}
last_qt9 = qt9;
if (qt10 - last_qt10 >= qt_diff_threshold) {
Serial.print("qt10(R): "); Serial.print(qt10);
Serial.print(" last_qt10: "); Serial.println(last_qt10);
Key10_act();
}
last_qt10 = qt10;
delay(10);
}
void Key0_act() {
Keyboard.press(KEY_RETURN);
delay(key_press_delay);
Keyboard.releaseAll();
digitalWrite(LED_BUILTIN, LOW);// XIAOでは、LOW = On
KeyRelease();
}
void Key1_act() {
Keyboard.press(KEY_BACKSPACE);
delay(key_press_delay);
Keyboard.releaseAll();
digitalWrite(LED_BUILTIN, LOW);
KeyRelease();
}
void Key7_act() {
Keyboard.press(KEY_DOWN_ARROW);
delay(key_press_delay);
Keyboard.releaseAll();
}
void Key8_act() {
Keyboard.press(KEY_LEFT_ARROW);
delay(key_press_delay);
Keyboard.releaseAll();
}
void Key9_act() {
Keyboard.press(KEY_UP_ARROW);
delay(key_press_delay);
Keyboard.releaseAll();
}
void Key10_act() {
Keyboard.press(KEY_RIGHT_ARROW);
delay(key_press_delay);
Keyboard.releaseAll();
}
void KeyRelease() {
Keyboard.releaseAll();
delay(key_press_delay);
digitalWrite(LED_BUILTIN, HIGH);// XIAO HIGH = Off
}
/*Example
Keyboard.write('a'); //press and release 'a' key
uint8_t Multiple[3]= {'1','2','3'};
Keyboard.write(Multiple,3); //multiple keys sequentially from a buffer
Keyboard.print("456"); //print a string
Keyboard.println("789"); //print another string with a line
Keyboard.press(KEY_LEFT_SHIFT); //hold down the shift
Keyboard.println("a uppercase sentence"); //this will be all caps
Keyboard.release(KEY_LEFT_SHIFT);//release the shift
Keyboard.println ("back to lowercase");
Keyboard.press(KEY_LEFT_SHIFT); //press shift
Keyboard.println("1234"); //some text
Keyboard.releaseAll(); //release all
Keyboard.println("1234"); //not shifted
Keyboard.print("A mistake"); //will attempt to erase this
delay(1000);
Keyboard.press(KEY_LEFT_CTRL); //will attempt control-z
Keyboard.write('z');
Keyboard.releaseAll(); //release the control key
*/
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment