bullshit commit, sync for travel (to zn00H!) :)
[my-code/arm.git] / betty / buttons.c
1 /*
2  * buttons.c - button api
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 #include "buttons.h"
9
10 #define BUTTON_RESET \
11         IOSET2=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24))
12
13 #define BUTTON_UNSET \
14         IOCLR2=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24))
15
16 #define COUNT_AND_CHECK         cnt++; \
17                                 if(cnt==BUTTON_MAX) \
18                                         break
19
20 void button_init(t_button *button) {
21
22         /*
23          * input:
24          *  - p0.30 : interrupt! wtf is the 'vw 3 p9'? a transistor?
25          *  - p0.28, p0.27, p3.21, p3.20, p0.22, p0.13 : the columns
26          *
27          * output:
28          *  - p2.18-p2.24 : the rows
29          */
30
31         /* poll / interrupt mode */
32         if(button->mode&BUTTON_INT)
33                 BUTTON_UNSET;
34         else if(button->mode&BUTTON_POLL)
35                 BUTTON_RESET;
36 }
37
38 void button_set_retries(t_button *button,int retries) {
39
40         button->retries=retries;
41 }
42
43 void button_select_row(u8 row) {
44
45         BUTTON_RESET;
46         if(row>6)
47                 return;
48         IOCLR2=(1<<(18+row));
49 }
50
51 u8 button_get_event(t_button *button) {
52
53         u8 row;
54         u8 offset;
55         u8 cnt;
56         u32 port0,port3;
57         u8 tries;
58
59         cnt=0;
60         tries=button->retries+1;
61
62         while(tries--) {
63                 /* rest offset counter */
64                 offset=0;
65                 /* rows */
66                 for(row=0;row<7;row++) {
67                         /* select the row */
68                         button_select_row(row);
69                         /* scan the columns 6 */
70                         port0=IOPIN0;
71                         port3=IOPIN3;
72                         if(!(port0&(1<<28))) {
73                                 button->key[cnt]=offset+0;
74                                 COUNT_AND_CHECK;
75                         }
76                         if(!(port0&(1<<27))) {
77                                 button->key[cnt]=offset+1;
78                                 COUNT_AND_CHECK;
79                         }
80                         if(!(port0&(1<<22))) {
81                                 button->key[cnt]=offset+2;
82                                 COUNT_AND_CHECK;
83                         }
84                         if(!(port0&(1<<13))) {
85                                 button->key[cnt]=offset+3;
86                                 COUNT_AND_CHECK;
87                         }
88                         if(!(port3&(1<<21))) {
89                                 button->key[cnt]=offset+4;
90                                 COUNT_AND_CHECK;
91                         }
92                         if(!(port3&(1<<20))) {
93                                 button->key[cnt]=offset+5;
94                                 COUNT_AND_CHECK;
95                         }
96                         offset+=6;
97                 }
98                 if(cnt)
99                         break;
100         }
101
102         if(button->mode&BUTTON_INT)
103                 BUTTON_UNSET;
104         else if(button->mode&BUTTON_POLL)
105                 BUTTON_RESET;
106
107         return cnt;
108 }
109