diff options
Diffstat (limited to 'test-shell.vala')
-rw-r--r-- | test-shell.vala | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/test-shell.vala b/test-shell.vala new file mode 100644 index 0000000..4e78406 --- /dev/null +++ b/test-shell.vala @@ -0,0 +1,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; +} |