Anyway, I have finally got back onto my Arduino Taipan! port, mainly since I have run out of both steel and oxygen so I can’t work on the Austin 7 until the weekend when I can get more.
I did end up getting the tiny LCD screen from Deal Extreme. It is really quite nice. Small, cheap (less than $30NZ delivered) runs off 12 volts (and lower apparently) and it has two video inputs and will do NTSC and PAL. I found that it worked better using NTSC so that is what I have the tvout library configured to use. With some fiddling I found I could run in a maximum resolution of 144 by 104 pixels. This does cause issues but I shall get to them later. I played around with the library and did some display of static test screens and a bitmap I made, hand modified (in MS Paint no less!) from a screen shot of the original Apple 2 game. The screen resolution I have is smaller than the original Apple 2 screen resolution by quite a bit so I will need to be creative in how I display it all.
These were just static text and bitmaps however so I had to work out how to send the display Arduino information to display. Now this is where things get tricky. The most obvious thing to try is to use the Arduinos native serial library and send it data that way. Nope, won’t work. And the reason it won’t work is one of the annoying things about using Arduinos – library incompatibility.
There are many libraries available for the Arduino to do many interesting things. Some come as part of the IDE, others are written by third parties (such as tvout). The issue is some of these libraries will be using the same hardware resources on the microcontroller. If two libraries happen to try to use the same resource bad things can happen. Your code will compile fine, it just won’t run how you expect, or at all. It can be very frustrating trying to work out why it doesn’t work.
So, turns out the tvout library clashes with the serial library so you can’t use them both. They did however provide a work around, a polling based serial library to use. Unfortunately, and this is a problem with a lot of libraries for the Arduino, this is not clearly documented! I discovered it through Google searches and finding comments in other blogs, mainly this wonderful rant, which beautifully explains one of the annoying things about the Arduino – so much stuff is hidden. You end up having to poke about in the code to work out what the hell it is doing and if you’re going to have issues. I guess for simple things it’s not and issue and what I am trying to do is probably pushing things a little but still, how hard would it be in the documentation to list what resources the library is using?
Right, my rant over back to what I was trying to do. Using the totally undocumented (OK, not quite over) pserial provided with tvout I was able to send serial data to my Arduino and display it on the small LCD. I even wrote some code to start playing around with things like entering your firm name and started trying to work out how to do the ship fighting graphics. I did have al modify the tvout library to add a get cursor function. There is one to set the cursor position but then you can’t find out what it is. I needed this so I could write then delete characters on the screen. This did all work but every so often the serial stream seemed to get corrupted for some reason. I wasn’t exactly sure why.
With this basically working I set about working out how to send serial data from one Arduino to another. Originally I wanted to use I2C since I am already using that on the main Arduino (the one running the code) to read from the EEPROM containing all the text strings. That won’t work on the display Arduino (the one running tvout) for the resource incompatibility issues mentioned above. I can however use the serial pins on the Arduino, not through the USB interface but direct connecting the TX to RX pins. I know the main board can output serial and that the display board can receive serial so that should work.
Physically doing this turned out to be a pain as I only had one Arduino. So I built up a standalone breadboard version so I could run two Arduinos at once. This did work but programming them turned out to be a pain in the neck as you must remove the chip form the Arduino board every time you want to program the breadboard version. Also the breadboard one can’t be plugged into the PC via USB so you have no serial output for debugging.
This was all too difficult so today after work I went and bought a second Arduino. Actually a Freetronics Arduino Eleven. This is a Arduino Uno compatible board. I have a Freetronics Ethernet shield about here somewhere too and they make nice products. They are Aussies but they’re the OK kind.
Things are MUCH easier now. I am able to get both Arduinos working at once on different serial ports on the PC. I can program them without unplugging cables and I can see what both are doing via the serial connections. So tonight I was able to set up one Arduino to run my existing Taipan code which spews text out of the serial port and connect that up to my second Arduino running tvout and working like a serial terminal. Brilliant!
Only it didn’t bloody work!
I would get a few characters then just rubbish. It looked very much like a buffer overflow. I did finally get it to work by using the example serial terminal that comes with the tvout library. I compared it to my code and discovered the difference. The screen resolutions. I finally twigged to what is happening (I think). I am running out of memory. Again. The same thing that happened to me right at the start when I had too many strings in my code.
Ever get the feeling you’re going around in circles?
I dropped my resolution to 144 x 96 (144 x 104 doesn’t work at all) and now, finally I can see the output from the main board on the tiny LCD and it is working reliably. There is no formatting so the text lines are broken in odd places and stuff scrolls off the top of the screen quickly since I only have 15 lines of text on screen but it does work and the game is playable.
It seems the resolution affect the amount of free memory a lot. At 144 x 104 there were 66 bytes free (and nothing ran). At 144 x 96 I have 150 bytes free (and things ran). At the example resolution of 184 x 72 I have 222 bytes free and at 128 x 96 (the tvout default resolution) I have 342 bytes to play with. I am probably going to have to mess about with finding the optimal resolution to have a sensible looking screen and still have enough SRAM to actually be able to do things. The amount of memory used is affected by the size of the frame buffer which is directly affected by the resolution of course. The x dimension has to be in multiples of 8 by the way.
Oh, one thing to note is if you do have two Arduinos connected via their RX/TX pins make sure you disconnect them before trying to program the chips. Otherwise you get the cryptic error below and your COM port stops working until you unplug the USB cable:
avrdude: stk500_getsync(): not in sync: resp=0x00
So, now I have them talking and know I have serious memory issues what’s next? Basically I need to see if I can now get the main board to send data to the display board in a sensible way. And have the display board show it in a sensible way. I will do this by sending messages with special control characters so the display knows how to display them.
If all this fails, and I think it might due to the memory constraints, I have a plan C. I wonder at what point should I say “Bah! Too hard!” and give up?
Oh well, here is how it looks now anyway.
Oh, I did try reading the Tai-pan book.