mirror of
https://github.com/gdsports/USBHost_t36
synced 2024-12-21 23:08:55 -05:00
new function hooks in KeyboardController class
in KeyboardController class: - added a new hook, reportReaderFunction, to enable the client code to read raw incoming keybooard state reports (called from method new_data) - added a new hook, modifiersChangedFunction, to enable the client code to be notified when modifiers state is changed but no key is pressed or released - initialized all function pointers to zero in both KeyboardController constructors - changed led state change behavior: leds are updated when keys are pressed, not when they are released, as with most keyboards (methods key_release and key_press) - added hooks to be notified of key presses and key releases with oem codes instead of unicode codepoints (keyPressedRawFunction and keyReleasedRawFunction).
This commit is contained in:
parent
207ffd7682
commit
44949b0bed
@ -680,8 +680,24 @@ typedef union {
|
||||
uint8_t byte;
|
||||
} KBDLeds_t;
|
||||
public:
|
||||
KeyboardController(USBHost &host) { init(); }
|
||||
KeyboardController(USBHost *host) { init(); }
|
||||
KeyboardController(USBHost &host) :
|
||||
keyPressedFunction(NULL),
|
||||
keyReleasedFunction(NULL),
|
||||
keyPressedRawFunction(NULL),
|
||||
keyReleasedRawFunction(NULL),
|
||||
modifiersChangedFunction(NULL),
|
||||
extrasKeyPressedFunction(NULL),
|
||||
extrasKeyReleasedFunction(NULL)
|
||||
{ init(); }
|
||||
KeyboardController(USBHost *host) :
|
||||
keyPressedFunction(NULL),
|
||||
keyReleasedFunction(NULL),
|
||||
keyPressedRawFunction(NULL),
|
||||
keyReleasedRawFunction(NULL),
|
||||
modifiersChangedFunction(NULL),
|
||||
extrasKeyPressedFunction(NULL),
|
||||
extrasKeyReleasedFunction(NULL)
|
||||
{ init(); }
|
||||
|
||||
// Some methods are in both public classes so we need to figure out which one to use
|
||||
operator bool() { return (device != nullptr); }
|
||||
@ -691,6 +707,10 @@ public:
|
||||
uint8_t getOemKey() { return keyOEM; }
|
||||
void attachPress(void (*f)(int unicode)) { keyPressedFunction = f; }
|
||||
void attachRelease(void (*f)(int unicode)) { keyReleasedFunction = f; }
|
||||
void attachPressRaw(void (*f)(uint8_t oem_key, uint8_t mod)) { keyPressedRawFunction = f; }
|
||||
void attachReleaseRaw(void (*f)(uint8_t oem_key, uint8_t mod)) { keyReleasedRawFunction = f; }
|
||||
void attachModifiersChange(void (*f) (uint8_t modifierState)) {modifiersChangedFunction = f; }
|
||||
void attachReportReader(void (*f)(const uint8_t report[8])) {reportReaderFunction = f; }
|
||||
void LEDS(uint8_t leds);
|
||||
uint8_t LEDS() {return leds_.byte;}
|
||||
void updateLEDS(void);
|
||||
@ -729,6 +749,10 @@ private:
|
||||
void key_release(uint32_t mod, uint32_t key);
|
||||
void (*keyPressedFunction)(int unicode);
|
||||
void (*keyReleasedFunction)(int unicode);
|
||||
void (*keyPressedRawFunction)(uint8_t oem_key, uint8_t mod);
|
||||
void (*keyReleasedRawFunction)(uint8_t oem_key, uint8_t mod);
|
||||
void (*modifiersChangedFunction)(uint8_t modifierState);
|
||||
void (*reportReaderFunction)(const uint8_t report[8]);
|
||||
Pipe_t *datapipe;
|
||||
setup_t setup;
|
||||
uint8_t report[8];
|
||||
|
41
keyboard.cpp
41
keyboard.cpp
@ -194,6 +194,7 @@ void KeyboardController::new_data(const Transfer_t *transfer)
|
||||
println("KeyboardController Callback (member)");
|
||||
print(" KB Data: ");
|
||||
print_hexbytes(transfer->buffer, 8);
|
||||
if(reportReaderFunction) reportReaderFunction(report);
|
||||
for (int i=2; i < 8; i++) {
|
||||
uint32_t key = prev_report[i];
|
||||
if (key >= 4 && !contains(key, report)) {
|
||||
@ -206,6 +207,9 @@ void KeyboardController::new_data(const Transfer_t *transfer)
|
||||
key_press(report[0], key);
|
||||
}
|
||||
}
|
||||
if(modifiersChangedFunction && (prev_report[0] != report[0])) {
|
||||
modifiersChangedFunction(report[0]);
|
||||
}
|
||||
memcpy(prev_report, report, 8);
|
||||
queue_Data_Transfer(datapipe, report, 8, this);
|
||||
}
|
||||
@ -238,21 +242,10 @@ void KeyboardController::key_press(uint32_t mod, uint32_t key)
|
||||
println(" press, key=", key);
|
||||
modifiers = mod;
|
||||
keyOEM = key;
|
||||
keyCode = convert_to_unicode(mod, key);
|
||||
println(" unicode = ", keyCode);
|
||||
if (keyPressedFunction) {
|
||||
keyPressedFunction(keyCode);
|
||||
} else {
|
||||
keyPressed();
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardController::key_release(uint32_t mod, uint32_t key)
|
||||
{
|
||||
// TODO: queue events, perform callback from Task
|
||||
println(" release, key=", key);
|
||||
modifiers = mod;
|
||||
keyOEM = key;
|
||||
if(keyPressedRawFunction) {
|
||||
keyPressedRawFunction(keyOEM, modifiers);
|
||||
}
|
||||
|
||||
// Look for modifier keys
|
||||
if (key == M(KEY_NUM_LOCK)) {
|
||||
@ -264,6 +257,26 @@ void KeyboardController::key_release(uint32_t mod, uint32_t key)
|
||||
} else if (key == M(KEY_SCROLL_LOCK)) {
|
||||
scrollLock(!leds_.scrollLock);
|
||||
} else {
|
||||
keyCode = convert_to_unicode(mod, key);
|
||||
println(" unicode = ", keyCode);
|
||||
if (keyPressedFunction) {
|
||||
keyPressedFunction(keyCode);
|
||||
} else {
|
||||
keyPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardController::key_release(uint32_t mod, uint32_t key)
|
||||
{
|
||||
// TODO: queue events, perform callback from Task
|
||||
println(" release, key=", key);
|
||||
modifiers = mod;
|
||||
keyOEM = key;
|
||||
if(keyReleasedRawFunction) {
|
||||
keyReleasedRawFunction(keyOEM, modifiers);
|
||||
}
|
||||
if (key != M(KEY_NUM_LOCK) && key != M(KEY_CAPS_LOCK) && key != M(KEY_SCROLL_LOCK)) {
|
||||
keyCode = convert_to_unicode(mod, key);
|
||||
if (keyReleasedFunction) {
|
||||
keyReleasedFunction(keyCode);
|
||||
|
Loading…
Reference in New Issue
Block a user