summaryrefslogtreecommitdiff
path: root/tmk_core/protocol/lufa/lufa.c
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core/protocol/lufa/lufa.c')
-rw-r--r--tmk_core/protocol/lufa/lufa.c167
1 files changed, 94 insertions, 73 deletions
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index 1d019d1695..92070a8fa3 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -145,9 +145,7 @@ static void send_keyboard(report_keyboard_t *report);
static void send_mouse(report_mouse_t *report);
static void send_system(uint16_t data);
static void send_consumer(uint16_t data);
-host_driver_t lufa_driver = {
- keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer,
-};
+host_driver_t lufa_driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
#ifdef VIRTSER_ENABLE
// clang-format off
@@ -377,45 +375,44 @@ const WebUSB_URL_Descriptor_t PROGMEM WebUSB_LandingPage = WEBUSB_URL_DESCRIPTOR
void send_joystick_packet(joystick_t *joystick) {
uint8_t timeout = 255;
- joystick_report_t r = {
+ static joystick_report_t r;
+ r = (joystick_report_t) {
# if JOYSTICK_AXES_COUNT > 0
.axes =
- {
- joystick->axes[0],
+ { joystick->axes[0],
# if JOYSTICK_AXES_COUNT >= 2
- joystick->axes[1],
+ joystick->axes[1],
# endif
# if JOYSTICK_AXES_COUNT >= 3
- joystick->axes[2],
+ joystick->axes[2],
# endif
# if JOYSTICK_AXES_COUNT >= 4
- joystick->axes[3],
+ joystick->axes[3],
# endif
# if JOYSTICK_AXES_COUNT >= 5
- joystick->axes[4],
+ joystick->axes[4],
# endif
# if JOYSTICK_AXES_COUNT >= 6
- joystick->axes[5],
+ joystick->axes[5],
# endif
- },
+ },
# endif // JOYSTICK_AXES_COUNT>0
# if JOYSTICK_BUTTON_COUNT > 0
- .buttons =
- {
- joystick->buttons[0],
+ .buttons = {
+ joystick->buttons[0],
# if JOYSTICK_BUTTON_COUNT > 8
- joystick->buttons[1],
+ joystick->buttons[1],
# endif
# if JOYSTICK_BUTTON_COUNT > 16
- joystick->buttons[2],
+ joystick->buttons[2],
# endif
# if JOYSTICK_BUTTON_COUNT > 24
- joystick->buttons[3],
+ joystick->buttons[3],
# endif
- }
+ }
# endif // JOYSTICK_BUTTON_COUNT>0
};
@@ -595,6 +592,11 @@ void EVENT_USB_Device_ConfigurationChanged(void) {
/* Setup joystick endpoint */
ConfigSuccess &= Endpoint_ConfigureEndpoint((JOYSTICK_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
#endif
+
+#if defined(DIGITIZER_ENABLE) && !defined(DIGITIZER_SHARED_EP)
+ /* Setup digitizer endpoint */
+ ConfigSuccess &= Endpoint_ConfigureEndpoint((DIGITIZER_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, DIGITIZER_EPSIZE, 1);
+#endif
}
/* FIXME: Expose this table in the docs somehow
@@ -782,9 +784,7 @@ static void send_keyboard(report_keyboard_t *report) {
uint8_t timeout = 255;
#ifdef BLUETOOTH_ENABLE
- uint8_t where = where_to_send();
-
- if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
+ if (where_to_send() == OUTPUT_BLUETOOTH) {
# ifdef MODULE_ADAFRUIT_BLE
adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys));
# elif MODULE_RN42
@@ -797,9 +797,6 @@ static void send_keyboard(report_keyboard_t *report) {
serial_send(report->keys[i]);
}
# endif
- }
-
- if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
return;
}
#endif
@@ -840,9 +837,7 @@ static void send_mouse(report_mouse_t *report) {
uint8_t timeout = 255;
# ifdef BLUETOOTH_ENABLE
- uint8_t where = where_to_send();
-
- if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
+ if (where_to_send() == OUTPUT_BLUETOOTH) {
# ifdef MODULE_ADAFRUIT_BLE
// FIXME: mouse buttons
adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons);
@@ -857,9 +852,6 @@ static void send_mouse(report_mouse_t *report) {
serial_send(report->h); // should try sending the wheel h here
serial_send(0x00);
# endif
- }
-
- if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
return;
}
# endif
@@ -889,7 +881,8 @@ static void send_extra(uint8_t report_id, uint16_t data) {
if (USB_DeviceState != DEVICE_STATE_Configured) return;
- report_extra_t r = {.report_id = report_id, .usage = data};
+ static report_extra_t r;
+ r = (report_extra_t){.report_id = report_id, .usage = data};
Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
/* Check if write ready for a polling interval around 10ms */
@@ -918,9 +911,13 @@ static void send_system(uint16_t data) {
static void send_consumer(uint16_t data) {
#ifdef EXTRAKEY_ENABLE
# ifdef BLUETOOTH_ENABLE
+<<<<<<< HEAD
uint8_t where = where_to_send();
if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) {
+=======
+ if (where_to_send() == OUTPUT_BLUETOOTH) {
+>>>>>>> 0.12.52~1
# ifdef MODULE_ADAFRUIT_BLE
adafruit_ble_send_consumer_key(data);
# elif MODULE_RN42
@@ -934,9 +931,12 @@ static void send_consumer(uint16_t data) {
serial_send(bitmap & 0xFF);
serial_send((bitmap >> 8) & 0xFF);
# endif
+<<<<<<< HEAD
}
if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) {
+=======
+>>>>>>> 0.12.52~1
return;
}
# endif
@@ -955,9 +955,14 @@ static void send_consumer(uint16_t data) {
* FIXME: Needs doc
*/
int8_t sendchar(uint8_t c) {
+<<<<<<< HEAD
// Not wait once timeouted.
+=======
+ // Do not wait if the previous write has timed_out.
+>>>>>>> 0.12.52~1
// Because sendchar() is called so many times, waiting each call causes big lag.
- static bool timeouted = false;
+ // The `timed_out` state is an approximation of the ideal `is_listener_disconnected?` state.
+ static bool timed_out = false;
// prevents Console_Task() from running during sendchar() runs.
// or char will be lost. These two function is mutually exclusive.
@@ -971,11 +976,11 @@ int8_t sendchar(uint8_t c) {
goto ERROR_EXIT;
}
- if (timeouted && !Endpoint_IsReadWriteAllowed()) {
+ if (timed_out && !Endpoint_IsReadWriteAllowed()) {
goto ERROR_EXIT;
}
- timeouted = false;
+ timed_out = false;
uint8_t timeout = SEND_TIMEOUT;
while (!Endpoint_IsReadWriteAllowed()) {
@@ -986,7 +991,7 @@ int8_t sendchar(uint8_t c) {
goto ERROR_EXIT;
}
if (!(timeout--)) {
- timeouted = true;
+ timed_out = true;
goto ERROR_EXIT;
}
_delay_ms(1);
@@ -1108,6 +1113,23 @@ void virtser_send(const uint8_t byte) {
}
#endif
+void send_digitizer(report_digitizer_t *report) {
+#ifdef DIGITIZER_ENABLE
+ uint8_t timeout = 255;
+
+ if (USB_DeviceState != DEVICE_STATE_Configured) return;
+
+ Endpoint_SelectEndpoint(DIGITIZER_IN_EPNUM);
+
+ /* Check if write ready for a polling interval around 10ms */
+ while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
+ if (!Endpoint_IsReadWriteAllowed()) return;
+
+ Endpoint_Write_Stream_LE(report, sizeof(report_digitizer_t), NULL);
+ Endpoint_ClearIN();
+#endif
+}
+
/*******************************************************************************
* main
******************************************************************************/
@@ -1120,8 +1142,13 @@ static void setup_mcu(void) {
MCUSR &= ~_BV(WDRF);
wdt_disable();
- /* Disable clock division */
+// For boards running at 3.3V and crystal at 16 MHz
+#if (F_CPU == 8000000 && F_USB == 16000000)
+ /* Divide clock by 2 */
+ clock_prescale_set(clock_div_2);
+#else /* Disable clock division */
clock_prescale_set(clock_div_1);
+#endif
}
/** \brief Setup USB
@@ -1136,21 +1163,18 @@ static void setup_usb(void) {
// for Console_Task
USB_Device_EnableSOFEvents();
- print_set_sendchar(sendchar);
}
-/** \brief Main
- *
- * FIXME: Needs doc
- */
-int main(void) __attribute__((weak));
-int main(void) {
+void protocol_setup(void) {
#ifdef MIDI_ENABLE
setup_midi();
#endif
setup_mcu();
keyboard_setup();
+}
+
+void protocol_init(void) {
setup_usb();
sei();
@@ -1184,48 +1208,50 @@ int main(void) {
#endif
print("Keyboard start.\n");
- while (1) {
+}
+
+void protocol_task(void) {
#if !defined(NO_USB_STARTUP_CHECK)
- if (USB_DeviceState == DEVICE_STATE_Suspended) {
- print("[s]");
- while (USB_DeviceState == DEVICE_STATE_Suspended) {
- suspend_power_down();
- if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
- USB_Device_SendRemoteWakeup();
- clear_keyboard();
+ if (USB_DeviceState == DEVICE_STATE_Suspended) {
+ print("[s]");
+ while (USB_DeviceState == DEVICE_STATE_Suspended) {
+ suspend_power_down();
+ if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
+ USB_Device_SendRemoteWakeup();
+ clear_keyboard();
# if USB_SUSPEND_WAKEUP_DELAY > 0
- // Some hubs, kvm switches, and monitors do
- // weird things, with USB device state bouncing
- // around wildly on wakeup, yielding race
- // conditions that can corrupt the keyboard state.
- //
- // Pause for a while to let things settle...
- wait_ms(USB_SUSPEND_WAKEUP_DELAY);
+ // Some hubs, kvm switches, and monitors do
+ // weird things, with USB device state bouncing
+ // around wildly on wakeup, yielding race
+ // conditions that can corrupt the keyboard state.
+ //
+ // Pause for a while to let things settle...
+ wait_ms(USB_SUSPEND_WAKEUP_DELAY);
# endif
- }
}
- suspend_wakeup_init();
}
+ suspend_wakeup_init();
+ }
#endif
- keyboard_task();
+ keyboard_task();
#ifdef MIDI_ENABLE
- MIDI_Device_USBTask(&USB_MIDI_Interface);
+ MIDI_Device_USBTask(&USB_MIDI_Interface);
#endif
#ifdef MODULE_ADAFRUIT_BLE
- adafruit_ble_task();
+ adafruit_ble_task();
#endif
#ifdef VIRTSER_ENABLE
- virtser_task();
- CDC_Device_USBTask(&cdc_device);
+ virtser_task();
+ CDC_Device_USBTask(&cdc_device);
#endif
#ifdef RAW_ENABLE
- raw_hid_task();
+ raw_hid_task();
#endif
#ifdef WEBUSB_ENABLE
@@ -1233,13 +1259,8 @@ int main(void) {
#endif
#if !defined(INTERRUPT_CONTROL_ENDPOINT)
- USB_USBTask();
+ USB_USBTask();
#endif
-
- // Run housekeeping
- housekeeping_task_kb();
- housekeeping_task_user();
- }
}
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint16_t wIndex, const void **const DescriptorAddress) { return get_usb_descriptor(wValue, wIndex, DescriptorAddress); }