Cool Cap Engineer

Engineering by an anime nerd

Projects: Pygames Controller Update #1

Leave a comment

broken_trigger
So last Monday, my parts for all of my Pygames controller project, which was previously my N64 emulator controller project,came in. The parts included two analog sticks taken from a PS2 controller, 12 large tactile buttons, 2 triggers taken from an X-Box 360 controller, and breakout boards for the analog sticks and the X-Box 360 triggers.However, I had two very unpleasant surprises when I opened my package. The first problem was the absence of solder for the breakout boards. I had to wait until Wednesday to get the right solder for the breakout board. The last nasty surprise was one of the X-Box 360 controller triggers were broken. Since one of the triggers was broken, I’ll just make due without them. The triggers will be added to a future version of the controller.
debug_window
On Wednesday, I finally got some cheap solder from Radioshack. After going to Radioshack, I immediately soldered the analog sticks to their respective breakout board. Once I finished soldering, I quickly started to write test code for one of the analog sticks. Normally, I would use an USART terminal window like the serial window from the Arduino IDE to see the values from the analog stick, but I wanted to do something different. Instead, I used my 160×120 serial LCD as a debug window. As you see in the figure above, the debug window shows digital values associated with the horizontal and vertical pots of the joystick.

Afterwards, I started to write the actual code of the PIC MCU side of the controller. As of right now, the controller will have four listed functions:
IsControllerConnected (): Checks to see if the controller is connected to the computer. Returns 1 for true and 0 for false.
ReadJoystickX(): Read the state of the joystick’s x direction for the controller. Returns 0 for the neutral position, 1 for right and -1 for left.
ReadJoystickY (): Read the state of the joystick’s y direction for the controller. Returns 0 for neutral position, 1 for up and -1 for down.
IsButtonPressed () : Checks to see if the button is pressed. Returns 1 for true, and 0 for false.

I spent all of Thursday to write for the PIC MCU side of the controller. To make coding a little bit more organized, I decided to create 3 structures: joystick, button, and controller. Here’s the initial controller.h file.

#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON MY_BUTTON); 

I finished 3/4 of the code, until I ran into a stumbling block with creating the header files and the c files for the controller. Since my controller.h and controller.c needed to use the provided pic18f4553.h library to work, I had some difficulty figuring out how to include the file for not only my controller.c but also my main.c.However, I solved the problem by using ifndef, define, and endif.

#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON MY_BUTTON); 
#endif

From reading a couple tutorials on creating .h files, this piece of code made sure the header files included in the other .h file gets read. Although the problem with creating the .h and .c files was solved, a new problem occurred. Apparently, the function I wrote to set the ADC channel for the horizontal and vertical components of the joystick was not set, which caused a huge mess in my ADC readings. I simply solved this problem by passing my structures by reference whenever I need to use them for a function. I forgot that to edit the values from a structure, they must be passed by reference. Here’s the current version of my controller.h file.

#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <p18f4553.h>
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
#include <delays.h>
#include <delays.h>
#include <portb.h>
//===============================================================================================
//	Joystick Structure
//===============================================================================================
typedef struct
{
	int X_ADC_CHANNEL;			//	ADC channel for the horizontal component of the joystick
	int Y_ADC_CHANNEL;			//	ADC channel for the vertical component of the joystick
	int	X_ADC_READING;				//	Stores the ADC reading for the horizontal component
	int	Y_ADC_READING;				//	Stores the ADC reading for the vertical component
}JOYSTICK;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Button Structure
//===============================================================================================
typedef struct
{
	unsigned int BUTTON_PIN;		//	Pin which the button is connected to
	unsigned int BUTTON_READING;		//	Digital reading of the button.
}BUTTON;
//===============================================================================================
//
//===============================================================================================




//===============================================================================================
//	Controller Structure
//===============================================================================================
typedef struct
{
	JOYSTICK LEFT_JOYSTICK;				//	Left Joystick
	JOYSTICK RIGHT_JOYSTICK;			//	Right Joystick
	BUTTON	YELLOW_BUTTON;				//	Yellow Button
	BUTTON	RED_BUTTON;                             //	Red Button
	BUTTON	GREEN_BUTTON;				//	Green Button
	BUTTON	BLUE_BUTTON;				//	Blue Button
}CONTROLLER;
//===============================================================================================
//
//===============================================================================================




void CONTROLLER_INIT(CONTROLLER * MY_CONTROLLER);
int READJOYSTICKX(JOYSTICK * MY_JOYSTICK);
int READJOYSTICKY(JOYSTICK * MY_JOYSTICK);
unsigned int ISBUTTONPRESSED(BUTTON * MY_BUTTON);
#endif

I’m still trying to finish up my controller.c code, but I got it working to a point where the code can tell if the joystick is pointing left, pointing right, or in the neutral position.
20121227_231535
Hopefully I should finish the PIC MCU side of the Pygames controller sometime this weekend if not next week. Anyway, hope you guys have a good weekend!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s