PageRenderTime 31ms CodeModel.GetById 17ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/firmware/src/Motherboard/boards/mb24/ButtonArray.cc

http://github.com/makerbot/G3Firmware
C++ | 76 lines | 60 code | 14 blank | 2 comment | 14 complexity | 8355c80f5b7f2ae1f2bc2b36a276db52 MD5 | raw file
 1#include "ButtonArray.hh"
 2
 3static uint8_t previousL;
 4static uint8_t previousC;
 5
 6void ButtonArray::init() {
 7        previousL = 0;
 8        previousC = 0;
 9
10        // Set all of the known buttons to inputs (see above note)
11        DDRL = DDRL & 0x1;
12        DDRC = DDRC & 0xF9;
13        PORTL = PORTL & 0x1;
14        PORTC = PORTC & 0xF9;
15}
16
17void ButtonArray::scanButtons() {
18        // Don't bother scanning if we already have a button.
19        if (buttonPressWaiting) {
20                return;
21        }
22
23        uint8_t newL = PINL;// & 0xFE;
24        uint8_t newC = PINC;// & 0x06;
25
26        if (newL != previousL) {
27                uint8_t diff = newL ^ previousL;
28
29                for(uint8_t i = 1; i < 8; i++) {
30                        if (diff&(1<<i)) {
31                                if (!(newL&(1<<i))) {
32                                        if (!buttonPressWaiting) {
33                                                buttonPress = i;
34                                                buttonPressWaiting = true;
35                                        }
36                                }
37                        }
38                }
39        }
40
41        if (newC != previousC) {
42                uint8_t diff = newC ^ previousC;
43
44                for(uint8_t i = 1; i < 3; i++) {
45                        if (diff&(1<<i)) {
46                                if (!(newC&(1<<i))) {
47                                        if (!buttonPressWaiting) {
48                                                buttonPress = i+10;
49                                                buttonPressWaiting = true;
50                                        }
51                                }
52                        }
53                }
54        }
55
56        previousL = newL;
57        previousC = newC;
58}
59
60bool ButtonArray::getButton(ButtonName& button) {
61        bool buttonValid;
62        uint8_t buttonNumber;
63
64        ATOMIC_BLOCK(ATOMIC_FORCEON)
65        {
66                buttonValid =  buttonPressWaiting;
67                buttonNumber = buttonPress;
68                buttonPressWaiting = false;
69        }
70
71        if (buttonValid) {
72                button = (ButtonName)(buttonNumber);
73        }
74
75        return buttonValid;
76}