Changeset bbcd1f8 in avrstuff


Ignore:
Timestamp:
Sep 7, 2019, 4:53:59 PM (5 years ago)
Author:
Adrien Destugues <pulkomandy@…>
Branches:
main
Children:
6f22754
Parents:
38a6d09
Message:

Rewrite main loop in non-blocking way

Otherwise it is not possible to handle multiple pads at the same time.

Location:
grip2hid
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • grip2hid/Makefile

    r38a6d09 rbbcd1f8  
    11#Set vars
    22MCU=at90usb1287
    3 COMPILE = avr-gcc -Wall -Os -Iusbdrv -I. -std=c99 -mmcu=$(MCU) -DF_CPU=16000000
     3COMPILE = avr-gcc -Wall -Wextra -Werror -Os -Iusbdrv -I. -mmcu=$(MCU) -DF_CPU=16000000
    44PROJECT=grip2hid
    55
     
    1919
    2020flash: $(PROJECT).hex
    21         avrdude -c flip1 -p $(MCU) -Uflash:w:$^
     21        avrdude -u -c flip1 -p $(MCU) -Uflash:w:$^
    2222
    2323clean:
  • grip2hid/main.cpp

    r38a6d09 rbbcd1f8  
    4343private:
    4444        uint32_t word = 0;
    45         uint32_t count = 0;
    46         uint32_t state = 0;
     45        uint8_t count = 0;
     46        uint8_t state = 0;
    4747};
    4848
    4949int main() {
    5050        wdt_enable(WDTO_2S);
    51     // configure timer 0 for a rate of 16M/(256 * 256) = ~244Hz
    52     TCCR0A = 0;          // timer 0 prescaler: 256
    53         TCCR0B = 4;
    5451
    5552        // debug LEDs - output
    5653        DDRD |= 0xF0;
     54        PORTD = 0;
    5755
    5856        // GrIP input
    5957        DDRB = 0;
     58        uint8_t oldv = PINB;
    6059
    61         PORTD = 0;
    6260        GrIP pad;
    6361
     
    6563                wdt_reset();
    6664
    67                 // Wait for next bit
    68                 while(!(PINB & 2));
    69                 while(PINB & 2);
     65                uint8_t newv = PINB;
    7066
    71                 // Read bit
    72                 uint32_t bit = (PINB & 1) != 0;
    73                 pad.PushBit(bit);
     67                if (!(newv & 2)) { // clock is down
     68                        if (oldv & 2) { // and it was up at previous read
     69                                // Read bit
     70                                uint32_t bit = (newv & 1) != 0;
     71                                pad.PushBit(bit);
     72                        }
     73                }
     74                oldv = newv;
    7475        }
    7576
Note: See TracChangeset for help on using the changeset viewer.