summaryrefslogtreecommitdiffstats
path: root/actuators/blinkm.vala
blob: 8068666cd0c30b3071163b64329388911c964587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Copyright 2012, Sebastian Reichel <sre@ring0.de>
 *
 * 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);
	}
}