Add delay mechanism back

This commit is contained in:
Xun Zhang 2023-12-24 21:30:29 -08:00
parent 0056f09789
commit e085187f46
3 changed files with 30 additions and 15 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
build/
.vscode/
.vscode/
.DS_Store

View File

@ -1,10 +1,10 @@
#define CHANNELS 4
#define SAMPLE_CACHE_LENGTH 32 // Must be power of 2 (8, 16, etc.); See cache.h for implementation
#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 300 // changed SAMPLE_CACHE_LENGTH, you must also adjust thresholds
#define SAMPLING_PERIOD 1000 // Sampling period in microseconds (us), 1000us = 1ms = 0.001s
#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 1
#define DEBUG 0
#include "cache.h"
#include "keyboard.h"
@ -16,9 +16,9 @@ unsigned long lastPower[CHANNELS];
bool triggered;
unsigned long triggeredTime[CHANNELS];
const byte inPins[] = {35, 34, 39, 36}; // L don, L kat, R don, R kat
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
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};
@ -36,12 +36,13 @@ void setup() {
power[i] = 0;
lastPower[i] = 0;
triggered = false;
pinMode(inPins[i], INPUT);
pinMode(outPins[i], OUTPUT);
}
lastTime = 0;
maxIndex = -1;
maxPower = 0;
xTaskCreate(bluetoothTask, "bluetooth", 20000, NULL, 5, NULL);
lastTime = micros();
}
void loop() {
@ -74,7 +75,7 @@ void loop() {
digitalWrite(outPins[maxIndex], HIGH);
#if !DEBUG
if (isBleConnected) {
typeText(outKeys[maxIndex]);
typeChar(outKeys[maxIndex]);
}
#endif
}
@ -82,9 +83,9 @@ void loop() {
Serial.print("\n");
#endif
// unsigned int frameTime = micros() - lastTime;
// lastTime = micros();
// if (frameTime < SAMPLING_PERIOD) {
// delayMicroseconds(SAMPLING_PERIOD - frameTime);
// }
unsigned int frameTime = micros() - lastTime;
if (frameTime < SAMPLING_PERIOD) {
delayMicroseconds(SAMPLING_PERIOD - frameTime);
}
lastTime = micros();
}

View File

@ -1,4 +1,3 @@
// Bluetooth keyboard implemetation by manuelbl:
// https://gist.github.com/manuelbl/66f059effc8a7be148adb1f104666467
@ -216,4 +215,18 @@ void typeText(const char* text) {
delay(5);
}
}
void typeChar(char c) {
uint8_t val = (uint8_t)c;
KEYMAP map = keymap[val];
InputReport report = {.modifiers = map.modifier,
.reserved = 0,
.pressedKeys = {map.usage, 0, 0, 0, 0, 0}};
input->setValue((uint8_t*)&report, sizeof(report));
input->notify();
delay(1);
input->setValue((uint8_t*)&NO_KEY_PRESSED, sizeof(NO_KEY_PRESSED));
input->notify();
delay(1);
}