This week, we will present something that will be published shortly: the high-level API for Objective-C. The most insightful readers may have already picked up some hints about it. For now, this is only a Mac OS X version, but IOS support will follow after, don't worry.
With the opening of our on-line shop and the first orders, we did not have time this week for an ambitious project. But with Christmas time coming, we have decided to simply lighten a Christmas tree using a few Yocto-Color modules. This is good enough as a simple example to test the Objective-C API.
We start by including the classes to be used.
#import "yocto_colorled.h"
As usual, we then register the hub on which the devices will be connected, In this example, the modules are directly connected to the MacBook Air, so we will register the USB hub. The code looks almost the same as the C++ version, the only difference is the type used for the character string.
// Setup the API to use local USB devices
if(yRegisterHub(@"usb",&errmsg) != YAPI_SUCCESS) {
NSLog(@"RegisterHub error: %@\n",errmsg);
return 1;
}
The body of the code is an endless loop that will enumerate all RGB leds using the function yFirstColorLed() and the method nextColorLed. The function yFirstColorLed() retrieves a pointer to the first YColorLed object, and we assign it to our variable led. As for other languages, the returned value is a pointer to an object belonging to the library, that should never be explicitely freed by the caller.
Then we pick a random color, using HSL format so that we can keep a constant luminosity and saturation. This will make our animation look nicer, by avoiding big changes in illumination. We call the method set_hslColor (or, to make it more Objective-C, we send the message set_hslColor) with the computed value.
[led set_hslColor:hsl];
The YColorLed object has a method nextColorLed that returns a pointer to the next YColorLed object, or NULL when the enumeration is completed. We can therefore simply call nextColorLed on each object to set the right color on all leds.
while(led){
unsigned hsl = (unsigned)(random() % 256)<<16 | 0xff80;
[led set_hslColor:hsl];
led = [led nextColorLed];
}
Here is the complete code:
#import "yocto_api.h"
#import "yocto_colorled.h"
int main (int argc, const char * argv[])
{
NSString *errmsg;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Setup the API to use local USB devices
if(yRegisterHub(@"usb",&errmsg) != YAPI_SUCCESS) {
NSLog(@"RegisterHub error: %@\n",errmsg);
[pool drain];
return 1;
}
srandom((unsigned)time(NULL));
while(1){
YColorLed *led = yFirstColorLed();
while(led){
unsigned hsl = (unsigned)(random() % 256)<<16 | 0xff80;
[led set_hslColor:hsl];
led = [led nextColorLed];
}
}
[pool drain];
return 0;
}
And here is a short video of the result:
The Objective-C library should be published before the end of the year. If you want to test it before, don't hesitate to send us an e-mail. Next programming languages that we will implement are most probably Java and the command shell.