Added ESP32-S3
This commit is contained in:
parent
0e043e792e
commit
b972a8bab6
89
ESP32-S3/ESP32-S3.ino
Normal file
89
ESP32-S3/ESP32-S3.ino
Normal file
@ -0,0 +1,89 @@
|
||||
#define CHANNELS 4
|
||||
#define SAMPLE_CACHE_LENGTH 16 // Must be power of 2 (8, 16, etc.); See cache.h for implementation
|
||||
#define HIT_THRES 750 // The thresholds are also dependent on SAMPLE_CACHE_LENGTH, if you
|
||||
#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
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
#include "USB.h"
|
||||
#include "USBHIDKeyboard.h"
|
||||
#include "cache.h"
|
||||
|
||||
USBHIDKeyboard Keyboard;
|
||||
|
||||
Cache<int, SAMPLE_CACHE_LENGTH> inputWindow[CHANNELS];
|
||||
unsigned long power[CHANNELS];
|
||||
unsigned long lastPower[CHANNELS];
|
||||
|
||||
bool triggered;
|
||||
unsigned long triggeredTime[CHANNELS];
|
||||
|
||||
const byte inPins[] = {36, 39, 34, 35}; // L don, L kat, R don, R kat
|
||||
const byte outPins[] = {25, 26, 27, 14}; // LED visualization (optional)
|
||||
const char outKeys[] = {'f', 'd', 'j', 'k'}; // L don, L kat, R don, R kat
|
||||
|
||||
float sensitivity[] = {1.0, 1.0, 1.0, 1.0};
|
||||
|
||||
short maxIndex;
|
||||
float maxPower;
|
||||
|
||||
unsigned long lastTime;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(250000);
|
||||
for (byte i = 0; i < CHANNELS; i++) {
|
||||
power[i] = 0;
|
||||
lastPower[i] = 0;
|
||||
triggered = false;
|
||||
pinMode(inPins[i], INPUT);
|
||||
pinMode(outPins[i], OUTPUT);
|
||||
}
|
||||
maxIndex = -1;
|
||||
maxPower = 0;
|
||||
lastTime = micros();
|
||||
Keyboard.begin();
|
||||
USB.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (maxIndex != -1 && lastPower[maxIndex] < RESET_THRES) {
|
||||
triggered = false;
|
||||
digitalWrite(outPins[maxIndex], LOW);
|
||||
maxIndex = -1;
|
||||
maxPower = 0;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if (!triggered && maxPower >= HIT_THRES) {
|
||||
triggered = true;
|
||||
digitalWrite(outPins[maxIndex], HIGH);
|
||||
#if !DEBUG
|
||||
Keyboard.write(outKeys[maxIndex]);
|
||||
#endif
|
||||
}
|
||||
#if DEBUG
|
||||
Serial.print("\n");
|
||||
#endif
|
||||
|
||||
unsigned int frameTime = micros() - lastTime;
|
||||
if (frameTime < SAMPLING_PERIOD) {
|
||||
delayMicroseconds(SAMPLING_PERIOD - frameTime);
|
||||
}
|
||||
lastTime = micros();
|
||||
}
|
31
ESP32-S3/cache.h
Normal file
31
ESP32-S3/cache.h
Normal file
@ -0,0 +1,31 @@
|
||||
/***************************************************************
|
||||
* *
|
||||
* Taiko Sanro - Arduino *
|
||||
* Cache data structure *
|
||||
* *
|
||||
* Chris *
|
||||
* wisaly@gmail.com *
|
||||
* *
|
||||
***************************************************************/
|
||||
|
||||
#ifndef CACHE_H
|
||||
#define CACHE_H
|
||||
|
||||
template <class T, int L>
|
||||
class Cache {
|
||||
public:
|
||||
Cache() { memset(data_, 0, sizeof(data_)); }
|
||||
inline void put(T value) {
|
||||
current_ = (current_ + 1) & (L - 1);
|
||||
data_[current_] = value;
|
||||
}
|
||||
inline T get(int offset = 0) const {
|
||||
return data_[(current_ + offset) & (L - 1)];
|
||||
}
|
||||
|
||||
private:
|
||||
T data_[L];
|
||||
int current_ = 0;
|
||||
};
|
||||
|
||||
#endif // CACHE_H
|
Loading…
Reference in New Issue
Block a user