uint8_t getOemKey() { return keyOEM; } | uint8_t getOemKey() { return keyOEM; } | ||||
void attachPress(void (*f)(int unicode)) { keyPressedFunction = f; } | void attachPress(void (*f)(int unicode)) { keyPressedFunction = f; } | ||||
void attachRelease(void (*f)(int unicode)) { keyReleasedFunction = f; } | void attachRelease(void (*f)(int unicode)) { keyReleasedFunction = f; } | ||||
void attachRawPress(void (*f)(uint8_t keycode)) { rawKeyPressedFunction = f; } | |||||
void attachRawRelease(void (*f)(uint8_t keycode)) { rawKeyReleasedFunction = f; } | |||||
void LEDS(uint8_t leds); | void LEDS(uint8_t leds); | ||||
uint8_t LEDS() {return leds_.byte;} | uint8_t LEDS() {return leds_.byte;} | ||||
void updateLEDS(void); | void updateLEDS(void); | ||||
void key_release(uint32_t mod, uint32_t key); | void key_release(uint32_t mod, uint32_t key); | ||||
void (*keyPressedFunction)(int unicode); | void (*keyPressedFunction)(int unicode); | ||||
void (*keyReleasedFunction)(int unicode); | void (*keyReleasedFunction)(int unicode); | ||||
void (*rawKeyPressedFunction)(uint8_t keycode) = nullptr; | |||||
void (*rawKeyReleasedFunction)(uint8_t keycode) = nullptr; | |||||
Pipe_t *datapipe; | Pipe_t *datapipe; | ||||
setup_t setup; | setup_t setup; | ||||
uint8_t report[8]; | uint8_t report[8]; |
Serial.println("USB Host Testing"); | Serial.println("USB Host Testing"); | ||||
myusb.begin(); | myusb.begin(); | ||||
keyboard1.attachPress(OnPress); | keyboard1.attachPress(OnPress); | ||||
keyboard1.attachRawPress(OnRawPress); | |||||
keyboard1.attachRawRelease(OnRawRelease); | |||||
keyboard2.attachPress(OnPress); | keyboard2.attachPress(OnPress); | ||||
midi1.setHandleNoteOff(OnNoteOff); | midi1.setHandleNoteOff(OnNoteOff); | ||||
midi1.setHandleNoteOn(OnNoteOn); | midi1.setHandleNoteOn(OnNoteOn); | ||||
//Serial.println(); | //Serial.println(); | ||||
} | } | ||||
void OnRawPress(uint8_t keycode) | |||||
{ | |||||
Serial.print("raw key press: "); | |||||
Serial.println((int)keycode); | |||||
} | |||||
void OnRawRelease(uint8_t keycode) | |||||
{ | |||||
Serial.print("raw key release: "); | |||||
Serial.println((int)keycode); | |||||
} | |||||
void OnNoteOn(byte channel, byte note, byte velocity) | void OnNoteOn(byte channel, byte note, byte velocity) | ||||
{ | { | ||||
Serial.print("Note On, ch="); | Serial.print("Note On, ch="); |
uint32_t key = prev_report[i]; | uint32_t key = prev_report[i]; | ||||
if (key >= 4 && !contains(key, report)) { | if (key >= 4 && !contains(key, report)) { | ||||
key_release(prev_report[0], key); | key_release(prev_report[0], key); | ||||
if (rawKeyReleasedFunction) { | |||||
rawKeyReleasedFunction(key); | |||||
} | |||||
} | |||||
} | |||||
if (rawKeyReleasedFunction) { | |||||
// each modifier key is represented by a bit in the first byte | |||||
for (int i = 0; i < 8; ++i) | |||||
{ | |||||
uint8_t keybit = 1 << i; | |||||
if ((prev_report[0] & keybit) && !(report[0] & keybit)) { | |||||
rawKeyReleasedFunction(103 + i); | |||||
} | |||||
} | } | ||||
} | } | ||||
for (int i=2; i < 8; i++) { | for (int i=2; i < 8; i++) { | ||||
uint32_t key = report[i]; | uint32_t key = report[i]; | ||||
if (key >= 4 && !contains(key, prev_report)) { | if (key >= 4 && !contains(key, prev_report)) { | ||||
key_press(report[0], key); | key_press(report[0], key); | ||||
if (rawKeyPressedFunction) { | |||||
rawKeyPressedFunction(key); | |||||
} | |||||
} | |||||
} | |||||
if (rawKeyPressedFunction) { | |||||
for (int i = 0; i < 8; ++i) | |||||
{ | |||||
uint8_t keybit = 1 << i; | |||||
if (!(prev_report[0] & keybit) && (report[0] & keybit)) { | |||||
rawKeyPressedFunction(103 + i); | |||||
} | |||||
} | } | ||||
} | } | ||||
memcpy(prev_report, report, 8); | memcpy(prev_report, report, 8); |