easylogger mouse and keyboard device
Posted: Tue Apr 20, 2010 10:00 pm
hi can help me fix this code
i want make a USB Composite Device (mouse + keyboard)
hope somebody can help me fix this code ~
thank you
[CODE]
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "usbdrv.h"
#include "oddebug.h"
/*
Pin assignment:
PB0, PB2 = USB data lines
*/
#define BIT_LED 4
#define BIT_KEY 1
#define UTIL_BIN4(x) (uchar)((0##x & 01000)/64 + (0##x & 0100)/16 + (0##x & 010)/4 + (0##x & 1))
#define UTIL_BIN8(hi, lo) (uchar)(UTIL_BIN4(hi) * 16 + UTIL_BIN4(lo))
#ifndef NULL
#define NULL ((void *)0)
#endif
/* ------------------------------------------------------------------------- */
static uchar reportBuffer[2]; /* buffer for HID reports */
static uchar idleRate; /* in 4 ms units */
static uchar FlagKey;
/* ------------------------------------------------------------------------- */
PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
/* We use a simplifed keyboard report descriptor which does not support the
* boot protocol. We don't allow setting status LEDs and we only allow one
* simultaneous key press (except modifiers). We can therefore use short
* 2 byte input reports.
* The report descriptor has been created with usb.org's "HID Descriptor Tool"
* which can be downloaded from http://www.usb.org/developers/hidpage/.
* Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
* for the second INPUT item.
*/
/* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
* 10 Keyboard/Keypad Page for more codes.
*/
#define MOD_CONTROL_LEFT (1<<0)
#define MOD_SHIFT_LEFT (1<<1)
#define MOD_ALT_LEFT (1<<2)
#define MOD_GUI_LEFT (1<<3)
#define MOD_CONTROL_RIGHT (1<<4)
#define MOD_SHIFT_RIGHT (1<<5)
#define MOD_ALT_RIGHT (1<<6)
#define MOD_GUI_RIGHT (1<<7)
// 修改1 ,定義上下左右
#define KEY_UP 82
#define KEY_DOWN 81
#define KEY_LEFT 80
#define KEY_RIGHT 79
/* ------------------------------------------------------------------------- */
static void timerPoll(void)
{
static uchar timerCnt;
static uchar i;
if(TIFR & (1 << TOV1)){
TIFR = (1 << TOV1); /* clear overflow */
if(++timerCnt >= 63){ /* ~ 1 second interval */
timerCnt = 0;
if (!FlagKey) {
FlagKey = 1;
if (++i > 3)
i=0;
switch (i) {
case 0:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_RIGHT;
break;
case 1:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_DOWN;
break;
case 2:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_LEFT;
break;
case 3:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_UP;
break;
}
}
}
}
}
/* ------------------------------------------------------------------------- */
static void timerInit(void)
{
TCCR1 = 0x0b; /* select clock: 16.5M/1k -> overflow rate = 16.5M/256k = 62.94 Hz */
}
/* ------------------------------------------------------------------------- */
/* ------------------------ interface to USB driver ------------------------ */
/* ------------------------------------------------------------------------- */
uchar usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
usbMsgPtr = reportBuffer;
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = 0;
return sizeof(reportBuffer);
}else if(rq->bRequest == USBRQ_HID_GET_IDLE){
usbMsgPtr = &idleRate;
return 1;
}else if(rq->bRequest == USBRQ_HID_SET_IDLE){
idleRate = rq->wValue.bytes[1];
}
}else{
/* no vendor specific requests implemented */
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* --------------------------------- main ---------------------------------- */
/* ------------------------------------------------------------------------- */
int main(void)
{
uchar i;
/* Calibrate the RC oscillator to 8.25 MHz. The core clock of 16.5 MHz is
* derived from the 66 MHz peripheral clock by dividing. We assume that the
* EEPROM contains a calibration value in location 0. If no calibration value
* has been stored during programming, we offset Atmel's 8 MHz calibration
* value according to the clock vs OSCCAL diagram in the data sheet. This
* seems to be sufficiently precise (<= 1%).
*/
uchar calibrationValue = eeprom_read_byte(0);
if(calibrationValue != 0xff){
OSCCAL = calibrationValue; /* a calibration value is supplied */
}else{
/* we have no calibration value, assume 8 MHz calibration and adjust from there */
if(OSCCAL < 125){
OSCCAL += 3; /* should be 3.5 */
}else if(OSCCAL >= 128){
OSCCAL += 7; /* should be 7 */
}else{ /* must be between 125 and 128 */
OSCCAL = 127; /* maximum possible avoiding discontinuity */
}
}
odDebugInit();
DDRB = (1 << USB_CFG_DMINUS_BIT) | (1 << USB_CFG_DPLUS_BIT);
PORTB = 0; /* indicate USB disconnect to host */
for(i=0;i<20;i++){ /* 300 ms disconnect, also allows our oscillator to stabilize */
_delay_ms(15);
}
DDRB = 1 << BIT_LED; /* output for LED */
PORTB = 1 << BIT_KEY; /* pull-up on key input */
wdt_enable(WDTO_1S);
timerInit();
usbInit();
sei();
for(;;){ /* main event loop */
wdt_reset();
usbPoll();
if(usbInterruptIsReady() ){ /* we can send another key */
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
FlagKey = 0;
}
timerPoll();
}
return 0;
}
i want make a USB Composite Device (mouse + keyboard)
hope somebody can help me fix this code ~
thank you
[CODE]
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "usbdrv.h"
#include "oddebug.h"
/*
Pin assignment:
PB0, PB2 = USB data lines
*/
#define BIT_LED 4
#define BIT_KEY 1
#define UTIL_BIN4(x) (uchar)((0##x & 01000)/64 + (0##x & 0100)/16 + (0##x & 010)/4 + (0##x & 1))
#define UTIL_BIN8(hi, lo) (uchar)(UTIL_BIN4(hi) * 16 + UTIL_BIN4(lo))
#ifndef NULL
#define NULL ((void *)0)
#endif
/* ------------------------------------------------------------------------- */
static uchar reportBuffer[2]; /* buffer for HID reports */
static uchar idleRate; /* in 4 ms units */
static uchar FlagKey;
/* ------------------------------------------------------------------------- */
PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
/* We use a simplifed keyboard report descriptor which does not support the
* boot protocol. We don't allow setting status LEDs and we only allow one
* simultaneous key press (except modifiers). We can therefore use short
* 2 byte input reports.
* The report descriptor has been created with usb.org's "HID Descriptor Tool"
* which can be downloaded from http://www.usb.org/developers/hidpage/.
* Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
* for the second INPUT item.
*/
/* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
* 10 Keyboard/Keypad Page for more codes.
*/
#define MOD_CONTROL_LEFT (1<<0)
#define MOD_SHIFT_LEFT (1<<1)
#define MOD_ALT_LEFT (1<<2)
#define MOD_GUI_LEFT (1<<3)
#define MOD_CONTROL_RIGHT (1<<4)
#define MOD_SHIFT_RIGHT (1<<5)
#define MOD_ALT_RIGHT (1<<6)
#define MOD_GUI_RIGHT (1<<7)
// 修改1 ,定義上下左右
#define KEY_UP 82
#define KEY_DOWN 81
#define KEY_LEFT 80
#define KEY_RIGHT 79
/* ------------------------------------------------------------------------- */
static void timerPoll(void)
{
static uchar timerCnt;
static uchar i;
if(TIFR & (1 << TOV1)){
TIFR = (1 << TOV1); /* clear overflow */
if(++timerCnt >= 63){ /* ~ 1 second interval */
timerCnt = 0;
if (!FlagKey) {
FlagKey = 1;
if (++i > 3)
i=0;
switch (i) {
case 0:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_RIGHT;
break;
case 1:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_DOWN;
break;
case 2:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_LEFT;
break;
case 3:
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = KEY_UP;
break;
}
}
}
}
}
/* ------------------------------------------------------------------------- */
static void timerInit(void)
{
TCCR1 = 0x0b; /* select clock: 16.5M/1k -> overflow rate = 16.5M/256k = 62.94 Hz */
}
/* ------------------------------------------------------------------------- */
/* ------------------------ interface to USB driver ------------------------ */
/* ------------------------------------------------------------------------- */
uchar usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
usbMsgPtr = reportBuffer;
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
reportBuffer[0] = 0; /* no modifiers */
reportBuffer[1] = 0;
return sizeof(reportBuffer);
}else if(rq->bRequest == USBRQ_HID_GET_IDLE){
usbMsgPtr = &idleRate;
return 1;
}else if(rq->bRequest == USBRQ_HID_SET_IDLE){
idleRate = rq->wValue.bytes[1];
}
}else{
/* no vendor specific requests implemented */
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* --------------------------------- main ---------------------------------- */
/* ------------------------------------------------------------------------- */
int main(void)
{
uchar i;
/* Calibrate the RC oscillator to 8.25 MHz. The core clock of 16.5 MHz is
* derived from the 66 MHz peripheral clock by dividing. We assume that the
* EEPROM contains a calibration value in location 0. If no calibration value
* has been stored during programming, we offset Atmel's 8 MHz calibration
* value according to the clock vs OSCCAL diagram in the data sheet. This
* seems to be sufficiently precise (<= 1%).
*/
uchar calibrationValue = eeprom_read_byte(0);
if(calibrationValue != 0xff){
OSCCAL = calibrationValue; /* a calibration value is supplied */
}else{
/* we have no calibration value, assume 8 MHz calibration and adjust from there */
if(OSCCAL < 125){
OSCCAL += 3; /* should be 3.5 */
}else if(OSCCAL >= 128){
OSCCAL += 7; /* should be 7 */
}else{ /* must be between 125 and 128 */
OSCCAL = 127; /* maximum possible avoiding discontinuity */
}
}
odDebugInit();
DDRB = (1 << USB_CFG_DMINUS_BIT) | (1 << USB_CFG_DPLUS_BIT);
PORTB = 0; /* indicate USB disconnect to host */
for(i=0;i<20;i++){ /* 300 ms disconnect, also allows our oscillator to stabilize */
_delay_ms(15);
}
DDRB = 1 << BIT_LED; /* output for LED */
PORTB = 1 << BIT_KEY; /* pull-up on key input */
wdt_enable(WDTO_1S);
timerInit();
usbInit();
sei();
for(;;){ /* main event loop */
wdt_reset();
usbPoll();
if(usbInterruptIsReady() ){ /* we can send another key */
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
FlagKey = 0;
}
timerPoll();
}
return 0;
}