forked from vogelchr/avr_cobalt_panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cobalt_buttons.c
96 lines (74 loc) · 2.25 KB
/
cobalt_buttons.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* ----------------------------------------------------------------------------
This file is part of avr_cobalt_panel.
(c) 2012 Christian Vogel <[email protected]>
avr_cobalt_panel is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
avr_cobalt_panel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License see http://www.gnu.org/licenses/.
---------------------------------------------------------------------------- */
#include "cobalt_buttons.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "hd44780.h"
#define PORT_CTRL PORTB
#define DDR_CTRL DDRB
#define BIT_nOE _BV(5)
void
cobalt_buttons_init()
{
PORT_CTRL |= BIT_nOE;
DDR_CTRL |= BIT_nOE;
}
static uint8_t cobalt_buttons_last=0; /* last read button value */
static uint8_t cobalt_buttons_ctr=0; /* debounce timer */
uint8_t
cobalt_buttons_debounce(uint8_t *buttons,uint8_t debounce_counter)
{
uint8_t b = cobalt_buttons_get();
/* different than last time? Set debounce timer to given value. */
if(b != cobalt_buttons_last){
cobalt_buttons_last = b; /* update last read button value */
cobalt_buttons_ctr = debounce_counter;
return 0;
}
if(cobalt_buttons_ctr == 0)
return 0; /* debounce counter has already expired, no changes */
cobalt_buttons_ctr--;
if(cobalt_buttons_ctr > 0)
return 0; /* debounce counter has not yet expired */
/* debounce counter has expired, return button to user */
*buttons = b;
return 1;
}
uint8_t
cobalt_buttons_get()
{
uint8_t ret;
cli();
PORT_CTRL &= ~BIT_nOE;
_delay_us(2);
ret = hd44780_dbus_read();
PORT_CTRL |= BIT_nOE;
sei();
ret ^= 0xff;
return ret & COBALT_BUTTONS_ALL;
}
const prog_char button_chars[8]="0PLUDRES";
char
cobalt_buttons_simple_char(uint8_t buttons){
uint8_t k;
char * p = button_chars;
for(k=0;k<8;k++){
if(buttons == (1<<k))
return pgm_read_byte(p);
p++;
}
return '\0';
}