Formalizing code module structure and inheritance (Large Commit)
- Commenting cleaned up - usb_keyboard_debug is not finished yet in terms of cleanup (needs DEBUB_PRINT #ifdef's) - Added LoadFile script generation - Formalized KeyIndex and USBKeys data containers - Split parts of scan_loop into Macro, USB, and main.c - Added interface to USB modules - Added interface to Macro modules - Added proper Debug module enable/disable
This commit is contained in:
parent
a0439f627b
commit
c01efa2d53
29 changed files with 774 additions and 198 deletions
|
@ -12,6 +12,7 @@
|
|||
#
|
||||
|
||||
set( USB_SRCS
|
||||
usb_com.c
|
||||
usb_keyboard_debug.c
|
||||
)
|
||||
|
||||
|
|
85
USB/pjrc/usb_com.c
Normal file
85
USB/pjrc/usb_com.c
Normal file
|
@ -0,0 +1,85 @@
|
|||
/* Copyright (C) 2011 by Jacob Alexander
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// ----- Includes -----
|
||||
|
||||
// Compiler Includes
|
||||
#include <util/delay.h>
|
||||
|
||||
// AVR Includes
|
||||
|
||||
// Project Includes
|
||||
#include "usb_keyboard_debug.h"
|
||||
|
||||
// Local Includes
|
||||
#include "usb_com.h"
|
||||
|
||||
|
||||
|
||||
// ----- Variables -----
|
||||
|
||||
// which modifier keys are currently pressed
|
||||
// 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
|
||||
// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
|
||||
uint8_t USBKeys_Modifiers = 0;
|
||||
|
||||
// which keys are currently pressed, up to 6 keys may be down at once
|
||||
uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
|
||||
|
||||
// The number of keys sent to the usb in the array
|
||||
uint8_t USBKeys_Sent;
|
||||
|
||||
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
|
||||
volatile uint8_t USBKeys_LEDs = 0;
|
||||
|
||||
|
||||
|
||||
// ----- Functions -----
|
||||
|
||||
// USB Module Setup
|
||||
void usb_setup(void)
|
||||
{
|
||||
// Initialize the USB, and then wait for the host to set configuration.
|
||||
// If the Teensy is powered without a PC connected to the USB port,
|
||||
// this will wait forever.
|
||||
usb_init();
|
||||
while ( !usb_configured() ) /* wait */ ;
|
||||
|
||||
// Wait an extra second for the PC's operating system to load drivers
|
||||
// and do whatever it does to actually be ready for input
|
||||
_delay_ms(1000);
|
||||
}
|
||||
|
||||
|
||||
// USB Data Send
|
||||
void usb_send(void)
|
||||
{
|
||||
// TODO undo potentially old keys
|
||||
for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
|
||||
USBKeys_Array[c] = 0;
|
||||
|
||||
// Send keypresses
|
||||
usb_keyboard_send();
|
||||
|
||||
// Clear modifiers
|
||||
USBKeys_Modifiers = 0;
|
||||
}
|
||||
|
73
USB/pjrc/usb_com.h
Normal file
73
USB/pjrc/usb_com.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
/* Copyright (C) 2011 by Jacob Alexander
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef __usb_com_h
|
||||
#define __usb_com_h
|
||||
|
||||
// ----- Includes -----
|
||||
|
||||
// AVR Includes
|
||||
|
||||
// Compiler Includes
|
||||
#include <stdint.h>
|
||||
|
||||
// Local Includes
|
||||
|
||||
|
||||
|
||||
// ----- Defines -----
|
||||
|
||||
// Indicator for other modules through USBKeys_MaxSize for how capable the USB module is when sending large number of keypresses
|
||||
#define USB_MAX_KEY_SEND 6
|
||||
|
||||
|
||||
// You can change these to give your code its own name.
|
||||
#define STR_MANUFACTURER L"MfgName"
|
||||
#define STR_PRODUCT L"Keyboard"
|
||||
|
||||
|
||||
// Mac OS-X and Linux automatically load the correct drivers. On
|
||||
// Windows, even though the driver is supplied by Microsoft, an
|
||||
// INF file is needed to load the driver. These numbers need to
|
||||
// match the INF file.
|
||||
#define VENDOR_ID 0x16C0
|
||||
#define PRODUCT_ID 0x047D
|
||||
|
||||
|
||||
|
||||
// ----- Variables -----
|
||||
|
||||
// Variables used to communciate to the usb module
|
||||
extern uint8_t USBKeys_Modifiers;
|
||||
extern uint8_t USBKeys_Array[USB_MAX_KEY_SEND];
|
||||
extern uint8_t USBKeys_Sent;
|
||||
extern volatile uint8_t USBKeys_LEDs;
|
||||
static const uint8_t USBKeys_MaxSize = USB_MAX_KEY_SEND;
|
||||
|
||||
|
||||
|
||||
// ----- Functions -----
|
||||
|
||||
void usb_setup(void);
|
||||
void usb_send(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -33,19 +33,6 @@
|
|||
*
|
||||
**************************************************************************/
|
||||
|
||||
// You can change these to give your code its own name.
|
||||
#define STR_MANUFACTURER L"MfgName"
|
||||
#define STR_PRODUCT L"Keyboard"
|
||||
|
||||
|
||||
// Mac OS-X and Linux automatically load the correct drivers. On
|
||||
// Windows, even though the driver is supplied by Microsoft, an
|
||||
// INF file is needed to load the driver. These numbers need to
|
||||
// match the INF file.
|
||||
#define VENDOR_ID 0x16C0
|
||||
#define PRODUCT_ID 0x047D
|
||||
|
||||
|
||||
// USB devices are supposed to implment a halt feature, which is
|
||||
// rarely (if ever) used. If you comment this line out, the halt
|
||||
// code will be removed, saving 102 bytes of space (gcc 4.3.0).
|
||||
|
@ -286,14 +273,6 @@ static volatile uint8_t usb_configuration=0;
|
|||
// packet, or send a zero length packet.
|
||||
static volatile uint8_t debug_flush_timer=0;
|
||||
|
||||
// which modifier keys are currently pressed
|
||||
// 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
|
||||
// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
|
||||
uint8_t keyboard_modifier_keys=0;
|
||||
|
||||
// which keys are currently pressed, up to 6 keys may be down at once
|
||||
uint8_t keyboard_keys[6]={0,0,0,0,0,0};
|
||||
|
||||
// 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.
|
||||
|
@ -306,9 +285,6 @@ static uint8_t keyboard_idle_config=125;
|
|||
// count until idle timeout
|
||||
static uint8_t keyboard_idle_count=0;
|
||||
|
||||
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
|
||||
volatile uint8_t keyboard_leds=0;
|
||||
|
||||
|
||||
/**************************************************************************
|
||||
*
|
||||
|
@ -344,16 +320,16 @@ int8_t usb_keyboard_press(uint8_t key, uint8_t modifier)
|
|||
{
|
||||
int8_t r;
|
||||
|
||||
keyboard_modifier_keys = modifier;
|
||||
keyboard_keys[0] = key;
|
||||
USBKeys_Modifiers = modifier;
|
||||
USBKeys_Array[0] = key;
|
||||
r = usb_keyboard_send();
|
||||
if (r) return r;
|
||||
keyboard_modifier_keys = 0;
|
||||
keyboard_keys[0] = 0;
|
||||
USBKeys_Modifiers = 0;
|
||||
USBKeys_Array[0] = 0;
|
||||
return usb_keyboard_send();
|
||||
}
|
||||
|
||||
// send the contents of keyboard_keys and keyboard_modifier_keys
|
||||
// send the contents of USBKeys_Array and USBKeys_Modifiers
|
||||
int8_t usb_keyboard_send(void)
|
||||
{
|
||||
uint8_t i, intr_state, timeout;
|
||||
|
@ -376,10 +352,10 @@ int8_t usb_keyboard_send(void)
|
|||
cli();
|
||||
UENUM = KEYBOARD_ENDPOINT;
|
||||
}
|
||||
UEDATX = keyboard_modifier_keys;
|
||||
UEDATX = USBKeys_Modifiers;
|
||||
UEDATX = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
UEDATX = keyboard_keys[i];
|
||||
UEDATX = USBKeys_Array[i];
|
||||
}
|
||||
UEINTX = 0x3A;
|
||||
keyboard_idle_count = 0;
|
||||
|
@ -505,10 +481,10 @@ ISR(USB_GEN_vect)
|
|||
keyboard_idle_count++;
|
||||
if (keyboard_idle_count == keyboard_idle_config) {
|
||||
keyboard_idle_count = 0;
|
||||
UEDATX = keyboard_modifier_keys;
|
||||
UEDATX = USBKeys_Modifiers;
|
||||
UEDATX = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
UEDATX = keyboard_keys[i];
|
||||
UEDATX = USBKeys_Array[i];
|
||||
}
|
||||
UEINTX = 0x3A;
|
||||
}
|
||||
|
@ -679,10 +655,10 @@ ISR(USB_COM_vect)
|
|||
if (bmRequestType == 0xA1) {
|
||||
if (bRequest == HID_GET_REPORT) {
|
||||
usb_wait_in_ready();
|
||||
UEDATX = keyboard_modifier_keys;
|
||||
UEDATX = USBKeys_Modifiers;
|
||||
UEDATX = 0;
|
||||
for (i=0; i<6; i++) {
|
||||
UEDATX = keyboard_keys[i];
|
||||
UEDATX = USBKeys_Array[i];
|
||||
}
|
||||
usb_send_in();
|
||||
return;
|
||||
|
@ -703,7 +679,7 @@ ISR(USB_COM_vect)
|
|||
if (bmRequestType == 0x21) {
|
||||
if (bRequest == HID_SET_REPORT) {
|
||||
usb_wait_receive_out();
|
||||
keyboard_leds = UEDATX;
|
||||
USBKeys_LEDs = UEDATX;
|
||||
usb_ack_out();
|
||||
usb_send_in();
|
||||
return;
|
||||
|
|
|
@ -2,15 +2,14 @@
|
|||
#define usb_serial_h__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "usb_com.h"
|
||||
|
||||
void usb_init(void); // initialize everything
|
||||
uint8_t usb_configured(void); // is the USB port configured
|
||||
|
||||
int8_t usb_keyboard_press(uint8_t key, uint8_t modifier);
|
||||
int8_t usb_keyboard_send(void);
|
||||
extern uint8_t keyboard_modifier_keys;
|
||||
extern uint8_t keyboard_keys[6];
|
||||
extern volatile uint8_t keyboard_leds;
|
||||
|
||||
|
||||
int8_t usb_debug_putchar(uint8_t c); // transmit a character
|
||||
void usb_debug_flush_output(void); // immediately transmit any buffered output
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue