Friday 3 October 2008

Falickaway: a flickr viewer

Available now: flickaway (demo)

I love flickr.

I also love my pictures, and sometimes flickr feels too... generic? impersonal?
Anyway. I decided to spend some time coding this flickr viewer so to have a website with my style and the same pictures I have on flickr.

I thought someone else might find it useful. If you like it, just go there, run the wizard, copy the generated html in an html file. Deploy to a web server. done.
Yes, the wizard is javascript-only as well.

I almost forgot: that html page is less than 2k.
All the javascript and the styles will be fetched from my website (or google, or yahoo), so you'll always get the newest and best version.

A brief on technology:
I'm using jQuery (from googleapis.com), google feed apis and flickr rest api (on the wizard, to discover your feeds). All files are gzipped then deployed on Amazon S3.

Matteo

Monday 8 September 2008

[C Testing] MinUnit - less is more

- Can a whole framework be 3 lines long? -

The C project I'm working on is quite small in LOC and files. It won't grow much as it's deployed on a chip with 4k program space and 256 bytes of ram.

No matter how small, test all your code. Always. Period.

Problem is, where to start?

Approaching unit testing may go down two different paths.

Write few functions with ifs and printfs, call them from a main(), execute and see. It's prone to code duplication and you'll soon start reinventing the wheel. You are paving your way to a maintainability hell. Anyway, It might work for small projects and is very easy to get started with. 

Otherwise you can embrace a framework such as CppUnit o Check, learn it and start writing tests. You get more power and less duplication. You'll benefit from standardization, automation and much more. It's the best approach, except for the steep learning curve. Add that writing tests is not exciting nor easy, and you might end up being so frustrated that you'll never get your test suite done. 

Introducing the solution: MinUnit, a unit testing framework in 3 lines of code (!!!!):

/* file: minunit.h */
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(test) do { char *message = test(); tests_run++; \
) extern int tests_run;
if (message) return message; } while (
0

(This kind of magic is why I really do miss macros in Java)

At least it is the solution for my small-scale project.

Just copy those few lines in a header file, include it and start writing tests. It is really that easy. And gone is your excuse for not testing. 

Really, this might be all you need even for a mid-sized project. Especially if you are introducing unit testing in an existing project, the jump start can really pay off.

One important feature of (some) bigger frameworks is that the test runner executes your test code in a separate address space. This way if your buggy code picks the wrong pointer, it won't bring down the test runner, but only that unit. The runner will report it as a failure and move on. Do that with MinUnit and probably you'll have no clue of what hit you.

Make no excuses, test-your-code.

For the records, I modified it a little bit, adding a specialized assert:
#define mu_assert_eq(expected, actual) {\
int a = (expected); \
int b = (actual); \
do { if ((a) != (b)) { \
sprintf(minunit_msg, "FAILURE %s:%d expected %d, got %u", __FILE__, __LINE__, a, b);\
return minunit_msg; }; \
} while(0); }

There is a complete example on the project page or have a look at a real world example: test_ring_buffer.h and test.c

Matteo

Sunday 7 September 2008

CYZ_RGB alpha 2 - feature complete

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

The latest release implements all the features of the original firmware.

The program has been slightly tested at a functional level (sitting there and watching the lights go on end off), but has no unit tests.


I usually write loads of tests, possibly before the actual code; this time I was wandering in a completely new area and I knew most of the code was going to be highly disposable. Now that all hurdles have been surpassed, the refactoring stage will go hand-to-hand with writing a thorough test suite. Wish me good luck.

Go read the details.

Goals for next release:
  • Write a comprehensive test suite
  • Decouple cyz_cmd from cyz_rgb
  • Decouple usiTwiSlave from cyz_cmd
  • Reduce binary footprint (now 4072b of 4096 available)
  • Add ability to write 2 scripts
-Matteo

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