finished/fixed button api
[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 void button_init(t_button *button) {
11
12         /* 
13          * select as input the following pins
14          *  - p0.30 : interrupt! wtf is the 'vw 3 p9'?
15          *  - p0.28, p0.27, p3.21, p3.20, p0.22, p0.13 : column select
16          *
17          *  select as output
18          *  - p2.18-p2.24 : the rows
19          */
20
21         // gpio, as is: p3.20, p3.21
22         PINSEL1&=~((1<<29)|(1<<28));    // p0.30
23         PINSEL1&=~((1<<25)|(1<<24));    // p0.28
24         PINSEL1&=~((1<<23)|(1<<22));    // p0.27
25         PINSEL1&=~((1<<13)|(1<<12));    // p0.22
26         PINSEL0&=~((1<<27)|(1<<26));    // p0.13
27
28         // ctrl databus for p2.18 - p2.24
29         PINSEL2&=(PINSEL2&(~((1<<5)|(1<<4))))|(1<<4);
30
31         // ctrl addr bus for p3.20, p3.21
32         PINSEL2=(PINSEL2&(~((1<<27)|(1<<26)|(1<<25))))|(1<<27)|(1<<26);
33
34         // input
35         IODIR0&=~((1<<30)|(1<<28)|(1<<27)|(1<<22)|(1<<13));
36         IODIR3&=~((1<<21)|(1<<20));
37
38         // output + pull them high
39         IODIR2|=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24));
40         IOSET2=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24));
41
42         /* poll / interrupt mode */
43         if(button->mode&BUTTON_INT) {
44         }
45         else if(button->mode&BUTTON_POLL) {
46         }
47 }
48
49 void button_set_retries(t_button *button,int retries) {
50
51         button->retries=retries;
52 }
53
54 #define BUTTON_RESET \
55         IOSET2=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24))
56
57 void button_select_row(u8 row) {
58
59         BUTTON_RESET;
60         if(row>6)
61                 return;
62         IOCLR2=(1<<(18+row));
63 }
64
65 #define COUNT_AND_CHECK         cnt++; \
66                                 if(cnt==BUTTON_MAX) \
67                                         break
68
69 u8 button_get_event(t_button *button) {
70
71         u8 row;
72         u8 offset;
73         u8 cnt;
74         u32 port0,port3;
75         u8 retries;
76
77         cnt=0;
78         retries=button->retries;
79
80         while(retries--) {
81                 /* rest offset counter */
82                 offset=0;
83                 /* rows */
84                 for(row=0;row<7;row++) {
85                         /* select the row */
86                         button_select_row(row);
87                         /* scan the columns 6 */
88                         port0=IOPIN0;
89                         port3=IOPIN3;
90                         if(!(port0&(1<<28))) {
91                                 button->key[cnt]=offset+0;
92                                 COUNT_AND_CHECK;
93                         }
94                         if(!(port0&(1<<27))) {
95                                 button->key[cnt]=offset+1;
96                                 COUNT_AND_CHECK;
97                         }
98                         if(!(port0&(1<<22))) {
99                                 button->key[cnt]=offset+2;
100                                 COUNT_AND_CHECK;
101                         }
102                         if(!(port0&(1<<13))) {
103                                 button->key[cnt]=offset+3;
104                                 COUNT_AND_CHECK;
105                         }
106                         if(!(port3&(1<<21))) {
107                                 button->key[cnt]=offset+4;
108                                 COUNT_AND_CHECK;
109                         }
110                         if(!(port3&(1<<20))) {
111                                 button->key[cnt]=offset+5;
112                                 COUNT_AND_CHECK;
113                         }
114                         offset+=6;
115                 }
116                 if(cnt)
117                         break;
118         }
119
120         BUTTON_RESET;
121         button->cnt=cnt;
122
123         return cnt;
124 }
125