Project

General

Profile

Statistics
| Revision:

root / branches / encoders / code / lib / src / libdragonfly / dio.c @ 1345

History | View | Annotate | Download (5.81 KB)

1 7 bcoltin
/*
2
dio.c
3
*/
4
5
#include <avr/interrupt.h>
6
#include <dio.h>
7
#include <time.h>
8
#include <lights.h>
9
10
/**
11
 * @defgroup dio Digital Input / Output
12
 * @brief Controls digital input and output
13
 *
14
 * A general note on how port / pin numbers work:<br>
15
 * The portpin is used to select both the bank and which pin is selected.
16
 * 6 bits are used (lower 6, ex: 0b00abcdef).
17
 * The first 3 (abc in this example) are used to select the bank.<br>
18
 * A = 001<br>
19
 * B = 010<br>
20
 * C = 011<br>
21
 * D = 100<br>
22
 * E = 101<br>
23
 * F = 110<br>
24
 * G = 111<br><br>
25
 *
26
 * The bank can be found by doing portpin >> 3. <br>
27
 *
28
 * The next three (def in this example) are used to select the pin number.
29
 * These three bits are just the binary representation of the pin number.<br>
30
 * <br>
31
 * The pin number can be found by doing portpin & 0b111.<br><br>
32
 *
33
 * Include dio.h to access these functions.
34
 **/
35
36
/**
37
 * Reads the selected portpin.
38
 *
39
 * @param portpin The portpin to be read. See the general description
40
 * for a description of portpins.
41
 *
42
 * @return 1 or 0, depending on the value of the portpin.
43
 **/
44
int digital_input(int portpin){
45
  int pin = portpin & 0x7;
46
  int pin_val = 0;
47
48
  switch(portpin >> 3){
49
  case _PORT_A:
50
    DDRA &= ~_BV(pin);
51
    pin_val = PINA;
52
    return (pin_val >> pin) & 1;
53
  case _PORT_B:
54
    DDRB &= ~_BV(pin);
55
    pin_val = PINB;
56
    return (pin_val >> pin) & 1;
57
  case _PORT_C:
58
    DDRC &= ~_BV(pin);
59
    pin_val = PINC;
60
    return (pin_val >> pin) & 1;
61
  case _PORT_D:
62
    DDRD &= ~_BV(pin);
63
    pin_val = PIND;
64
    return (pin_val >> pin) & 1;
65
  case _PORT_E:
66
    DDRE &= ~_BV(pin);
67
    pin_val = PINE;
68
    return (pin_val >> pin) & 1;
69
  case _PORT_F:
70
    if(pin>=4){
71
      MCUSR|=1<<7;
72
      MCUSR|=1<<7;
73
    }
74
    DDRF &= ~_BV(pin);
75
    pin_val = PINF;
76
    return (pin_val >> pin) & 1;
77
  case _PORT_G:
78
    DDRG &= ~_BV(pin);
79
    pin_val = PING;
80
    return (pin_val >> pin) & 1;
81
  default: break;
82
  }
83
84
  return -1;
85
}
86
87
/**
88
 * Enables pullup on a pin. If it is an output pin, the pin will output
89
 * 1.
90
 *
91
 * @param portpin the pin to enable pullup on. See the general description
92
 * for a discussion of portpins.
93
 **/
94
void digital_pull_up(int portpin) {
95
  int pins = portpin & 0x07;
96
97
  switch(portpin >> 3) {
98
  case _PORT_A:
99
    PORTA |= _BV(pins);
100
    break;
101
  case _PORT_B:
102
    PORTB |= _BV(pins);
103
    break;
104
  case _PORT_C:
105
    PORTC |= _BV(pins);
106
    break;
107
  case _PORT_D:
108
    PORTD |= _BV(pins);
109
    break;
110
  case _PORT_E:
111
    PORTE |= _BV(pins);
112
    break;
113
  case _PORT_F:
114
    PORTF |= _BV(pins);
115
    break;
116
  case _PORT_G:
117
    PORTG |= _BV(pins);
118
    break;
119
  }
120
}
121
122
/**
123
 * Sets portpin to the given value.
124
 *
125
 * @param portpin the portpin to output to. See the general
126
 * description for a discussion of portpins.
127
 *
128
 * @param val the value to set the portpin to. 0 for off,
129
 * nonzero for on.
130
 **/
