/* Copyright 2012, Sebastian Reichel * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ public class BlinkM : I2CDevice { public struct Command { uint8 id; uint8 line; uint8 duration; uint8 cmd; uint8 parameter1; uint8 parameter2; uint8 parameter3; } public enum Script { STARTUP, RGB, WHITE_FLASH, RED_FLASH, GREEN_FLASH, BLUE_FLASH, CYAN_FLASH, MAGENTA_FLASH, YELLOW_FLASH, BLACK, HUE_CYCLE, MOOD_LIGHT, VIRTUAL_CANDLE, WATER_REFLECTIONS, OLD_NEON, THE_SEASONS, THUNDERSTORM, STOP_LIGHT, MORSE_CODE } public BlinkM(uint8 dev, uint8 addr = 0x09) throws I2CError { setup(dev, addr); } public void get_firmware_version(out char major, out char minor) throws I2CError { write_byte('Z'); major = (char) read_byte(); minor = (char) read_byte(); } public uint8 get_address() throws I2CError { write_byte('a'); return read_byte(); } public void set_address(uint8 addr) throws I2CError { write_byte('A'); write_byte(addr); write_byte(0xd0); write_byte(0x0d); write_byte(addr); setup_address(addr); } public void script_stop() throws I2CError { write_byte('o'); } public void script_play(Script id, uint8 repeat = 0, uint8 start = 0) throws I2CError { write_byte('p'); write_byte(id); write_byte(repeat); write_byte(start); } public void set_color(uint8 red, uint8 green, uint8 blue) throws I2CError { write_byte('n'); write_byte(red); write_byte(green); write_byte(blue); } public void get_color(out uint8 red, out uint8 green, out uint8 blue) throws I2CError { write_byte('g'); red = read_byte(); green = read_byte(); blue = read_byte(); } public void fade(uint8 red, uint8 green, uint8 blue) throws I2CError { write_byte('c'); write_byte(red); write_byte(green); write_byte(blue); } public void set_fade_speed(uint8 speed) throws I2CError { write_byte('f'); write_byte(speed); } public void set_time_adjust(int8 adjust) throws I2CError { write_byte('t'); write_byte((uint8) adjust); } public void script_set_line(Command cmd) throws I2CError { write_byte('W'); write_byte(cmd.id); write_byte(cmd.line); write_byte(cmd.duration); write_byte(cmd.cmd); write_byte(cmd.parameter1); write_byte(cmd.parameter2); write_byte(cmd.parameter3); } public void script_get_line(out Command cmd) throws I2CError { write_byte('R'); cmd = Command(); cmd.id = read_byte(); cmd.line = read_byte(); cmd.duration = read_byte(); cmd.cmd = read_byte(); cmd.parameter1 = read_byte(); cmd.parameter2 = read_byte(); cmd.parameter3 = read_byte(); } public void script_setup(Script id, uint8 length, uint8 repeats) throws I2CError requires (id == 0) requires (length <= 50) { write_byte('L'); write_byte(id); write_byte(length); write_byte(repeats); } public void setup_startup(uint8 mode, Script id, uint8 repeat, uint8 fade_speed, int8 time_adjust) throws I2CError { write_byte('B'); write_byte(mode); write_byte(id); write_byte(repeat); write_byte(fade_speed); write_byte(time_adjust); } }