Changeset 5b8c7e2 in avrstuff


Ignore:
Timestamp:
Sep 8, 2019, 3:12:32 PM (5 years ago)
Author:
Adrien Destugues <pulkomandy@…>
Branches:
main
Children:
62256f0
Parents:
5b8020c
Message:

grip2hid: make it work (almost) with all 4 pads

File:
1 edited

Legend:

Unmodified
Added
Removed
  • grip2hid/main.cpp

    r5b8020c r5b8c7e2  
    1717#define GRIP_DAT_B  (1 << 3)
    1818#define GRIP_CLK_B  (1 << 2)
     19#define GRIP_DAT_C  (1 << 7)
     20#define GRIP_CLK_C  (1 << 6)
     21#define GRIP_DAT_D  (1 << 5)
     22#define GRIP_CLK_D  (1 << 4)
    1923
    2024class GrIP
     
    6569
    6670int main() {
    67         GrIP pad[2];
    68 
    6971        wdt_enable(WDTO_2S);
    7072
    71         // debug LEDs - output
    72         DDRD |= 0x7C;
    73         PORTD = 0;
    74 
    7573        // GrIP input
    76         GRIP_PORT = 0; // Disable pull-ups
     74        GRIP_PORT = 0xFF; // Enable pull-ups
    7775        GRIP_DDR = 0;  // Port as input
    78         uint8_t oldv = GRIP_PIN;
    7976
    8077        USARTInit();
     
    8380        USARTWriteChar('\n');
    8481
     82        // Initial value of oldv for edge detection
     83        uint8_t oldv = GRIP_PIN;
    8584
    8685        for (;;) {
     
    10099                // should rather use a timer with a known period?
    101100                // Process pad1
     101                GrIP pad;
    102102                uint8_t i;
    103103                for (i = 0; i < 255; i++) {
     
    110110                                        // Read bit
    111111                                        uint32_t bit = (newv & GRIP_DAT_A) != 0;
    112                                         i = 0;
    113                                         if (pad[0].PushBit(bit)) {
    114                                                 pad[0].Dump();
    115                                                 USARTWriteChar(' ');
     112                                        if (pad.PushBit(bit)) {
    116113                                                break;
    117114                                        }
     
    120117                        oldv = newv;
    121118                }
     119
     120                pad.Dump();
     121                USARTWriteChar(' ');
    122122
    123123                // Process pad 2
     
    130130                                        // Read bit
    131131                                        uint32_t bit = (newv & GRIP_DAT_B) != 0;
    132                                         i = 0;
    133                                         if (pad[1].PushBit(bit)) {
    134                                                 pad[1].Dump();
    135                                                 USARTWriteChar(' ');
     132                                        if (pad.PushBit(bit)) {
    136133                                                break;
    137134                                        }
     
    140137                        oldv = newv;
    141138                }
     139
     140                pad.Dump();
     141                USARTWriteChar(' ');
     142
     143                // Process pad 3
     144                for (i = 0; i < 255; i++) {
     145                        wdt_reset();
     146
     147                        uint8_t newv = GRIP_PIN;
     148                        if (!(newv & GRIP_CLK_C)) { // clock is down
     149                                if (oldv & GRIP_CLK_C) { // and it was up at previous read
     150                                        // Read bit
     151                                        uint32_t bit = (newv & GRIP_DAT_C) != 0;
     152                                        if (pad.PushBit(bit)) {
     153                                                break;
     154                                        }
     155                                }
     156                        }
     157                        oldv = newv;
     158                }
     159
     160                pad.Dump();
     161                USARTWriteChar(' ');
     162
     163                // Process pad 4
     164                for (i = 0; i < 255; i++) {
     165                        wdt_reset();
     166
     167                        uint8_t newv = GRIP_PIN;
     168                        if (!(newv & GRIP_CLK_D)) { // clock is down
     169                                if (oldv & GRIP_CLK_D) { // and it was up at previous read
     170                                        // Read bit
     171                                        uint32_t bit = (newv & GRIP_DAT_D) != 0;
     172                                        if (pad.PushBit(bit)) {
     173                                                break;
     174                                        }
     175                                }
     176                        }
     177                        oldv = newv;
     178                }
     179
     180                pad.Dump();
    142181                USARTWriteChar('\r');
    143182                USARTWriteChar('\n');
Note: See TracChangeset for help on using the changeset viewer.