Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/bt_keyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <vector>
#include <map>
#include <string>

// #include "esp32-hal-bt.h"

Expand All @@ -66,6 +67,15 @@ class BTKeyboard
R_META = 0x80
};

enum KeyLed : uint8_t
{
KEYBOARD_LED_NUMLOCK = 1 << 0,
KEYBOARD_LED_CAPSLOCK = 1 << 1,
KEYBOARD_LED_SCROLLLOCK = 1 << 2,
KEYBOARD_LED_COMPOSE = 1 << 3,
KEYBOARD_LED_KANA = 1 << 4
};

const uint8_t CTRL_MASK = ((uint8_t)KeyModifier::L_CTRL) | ((uint8_t)KeyModifier::R_CTRL);
const uint8_t SHIFT_MASK = ((uint8_t)KeyModifier::L_SHIFT) | ((uint8_t)KeyModifier::R_SHIFT);
const uint8_t ALT_MASK = ((uint8_t)KeyModifier::L_ALT) | ((uint8_t)KeyModifier::R_ALT);
Expand Down Expand Up @@ -119,6 +129,7 @@ class BTKeyboard

static SemaphoreHandle_t bt_hidh_cb_semaphore;
static SemaphoreHandle_t ble_hidh_cb_semaphore;
static SemaphoreHandle_t led_device_map_semaphore;

struct esp_hid_scan_result_t
{
Expand Down Expand Up @@ -216,6 +227,14 @@ class BTKeyboard
static std::map<std::pair<esp_hidh_dev_t *, uint16_t>, hid_report_mouse> mouse_reports;
static KeyInfo infoKey;

typedef struct {
esp_hidh_dev_t *dev;
size_t map_index;
size_t report_id;
} led_device_info;

static std::map<std::string, led_device_info> led_device_map;

typedef enum
{
PARSE_WAIT_USAGE_PAGE,
Expand Down Expand Up @@ -293,6 +312,8 @@ class BTKeyboard
esp_err_t start_bt_scan(uint32_t seconds);
esp_err_t esp_hid_scan(uint32_t seconds, size_t *num_results, esp_hid_scan_result_t **results, bool enable_bt_classic);

static bool find_output_report(esp_hidh_dev_t *dev, size_t &map_index, size_t &report_id);

inline void set_battery_level(uint8_t level) { battery_level = level; }

void push_key(uint8_t *keys, uint8_t size);
Expand Down Expand Up @@ -351,6 +372,8 @@ class BTKeyboard

void quick_reconnect(void);

static void set_leds(uint8_t leds);

static bool isConnected; // hidh callback event CLOSE turns this false when kb disconnects
static bool btFound; // found a BT (not BLE) device during scan, let's wait for pairing
};
25 changes: 24 additions & 1 deletion include/esp32-ps2dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "esp_timer.h"
#include "esp_log.h"
#include <stdint.h>
#include "scan_codes_set_2.h"
#include "scan_codes.h"
#include <nvs_flash.h>
#include <string>

Expand Down Expand Up @@ -184,6 +184,13 @@ namespace esp32_ps2dev
RESET = 0xFF,
RESEND = 0xFE,
ACK = 0xFA,
SET_SPECIFIC_KEY_TO_MAKE_ONLY = 0xFD,
SET_SPECIFIC_KEY_TO_MAKE_RELEASE = 0xFC,
SET_SPECIFIC_KEY_TO_TYPEMATIC_AUTOREPEAT_ONLY=0xFB,
SET_ALL_KEYS_TO_TYPEMATIC_AUTOREPEAT_MAKE_RELEASE = 0xFA,
SET_ALL_KEYS_TO_MAKE_ONLY = 0xF9,
SET_ALL_KEYS_TO_MAKE_RELEASE = 0xF8,
SET_ALL_KEYS_TO_TYPEMATIC_AUTOREPEAT_ONLY = 0xF7,
SET_DEFAULTS = 0xF6,
DISABLE_DATA_REPORTING = 0xF5,
ENABLE_DATA_REPORTING = 0xF4,
Expand All @@ -194,6 +201,15 @@ namespace esp32_ps2dev
SET_RESET_LEDS = 0xED,
BAT_SUCCESS = 0xAA,
};

