Revision 891
Moved comments to headers. That was painful.
dio.c | ||
---|---|---|
38 | 38 |
#include <time.h> |
39 | 39 |
#include <lights.h> |
40 | 40 |
|
41 |
/** |
|
42 |
* @defgroup dio Digital Input / Output |
|
43 |
* @brief Controls digital input and output |
|
44 |
* |
|
45 |
* A general note on how port / pin numbers work:<br> |
|
46 |
* The portpin is used to select both the bank and which pin is selected. |
|
47 |
* 6 bits are used (lower 6, ex: 0b00abcdef). |
|
48 |
* The first 3 (abc in this example) are used to select the bank.<br> |
|
49 |
* A = 001<br> |
|
50 |
* B = 010<br> |
|
51 |
* C = 011<br> |
|
52 |
* D = 100<br> |
|
53 |
* E = 101<br> |
|
54 |
* F = 110<br> |
|
55 |
* G = 111<br><br> |
|
56 |
* |
|
57 |
* The bank can be found by doing portpin >> 3. <br> |
|
58 |
* |
|
59 |
* The next three (def in this example) are used to select the pin number. |
|
60 |
* These three bits are just the binary representation of the pin number.<br> |
|
61 |
* <br> |
|
62 |
* The pin number can be found by doing portpin & 0b111.<br><br> |
|
63 |
* |
|
64 |
* Include dio.h to access these functions. |
|
65 |
**/ |
|
66 | 41 |
|
67 |
/** |
|
68 |
* Reads the selected portpin. |
|
69 |
* |
|
70 |
* @param portpin The portpin to be read. See the general description |
|
71 |
* for a description of portpins. |
|
72 |
* |
|
73 |
* @return 1 or 0, depending on the value of the portpin. |
|
74 |
**/ |
|
75 | 42 |
int digital_input(int portpin) { |
76 | 43 |
int pin = portpin & 0x7; |
77 | 44 |
int pin_val = 0; |
... | ... | |
115 | 82 |
return -1; |
116 | 83 |
} |
117 | 84 |
|
118 |
/** |
|
119 |
* Enables pullup on a pin. If it is an output pin, the pin will output |
|
120 |
* 1. |
|
121 |
* |
|
122 |
* @param portpin the pin to enable pullup on. See the general description |
|
123 |
* for a discussion of portpins. |
|
124 |
**/ |
|
125 | 85 |
void digital_pull_up(int portpin) { |
126 | 86 |
int pins = portpin & 0x07; |
127 | 87 |
|
... | ... | |
150 | 110 |
} |
151 | 111 |
} |
152 | 112 |
|
153 |
/** |
|
154 |
* Sets portpin to the given value. |
|
155 |
* |
|
156 |
* @param portpin the portpin to output to. See the general |
|
157 |
* description for a discussion of portpins. |
|
158 |
* |
|
159 |
* @param val the value to set the portpin to. 0 for off, |
|
160 |
* nonzero for on. |
|
161 |
**/ |
|
162 | 113 |
void digital_output(int portpin, int val) { |
163 | 114 |
int pins = portpin & 0x07; |
164 | 115 |
|
... | ... | |
228 | 179 |
} |
229 | 180 |
} |
230 | 181 |
|
231 |
/** |
|
232 |
* Checks if button1 is currently pressed. |
|
233 |
* |
|
234 |
* @return 1 if button1 is pressed, 0 otherwise |
|
235 |
* |
|
236 |
* @see button1_wait, button1_click |
|
237 |
**/ |
|
238 | 182 |
int button1_read( void ) { |
239 | 183 |
int pin_val; |
240 | 184 |
DDRG &= ~_BV(PING0); |
... | ... | |
243 | 187 |
return !((pin_val & _BV(PING0))); |
244 | 188 |
} |
245 | 189 |
|
246 |
/** |
|
247 |
* Delays execution until button1 is pressed. |
|
248 |
* |
|
249 |
* @see button1_read, button1_click |
|
250 |
**/ |
|
251 | 190 |
void button1_wait( void ) { |
252 | 191 |
while(!button1_read() ) { |
253 | 192 |
delay_ms(15); |
254 | 193 |
} |
255 | 194 |
} |
256 | 195 |
|
257 |
/** |
|
258 |
* If button1 is pressed, waits until it is released before returning. |
|
259 |
* Otherwise, the function returns immediately. |
|
260 |
* |
|
261 |
* @return 1 if button1 has been pressed, 0 otherwise |
|
262 |
* |
|
263 |
* @see button1_read, button1_wait |
|
264 |
**/ |
|
265 | 196 |
int button1_click() { |
266 | 197 |
if(button1_read()) { |
267 | 198 |
while(button1_read()); |
... | ... | |
271 | 202 |
} |
272 | 203 |
} |
273 | 204 |
|
274 |
/** |
|
275 |
* Checks if button2 is currently pressed. |
|
276 |
* |
|
277 |
* @return 1 if button2 is pressed, 0 otherwise |
|
278 |
* |
|
279 |
* @see button2_wait, button2_click |
|
280 |
**/ |
|
281 | 205 |
int button2_read( void ) { |
282 | 206 |
int pin_val; |
283 | 207 |
DDRG &= ~_BV(PING1); |
... | ... | |
286 | 210 |
return !((pin_val & _BV(PING1))); |
287 | 211 |
} |
288 | 212 |
|
289 |
/** |
|
290 |
* Delays execution until button2 is pressed. |
|
291 |
* |
|
292 |
* @see button2_read, button2_click |
|
293 |
**/ |
|
294 | 213 |
void button2_wait( void ) { |
295 | 214 |
while(!button2_read()){ |
296 | 215 |
delay_ms(15); |
297 | 216 |
} |
298 | 217 |
} |
299 | 218 |
|
300 |
/** |
|
301 |
* If button2 is pressed, waits until it is released before returning. |
|
302 |
* Otherwise, the function returns immediately. |
|
303 |
* |
|
304 |
* @return 1 if button2 has been pressed, 0 otherwise |
|
305 |
* |
|
306 |
* @see button2_read, button2_wait |
|
307 |
**/ |
|
308 | 219 |
int button2_click() { |
309 | 220 |
if(button2_read()) { |
310 | 221 |
while(button2_read()); |
... | ... | |
314 | 225 |
} |
315 | 226 |
} |
316 | 227 |
|
317 |
/** @} **/ //end defgroup |
|
318 |
|
Also available in: Unified diff