1 //*****************************************************************************
 2 // controller_sensors.c
 3 // this file contains code for reading in the controller sensor values
 4 // author: chris madormo
 5 //*****************************************************************************
 6 
 7 // required includes
 8 #include <hidef.h>         /* common defines and macros */
 9 #include <mc9s12e128.h>    /* derivative information */
10 #include <S12e128bits.h>   /* bit definitions  */
11 #include <Bin_Const.h>     /* macros to allow specifying binary constants */
12 #include <termio.h>        /* to get prototype for kbhit() */
13 #include "S12eVec.h"
14 #include "ADS12.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 // my includes
18 #include "controller_sensors.h"
19 
20 // defines
21 #define LED_ON       1
22 #define LED_OFF      0
23 
24 // private function declarations
25 static unsigned char filterThrottleInput(short throttle);
26 static unsigned char filterDirectionInput(short direction);
27 static unsigned char convertToUC(short sup);
28 static short get_low_range(unsigned char range);
29 static short get_high_range(unsigned char range);
30 static void setA(char on);
31 static void setB(char on);
32 static void setC(char on);
33 static void setD(char on);
34 static void setE(char on);
35 static void setF(char on);
36 static void setG(char on);
37 
38 //*****************************************************************************
39 
40 // get throttle (flex sensor)
41 //unsigned char get_throttle(void) {
42 char get_throttle(void)
43 {
44     short throttle = ADS12_ReadADPin(0);
45     char signed_throttle = (char)(throttle>>2);
46 
47     if (signed_throttle < 0)
48         {
49             signed_throttle += 128;
50         }
51     else
52         {
53             signed_throttle -= 128;
54         }
55 
56     return signed_throttle;
57 }
58 
59 // get direction (accelerometer)
60 //unsigned char get_direction(void) {
61 char get_direction(void)
62 {
63     short direction = ADS12_ReadADPin(1);
64     char signed_direction = (char)(direction>>2);
65 
66     if (signed_direction < 0)
67         {
68             signed_direction += 128;
69         }
70     else
71         {
72             signed_direction -= 128;
73         }
74 
75     signed_direction = ~signed_direction;
76 
77     return signed_direction;
78 }
79 
80 // get shoot (hall sensor)
81 char get_shoot(void)
82 {
83     unsigned char shoot = 1 - ((PTT & BIT5HI)>>5);
84     return shoot;
85 }
86 
87 // connect button (lightsaber button)
88 unsigned char isButtonPressed(void)
89 {
90     return !((PTT & BIT6HI)>>6);
91 }
92 
93 // get boat address
94 unsigned char get_boat_address(void)
95 {
96     // get sensor value
97     short boat_value = ADS12_ReadADPin(2);
98 
99     // determine which boat was chosen
100     if ( (boat_value >= get_low_range(0)) && (boat_value < get_high_range(0)) )
101         {
102             setLEDDisplay(ONE);
103             return 0x81;
104         }
105     else if ( (boat_value >= get_low_range(1)) && (boat_value < get_high_range(1)) )
106         {
107             setLEDDisplay(TWO);
108             return 0x82;
109         }
110     else if ( (boat_value >= get_low_range(2)) && (boat_value < get_high_range(2)) )
111         {
112             setLEDDisplay(THREE);
113             return 0x83;
114         }
115     else if ( (boat_value >= get_low_range(3)) && (boat_value < get_high_range(3)) )
116         {
117             setLEDDisplay(FOUR);
118             return 0x84;
119         }
120     else if ( (boat_value >= get_low_range(4)) && (boat_value < get_high_range(4)) )
121         {
122             setLEDDisplay(FIVE);
123             return 0x85;
124         }
125     else if ( (boat_value >= get_low_range(5)) && (boat_value < get_high_range(5)) )
126         {
127             setLEDDisplay(SIX);
128             return 0x86;
129         }
130     else if ( (boat_value >= get_low_range(6)) && (boat_value < get_high_range(6)) )
131         {
132             setLEDDisplay(SEVEN);
133             return 0x87;
134         }
135     else if ( (boat_value >= get_low_range(7)) && (boat_value < get_high_range(7)) )
136         {
137             setLEDDisplay(EIGHT);
138             return 0x88;
139         }
140     else if ( (boat_value >= get_low_range(8)) && (boat_value < get_high_range(8)) )
141         {
142             setLEDDisplay(NINE);
143             return 0x89;
144         }
145     else if ( (boat_value >= get_low_range(9)) && (boat_value < get_high_range(9)) )
146         {
147             setLEDDisplay(TEN);
148             return 0x8A;
149         }
150     else if ( (boat_value >= get_low_range(10)) && (boat_value < get_high_range(10)) )
151         {
152             setLEDDisplay(ELEVEN);
153             return 0x8B;
154         }
155     else if ( (boat_value >= get_low_range(11)) && (boat_value < get_high_range(11)) )
156         {
157             setLEDDisplay(TWELVE);
158             return 0x8C;
159         }
160     else if ( (boat_value >= get_low_range(12)) && (boat_value < get_high_range(12)) )
161         {
162             setLEDDisplay(THIRTEEN);
163             return 0x8D;
164         }
165     else if ( (boat_value >= get_low_range(13)) && (boat_value < get_high_range(13)) )
166         {
167             setLEDDisplay(FOURTEEN);
168             return 0x8E;
169         }
170     else if ( (boat_value >= get_low_range(14)) && (boat_value < get_high_range(14)) )
171         {
172             setLEDDisplay(FIFTEEN);
173             return 0x8F;
174         }
175     else
176         {
177             // in between ranges
178             //printf("in between valid addresses\r\n");
179             setLEDDisplay(OFF);
180             return 100;
181         }
182 }
183 
184 // set the led display
185 void setLEDDisplay(DisplayChar_t charToDisplay)
186 {
187 
188     // uses T0-->T4, U6->U7 to light the display
189 
190     // figure out which pins to turn low
191     switch (charToDisplay)
192         {
193         case ZERO:
194             setA(LED_ON);
195             setB(LED_ON);
196             setC(LED_ON);
197             setD(LED_ON);
198             setE(LED_ON);
199             setF(LED_ON);
200             setG(LED_OFF);
201             break;
202         case ONE:
203             setA(LED_OFF);
204             setB(LED_ON);
205             setC(LED_ON);
206             setD(LED_OFF);
207             setE(LED_OFF);
208             setF(LED_OFF);
209             setG(LED_OFF);
210             break;
211         case TWO:
212             setA(LED_ON);
213             setB(LED_ON);
214             setC(LED_OFF);
215             setD(LED_ON);
216             setE(LED_ON);
217             setF(LED_OFF);
218             setG(LED_ON);
219             break;
220         case THREE:
221             setA(LED_ON);
222             setB(LED_ON);
223             setC(LED_ON);
224             setD(LED_ON);
225             setE(LED_OFF);
226             setF(LED_OFF);
227             setG(LED_ON);
228             break;
229         case FOUR:
230             setA(LED_OFF);
231             setB(LED_ON);
232             setC(LED_ON);
233             setD(LED_OFF);
234             setE(LED_OFF);
235             setF(LED_ON);
236             setG(LED_ON);
237             break;
238         case FIVE:
239             setA(LED_ON);
240             setB(LED_OFF);
241             setC(LED_ON);
242             setD(LED_ON);
243             setE(LED_OFF);
244             setF(LED_ON);
245             setG(LED_ON);
246             break;
247         case SIX:
248             setA(LED_ON);
249             setB(LED_OFF);
250             setC(LED_ON);
251             setD(LED_ON);
252             setE(LED_ON);
253             setF(LED_ON);
254             setG(LED_ON);
255             break;
256         case SEVEN:
257             setA(LED_ON);
258             setB(LED_ON);
259             setC(LED_ON);
260             setD(LED_OFF);
261             setE(LED_OFF);
262             setF(LED_OFF);
263             setG(LED_OFF);
264             break;
265         case EIGHT:
266             setA(LED_ON);
267             setB(LED_ON);
268             setC(LED_ON);
269             setD(LED_ON);
270             setE(LED_ON);
271             setF(LED_ON);
272             setG(LED_ON);
273             break;
274         case NINE:
275             setA(LED_ON);
276             setB(LED_ON);
277             setC(LED_ON);
278             setD(LED_ON);
279             setE(LED_OFF);
280             setF(LED_ON);
281             setG(LED_ON);
282             break;
283         case TEN: // A
284             setA(LED_ON);
285             setB(LED_ON);
286             setC(LED_ON);
287             setD(LED_OFF);
288             setE(LED_ON);
289             setF(LED_ON);
290             setG(LED_ON);
291             break;
292         case ELEVEN: // B
293             setA(LED_OFF);
294             setB(LED_OFF);
295             setC(LED_ON);
296             setD(LED_ON);
297             setE(LED_ON);
298             setF(LED_ON);
299             setG(LED_ON);
300             break;
301         case TWELVE: // C
302             setA(LED_ON);
303             setB(LED_OFF);
304             setC(LED_OFF);
305             setD(LED_ON);
306             setE(LED_ON);
307             setF(LED_ON);
308             setG(LED_OFF);
309             break;
310         case THIRTEEN: // D
311             setA(LED_OFF);
312             setB(LED_ON);
313             setC(LED_ON);
314             setD(LED_ON);
315             setE(LED_ON);
316             setF(LED_OFF);
317             setG(LED_ON);
318             break;
319         case FOURTEEN: // E
320             setA(LED_ON);
321             setB(LED_OFF);
322             setC(LED_OFF);
323             setD(LED_ON);
324             setE(LED_ON);
325             setF(LED_ON);
326             setG(LED_ON);
327             break;
328         case FIFTEEN: // F
329             setA(LED_ON);
330             setB(LED_OFF);
331             setC(LED_OFF);
332             setD(LED_OFF);
333             setE(LED_ON);
334             setF(LED_ON);
335             setG(LED_ON);
336             break;
337         case OFF:
338             setA(LED_OFF);
339             setB(LED_OFF);
340             setC(LED_OFF);
341             setD(LED_OFF);
342             setE(LED_OFF);
343             setF(LED_OFF);
344             setG(LED_OFF);
345             break;
346         default:
347             // should never be here
348             break;
349         }
350 }
351 
352 //*****************************************************************************
353 
354 // private function definitions
355 
356 // calculate simple moving average of throttle input
357 static unsigned char filterThrottleInput(short throttle)
358 {
359     static unsigned char tbuff[10] = {0,0,0,0,0,0,0,0,0,0};
360     unsigned int sum = 0;
361     int i;
362 
363     // move all parts of buffer right one
364     for (i = 0; i < 9; i++)
365         {
366             tbuff[i] = tbuff[i+1];
367         }
368 
369     tbuff[9] = convertToUC(throttle);
370 
371     for (i = 0; i < 10; i++)
372         {
373             sum += tbuff[i];
374         }
375 
376     sum /= 10;
377 
378     return (unsigned char)sum;
379 }
380 
381 // calculate simple moving average of direction input
382 static unsigned char filterDirectionInput(short direction)
383 {
384     static unsigned char dbuff[10] = {0,0,0,0,0,0,0,0,0,0};
385     unsigned int sum = 0;
386     int i;
387 
388     // move all parts of buffer right one
389     for (i = 0; i < 9; i++)
390         {
391             dbuff[i] = dbuff[i+1];
392         }
393     dbuff[9] = convertToUC(direction);
394 
395     for (i = 0; i < 10; i++)
396         {
397             sum += dbuff[i];
398         }
399 
400     sum /= 10;
401 
402     return (unsigned char)sum;
403 
404 }
405 
406 static unsigned char convertToUC(short sup)
407 {
408     return ( (unsigned char)(sup>>2) );
409 }
410 
411 static short get_low_range(unsigned char range)
412 {
413     return (34 + range*68 - 30);
414 }
415 
416 static short get_high_range(unsigned char range)
417 {
418     return (34 + range*68 + 30);
419 }
420 
421 // for use with LED display
422 static void setA(char on)
423 {
424     if (on == 1)
425         {
426             PTU &= BIT7LO;
427         }
428     else
429         {
430             PTU |= BIT7HI;
431         }
432 }
433 
434 // for use with LED display
435 static void setB(char on)
436 {
437     if (on == 1)
438         {
439             PTU &= BIT6LO;
440         }
441     else
442         {
443             PTU |= BIT6HI;
444         }
445 }
446 
447 // for use with LED display
448 static void setC(char on)
449 {
450     if (on == 1)
451         {
452             PTT &= BIT0LO;
453         }
454     else
455         {
456             PTT |= BIT0HI;
457         }
458 }
459 
460 // for use with LED display
461 static void setD(char on)
462 {
463     if (on == 1)
464         {
465             PTT &= BIT4LO;
466         }
467     else
468         {
469             PTT |= BIT4HI;
470         }
471 }
472 
473 // for use with LED display
474 static void setE(char on)
475 {
476     if (on == 1)
477         {
478             PTT &= BIT3LO;
479         }
480     else
481         {
482             PTT |= BIT3HI;
483         }
484 }
485 
486 // for use with LED display
487 static void setF(char on)
488 {
489     if (on == 1)
490         {
491             PTT &= BIT2LO;
492         }
493     else
494         {
495             PTT |= BIT2HI;
496         }
497 }
498 
499 // for use with LED display
500 static void setG(char on)
501 {
502     if (on == 1)
503         {
504             PTT &= BIT1LO;
505         }
506     else
507         {
508             PTT |= BIT1HI;
509         }
510 }
511 
512 //*****************************************************************************