Skip to content

Commit

Permalink
Merge pull request #11 from fkromer/d-led
Browse files Browse the repository at this point in the history
add D script to control a led
  • Loading branch information
derekmolloy committed Aug 7, 2017
2 parents 4ff7eca + e7e4ade commit a151288
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chp05/dLED/build
@@ -0,0 +1,3 @@
#!/bin/bash
echo "Building the D led example script led"
gdc dLED.d -o led
44 changes: 44 additions & 0 deletions 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;
}

0 comments on commit a151288

Please sign in to comment.