Skip to content

Instantly share code, notes, and snippets.

@azenla
Created November 3, 2015 21:41
Show Gist options
  • Save azenla/d1cf8bfa26ec8a6034ee to your computer and use it in GitHub Desktop.
Save azenla/d1cf8bfa26ec8a6034ee to your computer and use it in GitHub Desktop.
import "package:gpio/gpio.dart";
import "package:raspberry_pi/raspberry_pi.dart";
import "package:os/os.dart";
main() {
PiMemoryMappedGPIO gpio = new PiMemoryMappedGPIO();
var lines = [];
for (var i = 0; i < 40; i += 2) {
var mode1 = gpio.getMode(i);
if (mode1 == Mode.input) {
mode1 = "IN";
} else {
mode1 = "OUT";
}
var state1 = gpio.getPin(i);
var mode2 = gpio.getMode(i + 1);
if (mode2 == Mode.input) {
mode2 = "IN";
} else {
mode2 = "OUT";
}
state1 = state1 ? 1 : 0;
var state2 = gpio.getPin(i + 1);
state2 = state2 ? 1 : 0;
var out = alignString("Pin ${i}: ${state1} (${mode1})", 20);
out += "| " + alignString("Pin ${i + 1}: ${state2} (${mode2})", 20);
lines.add("| " + out + " |");
}
var clone = new List.from(lines);
clone.sort();
var len = clone.first.length;
lines.insert(0, "-" * len);
lines.add("-" * len);
print(lines.join("\n"));
}
String alignString(String input, int len) {
if (input.length > len) {
throw new Exception("Input is longer than the allowed length.");
}
var pads = ((len - input.length) / 2).toInt();
var result = (" " * pads) + input + (" " * pads);
if (result.length < len) {
result += " " * (len - result.length);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment