Taiko-Nijiiro-Analog-IOBoard/ESP32/ESP32.ino

92 lines
2.4 KiB
Arduino
Raw Normal View History

2023-12-22 02:54:39 +01:00
#define CHANNELS 4
2023-12-25 06:30:29 +01:00
#define SAMPLE_CACHE_LENGTH 16 // Must be power of 2 (8, 16, etc.); See cache.h for implementation
2023-12-22 02:54:39 +01:00
#define HIT_THRES 750 // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you
2023-12-25 06:30:29 +01:00
#define RESET_THRES 100 // changed SAMPLE_CACHE_LENGTH, you must also adjust thresholds
#define SAMPLING_PERIOD 500 // Sampling period in microseconds, 500us = 0.5ms = 2000Hz
2017-11-21 23:34:09 +01:00
2023-12-25 06:30:29 +01:00
#define DEBUG 0
2017-11-21 23:34:09 +01:00
2017-11-24 21:17:11 +01:00
#include "cache.h"
2023-12-25 03:40:11 +01:00
#include "keyboard.h"
2017-11-21 23:34:09 +01:00
2023-12-22 02:54:39 +01:00
Cache<int, SAMPLE_CACHE_LENGTH> inputWindow[CHANNELS];
unsigned long power[CHANNELS];
unsigned long lastPower[CHANNELS];
2017-11-21 23:34:09 +01:00
2017-11-25 04:13:36 +01:00
bool triggered;
2023-12-22 02:54:39 +01:00
unsigned long triggeredTime[CHANNELS];
2017-11-21 23:34:09 +01:00
2023-12-25 06:30:29 +01:00
const byte inPins[] = {36, 39, 34, 35}; // L don, L kat, R don, R kat
2023-12-25 03:40:11 +01:00
const byte outPins[] = {25, 26, 27, 14}; // LED visualization (optional)
2023-12-25 06:30:29 +01:00
const char outKeys[] = {'f', 'd', 'j', 'k'}; // L don, L kat, R don, R kat
2017-11-21 23:34:09 +01:00
2023-12-22 02:54:39 +01:00
float sensitivity[] = {1.0, 1.0, 1.0, 1.0};
short maxIndex;
float maxPower;
2017-11-24 21:17:11 +01:00
2023-12-25 03:40:11 +01:00
void bluetoothTask(void*);
void typeText(const char* text);
unsigned long lastTime;
2017-11-21 23:34:09 +01:00
void setup() {
2023-12-25 03:40:11 +01:00
Serial.begin(250000);
2023-12-22 02:54:39 +01:00
for (byte i = 0; i < CHANNELS; i++) {
power[i] = 0;
lastPower[i] = 0;
triggered = false;
2023-12-25 06:30:29 +01:00
pinMode(inPins[i], INPUT);
2023-12-25 03:40:11 +01:00
pinMode(outPins[i], OUTPUT);
2023-12-22 02:54:39 +01:00
}
maxIndex = -1;
maxPower = 0;
2023-12-25 03:40:11 +01:00
xTaskCreate(bluetoothTask, "bluetooth", 20000, NULL, 5, NULL);
2023-12-25 06:30:29 +01:00
lastTime = micros();
2017-11-21 23:34:09 +01:00
}
void loop() {
2023-12-22 02:54:39 +01:00
if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) {
triggered = false;
digitalWrite(outPins[maxIndex], LOW);
maxIndex = -1;
maxPower = 0;
2017-11-22 05:25:19 +01:00
}
2023-12-22 02:54:39 +01:00
for (byte i = 0; i < CHANNELS; i++) {
inputWindow[i].put(analogRead(inPins[i]));
power[i] = sensitivity[i] * (power[i] - inputWindow[i].get(1) + inputWindow[i].get());
if (lastPower[i] > maxPower && power[i] < lastPower[i]) {
maxPower = lastPower[i];
maxIndex = i;
}
lastPower[i] = power[i];
#if DEBUG
Serial.print(power[i]);
Serial.print(" ");
#endif
2017-11-25 04:13:36 +01:00
}
2017-11-24 21:17:11 +01:00
2023-12-22 02:54:39 +01:00
if (!triggered && maxPower >= HIT_THRES) {
triggered = true;
digitalWrite(outPins[maxIndex], HIGH);
#if !DEBUG
2023-12-25 03:40:11 +01:00
if (isBleConnected) {
2023-12-25 06:30:29 +01:00
typeChar(outKeys[maxIndex]);
2023-12-25 03:40:11 +01:00
}
2023-12-22 02:54:39 +01:00
#endif
}
#if DEBUG
Serial.print("\n");
#endif
2023-12-25 03:40:11 +01:00
2023-12-25 06:30:29 +01:00
unsigned int frameTime = micros() - lastTime;
if (frameTime < SAMPLING_PERIOD) {
delayMicroseconds(SAMPLING_PERIOD - frameTime);
}
lastTime = micros();
2017-11-21 23:34:09 +01:00
}