Project

General

Profile

Revision 336

Updated wireless to use a circular buffer instead of a queue using malloc. Tested on both the computer and robots with a token ring, and was successful.

View differences:

dio.c
1 1
/**
2 2
 * Copyright (c) 2007 Colony Project
3
 *
3
 * 
4 4
 * Permission is hereby granted, free of charge, to any person
5 5
 * obtaining a copy of this software and associated documentation
6 6
 * files (the "Software"), to deal in the Software without
......
9 9
 * copies of the Software, and to permit persons to whom the
10 10
 * Software is furnished to do so, subject to the following
11 11
 * conditions:
12
 *
12
 * 
13 13
 * The above copyright notice and this permission notice shall be
14 14
 * included in all copies or substantial portions of the Software.
15
 *
15
 * 
16 16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
......
41 41
/**
42 42
 * @defgroup dio Digital Input / Output
43 43
 * @brief Controls digital input and output
44
 *
44
 * 
45 45
 * A general note on how port / pin numbers work:<br>
46 46
 * The portpin is used to select both the bank and which pin is selected.
47 47
 * 6 bits are used (lower 6, ex: 0b00abcdef).
......
53 53
 * E = 101<br>
54 54
 * F = 110<br>
55 55
 * G = 111<br><br>
56
 *
56
 * 		
57 57
 * The bank can be found by doing portpin >> 3. <br>
58
 *
58
 * 		
59 59
 * The next three (def in this example) are used to select the pin number.
60 60
 * These three bits are just the binary representation of the pin number.<br>
61 61
 * <br>
......
66 66

  
67 67
/**
68 68
 * Reads the selected portpin.
69
 *
69
 * 
70 70
 * @param portpin The portpin to be read. See the general description
71 71
 * for a description of portpins.
72 72
 *
......
75 75
int digital_input(int portpin){
76 76
  int pin = portpin & 0x7;
77 77
  int pin_val = 0;
78

  
78
  
79 79
  switch(portpin >> 3){
80 80
  case _PORT_A:
81 81
    DDRA &= ~_BV(pin);
......
111 111
    return (pin_val >> pin) & 1;
112 112
  default: break;
113 113
  }
114

  
114
  
115 115
  return -1;
116 116
}
117 117

  
......
131 131
    break;
132 132
  case _PORT_B:
133 133
    PORTB |= _BV(pins);
134
    break;
134
    break;    
135 135
  case _PORT_C:
136 136
    PORTC |= _BV(pins);
137
    break;
137
    break;    
138 138
  case _PORT_D:
139 139
    PORTD |= _BV(pins);
140
    break;
140
    break;    
141 141
  case _PORT_E:
142 142
    PORTE |= _BV(pins);
143
    break;
143
    break;    
144 144
  case _PORT_F:
145 145
    PORTF |= _BV(pins);
146
    break;
146
    break;    
147 147
  case _PORT_G:
148 148
    PORTG |= _BV(pins);
149 149
    break;
......
152 152

  
153 153
/**
154 154
 * Sets portpin to the given value.
155
 *
155
 * 
156 156
 * @param portpin the portpin to output to. See the general
157 157
 * description for a discussion of portpins.
158 158
 *
......
161 161
 **/
