Matteo Caprari home

How (not) to code and new functionalities

posted on 15 Aug 2008

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
blog comments powered by Disqus