This example shows how to restore the BlinkM RGB LED to its factory settings by communicating with it over the I2C bus.
To begin, create an I2C object. For this example, we are using the BlinkM RGB LED which has board index as 0 and address (hex) as 9h. To connect the computer to the I2C bus, a USB-I2C adaptor from Aardvark is used. For I2C object creation, this translates to:
BoardIndex
= 0
RemoteAddress
= 9h
Vendor
= Aardvark.
blinkM = i2c('aardvark',0,hex2dec('09'))
I2C Object : I2C-0-9h Communication Settings BoardIndex 0 BoardSerial 2237481561 BitRate: 100 RemoteAddress: 9h Vendor: aardvark Communication State Status: closed RecordStatus: off Read/Write State TransferStatus: idle
Before you can perform a read or write operation, you must connect the
I2C object to the device with the fopen
function. If the object was
successfully connected, its Status property is automatically configured
to open.
fopen(blinkM);
get(blinkM, 'Status')
ans = open
The default factory setting for the BlinkM is to play the colors white-red-green-blue-off in a sequence. The white color stays for 100 clock ticks while the colors red, green, blue and the off state stay for 50 clock ticks. This example uses a script to play this sequence on the BlinkM RGB LED.
The BlinkM data sheet specifies that the script length can be set by
writing 'L' to the device followed by 3 arguments. We will be using the
fwrite
function to write binary data onto the I2C bus.
First argument - As we want the default factory settings, use the script with ID 0.
Second argument - Length of the script, this script has 6 lines of code.
Third argument - Number of the repeats of the script, to play the script forever, we set the number of repeats to be 0.
fwrite(blinkM,['L' 0 6 0]);
Note: The eeprom write time is approximately 20 ms for the BlinkM. Hence you might have to pause to ensure that the command is written successfully.
According to the data sheet, a script line can be written to the device by writing 'W' followed by 7 arguments.
First argument - Script ID, 0 in this case.
Second argument - Line number you want to write.
Third argument - Duration in ticks that the command will last.
Arguments 4:7 - Actual BlinkM command and its arguments.
To start with, first set the fade speed for the device by writing 'f' followed by the speed to the device.
fwrite(blinkM,['W' 0 0 1 'f' 10 0 0]);
In the next 5 commands, specify the color sequence to be white, red, green, blue and off. A color can be set on the LED by writing 'n' to it followed by the R, G and B values. E.g., set the first color in the sequence to be white by setting the R, G, B values to 255.
fwrite(blinkM,['W' 0 1 100 'n' 255 255 255]); fwrite(blinkM,['W' 0 2 50 'n' 255 0 0]); fwrite(blinkM,['W' 0 3 50 'n' 0 255 0]); fwrite(blinkM,['W' 0 4 50 'n' 0 0 255]); fwrite(blinkM,['W' 0 5 50 'n' 0 0 0]);
The BlinkM data sheet specifies that the command to start up (or 'boot') the device is 'B' followed by 5 arguments.
First argument - Set to 1 to play a script.
Second argument - Script ID, 0 in this case.
Third argument - Number of the repeats of the script, to play the script forever, we set the number of repeats to be 0.
Fourth argument - Fade speed.
Fifth argument - Time adjust.
fwrite(blinkM,['B' 1 0 0 8 0]);
The previously written script can be played by writing 'p' followed by 3 arguments.
First argument - Script ID, 0 in this case.
Second argument - Number of the repeats of the script, to play the script forever, we set the number of repeats to be 0.
Third argument - Script line number to start playing from.
fwrite(blinkM,['p' 0 0 0]);
Disconnect it from the device, remove it from memory, and remove it from the workspace.
fclose(blinkM);
delete(blinkM);
clear ('blinkM');
This concludes the example for restoring the BlinkM to its default state by writing a script to it. This script can be updated to play customised sequences on the BlinkM RGB LED.