162 162
void digital_output(int portpin, int val) {
163 163
  int pins = portpin & 0x07;
164

  
164
  
165 165
  /* if you want to set to 0... */
166
  if (val == 0) {
166
  if(val == 0) {
167 167
    switch(portpin >> 3) {
168 168
    case _PORT_A:
169 169
      DDRA |= _BV(pins);
......
172 172
    case _PORT_B:
173 173
      DDRB |= _BV(pins);
174 174
      PORTB &= (0XFF - _BV(pins));
175
      break;
175
      break;    
176 176
    case _PORT_C:
177 177
      DDRC |= _BV(pins);
178 178
      PORTC &= (0XFF - _BV(pins));
179
      break;
179
      break;    
180 180
    case _PORT_D:
181 181
      DDRD |= _BV(pins);
182 182
      PORTD &= (0XFF - _BV(pins));
183
      break;
183
      break;    
184 184
    case _PORT_E:
185 185
      DDRE |= _BV(pins);
186 186
      PORTE &= (0XFF - _BV(pins));
187
      break;
187
      break;    
188 188
    case _PORT_F:
189 189
      DDRF |= _BV(pins);
190 190
      PORTF &= (0XFF - _BV(pins));
191
      break;
191
      break;    
192 192
    case _PORT_G:
193 193
      DDRG |= _BV(pins);
194 194
      PORTG &= (0XFF - _BV(pins));
195 195
      break;
196 196
    }
197
  } else { /* ( val == 1) */
197
  }else { /* ( val == 1) */ 
198 198
    switch(portpin >> 3) {
199 199
    case _PORT_A:
200 200
      DDRA |= _BV(pins);
......
203 203
    case _PORT_B:
204 204
      DDRB |= _BV(pins);
205 205
      PORTB |= _BV(pins);
206
      break;
206
      break;    
207 207
    case _PORT_C:
208 208
      DDRC |= _BV(pins);
209 209
      PORTC |= _BV(pins);
210
      break;
210
      break;    
211 211
    case _PORT_D:
212 212
      DDRD |= _BV(pins);
213 213
      PORTD |= _BV(pins);
214
      break;
214
      break;    
215 215
    case _PORT_E:
216 216
      DDRE |= _BV(pins);
217 217
      PORTE |= _BV(pins);
218
      break;
218
      break;    
219 219
    case _PORT_F:
220 220
      DDRF |= _BV(pins);
221 221
      PORTF |= _BV(pins);
222
      break;
222
      break;    
223 223
    case _PORT_G:
224 224
      DDRG |= _BV(pins);
225 225
      PORTG |= _BV(pins);
......
230 230

  
231 231
/**
232 232
 * Checks if button1 is currently pressed.
233
 *
233
 * 
234 234
 * @return 1 if button1 is pressed, 0 otherwise
235 235
 *
236 236
 * @see button1_wait, button1_click
237 237
 **/
238 238
int button1_read( void )
239 239
{
240
  int pin_val;
241
  DDRG &= ~_BV(PING0);
242
  PORTG|= _BV(PING0);
243
  pin_val = PING;
244
  return !((pin_val & _BV(PING0)));
240
	int pin_val;
241
	DDRG &= ~_BV(PING0);
242
	PORTG|= _BV(PING0);
243
    pin_val = PING;
244
	return !((pin_val & _BV(PING0)));
245
  
245 246
}
246 247

  
247 248
/**
......
251 252
 **/
252 253
void button1_wait( void )
253 254
{
254
  while (!button1_read()) {
255
  while(!button1_read() ) {
255 256
    delay_ms(15);
256 257
  }
257 258
}
......
266 267
 **/
267 268
int button1_click()
268 269
{
269
  if (button1_read()) {
270
    while (button1_read());
270
  if(button1_read()){
271
    while(button1_read());
271 272
    return 1;
272
  } else {
273
  }else{
273 274
    return 0;
274 275
  }
275 276
}
276 277

  
277 278
/**
278 279
 * Checks if button2 is currently pressed.
279
 *
280
 * 
280 281
 * @return 1 if button2 is pressed, 0 otherwise
281 282
 *
282 283
 * @see button2_wait, button2_click
283 284
 **/
284 285
int button2_read( void )
285 286
{
286
  int pin_val;
287
  DDRG &= ~_BV(PING1);
288
  PORTG|= _BV(PING1);
289
  pin_val = PING;
290
  return !((pin_val & _BV(PING1)));
287
	int pin_val;
288
	DDRG &= ~_BV(PING1);
289
	PORTG|= _BV(PING1);
290
    pin_val = PING;
291
	return !((pin_val & _BV(PING1)));
291 292
}
292 293

  
293 294
/**
......
295 296
 *
296 297
 * @see button2_read, button2_click
297 298
 **/
298
void button2_wait(void)
299
void button2_wait( void )
299 300
{
300
  while (!button2_read()) {
301
  while(!button2_read()){
301 302
    delay_ms(15);
302 303
  }
303 304
}
......
312 313
 **/
313 314
int button2_click()
314 315
{
315
  if (button2_read()) {
316
    while (button2_read());
316
	if(button2_read()){
317
    while(button2_read());
317 318
    return 1;
318
  } else {
319
    return 0;
319
  }else{
320
		return 0;
320 321
  }
321 322
}
322 323

  
323 324
/** @} **/ //end defgroup
325

  

Also available in: Unified diff