summaryrefslogtreecommitdiff
path: root/pjrc
diff options
context:
space:
mode:
Diffstat (limited to 'pjrc')
-rw-r--r--pjrc/host.c135
-rwxr-xr-xpjrc/usb.c8
-rw-r--r--pjrc/usb.h21
-rw-r--r--pjrc/usb_keyboard.c183
-rw-r--r--pjrc/usb_keyboard.h68
5 files changed, 172 insertions, 243 deletions
diff --git a/pjrc/host.c b/pjrc/host.c
new file mode 100644
index 0000000000..7247288bd7
--- /dev/null
+++ b/pjrc/host.c
@@ -0,0 +1,135 @@
+#include <stdint.h>
+#include "usb_keycodes.h"
+#include "usb_keyboard.h"
+#include "usb_mouse.h"
+#include "debug.h"
+#include "host.h"
+
+
+#ifdef USB_NKRO_ENABLE
+bool keyboard_nkro = false;
+#endif
+
+static report_keyboard_t report0;
+static report_keyboard_t report1;
+report_keyboard_t *keyboard_report = &report0;
+report_keyboard_t *keyboard_report_prev = &report1;
+
+static inline void add_key_byte(uint8_t code);
+static inline void add_key_bit(uint8_t code);
+
+
+uint8_t host_keyboard_leds(void)
+{
+ return usb_keyboard_leds;
+}
+
+/* keyboard report operations */
+void host_add_key(uint8_t key)
+{
+#ifdef USB_NKRO_ENABLE
+ if (keyboard_nkro) {
+ add_key_bit(key);
+ return;
+ }
+#endif
+ add_key_byte(key);
+}
+
+void host_add_mod_bit(uint8_t mod)
+{
+ keyboard_report->mods |= mod;
+}
+
+void host_set_mods(uint8_t mods)
+{
+ keyboard_report->mods = mods;
+}
+
+void host_add_code(uint8_t code)
+{
+ if (IS_MOD(code)) {
+ host_add_mod_bit(MOD_BIT(code));
+ } else {
+ host_add_key(code);
+ }
+}
+
+void host_swap_keyboard_report(void)
+{
+ report_keyboard_t *tmp = keyboard_report_prev;
+ keyboard_report_prev = keyboard_report;
+ keyboard_report = tmp;
+}
+
+void host_clear_keyboard_report(void)
+{
+ keyboard_report->mods = 0;
+ for (int8_t i = 0; i < REPORT_KEYS; i++) {
+ keyboard_report->keys[i] = 0;
+ }
+}
+
+uint8_t host_has_anykey(void)
+{
+ uint8_t cnt = 0;
+ for (int i = 0; i < REPORT_KEYS; i++) {
+ if (keyboard_report->keys[i])
+ cnt++;
+ }
+ return cnt;
+}
+
+uint8_t *host_get_keys(void)
+{
+ return keyboard_report->keys;
+}
+
+uint8_t host_get_mods(void)
+{
+ return keyboard_report->mods;
+}
+
+
+void host_send_keyboard_report(void)
+{
+ usb_keyboard_send_report(keyboard_report);
+}
+
+void host_mouse_send(report_mouse_t *report)
+{
+ usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
+}
+
+
+static inline void add_key_byte(uint8_t code)
+{
+ // TODO: fix ugly code
+ int8_t i = 0;
+ int8_t empty = -1;
+ for (; i < REPORT_KEYS; i++) {
+ if (keyboard_report_prev->keys[i] == code) {
+ keyboard_report->keys[i] = code;
+ break;
+ }
+ if (empty == -1 &&
+ keyboard_report_prev->keys[i] == 0 &&
+ keyboard_report->keys[i] == 0) {
+ empty = i;
+ }
+ }
+ if (i == REPORT_KEYS) {
+ if (empty != -1) {
+ keyboard_report->keys[empty] = code;
+ }
+ }
+}
+
+static inline void add_key_bit(uint8_t code)
+{
+ if ((code>>3) < REPORT_KEYS) {
+ keyboard_report->keys[code>>3] |= 1<<(code&7);
+ } else {
+ debug("add_key_bit: can't add: "); phex(code); debug("\n");
+ }
+}
diff --git a/pjrc/usb.c b/pjrc/usb.c
index 845b00bea7..9fd30dee3c 100755
--- a/pjrc/usb.c
+++ b/pjrc/usb.c
@@ -687,10 +687,10 @@ ISR(USB_GEN_vect)
usb_keyboard_idle_count++;
if (usb_keyboard_idle_count == usb_keyboard_idle_config) {
usb_keyboard_idle_count = 0;
- UEDATX = usb_keyboard_mods;
+ UEDATX = keyboard_report->mods;
UEDATX = 0;
for (i=0; i<6; i++) {
- UEDATX = usb_keyboard_keys[i];
+ UEDATX = keyboard_report->keys[i];
}
UEINTX = 0x3A;
}
@@ -873,10 +873,10 @@ ISR(USB_COM_vect)
if (bmRequestType == 0xA1) {
if (bRequest == HID_GET_REPORT) {
usb_wait_in_ready();
- UEDATX = usb_keyboard_mods;
+ UEDATX = keyboard_report->mods;
UEDATX = 0;
for (i=0; i<6; i++) {
- UEDATX = usb_keyboard_keys[i];
+ UEDATX = keyboard_report->keys[i];
}
usb_send_in();
return;
diff --git a/pjrc/usb.h b/pjrc/usb.h
index ee72a1e39f..4f0fd8f1e8 100644
--- a/pjrc/usb.h
+++ b/pjrc/usb.h
@@ -14,8 +14,6 @@ uint8_t usb_configured(void); // is the USB port configured
void usb_remote_wakeup(void);
-
-
#define EP_TYPE_CONTROL 0x00
#define EP_TYPE_BULK_IN 0x81
#define EP_TYPE_BULK_OUT 0x80
@@ -88,4 +86,23 @@ void usb_remote_wakeup(void);
#define ENDPOINT_HALT 0
#define TEST_MODE 2
+
+/*------------------------------------------------------------------*
+ * Keyboard descriptor setting
+ *------------------------------------------------------------------*/
+#define KBD_INTERFACE 0
+#define KBD_ENDPOINT 1
+#define KBD_SIZE 8
+#define KBD_BUFFER EP_DOUBLE_BUFFER
+#define KBD_REPORT_KEYS (KBD_SIZE - 2)
+
+// secondary keyboard
+#ifdef USB_NKRO_ENABLE
+#define KBD2_INTERFACE 4
+#define KBD2_ENDPOINT 5
+#define KBD2_SIZE 16
+#define KBD2_BUFFER EP_DOUBLE_BUFFER
+#define KBD2_REPORT_KEYS (KBD2_SIZE - 1)
+#endif
+
#endif
diff --git a/pjrc/usb_keyboard.c b/pjrc/usb_keyboard.c
index 57e23d5fce..e29c5c9e91 100644
--- a/pjrc/usb_keyboard.c
+++ b/pjrc/usb_keyboard.c
@@ -5,14 +5,9 @@
#include "print.h"
#include "debug.h"
#include "util.h"
+#include "host.h"
-// keyboard report.
-static usb_keyboard_report_t _report0 = { {0}, 0, false };
-static usb_keyboard_report_t _report1 = { {0}, 0, false };
-usb_keyboard_report_t *usb_keyboard_report = &_report0;
-usb_keyboard_report_t *usb_keyboard_report_prev = &_report1;
-
// protocol setting from the host. We use exactly the same report
// either way, so this variable only stores the setting since we
// are required to be able to report which setting is in use.
@@ -28,167 +23,42 @@ uint8_t usb_keyboard_idle_count=0;
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
volatile uint8_t usb_keyboard_leds=0;
-// enable USB NKRO
-bool usb_keyboard_nkro = false;
+static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
-int8_t usb_keyboard_send(void)
-{
- return usb_keyboard_send_report(usb_keyboard_report);
-}
-static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
-int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
+int8_t usb_keyboard_send_report(report_keyboard_t *report)
{
int8_t result = 0;
#ifdef USB_NKRO_ENABLE
- if (usb_keyboard_nkro)
- result = _send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
+ if (keyboard_nkro)
+ result = send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
else
#endif
{
if (usb_keyboard_protocol)
- result = _send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
+ result = send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
else
- result = _send_report(report, KBD_ENDPOINT, 0, 6);
+ result = send_report(report, KBD_ENDPOINT, 0, 6);
}
if (result) return result;
usb_keyboard_idle_count = 0;
- report->is_sent =true;
usb_keyboard_print_report(report);
return 0;
}
-void usb_keyboard_swap_report(void) {
- usb_keyboard_report_t *tmp = usb_keyboard_report_prev;
- usb_keyboard_report_prev = usb_keyboard_report;
- usb_keyboard_report = tmp;
-}
-
-void usb_keyboard_clear_report(void) {
- usb_keyboard_clear_keys();
- usb_keyboard_clear_mods();
- usb_keyboard_report->is_sent = false;
-}
-
-void usb_keyboard_clear_keys(void) {
- for (int i = 0; i < KEYS_MAX; i++) usb_keyboard_report->keys[i] = 0;
-}
-
-void usb_keyboard_clear_mods(void)
-{
- usb_keyboard_report->mods = 0;
-}
-
-void usb_keyboard_set_keys(uint8_t *keys)
-{
- for (int i = 0; i < KEYS_MAX; i++)
- usb_keyboard_report->keys[i] = keys[i];
-}
-
-void usb_keyboard_set_mods(uint8_t mods)
-{
- usb_keyboard_report->mods = mods;
-}
-
-void usb_keyboard_add_code(uint8_t code)
-{
- if (IS_MOD(code)) {
- usb_keyboard_add_mod(code);
- } else {
- usb_keyboard_add_key(code);
- }
-}
-
-static inline void _add_key_byte(uint8_t code);
-static inline void _add_key_bit(uint8_t code);
-void usb_keyboard_add_key(uint8_t code)
-{
-#ifdef USB_NKRO_ENABLE
- if (usb_keyboard_nkro) {
- _add_key_bit(code);
- return;
- }
-#endif
- _add_key_byte(code);
-}
-
-void usb_keyboard_add_mod(uint8_t code)
-{
- usb_keyboard_report->mods |= MOD_BIT(code);
-}
-
-void usb_keyboard_del_code(uint8_t code)
-{
- if (IS_MOD(code)) {
- usb_keyboard_del_mod(code);
- } else {
- usb_keyboard_del_key(code);
- }
-}
-
-void usb_keyboard_del_key(uint8_t code)
-{
-#ifdef USB_NKRO_ENABLE
- if ((code>>3) < KEYS_MAX) {
- usb_keyboard_keys[code>>3] &= ~(1<<(code&7));
- }
-#else
- for (int i = 0; i < KEYS_MAX; i++) {
- if (usb_keyboard_report->keys[i] == code) {
- usb_keyboard_report->keys[i] = KB_NO;
- return;
- }
- }
-#endif
-}
-
-void usb_keyboard_del_mod(uint8_t code)
-{
- usb_keyboard_report->mods &= ~MOD_BIT(code);
-}
-
-bool usb_keyboard_is_sent(void)
-{
- return usb_keyboard_report->is_sent;
-}
-
-bool usb_keyboard_has_key(void)
-{
- uint8_t keys = 0;
- for (int i = 0; i < KEYS_MAX; i++) keys |= usb_keyboard_report->keys[i];
- return keys ? true : false;
-}
-
-bool usb_keyboard_has_mod(void)
-{
- return usb_keyboard_report->mods ? true : false;
-}
-
-uint8_t usb_keyboard_get_key(void)
-{
-#ifdef USB_NKRO_ENABLE
- if (usb_keyboard_nkro) {
- uint8_t i = 0;
- for (; i < KEYS_MAX && !usb_keyboard_keys[i]; i++);
- return i<<3 | biton(usb_keyboard_keys[i]);
- }
-#endif
- return usb_keyboard_keys[0];
-}
-
-void usb_keyboard_print_report(usb_keyboard_report_t *report)
+void usb_keyboard_print_report(report_keyboard_t *report)
{
if (!debug_keyboard) return;
print("keys: ");
- for (int i = 0; i < KEYS_MAX; i++) { phex(report->keys[i]); print(" "); }
+ for (int i = 0; i < REPORT_KEYS; i++) { phex(report->keys[i]); print(" "); }
print(" mods: "); phex(report->mods); print("\n");
}
-static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
+static inline int8_t send_report(report_keyboard_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
{
uint8_t intr_state, timeout;
@@ -211,7 +81,7 @@ static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoin
UENUM = endpoint;
}
UEDATX = report->mods;
- if (!usb_keyboard_nkro)
+ if (!keyboard_nkro)
UEDATX = 0;
for (uint8_t i = keys_start; i < keys_end; i++) {
UEDATX = report->keys[i];
@@ -220,34 +90,3 @@ static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoin
SREG = intr_state;
return 0;
}
-
-static inline void _add_key_byte(uint8_t code)
-{
- // TODO: fix ugly code
- int8_t i = 0;
- int8_t empty = -1;
- for (; i < KEYS_MAX; i++) {
- if (usb_keyboard_keys_prev[i] == code) {
- usb_keyboard_keys[i] = code;
- break;
- }
- if (empty == -1 &&
- usb_keyboard_keys_prev[i] == 0 &&
- usb_keyboard_keys[i] == 0) {
- empty = i;
- }
- }
- if (i == KEYS_MAX) {
- if (empty != -1) {
- usb_keyboard_keys[empty] = code;
- }
- }
-}
-
-static inline void _add_key_bit(uint8_t code)
-{
- if ((code>>3) < KEYS_MAX) {
- usb_keyboard_keys[code>>3] |= 1<<(code&7);
- }
-}
-
diff --git a/pjrc/usb_keyboard.h b/pjrc/usb_keyboard.h
index 53de4336d9..22287a056f 100644
--- a/pjrc/usb_keyboard.h
+++ b/pjrc/usb_keyboard.h
@@ -4,78 +4,16 @@
#include <stdint.h>
#include <stdbool.h>
#include "usb.h"
+#include "host.h"
-#define KBD_INTERFACE 0
-#define KBD_ENDPOINT 1
-#define KBD_SIZE 8
-#define KBD_BUFFER EP_DOUBLE_BUFFER
-#define KBD_REPORT_KEYS (KBD_SIZE - 2)
-
-// secondary keyboard
-#ifdef USB_NKRO_ENABLE
-#define KBD2_INTERFACE 4
-#define KBD2_ENDPOINT 5
-#define KBD2_SIZE 16
-#define KBD2_BUFFER EP_DOUBLE_BUFFER
-#define KBD2_REPORT_KEYS (KBD2_SIZE - 1)
-#endif
-
-#if defined(KBD2_REPORT_KEYS) && KBD2_REPORT_KEYS > KBD_REPORT_KEYS
-#define KEYS_MAX KBD2_REPORT_KEYS
-#else
-#define KEYS_MAX KBD_REPORT_KEYS
-#endif
-
-
-typedef struct report {
- uint8_t keys[KEYS_MAX];
- uint8_t mods;
- bool is_sent;
-} usb_keyboard_report_t;
-
-
-#define usb_keyboard_keys usb_keyboard_report->keys
-#define usb_keyboard_mods usb_keyboard_report->mods
-#define usb_keyboard_keys_prev usb_keyboard_report_prev->keys
-#define usb_keyboard_mods_prev usb_keyboard_report_prev->mods
-
-
-extern usb_keyboard_report_t *usb_keyboard_report;
-extern usb_keyboard_report_t *usb_keyboard_report_prev;
extern uint8_t usb_keyboard_protocol;
extern uint8_t usb_keyboard_idle_config;
extern uint8_t usb_keyboard_idle_count;
extern volatile uint8_t usb_keyboard_leds;
-extern bool usb_keyboard_nkro;
-
-
-int8_t usb_keyboard_send(void);
-int8_t usb_keyboard_send_report(usb_keyboard_report_t *report);
-
-void usb_keyboard_swap_report(void);
-
-void usb_keyboard_clear_report(void);
-void usb_keyboard_clear_keys(void);
-void usb_keyboard_clear_mods(void);
-
-void usb_keyboard_set_keys(uint8_t *keys);
-void usb_keyboard_set_mods(uint8_t mods);
-
-void usb_keyboard_add_code(uint8_t code);
-void usb_keyboard_add_key(uint8_t code);
-void usb_keyboard_add_mod(uint8_t code);
-
-void usb_keyboard_del_code(uint8_t code);
-void usb_keyboard_del_key(uint8_t code);
-void usb_keyboard_del_mod(uint8_t code);
-
-bool usb_keyboard_is_sent(void);
-bool usb_keyboard_has_key(void);
-bool usb_keyboard_has_mod(void);
-uint8_t usb_keyboard_get_key(void);
-void usb_keyboard_print_report(usb_keyboard_report_t *report);
+int8_t usb_keyboard_send_report(report_keyboard_t *report);
+void usb_keyboard_print_report(report_keyboard_t *report);
#endif