summaryrefslogtreecommitdiffstats
path: root/test-shell.vala
blob: 4e78406b75f8a657e9069e122024c97058c89722 (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
155
156
157
158
/* 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.
 */

static int i;

public static string? generator(string cmd, int state) {
	string[] commands = { "firmware", "get", "set", "stop", "play", "help", "pressure", "temperature", "altitude", "gyro-temperature", "gyro-data" };

	if(state == 0)
		i = -1;

	while(i < commands.length-1) {
		i++;
		if(commands[i].has_prefix(cmd)) {
			return commands[i];
		}
	}

	return null;
}

public static string[]? completion(string cmd, int start, int end) {
	string[] matches = null;

	if(start == 0)
		matches = Readline.completion_matches(cmd, generator);

	return matches;
}

public static bool shell(out string cmd) {
	cmd = Readline.readline("[blinkm] $ ");
	Readline.History.add(cmd);

	if(cmd == "exit")
		cmd = null;

	if(cmd == null)
		stdout.printf("exit\n");

	return (cmd != null) ? true : false;
}

public static int main(string[] args) {
	Readline.attempted_completion_function = completion;
	Readline.bind_key('\t', Readline.complete);

	try {
		string cmd;
		BlinkM led = new BlinkM(3, 0x10);
#if BMP085
		BMP085 bmp085 = new BMP085(3);
#endif
		ITG3200 itg3200 = new ITG3200(3);

		while(shell(out cmd)) {
			var cmdparts = cmd.split(" ");
			switch(cmdparts[0]) {
				case null:
					break;
				case "firmware":
					char major, minor;
					led.get_firmware_version(out major, out minor);
					stdout.printf("firmware: (%c.%c)\n", major, minor);
					break;
				case "get":
					switch(cmdparts[1]) {
						case "address":
							stdout.printf("address: %02x\n", led.get_address());
							break;
						default:
							stdout.printf("unknown get parameter!\n");
							break;
					}
					break;
				case "stop":
					led.script_stop();
					break;
				case "play":
					led.script_play((BlinkM.Script) uint64.parse(cmdparts[1]));
					break;
				case "set":
					if(cmdparts[1] == "fade" && cmdparts[2] == "speed") {
						if(cmdparts.length == 4)
							led.set_fade_speed((uint8) uint64.parse(cmdparts[3]));
						else
							stdout.printf("\tset fade speed <speed>\n");
					} else if(cmdparts[1] == "time" && cmdparts[2] == "adjust") {
						if(cmdparts.length == 4)
							led.set_time_adjust((int8) int.parse(cmdparts[3]));
						else
							stdout.printf("\tset time adjust <time>\n");
					} else {
						if(cmdparts.length == 4)
							led.set((uint8) uint64.parse(cmdparts[1]), (uint8) uint64.parse(cmdparts[2]), (uint8) uint64.parse(cmdparts[3]));
						else
							stdout.printf("\tset <red> <green> <blue>\n");
					}
					break;
#if BMP085
				case "temperature":
					var temp = bmp085.get_temperature();
					stdout.printf("temperature: %d.%d °C\n", temp / 10, temp % 10);
					break;
				case "pressure":
					var pressure = bmp085.get_pressure();
					stdout.printf("pressure: %d.%d hPa\n", pressure / 100, pressure % 100);
					break;
				case "altitude":
					var altitude = bmp085.get_altitude();
					stdout.printf("altitude: %f m\n", altitude);
					break;
#endif
				case "gyro-temperature":
					var temp = itg3200.get_temperature();
					stdout.printf("temperature 2: %d.%d °C\n", temp / 10, temp % 10);
					break;
				case "gyro-data":
					int16 x, y, z;
					itg3200.get_data(out x, out y, out z);
					stdout.printf("data: %d %d %d\n", x, y, z);
					break;
				case "help":
					stdout.printf("commands: \n");
					stdout.printf("\tfirmware\n");
					stdout.printf("\taddress get\n");
					stdout.printf("\tset <red> <green> <blue>\n");
					stdout.printf("\tset fade speed <speed>\n");
					stdout.printf("\tset time adjust <time>\n");
					stdout.printf("\tstop\n");
					stdout.printf("\tplay <id>\n");
					stdout.printf("\texit\n");
					break;
				default:
					stdout.printf("unknown command!\n");
					break;
			}
		}
	} catch(I2CError e) {
		stderr.printf("%s\n", e.message);
	} catch(Error e) {
		stderr.printf("%s\n", e.message);
	}

	return 0;
}