From e7e4ade4ca53bd806dd539c297bc82ec0d24a10a Mon Sep 17 00:00:00 2001 From: Florian Kromer Date: Sun, 6 Aug 2017 13:35:48 +0200 Subject: [PATCH] add D script to control a led easiest is to install the compiler (here: gdc) on the target which runs Ubuntu with `sudo apt-get install gdc` use `./build` to build the executable script invoke the script with: - `./led --command=on` - `./led --command=flash` - `./led --command=off` --- chp05/dLED/build | 3 +++ chp05/dLED/dLED.d | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100755 chp05/dLED/build create mode 100644 chp05/dLED/dLED.d diff --git a/chp05/dLED/build b/chp05/dLED/build new file mode 100755 index 0000000..cdea085 --- /dev/null +++ b/chp05/dLED/build @@ -0,0 +1,3 @@ +#!/bin/bash +echo "Building the D led example script led" +gdc dLED.d -o led diff --git a/chp05/dLED/dLED.d b/chp05/dLED/dLED.d new file mode 100644 index 0000000..71989f1 --- /dev/null +++ b/chp05/dLED/dLED.d @@ -0,0 +1,44 @@ +import std.file; +import std.getopt; +import std.stdio; + +int main (string[] args) { + int ARGUMENT_COUNT = 2; + string LED3_PATH = "/sys/class/leds/beaglebone:green:usr3"; + + enum Command : string + { + on = "on", + off = "off", + flash = "flash" + } + + enum ReturnCode : int + { + pass = 0, + fail = 1 + } + + if (args.length != ARGUMENT_COUNT) { + writeln("too many or too little arguments"); + return ReturnCode.fail; + } + + string cmd = "invalid"; // no default, value force validation below to fail + getopt(args, "command", &cmd); + + chdir(LED3_PATH); + + if (cmd == Command.on) { + std.file.write("brightness", "1"); + } else if (cmd == Command.off) { + std.file.write("brightness", "0"); + } else if (cmd == Command.flash) { + std.file.write("trigger", "timer"); + } else { + writeln("command not supported"); + return ReturnCode.fail; + } + + return ReturnCode.pass; +}