Last active
July 30, 2022 04:43
-
-
Save chegewara/677819eaf7ab5620c8e13bbdd65dc2a0 to your computer and use it in GitHub Desktop.
ESP32 arduino library with new HID class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <BLEDevice.h> | |
#include <BLEUtils.h> | |
#include <BLEServer.h> | |
#include "BLE2902.h" | |
#include "BLEHIDDevice.h" | |
#include "HIDTypes.h" | |
#include <driver/adc.h> | |
static BLEHIDDevice* hid; | |
BLECharacteristic* input; | |
uint8_t buttons = 0; | |
bool connected = false; | |
class MyCallbacks : public BLEServerCallbacks { | |
void onConnect(BLEServer* pServer){ | |
connected = true; | |
} | |
void onDisconnect(BLEServer* pServer){ | |
connected = false; | |
} | |
}; | |
void taskServer(void*){ | |
BLEDevice::init("ESP32-Mouse"); | |
BLEServer *pServer = BLEDevice::createServer(); | |
pServer->setCallbacks(new MyCallbacks()); | |
hid = new BLEHIDDevice(pServer); | |
input = hid->inputReport(1); // <-- input REPORTID from report map | |
std::string name = "chegewara"; | |
hid->manufacturer()->setValue(name); | |
hid->pnp(0x02, 0xe502, 0xa111, 0x0210); | |
hid->hidInfo(0x00,0x01); | |
BLESecurity *pSecurity = new BLESecurity(); | |
pSecurity->setKeySize(); | |
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND); | |
const uint8_t report[] = { | |
0x05, 0x01, // Usage Page (Generic Desktop Ctrls) | |
0x09, 0x02, // Usage (Mouse) | |
0xA1, 0x01, // Collection (Application) | |
0x85, 0x01, // Report ID (1) | |
0x09, 0x01, // Usage (Pointer) | |
0xA1, 0x00, // Collection (Physical) | |
0x05, 0x09, // Usage Page (Button) | |
0x19, 0x01, // Usage Minimum (0x01) | |
0x29, 0x03, // Usage Maximum (0x03) | |
0x25, 0x01, // Logical Maximum (1) | |
0x75, 0x01, // Report Size (1) | |
0x95, 0x03, // Report Count (3) | |
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) | |
0x75, 0x05, // Report Size (5) | |
0x95, 0x01, // Report Count (1) | |
0x81, 0x01, // Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) | |
0x05, 0x01, // Usage Page (Generic Desktop Ctrls) | |
0x09, 0x30, // Usage (X) | |
0x26, 0x05,0x56, // Logical Maximum (127) | |
0x09, 0x31, // Usage (Y) | |
0x26, 0x03,0x00, // Logical Maximum (127) | |
0x09, 0x38, // Usage (Wheel) | |
0x15, 0x00, // Logical Minimum (-127) | |
0x25, 0x7f, // Logical Maximum (127) | |
0x75, 0x08, // Report Size (8) | |
0x95, 0x03, // Report Count (3) | |
0x81, 0x02, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position) | |
0xC0, // End Collection | |
0xC0, // End Collection | |
// 52 bytes | |
}; | |
hid->reportMap((uint8_t*)report, sizeof(report)); | |
hid->startServices(); | |
BLEAdvertising *pAdvertising = pServer->getAdvertising(); | |
pAdvertising->setAppearance(HID_MOUSE); | |
pAdvertising->addServiceUUID(hid->hidService()->getUUID()); | |
pAdvertising->start(); | |
hid->setBatteryLevel(5); | |
ESP_LOGD(LOG_TAG, "Advertising started!"); | |
delay(portMAX_DELAY); | |
}; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("Starting BLE work!"); | |
adc1_config_width(ADC_WIDTH_BIT_11); | |
adc1_config_channel_atten((adc1_channel_t)36, ADC_ATTEN_DB_11); | |
adc1_config_channel_atten((adc1_channel_t)39, ADC_ATTEN_DB_11); | |
pinMode(12, INPUT_PULLDOWN); | |
attachInterrupt(digitalPinToInterrupt(12), click, CHANGE); | |
pinMode(13, INPUT_PULLDOWN); | |
attachInterrupt(digitalPinToInterrupt(13), click, CHANGE); | |
pinMode(32, INPUT_PULLDOWN); | |
attachInterrupt(digitalPinToInterrupt(32), click, CHANGE); | |
xTaskCreate(taskServer, "server", 20000, NULL, 5, NULL); | |
} | |
void loop() { | |
if(connected){ | |
uint16_t x1 = analogRead(36); | |
uint16_t y1 = analogRead(39); | |
Serial.println(x1); | |
uint8_t val[] = {buttons, x1>>5, y1>>5, 0}; | |
input->setValue(val, 4); | |
input->notify(); | |
} | |
delay(50); | |
} | |
IRAM_ATTR void click(){ | |
buttons = digitalRead(12)<<0 | digitalRead(13)<<1 | digitalRead(32)<<2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
~/.platformio/packages/framework-arduinoespressif32/tools/sdk/include/driver/driver/adc.h
36 and 39 are conversing to int and rest 36 and 39
but there's no channel#36.