Friday, 15 August 2008

How (not) to code and new functionalities


How (not) to write code for a small chip


The project has two main set of procedures for color control and commands execution. Loving encapsulation and separation of concerns, I built two structs with function pointers, to mimic objects:

typedef struct CYZ_CMD {
 unsigned char rcv_cmd_buf[8];
unsigned char rcv_cmd_buf_cnt;
unsigned char rcv_cmd_len;
Cyz_rgb* cyz_rgb;
void (*execute)(void* this, unsigned char* buf);
void (*receive_one_byte)(void* this, unsigned char rcv);
} Cyz_cmd;
then allocating an "instance" with malloc, doing some initialization work and finally executing "methods" with
cyz_cmd->execute(cyz_cmd, play);

Smart you say? Not at all. I only ended up eating all available memory (a flattering 256 Bytes). I reverted to global variables and functions, dropped all the dynamic memory thing and learnt how not write code for an embedded system. True, neat and clean code is more maintainable, but I was spending precious memory to buy only an apparent advantage.

Adding new commands and functionalities will further reduce available memory: I'll probably learn some neat tricks. Can't wait...

Added some commands (only in svn head)

'W' Write script line

Script 0 can be modified and up to 10 lines added and executed. With some limitations:
  1. line duration is ignored
  2. script lines only support fade to rgb and go to rgb
  3. script is not persisted and will disappear at next power off
  4. script number is ignored, can only write script 0 (as in original blinkm)

'p' Play script

After writing a script with 'W', it can be xecuted with 'p'. Only applies to script 0


'o' Stop script

No limitations here :)

See the project page for details.




Matteo

Monday, 11 August 2008

CYZ_RGB alpha - blinkm alternative firmware

The first release of CYZ_RGB, an alternative firmware for BlinkM, is now ready for download.

Go read the details.

BlinkM is a Smart LED produced by ThingM:
BlinkM is a “Smart LED”, a networkable and
programmable full-color RGB LED for hobbyists,
industrial designers, prototypers, and experimenters.
Just connect it to an I2C bus and start sending commands like "show rgb color" or "fade to hsv value" and so on and so forth (it does much more; see the human-readable data sheet).

A bit of explanation: I liked the idea to do a few more things with the leds and decided to modify the firmware. The original firmware is actually closed source (due to legal issues) so I had to start writing a new one from scratch. This first release is very basic but hopefully adding new features won't be that hard.

The new firmware has two interesting additional features in the works: the ability to act as a master on the I2C bus and support for dallas 1-wire protocol, to leave one pin free for your creativity.

Download

If you decide to download and give it a try, you'll find slave.hex and master.hex: the first is the actual blinkm firmware replacement, the other is the experimental master. You can flash the files on two different blinkms, connect them the to the same i2c bus and watch they change color together.

Browse the source code.

Download the binary release.

Get the sources with svn: svn checkout http://codalyze.googlecode.com/svn/cyz_rgb/trunk cyz_rgb

Interested? I need help.

I'd like to hear from you. If you have any patch to submit it will be welcome.

Please not that my experience in C is very limited. And it approaches zero when it comes to microcontroller programming. So, if you are any good at it, please be patient and expect the worse... i'm a java programmer ;). When you stop feeling sick after reading the code, well, drop me a line

Matteo