buttons.cpp 989 Bytes
Newer Older
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "buttons.hpp"
#include "uart.hpp"
#include "time.hpp"
#include <avr/io.h>

static bool lastStateA = false;
static bool lastStateB = false;
static uint32_t lastMillisA = 0;
static uint32_t lastMillisB = 0;

void initButtons()
{
    DDRC &= ~(1 << DDC5) & ~(1 << DDC4);    // set PC5 and PC4 as inputs
    PORTC |= (1 << PORTC5) | (1 << PORTC4); // enable pullups
}

void updateButtons()
{
    uint32_t currMillis = getCurrentMillis();
    
    bool currStateA = (PINC & (1 << PINC5));
    bool currStateB = (PINC & (1 << PINC4));
    
    if (currStateA != lastStateA)
    {
        lastStateA = currStateA;
        
        if (!currStateA && (currMillis - lastMillisA) > 50) 
            writeUart('a');
        
        lastMillisA = currMillis;
    }
    
    if (currStateB != lastStateB)
    {
        lastStateB = currStateB;
        
        if (!currStateB && (currMillis - lastMillisB) > 50)
            writeUart('b');
        
        lastMillisB = currMillis;
    }
}