enum KeyLed: uint8_t {
KEYBOARD_LED_SCROLLLOCK = 1 << 0,
KEYBOARD_LED_NUMLOCK = 1 << 1,
KEYBOARD_LED_CAPSLOCK = 1 << 2,
};

typedef void (*_leds_callback)(uint8_t);

void begin();
bool data_reporting_enabled();
bool is_scroll_lock_led_on();
Expand All @@ -207,11 +223,18 @@ namespace esp32_ps2dev
void keyHid_send(uint8_t btkey, bool keyDown);
void keyHid_send_CCONTROL(uint16_t btkey, bool keyDown);

void set_leds_callback(_leds_callback cb) { leds_callback = cb; }
void trigger_leds_callback(uint8_t leds) { if (leds_callback != nullptr) leds_callback(leds); }

protected:
bool _data_reporting_enabled = true;
bool _led_scroll_lock = false;
bool _led_num_lock = false;
bool _led_caps_lock = false;
int _scan_code_set = 2;
// scancode set 3 parameters
bool _all_keys_to_make_only = false;
_leds_callback leds_callback = nullptr;
};

void _taskfn_process_host_request(void *arg);
Expand Down
137 changes: 137 additions & 0 deletions include/scan_codes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#pragma once

namespace esp32_ps2dev {
namespace scancodes {

typedef enum {
K_A,
K_B,
K_C,
K_D,
K_E,
K_F,
K_G,
K_H,
K_I,
K_J,
K_K,
K_L,
K_M,
K_N,
K_O,
K_P,
K_Q,
K_R,
K_S,
K_T,
K_U,
K_V,
K_W,
K_X,
K_Y,
K_Z,
K_0,
K_1,
K_2,
K_3,
K_4,
K_5,
K_6,
K_7,
K_8,
K_9,
K_BACKQUOTE,
K_MINUS,
K_EQUALS,
K_BACKSLASH,
K_BACKSPACE,
K_SPACE,
K_TAB,
K_CAPSLOCK,
K_LSHIFT,
K_LCTRL,
K_LSUPER,
K_LALT,
K_RSHIFT,
K_RCTRL,
K_RSUPER,
K_RALT,
K_MENU,
K_RETURN,
K_ESCAPE,
K_F1,
K_F2,
K_F3,
K_F4,
K_F5,
K_F6,
K_F7,
K_F8,
K_F9,
K_F10,
K_F11,
K_F12,
K_PRINT,
K_SCROLLOCK,
K_PAUSE,
K_LEFTBRACKET,
K_INSERT,
K_HOME,
K_PAGEUP,
K_DELETE,
K_END,
K_PAGEDOWN,
K_UP,
K_LEFT,
K_DOWN,
K_RIGHT,
K_NUMLOCK,
K_KP_DIVIDE,
K_KP_MULTIPLY,
K_KP_MINUS,
K_KP_PLUS,
K_KP_ENTER,
K_KP_PERIOD,
K_KP0,
K_KP1,
K_KP2,
K_KP3,
K_KP4,
K_KP5,
K_KP6,
K_KP7,
K_KP8,
K_KP9,
K_RIGHTBRACKET,
K_SEMICOLON,
K_QUOTE,
K_COMMA,
K_PERIOD,
K_SLASH,
K_ACPI_POWER,
K_ACPI_SLEEP,
K_ACPI_WAKE,
K_MEDIA_NEXT_TRACK,
K_MEDIA_PREV_TRACK,
K_MEDIA_STOP,
K_MEDIA_PLAY_PAUSE,
K_MEDIA_MUTE,
K_MEDIA_VOLUME_UP,
K_MEDIA_VOLUME_DOWN,
K_MEDIA_MEDIA_SELECT,
K_MEDIA_EMAIL,
K_MEDIA_CALC,
K_MEDIA_MY_COMPUTER,
K_MEDIA_WWW_SEARCH,
K_MEDIA_WWW_HOME,
K_MEDIA_WWW_BACK,
K_MEDIA_WWW_FORWARD,
K_MEDIA_WWW_STOP,
K_MEDIA_WWW_REFRESH,
K_MEDIA_WWW_FAVORITES,
} Key;

#include "scan_codes_set_2.h"
#include "scan_codes_set_3.h"
} // namespace scancodes
} // namespace esp32_ps2dev
Loading