Tuesday, March 24, 2015

Dual-Compiling Code

While working with Assembly (using TASM), I realized that the semicolon, which is used for comments, is just the end of a statement in C++ (and Java, of course, amongst others). In addition, you can comment blocks of code out in C++/Java quite easily. The question, then, became whether or not it was possible to write one program to be read differently by both languages - where one language is commented out in the other, provided the file extension is appropriate. I decided to use "Hello World" as a proof of concept - though I later had to switch from C++ to Java, because you need to #include <iostream> in C++, but you can't have any statements before the preprocessor, which you need to have to use comments in Assembly. Java doesn't require any includes to print to the screen.

You could do about anything like this, though it would be quite useless and none too pretty to read. I'm not quite sure if this would work with any other languages, either - though I'm interested to see if you could cram three or four in one file.

This code is as follows:


Friday, March 28, 2014

Forced Co-Op N64 Controllers

I'm not trying to incite an argument of any kind, but I simply cannot play Majora's Mask for any extended period of time. I can't do it. This is a challenge for me, because those around me claim they don't understand my pain and that they love the game. In an effort to finally complete the game, I've asked for their assistance as the other half of player 1.






The basic idea for this project is to have two N64 controllers that are attached to each other, combining both inputs into one player's controls. One controller (controller A) would send the analog stick + Z button, and the other (controller B) would provide the other buttons. D-Pad control could easily be sent from A to B as well, but I ran out of wiring and was certain that I was doing it wrong anyways (doing it the way I did the other controls would have added 8 wires, doubling the cable used).


I ordered two used official N64 controllers off of ebay for $20 and immediately tested them both to see which buttons worked and which didn't. Though the controllers were in rough shape aesthetically, they both worked perfectly fine. I chose the controller designations arbitrarily and then began to open them up and begin work. I didn't take a picture of the process, but I did find this circuit board for the controller online:

To help, I've boxed the two important locations that I worked with:


Location 1 (bottom left) has 6 contacts and is where the joystick is soldered to. Location 2 (middle right) is the two contacts that the Z button goes to.

The process is quite simple, actually. One needs only to solder the wires from A to B along these points and make sure that B's analog stick and Z button were disconnected, but not removed (so they felt normal, but did nothing). In addition, the cable that ran off of A was removed because it served no further use.

After this, I realized the size of the cables was too large to fit through the original cable hole (and in the case of B, there just plain wasn't a hole to use). This was easily remedied by drilling a larger hole in A and a new hole in B to route the wires through. I believe that size drill I used was 3/8" and it was perfect for what I needed.

After the soldering was done and the drilling, I coated the soldered wire ends in hot glue to prevent any accidental touching (even though there was none in the testing). The finished product looked like this:


Not pretty, but it worked. Putting the board back in the controller was a lot of fun, though.


But everything worked, no problems!

Portable Arcade Box

It was a few years ago that I had the idea for this, but I lacked the technical know-how to make it happen. The idea was to create a box with arcade controls built into the top of it, just like any USB arcade controller you find online. But I knew there was room for improvement, so I wanted to build the arcade computer into the box itself, so that you only need to plug the box into a TV and you'd be ready to go. Furthermore, I thought it could be done for less than $100.



I got the parts from many different places, Jamma Boards being the only one I can recall for the arcade parts (I know I at least got the joystick from here, probably all the buttons, too). I got my Raspberry Pi Model B from amazon for around $45, a clear case for $13, and a 32gb SD card. The wiring was just what I could find at the local electronics store (20 gauge, maybe?). And I had to get a Teensy (the 2.0 was the most recent model at time of purchase, so that's what I got) for $16.

I'd had, at this point, no electronics experience outside of basic soldering, and little construction experience, too. Everything ended up being a challenge for me during this project, but almost entirely due to my inexperience.

I first made sure all my wiring worked, so I had to learn C to program the teensy board (based off of someone else's USB gamepad code). During the soldering for the board, I ended up frying one of the contacts due to improper soldering equipment, so I had to reprogram the board to make up for it, so if you look at the code it will be a little... imperfect. But it all ended up working, every button registered with the computer as a generic USB gamepad (which was important, because the RPi ran Linux). I suppose that, if I really knew what I was doing, I could have just attached the buttons to the GPIO pins, but I wasn't about to tackle that beast. The code can be found here.

Secondly, I got the RPi ready to work with the controller. I went through the initial setup and installed MAME4all-Pi, and then the arcade games themselves.



Then I had to build the box itself. It's not well built, and it's a little thicker than I'd like, but due to hasty designing, I got what I got and haven't gotten around to fixing it yet. Everything works, it's just a little imperfect. I also had to drill a hole into the back of the box in order to run the external cables through. I took the controller design online and modified it a little bit and ended up with this:



I'll admit the button placement near the joystick was a horrible idea, but I was dealing with size constraints at the time.

After finishing the box, putting all the buttons in, screwing down the RPi case, and dealing with the wiring, I ended up with this on the inside:

 (not shown: teensy)

Plugging a keyboard into the RPi, I only have to navigate to usr/local/bin/indiecity/InstalledApps/mame4all_pi/Full and run ./mame and everything ran!


I found and old cost analysis spreadsheet I made awhile back and this was the official rundown at the end:

Item Cost ($) Quantity Total Cost ($)
Pushbutton 1.95 10 19.5
Joystick 8.95 1 8.95
Teensy 2.0 16.00 1 16.00
RPi 46.83 1 46.83
Power Cable 8.99 1 8.99
Case 13.59 1 13.59
32gb SDHC 27.74 1 27.74
141.60

More than I anticipated, but still not bad. There's definitely room for improvement, too, which can bring the price down (I suspect that it can still be done sub-$100).

In the beginning...

When I get an idea for something to make, I look it up first to see
a) if it's been done before
and b) if there's anything I should know beforehand.
Oftentimes it's small little blogs with a few posts about the matter, and that's a fantastic help. That's what I made this blog for, to be that help to at least one person. While I am by no means an engineer of any sort (I've had no classes on engineering of any sort), I do like to try making things and sometimes it ends up working.

It's a 100% certainty that anything you see here can be done a better way. I've accepted this. If you can tell me how, I'd love to keep learning! Drop a line and keep me informed.