Changeset 5649b6c in avrstuff for starkadroid/code/main.c


Ignore:
Timestamp:
Nov 14, 2010, 12:01:03 PM (13 years ago)
Author:
Adrien Destugues <pulkomandy@…>
Branches:
main
Children:
8f4b118
Parents:
98713b5
Message:
  • Report everything as buttons.
  • Don't periodically send reports. Only send one when there is an actual change of button state
  • Set the period to 20ms, as that's enough.
  • Some code cleanup.

git-svn-id: svn://pulkomandy.tk/avrstuff@31 c6672c3c-f6b6-47f9-9001-1fd6b12fecbe

File:
1 edited

Legend:

Unmodified
Added
Removed
  • starkadroid/code/main.c

    r98713b5 r5649b6c  
    1010#include "usbdrv/usbdrv.h"
    1111
    12 static uint8_t reportBuffer[5];
     12#define DDRIN DDRB
     13#define PORTIN PORTB
     14#define PININ PINB
     15
     16#define DDROUT DDRC
     17#define PORTOUT PORTC
     18
     19static uint8_t reportBuffer[6];
    1320static uint8_t idleRate;
    1421
    1522void main() {
     23        bool which = false;
    1624        uint8_t idleCounter = 0;
    1725
     
    2331        bool doReport;
    2432
    25         DDRB = 0; // Keyboard matrix out
    26         PORTB = 255; // Enable pull up
     33        DDROUT = 0; // Keyboard matrix out
     34        PORTOUT = 255; // Enable pull up
    2735                // We put all pins as input then output a 0 in only one at a time.
    2836                // All the other pins are high-Z to avoid short circuits when many buttons are pressed.
    29         DDRC = 0; // Keyboard matrix in
    30         PORTC = 255; // Enable pull up
     37        DDRIN = 0; // Keyboard matrix in
     38        PORTIN = 255; // Enable pull up
    3139
    3240    // configure timer 0 for a rate of 16M/(256 * 256) = ~244Hz
    3341    TCCR0 = 4;          // timer 0 prescaler: 256
    34        
     42
    3543        while(1) {
    3644                wdt_reset();
     
    3846
    3947                doReport = false;
    40                 for(int i=0; i < 5; i++) {
    41                         DDRB = ~(1<<i);
    42                         PORTB = ~(1<<i);
    43                         reportBuffer[i] = (~PINC)&0x3F;
     48                for(char i = 0; i != 6; i++) {
     49                        DDROUT = 1<<i;
     50                        PORTOUT = ~(1<<i);
     51                        if (reportBuffer[i] != ((~PININ)&0x3F))
     52                                doReport = true;
     53                        reportBuffer[i] = (~PININ)&0x3F;
    4454                }
     55                DDROUT = 0;
     56                PORTOUT = 255;
    4557
    46                 DDRB = ~(1<<5);
    47                 PORTB = ~(1<<5);
    48                 reportBuffer[0] |= (~PINC)<<6 & 0xC0;
    49                 reportBuffer[1] |= (~PINC)<<4 & 0xC0;
    50                 reportBuffer[2] |= (~PINC)<<2 & 0xC0;
    51                 // reportBuffer[3] |= (~PINC)<<0 & 0xC0; // These are not existing pins...
    52 
    53                 DDRB = 255;
    54                 PORTB = 255;
    55 
    56                 if (usbInterruptIsReady()) {
     58                if (doReport && usbInterruptIsReady()) {
    5759                        usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
    58 //                      LEDOFF;
    5960                        doReport = false;
    60                 }
    61 
    62                 if (TIFR & (1 << TOV0)) {
    63                         TIFR = (1 << TOV0); // reset flag
    64 
    65                         if(++idleCounter > 4){ // yes, but not yet
    66                                 idleCounter -= 5; // 22ms in units of 4ms
    67                         } else { // yes, it is time now
    68                                 idleCounter = idleRate;
    69                                 if (usbInterruptIsReady()) {
    70                                         usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
    71                                 }
    72                         }
    7361                }
    7462        }
     
    8169
    8270char PROGMEM usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = {
    83     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
     71        0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    8472    0x09, 0x05,                    // USAGE (Game Pad)
    8573    0xa1, 0x01,                    // COLLECTION (Application)
    86     0x09, 0x01,                    //   USAGE (Pointer)
    8774    0xa1, 0x00,                    //   COLLECTION (Physical)
    88     0x09, 0x30,                    //     USAGE (X)
    89     0x09, 0x31,                    //     USAGE (Y)
    90     0x15, 0xff,                    //     LOGICAL_MINIMUM (-1)
     75    0x05, 0x09,                    //     USAGE_PAGE (Button)
     76    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
     77    0x29, 0x30,                    //     USAGE_MAXIMUM (Button 40)
     78    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
    9179    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
    92     0x75, 0x02,                    //     REPORT_SIZE (2)
    93     0x95, 0x02,                    //     REPORT_COUNT (2)
     80    0x95, 0x30,                    //     REPORT_COUNT (40)
     81    0x75, 0x01,                    //     REPORT_SIZE (1)
    9482    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
    9583    0xc0,                          //   END_COLLECTION
    96     0x05, 0x09,                    //   USAGE_PAGE (Button)
    97     0x19, 0x01,                    //   USAGE_MINIMUM (Button 1)
    98     0x29, 0x24,                    //   USAGE_MAXIMUM (Button 36)
    99     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    100     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
    101     0x75, 0x01,                    //   REPORT_SIZE (1)
    102     0x95, 0x24,                    //   REPORT_COUNT (36)
    103     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
    10484    0xc0                           // END_COLLECTION
    10585};
Note: See TracChangeset for help on using the changeset viewer.