-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
157 lines (130 loc) · 5.76 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//===============================================================================================
// Includes, defines, and global variables
//===============================================================================================
#include "controller.h"
// config functions, do not mess with
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config PWRT = ON
#pragma config DEBUG=OFF
char message[100];
//===============================================================================================
//
//===============================================================================================
//===============================================================================================
// Main loop
//===============================================================================================
void main(void)
{
// Creates the variable to readin the commands from the python script
char reading;
// Creates the controller structure and initizle the corresponding values
CONTROLLER PY_GAMES_CONTROLLER;
CONTROLLER_INIT(&PY_GAMES_CONTROLLER);
// Set all of PORTC as outputs
TRISC=0x00;
// Configure UART
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10);
Delay1KTCYx(4);
while(1)
{
// Wait for the next branch of data to come in and store it to the reading variable
while(!DataRdyUSART());
reading=ReadUSART();
// Checks to see if the current reading is equal to c, which means check to see if the controller is connected
if(reading=='c')
{
// Sends 1 to the python script
sprintf(message,"1");
putsUSART(message);
Delay1KTCYx(4);
}
// Checks to see if reading is equal to j for joystick
else if(reading=='j')
{
// Waits for the next data to be sent and stores it to reading
while(!DataRdyUSART());
reading=ReadUSART();
// Checks to see if the next reading is equal to l, or left
if(reading=='l')
{
// Waits for the next data to come in and stores it to reading
while(!DataRdyUSART());
reading=ReadUSART();
// Checks to see if x axis data is requested
if(reading=='x')
{
// Reads in the left joystick's x axis and sends it to the python script
int result=READJOYSTICKX(&PY_GAMES_CONTROLLER.LEFT_JOYSTICK);
sprintf(message,"%d",result);
putsUSART(message);
Delay1KTCYx(4);
}
// Check to see if y axis data is requested
else if(reading=='y')
{
// Reads the left joystick's y axis and sends the results to the python script
int result=READJOYSTICKY(&PY_GAMES_CONTROLLER.LEFT_JOYSTICK);
sprintf(message,"%d",result);
putsUSART(message);
Delay1KTCYx(4);
}
}
// Checks to see if reading is equal to r for right
else if(reading=='r')
{
// Waits for the next data to come in and stores the results to reading
while(!DataRdyUSART());
reading=ReadUSART();
// Checks to see if x axis data is requested
if(reading=='x')
{
// Reads in the right joystick's x axis and sends the results to the python script
int result=READJOYSTICKX(&PY_GAMES_CONTROLLER.RIGHT_JOYSTICK);
sprintf(message,"%d",result);
putsUSART(message);
Delay1KTCYx(4);
}
// Check to see if y axis data is requested
else if(reading=='y')
{
// Checks the right joystick's y axis and sends the results to the python script
int result=READJOYSTICKY(&PY_GAMES_CONTROLLER.RIGHT_JOYSTICK);
sprintf(message,"%d",result);
putsUSART(message);
Delay1KTCYx(4);
}
}
}
// Checks to see if reading is equal to b for button
else if(reading=='b')
{
// Waits for the next data to come in and stores the results to reading
while(!DataRdyUSART());
reading=ReadUSART();
// Checks to see if reading is equal to y for yellow button
if(reading=='y')
{
// Checks the status of the yellow button and sends it to the python script
int result=ISBUTTONPRESSED(&PY_GAMES_CONTROLLER.YELLOW_BUTTON);
sprintf(message,"%d",result);
putsUSART(message);
Delay1KTCYx(4);
}
// Checks to see if reading is equal to r for red button
else if(reading=='r')
{
// Checks the status of the red button and sends the results to the python script
int result=ISBUTTONPRESSED(&PY_GAMES_CONTROLLER.RED_BUTTON);
sprintf(message,"%d",result);
putsUSART(message);
Delay1KTCYx(4);
}
}
}
}
//===============================================================================================
//
//===============================================================================================