From 5a3c24f09ab0c3797b8d856647a8da91b00123ff Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Sat, 26 Mar 2016 12:36:47 +0300 Subject: Cleanup keyboard.c --- keyboard/keyboard.c | 126 ++++++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/keyboard/keyboard.c b/keyboard/keyboard.c index 4aa1ffb..2e13bd6 100644 --- a/keyboard/keyboard.c +++ b/keyboard/keyboard.c @@ -65,26 +65,26 @@ volatile static uchar LED_state = 0xff; // received from PC static uchar idleRate; // repeat rate for keyboards usbMsgLen_t usbFunctionSetup(uchar data[8]) { - usbRequest_t *rq = (void *)data; - - if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { - switch(rq->bRequest) { - case USBRQ_HID_GET_REPORT: // send "no keys pressed" if asked here - // wValue: ReportType (highbyte), ReportID (lowbyte) - usbMsgPtr = (void *)&keyboard_report; // we only have this one - keyboard_report.modifier = 0; - keyboard_report.keycode[0] = 0; - return sizeof(keyboard_report); - case USBRQ_HID_SET_REPORT: // if wLength == 1, should be LED state - return (rq->wLength.word == 1) ? USB_NO_MSG : 0; - case USBRQ_HID_GET_IDLE: // send idle rate to PC as required by spec - usbMsgPtr = &idleRate; - return 1; - case USBRQ_HID_SET_IDLE: // save idle rate as required by spec - idleRate = rq->wValue.bytes[1]; - return 0; - } - } + usbRequest_t *rq = (void *)data; + + if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { + switch (rq->bRequest) { + case USBRQ_HID_GET_REPORT: // send "no keys pressed" if asked here + // wValue: ReportType (highbyte), ReportID (lowbyte) + usbMsgPtr = (void *)&keyboard_report; // we only have this one + keyboard_report.modifier = 0; + keyboard_report.keycode[0] = 0; + return sizeof(keyboard_report); + case USBRQ_HID_SET_REPORT: // if wLength == 1, should be LED state + return (rq->wLength.word == 1) ? USB_NO_MSG : 0; + case USBRQ_HID_GET_IDLE: // send idle rate to PC as required by spec + usbMsgPtr = &idleRate; + return 1; + case USBRQ_HID_SET_IDLE: // save idle rate as required by spec + idleRate = rq->wValue.bytes[1]; + return 0; + } + } return 0; // by default don't return any data } @@ -95,15 +95,14 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) { usbMsgLen_t usbFunctionWrite(uint8_t * data, uchar len) { if (data[0] == LED_state) - return 1; - else - LED_state = data[0]; + return 1; + else + LED_state = data[0]; - // LED state changed - if(LED_state & CAPS_LOCK) - PORTB |= 1 << PB0; // LED on + if (LED_state & CAPS_LOCK) + PORTB |= _BV(PB1); // LED on else - PORTB &= ~(1 << PB0); // LED off + PORTB &= ~_BV(PB1); // LED off return 1; // Data read, not expecting more } @@ -112,7 +111,7 @@ usbMsgLen_t usbFunctionWrite(uint8_t * data, uchar len) { void buildReport(uchar send_key) { keyboard_report.modifier = 0; - if(send_key >= 'a' && send_key <= 'z') + if (send_key >= 'a' && send_key <= 'z') keyboard_report.keycode[0] = 4+(send_key-'a'); else keyboard_report.keycode[0] = 0; @@ -125,34 +124,35 @@ void buildReport(uchar send_key) { int main() { uchar i, button_release_counter = 0, state = STATE_WAIT; - DDRB = 1 << PB0; // PB0 as output - PORTB = 1 << PB1; // PB1 is input with internal pullup resistor activated + DDRB |= _BV(DDB1); - for(i=0; i