131
void digital_output(int portpin, int val) {
132
  int pins = portpin & 0x07;
133
134
  /* if you want to set to 0... */
135
  if(val == 0) {
136
    switch(portpin >> 3) {
137
    case _PORT_A:
138
      DDRA |= _BV(pins);
139
      PORTA &= (0XFF - _BV(pins));
140
      break;
141
    case _PORT_B:
142
      DDRB |= _BV(pins);
143
      PORTB &= (0XFF - _BV(pins));
144
      break;
145
    case _PORT_C:
146
      DDRC |= _BV(pins);
147
      PORTC &= (0XFF - _BV(pins));
148
      break;
149
    case _PORT_D:
150
      DDRD |= _BV(pins);
151
      PORTD &= (0XFF - _BV(pins));
152
      break;
153
    case _PORT_E:
154
      DDRE |= _BV(pins);
155
      PORTE &= (0XFF - _BV(pins));
156
      break;
157
    case _PORT_F:
158
      DDRF |= _BV(pins);
159
      PORTF &= (0XFF - _BV(pins));
160
      break;
161
    case _PORT_G:
162
      DDRG |= _BV(pins);
163
      PORTG &= (0XFF - _BV(pins));
164
      break;
165
    }
166
  }else { /* ( val == 1) */
167
    switch(portpin >> 3) {
168
    case _PORT_A:
169
      DDRA |= _BV(pins);
170
      PORTA |= _BV(pins);
171
      break;
172
    case _PORT_B:
173
      DDRB |= _BV(pins);
174
      PORTB |= _BV(pins);
175
      break;
176
    case _PORT_C:
177
      DDRC |= _BV(pins);
178
      PORTC |= _BV(pins);
179
      break;
180
    case _PORT_D:
181
      DDRD |= _BV(pins);
182
      PORTD |= _BV(pins);
183
      break;
184
    case _PORT_E:
185
      DDRE |= _BV(pins);
186
      PORTE |= _BV(pins);
187
      break;
188
    case _PORT_F:
189
      DDRF |= _BV(pins);
190
      PORTF |= _BV(pins);
191
      break;
192
    case _PORT_G:
193
      DDRG |= _BV(pins);
194
      PORTG |= _BV(pins);
195
      break;
196
    }
197
  }
198
}
199
200
/**
201
 * Checks if button1 is currently pressed.
202
 *
203
 * @return 1 if button1 is pressed, 0 otherwise
204
 *
205
 * @see button1_wait, button1_click
206
 **/
207
int button1_read( void )
208
{
209
        int pin_val;
210
        DDRG &= ~_BV(PING0);
211
        PORTG|= _BV(PING0);
212
    pin_val = PING;
213
        return !((pin_val & _BV(PING0)));
214
215
}
216
217
/**
218
 * Delays execution until button1 is pressed.
219
 *
220
 * @see button1_read, button1_click
221
 **/
222
void button1_wait( void )
223
{
224
  while(!button1_read() ) {
225
    delay_ms(15);
226
  }
227
}
228
229
/**
230
 * If button1 is pressed, waits until it is released before returning.
231
 * Otherwise, the function returns immediately.
232
 *
233
 * @return 1 if button1 has been pressed, 0 otherwise
234
 *
235
 * @see button1_read, button1_wait
236
 **/
237
int button1_click()
238
{
239
  if(button1_read()){
240
    while(button1_read());
241
    return 1;
242
  }else{
243
    return 0;
244
  }
245
}
246
247
/**
248
 * Checks if button2 is currently pressed.
249
 *
250
 * @return 1 if button2 is pressed, 0 otherwise
251
 *
252
 * @see button2_wait, button2_click
253
 **/
254
int button2_read( void )
255
{
256
        int pin_val;
257
        DDRG &= ~_BV(PING1);
258
        PORTG|= _BV(PING1);
259
    pin_val = PING;
260
        return !((pin_val & _BV(PING1)));
261
}
262
263
/**
264
 * Delays execution until button2 is pressed.
265
 *
266
 * @see button2_read, button2_click
267
 **/
268
void button2_wait( void )
269
{
270
  while(!button2_read()){
271
    delay_ms(15);
272
  }
273
}
274
275
/**
276
 * If button2 is pressed, waits until it is released before returning.
277
 * Otherwise, the function returns immediately.
278
 *
279
 * @return 1 if button2 has been pressed, 0 otherwise
280
 *
281
 * @see button2_read, button2_wait
282
 **/
283
int button2_click()
284
{
285
        if(button2_read()){
286
    while(button2_read());
287
    return 1;
288
  }else{
289
                return 0;
290
  }
291
}
292
293
/** @} **/ //end defgroup