Moved ESP32 to main branch

This commit is contained in:
Xun Zhang 2023-12-24 18:40:11 -08:00
parent 86168388b8
commit 0056f09789
10 changed files with 485 additions and 61 deletions

90
ESP32/ESP32.ino Normal file
View file

@ -0,0 +1,90 @@
#define CHANNELS 4
#define SAMPLE_CACHE_LENGTH 32 // 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 DEBUG 1
#include "cache.h"
#include "keyboard.h"
Cache<int, SAMPLE_CACHE_LENGTH> inputWindow[CHANNELS];
unsigned long power[CHANNELS];
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 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;
void bluetoothTask(void*);
void typeText(const char* text);
unsigned long lastTime;
void setup() {
Serial.begin(250000);
for (byte i = 0; i < CHANNELS; i++) {
power[i] = 0;
lastPower[i] = 0;
triggered = false;
pinMode(outPins[i], OUTPUT);
}
lastTime = 0;
maxIndex = -1;
maxPower = 0;
xTaskCreate(bluetoothTask, "bluetooth", 20000, NULL, 5, NULL);
}
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
if (isBleConnected) {
typeText(outKeys[maxIndex]);
}
#endif
}
#if DEBUG
Serial.print("\n");
#endif
// unsigned int frameTime = micros() - lastTime;
// lastTime = micros();
// if (frameTime < SAMPLING_PERIOD) {
// delayMicroseconds(SAMPLING_PERIOD - frameTime);
// }
}

31
ESP32/cache.h Normal file
View 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

219
ESP32/keyboard.h Normal file
View file

