Sunday, February 13, 2011

DPHP 5 - Code Occupies the Space of it's Container



Yep, as the video title suggests, this was my second collection of animations.  Why is this one special?  Well, at this point I had moved off of the bog standard Arduino Diecimila with it's ATega168 processor to an Arduino Mega with a ATmega1280.  Why?  Well, the 168 has 1kB of SRAM, or 'operating memory' similar to the RAM in your computer or laptop.  The difference is when you run out of operating memory on a microcontroller, you don't have a page file to save your bacon.  Instead, it all goes to pot and it's nearly impossible to tell why or how.  And why is 1kB not enough space?  Well, let's look at the display.  I'm sure people hate math but bear with me.

One pixel of the display needs 12bits of information to describe it, in other words 4 bits per color.  That means that the whole display, which is 24x16 pixels, is 4608 bits, or divide you divide by 8, 576 bytes of information.  So, just to store a 'full copy' of all the information needed to send ONE frame to the display, you would be using over half of the available memory.  Specifically, 576/1024 =  56.25% of your available memory.  Yikes.

This made things like 'hue based colors' impossible with only 1kB of memory.  In the hue based color system I use, a single number correlates to a part of the color spectrum I can reproduce on my system (4096 total colors) at a fixed brightness.  Therefore, I fix the 'saturation' and 'brightness' values used in standard HSB/HSL color schemes.  One of these color values, for one pixel, requires a byte to store.  So that's 384 bytes to store the full display.  But, in order to send those colors to the display, you would need to convert from the hue buffer to the 12bit RGB buffer, which is 576 bytes.  So your total memory footprint before you even write code (which chews up memory with loops, if statements, etc) is going to be 960 bytes.  So you have 64 bytes left for everything else.  and I mean everything else.  There's just no way that can be done.

So obviously, I moved to a larger and more powerful controller ASAP.

Ok, Ok, I lied, I wasn't using the Arduino Mega just yet when that video was made, but I was finding out very quickly that the scale and scope of my project would require, nay necessitate the use of a controller with more memory.  In fact, probably a lot more.  Hence the use of the Mega with it's 8kB of memory.  I haven't run into a (memory related) problem since.

No comments:

Post a Comment