• Recent Tutorials

  • Tripping The Class Fantastic: Programs Within Programs

    The Results

    This code was used in the final version of the firmware and unlike my state machine based attempt, it is incredibly compact taking only 71 words in program memory (for comparison, that's about 0.9%). As it happens, I wasn't short of memory because I'd deliberately chosen the biggest device in that family, but I wanted to keep the code small so I could use the smallest device in production, thus maximizing profit (memory costs in these devices). The final firmware took a little over 1000 program words meaning I could use the 2K device. You could argue that maybe I should have stuck with my state-machine driven attempt, but it was the first thing I wrote because I needed feedback straight away, the thought of spending over 300 words just driving the LED made me panic a little... if the LED driver is that big, how big is the rest of it going to be?

    I'm sure you're wondering what has this got to do with Pascal or developing games?

    Well, the article isn't really about the code (although there is an explanation of that little block of C), it's more about thinking outside the box on things like this. Ask yourself... Do I really need a scripting engine? Can I do this without that complexity? Is there a simpler way of doing this?

    If you want a visual indicator in game that has simple things to indicate, can you do it simply with this sort of approach? For the PC it's even better as we have 32 and 64 bit numbers, I only had 8 bits to play with. We also have much bigger chunks of memory knocking around that we can use for storing our 'programs'.

    As I say, this isn't really about Pascal or games, and I'd go as fas as to say it's not really a tutorial, just a little story that one day may help you think outside the box and solve a problem using a method that is nice, clean and simple.
    Comments 10 Comments
    1. pstudio's Avatar
      pstudio -
      Interesting article. Of course there's a difference between writing programs on computers with almost unlimited memory compared to micro controllers but the point about keeping it simple and clean is absolutely still valid.
      I think many developers (myself included) sometimes have a tendency to make solutions more complicated than the really are. The reasons can be many. Maybe you're thinking of possible future usages of your code or maybe you just want to try a new more complex solution or maybe you just don't know better. Often the truth is that you could have settled with a simple and easy solution since that's all you really need and will need in the future.

      I've enjoyed reading this just like I've enjoyed reading your other articles through time. Looking forward to your next piece of writing.
    1. ricardo_sdl's Avatar
      ricardo_sdl -
      Very good article. Some good ideas.
    1. AthenaOfDelphi's Avatar
      AthenaOfDelphi -
      Thanks for the feedback guys, glad you enjoyed it
    1. chronozphere's Avatar
      chronozphere -
      Pretty interesting. Didn't know you did microcontroller programming. I'm currently exploring that stuff. I've got some ATmega and ATTiny chips here. I'd like to build some cool things with RGB LED's and perhaps do some audio projects.

      The raspberry pi is also interesting. It's GPIO pins allow you to drive all kinds of circuitry and since it can run a lightweight linux distribution, it will run pascal programs as well. You'd have to set up an ARMv6 cross compiler though.
    1. AthenaOfDelphi's Avatar
      AthenaOfDelphi -
      I've been tinkering with PICs on and off for about 6 years. Very cool little devices. My latest project is a digital controller for electrifying old mechanical clocks so they need very little attention (picture).

      Specs of this little adventure are:-

      • PIC18F97J94 (64,000 words of program memory)
      • 4x4 keypad
      • 20x4 LCD
      • 24 lines of 5v compatible digital IO (3 blocks of 8 bits which can be set to in or out)
      • Serial memory for configuration
      • Real time clock
      • Lead acid battery backup
      • High power DC switches for driving solenoids to strike chimes
      • USB interface for configuration/final setup


      In one of my old jobs, I was designing a commercial version of the First East Disco Dance Floor. My final prototype hooked up to a PC via RS232 and provided an 8x8 grid. The production model was going to use an Ethernet interface so we could lay bunches of these 8x8 1m square tiles next to each other and hook them up to a drive PC using 100Mbit Ethernet (pics of this are at the bottom of this page).

      Programming such small devices reminded me that sometimes you should keep things simple and compact... and that's what prompted this article
    1. SilverWarior's Avatar
      SilverWarior -
      Quote Originally Posted by AthenaOfDelphi View Post
      In one of my old jobs, I was designing a commercial version of the First East Disco Dance Floor. My final prototype hooked up to a PC via RS232 and provided an 8x8 grid. The production model was going to use an Ethernet interface so we could lay bunches of these 8x8 1m square tiles next to each other and hook them up to a drive PC using 100Mbit Ethernet (pics of this are at the bottom of this page).
      So thic could actually be done. I have similar idea in my head for quite some time. But for my idea I would probably need a bit bigger circuit as I would use bunch of Unltra Bright LED's (about 1200) on each panel. (basic RGB capability)
      So for this ethernet version would it be posible to automaticaly specify IP adress using router so that each board has its own IP.


      BTW Do you sell your circuits across EU. I might have saw an electronic circuit designed by you once. I cant remember for what it was for. But that Christina Loise Warne thext on electronic circuit seems verry familiar to me.
    1. AthenaOfDelphi's Avatar
      AthenaOfDelphi -
      Quote Originally Posted by SilverWarior View Post
      So thic could actually be done. I have similar idea in my head for quite some time. But for my idea I would probably need a bit bigger circuit as I would use bunch of Unltra Bright LED's (about 1200) on each panel. (basic RGB capability)
      So for this ethernet version would it be posible to automaticaly specify IP adress using router so that each board has its own IP.


      BTW Do you sell your circuits across EU. I might have saw an electronic circuit designed by you once. I cant remember for what it was for. But that Christina Loise Warne thext on electronic circuit seems verry familiar to me.
      The PIC I was looking at using for the dance floor had a built in Ethernet driver, all I had to do was add a socket with the various inductors etc. in that match the line to the circuit. Based on this, I have to assume it has a unique MAC address and thus, you could implement DHCP to provide a specific MAC address with a specific IP address. A lot of PICs have flash memory on board that you can store basic config in, but adding a dedicated config memory chip would cost you less than say 1 per PIC. Depending on volume of config you could probably get away with a single FRAM chip. Use one of your SPI/I2C channels for that, then all you need is a chip enable line (I prefer SPI for memory chips).

      Driving the LEDs, I used the MAX7313. This is an I2C chip, that can drive 16 intensity controlled channels. You can pop 64 of them on a single I2C bus using their addressing mechanism, so that works out at 21 per color (don't mix colours on one chip). That yields 336 pixel capability if you were to fully load a single I2C bus.

      As for selling my stuff... nope, I don't sell anything like that yet... I've not ruled it out, but to date I haven't sold anything. The difficulty with that sort of thing is cost of manufacture. You have to be sure you can sell in volume otherwise it's a big loss.

      I'm beginning to think maybe we should have a dedicated electronics forum
    1. SilverWarior's Avatar
      SilverWarior -
      Quote Originally Posted by AthenaOfDelphi View Post
      I'm beginning to think maybe we should have a dedicated electronics forum
      To be honest I not into electronics myself (I do know some basics) but I still got this idea.
    1. masonwheeler's Avatar
      masonwheeler -
      I have to wonder, though, in an age of Raspberry Pis and Arduinos, is any of this "low hardware, high-cost electronics" stuff at all relevant anymore?
    1. AthenaOfDelphi's Avatar
      AthenaOfDelphi -
      Absolutely it is. Whilst Pi's and Arduinos etc. are cheap, if you're going for volume it's very hard to beat one time programmable microcontrollers and electronics dedicated to the one task they have to perform. Pi's et al. are fine for prototyping, but they carry a lot of baggage that they require to make them general purpose, this adds unnecessary cost which every manufacturer will be wanting to keep to an absolute minimum. Every single component on custom boards like this should have a purpose... if not, it's added cost and should be removed.

      Plus, unless you start designing your own PCBs (which kind of negates one the benefits of development boards - the fact you don't have to spend a lot of time laying the board out), you can't do funky stuff like fit the PCB to the physical constraints of whatever it is you're working on. The vendor control board had to fit within the confines of the vending unit, accommodate the primary drive gear that effectively encapsulated the motor and one or two sensor mechanisms (optical beam switches). The attachments show the prototype PCB completely with hard wired programming cable so I could program it in situ in the vendor.

      Attachment 1272 Attachment 1271

      Pi's and Arduinos etc. definitely have a place in the engineers toolbox. I've got a number of projects I'm looking at that would be ideal for the Pi. I'm also looking at another project I designed and prototyped that has a few design flaws in it to see if I would be better off building a board that hosts a Pi as it's brain as opposed to a big fat PIC with lots of IO. Should have gone the Pi route in the beginning, but we live and learn... as a benefit of that particular project I now understand the thermal characteristics of my toaster oven that prevent it from being a good one to use for reflow soldering
Comodo SSL