@ -0,0 +1,219 @@
// Bluetooth keyboard implemetation by manuelbl:
// https://gist.github.com/manuelbl/66f059effc8a7be148adb1f104666467
#include "BLEDevice.h"
#include "BLEHIDDevice.h"
#include "HIDKeyboardTypes.h"
#include "HIDTypes.h"
#define DEVICE_NAME "ESP32 Bluetooth Keyboard"
bool isBleConnected = false;
// Message (report) sent when a key is pressed or released
struct InputReport {
uint8_t modifiers; // bitmask: CTRL = 1, SHIFT = 2, ALT = 4
uint8_t reserved; // must be 0
uint8_t pressedKeys[6]; // up to six concurrenlty pressed keys
};
// Message (report) received when an LED's state changed
struct OutputReport {
uint8_t leds; // bitmask: num lock = 1, caps lock = 2, scroll lock = 4,
// compose = 8, kana = 16
};
// The report map describes the HID device (a keyboard in this case) and
// the messages (reports in HID terms) sent and received.
static const uint8_t REPORT_MAP[] = {
USAGE_PAGE(1),
0x01, // Generic Desktop Controls
USAGE(1),
0x06, // Keyboard
COLLECTION(1),
0x01, // Application
REPORT_ID(1),
0x01, // Report ID (1)
USAGE_PAGE(1),
0x07, // Keyboard/Keypad
USAGE_MINIMUM(1),
0xE0, // Keyboard Left Control
USAGE_MAXIMUM(1),
0xE7, // Keyboard Right Control
LOGICAL_MINIMUM(1),
0x00, // Each bit is either 0 or 1
LOGICAL_MAXIMUM(1),
0x01,
REPORT_COUNT(1),
0x08, // 8 bits for the modifier keys
REPORT_SIZE(1),
0x01,
HIDINPUT(1),
0x02, // Data, Var, Abs
REPORT_COUNT(1),
0x01, // 1 byte (unused)
REPORT_SIZE(1),
0x08,
HIDINPUT(1),
0x01, // Const, Array, Abs
REPORT_COUNT(1),
0x06, // 6 bytes (for up to 6 concurrently pressed keys)
REPORT_SIZE(1),
0x08,
LOGICAL_MINIMUM(1),
0x00,
LOGICAL_MAXIMUM(1),
0x65, // 101 keys
USAGE_MINIMUM(1),
0x00,
USAGE_MAXIMUM(1),
0x65,
HIDINPUT(1),
0x00, // Data, Array, Abs
REPORT_COUNT(1),
0x05, // 5 bits (Num lock, Caps lock, Scroll lock, Compose, Kana)
REPORT_SIZE(1),
0x01,
USAGE_PAGE(1),
0x08, // LEDs
USAGE_MINIMUM(1),
0x01, // Num Lock
USAGE_MAXIMUM(1),
0x05, // Kana
LOGICAL_MINIMUM(1),
0x00,
LOGICAL_MAXIMUM(1),
0x01,
HIDOUTPUT(1),
0x02, // Data, Var, Abs
REPORT_COUNT(1),
0x01, // 3 bits (Padding)
REPORT_SIZE(1),
0x03,
HIDOUTPUT(1),
0x01, // Const, Array, Abs
END_COLLECTION(0) // End application collection
};
BLEHIDDevice* hid;
BLECharacteristic* input;
BLECharacteristic* output;
const InputReport NO_KEY_PRESSED = {};
/*
* Callbacks related to BLE connection
*/
class BleKeyboardCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* server) {
isBleConnected = true;
// Allow notifications for characteristics
BLE2902* cccDesc =
(BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
cccDesc->setNotifications(true);
Serial.println("Client has connected");
}
void onDisconnect(BLEServer* server) {
isBleConnected = false;
// Disallow notifications for characteristics
BLE2902* cccDesc =
(BLE2902*)input->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
cccDesc->setNotifications(false);
Serial.println("Client has disconnected");
}
};
/*
* Called when the client (computer, smart phone) wants to turn on or off
* the LEDs in the keyboard.
*
* bit 0 - NUM LOCK
* bit 1 - CAPS LOCK
* bit 2 - SCROLL LOCK
*/
class OutputCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic* characteristic) {
OutputReport* report = (OutputReport*)characteristic->getData();
Serial.print("LED state: ");
Serial.print((int)report->leds);
Serial.println();
}
};
void bluetoothTask(void*) {
// initialize the device
BLEDevice::init(DEVICE_NAME);
BLEServer* server = BLEDevice::createServer();
server->setCallbacks(new BleKeyboardCallbacks());
// create an HID device
hid = new BLEHIDDevice(server);
input = hid->inputReport(1); // report ID
output = hid->outputReport(1); // report ID
output->setCallbacks(new OutputCallbacks());
// set manufacturer name
hid->manufacturer()->setValue("Maker Community");
// set USB vendor and product ID
hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
// information about HID device: device is not localized, device can be
// connected
hid->hidInfo(0x00, 0x02);
// Security: device requires bonding
BLESecurity* security = new BLESecurity();
security->setAuthenticationMode(ESP_LE_AUTH_BOND);
// set report map
hid->reportMap((uint8_t*)REPORT_MAP, sizeof(REPORT_MAP));
hid->startServices();
// set battery level to 100%
hid->setBatteryLevel(100);
// advertise the services
BLEAdvertising* advertising = server->getAdvertising();
advertising->setAppearance(HID_KEYBOARD);
advertising->addServiceUUID(hid->hidService()->getUUID());
advertising->addServiceUUID(hid->deviceInfo()->getUUID());
advertising->addServiceUUID(hid->batteryService()->getUUID());
advertising->start();
Serial.println("BLE ready");
delay(portMAX_DELAY);
};
void typeText(const char* text) {
int len = strlen(text);
for (int i = 0; i < len; i++) {
// translate character to key combination
uint8_t val = (uint8_t)text[i];
if (val > KEYMAP_SIZE)
continue; // character not available on keyboard - skip
KEYMAP map = keymap[val];
// create input report
InputReport report = {.modifiers = map.modifier,
.reserved = 0,
.pressedKeys = {map.usage, 0, 0, 0, 0, 0}};
// send the input report
input->setValue((uint8_t*)&report, sizeof(report));
input->notify();
delay(5);
// release all keys between two characters; otherwise two identical
// consecutive characters are treated as just one key press
input->setValue((uint8_t*)&NO_KEY_PRESSED, sizeof(NO_KEY_PRESSED));
input->notify();
delay(5);
}
}