Cool Cap Engineer

Engineering by an anime nerd


Leave a comment

Projects: Pygames Controller Final Update

pygamescontroller

Well, I head back to RPI next Sunday. I finally can say I finally finished building the prototype of my Pygames Controller and successfully completed the “before I go back to RPI” requirement. The only thing I wished I could of adding for the project is a decoupling capacitor for each button. Otherwise, I am very proud of the prototype. The code for the project can be found at the following URL.  Feel free to post any problems or concerns you have about the code, and I will make sure I add it to the next version of the controller.Now I change the project status to complete on the projects page 😀 In fact, for now on, instead of placing code in these blog posts, I will upload code to Github. Anyway, I see you guys later!


Leave a comment

Projects: Pygames Controller Update #2

So I’ve been busy for the past couple of days for the Pygames Controller project. However, I would say that I’m 90% done with the Pygames controller project. Let me talk about what was been done thus far.

20130101_121929

After my last update, I finished writing the C file for the controller. For some reason, serial communication was not working at the time, so I had to test my code using LEDs. When I flick the left joystick to the left, it will turn on the smallest yellow LED on the board. When the right joystick is flicked up, then it will turn on the green LED. Finally, when I pressed down the yellow tactile switch, it will turn on the yellow LED next to the smallest yellow LED.From my LED tests, I had to modify a few things with the button press code, but the c file for the controller is fully functional.

406599_4456933653728_1151303803_n

As I said in my last post, I had to revise my original layout of the controller. Instead of having 2 joysticks, four tactile buttons, and two triggers, I had to reduce the parts of the controller to two analog sticks and two tactile buttons. Since I’m testing the controller on a breadboard, I wanted to select a layout that could fit onto the breadboard I current have.

debug_results

I did some final testing for the controller C code by seeing the states of the joysticks, and buttons using Arduino’s serial window.  Although testing the controller using the serial window severely slowed my computer down while I was watching the latest episode of JoJo’s Bizarre Adventure (which I highly recommend if you love 80’s anime like Fist of the North Star), I was fairly pleased by the results. However, when I work on a future version of the controller, which I’m 95% I will,decoupling capacitors will need to be added at the buttons terminals because when I pressed and release the button, it does not instantly go to an “off” state immediately.  This is a common, but simple, problem that exists when dealing with digital inputs for microcontrollers.

Right now, I’m writing the python class for the controller. I hope I can finish the python class by the end of this week. Afterwards, I will release the python class, c files, default schematic, and documentation on Google code.  One of my friends even agreed to help me test out my controller by creating a small python game designed exclusively for the controller.

Well, that’s all I have for you guys! Again, if you have any concerns or suggestions, feel free to post a comment.


Leave a comment

Projects: Pygames Controller Update #1

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!


Leave a comment

Projects: N64 Controller Now Pygames Controller

481580_4367919388427_284505977_n

So yesterday, I started testing the Python script to generate the keyboard board events that the emulator could recognize. The way the script works is that whenever I pressed the tact switch connected to the PIC8F4553 microcontroller, it sends a simple c character to the python script. When the python script sees the c character, it will then generate a keyboard event which the x key (A button on the N64 emulator) is pressed.

When I tested the script, it works in Windows perfectly. Heck, I could create a virtual keyboard and mouse for windows based for a couple test scripts alone (future project?). However, whenever I run the script, then run the emulator, the emulator ignores the keyboard events generated. To be fair, it recognizes it sometimes when I click the Python IDE then switch to the emulator window. I sent an email the Project64 creator, posted on the Stackoverflow forums, but it seems like no one knows why the emulator is ignoring the keyboard events.  However, I realized that I will shift the project focus from a controller designed for emulators, to a controller that can be used for Pygames.

By changing the project focus to Pygames, I know for a fact I can finish the project before January 21st as well making this a RCOS project for next semester. Not to mention I still use the parts I ordered a couple days ago. What I’m going to do is get an idea of functions that will allow Pygame developers to expand their games and extra parts I will need. One of the extra parts I will need an accelerometer to add motion controls. I’ll reveal the list of controller functions later on this week.