ReflexBall Rally
 All Data Structures Files Functions Variables Macros
gameport.c
Go to the documentation of this file.
1 #include <eZ8.h> // special encore constants, macros and flash routines
2 #include <sio.h> // special encore serial i/o routines
3 #include "gameport.h"
4 #include "ansi.h"
5 
6 unsigned int readADC(unsigned char channel) { // Read a specific analog channel
7  unsigned char inHigh, inLow;
8  unsigned int ADC_data;
9 
10  ADCCTL = 0x80 | 0x20 | (channel & 0x0F); // Enable ADC on the selected channel and use external voltage (3.3V) as VREF
11  while (ADCCTL & 0x80); // Wait for conversion to be completed
12  inHigh = ADCD_H; // ADC high byte
13  inLow = ADCD_L; // ADC low low byte
14  ADC_data = ((unsigned int)inHigh << 2)| inLow >> 6; // ADC output word
15 
16  return ADC_data;
17 }
18 
19 void initGameport() { // Initialize the steering wheel's digital buttons and analog wheel and pedals
20  PBDD = (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); // Use PB2, PB3, PB4 and PB5 as digial inputs
21  PBAF = (1 << 1) | (1 << 0); // Use PB0 and PB1 for ADC conversion
22 }
23 
24 unsigned char getGameportButtons() { // Read the digital buttons
25  return (((~PBIN) & 0x3C) >> 2);
26 }
27 
28 char readSteeringWheel() { // Read the steering wheel, this will output a value that can be directly used with moveStriker()
29  int val = readADC(1);
30 
31  //gotoxy(50,10);
32  //printf("Val: %04d",val);
33 
34  // The driving wheel is not linear therefor this table is needed
35  if (val > 1000)
36  return -6; // We will move it more aggressively to the side
37  else if (val > 796)
38  return -4;
39  else if (val > 712)
40  return -2;
41  else if (val > 610)
42  return -1;
43  else if (val > 570)
44  return 0;
45  else if (val > 500)
46  return 1;
47  else if (val > 466)
48  return 2;
49  else if (val > 440)
50  return 4;
51  else
52  return 6;
53 }