summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2010-07-17 12:10:54 +0200
committerJohannes Weiner <hannes@cmpxchg.org>2010-07-17 12:10:54 +0200
commitcefec02ae2f32b3d94c92b2bfe1f05de76bab150 (patch)
treea11f966ab6ff8965f4759a122573ef6b4957963f
parent058152eab7dcb54c11c042189bf200d479411908 (diff)
examples: add battery status script
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
-rw-r--r--examples/battery.sheep21
1 files changed, 21 insertions, 0 deletions
diff --git a/examples/battery.sheep b/examples/battery.sheep
new file mode 100644
index 0000000..ce9b5bc
--- /dev/null
+++ b/examples/battery.sheep
@@ -0,0 +1,21 @@
+#!/usr/bin/sheep
+#
+# battery - read first battery state on Linux
+
+(load io)
+
+(function percentage (part total)
+ (/ (* part 100) total))
+
+(function sysfs-number (path)
+ (number (io:readline (io:open path false))))
+
+(function battery-charge (name attr)
+ (sysfs-number
+ (concat "/sys/class/power_supply/" name "/charge_" attr)))
+
+(function battery-state (name)
+ (percentage (battery-charge name "now")
+ (battery-charge name "full")))
+
+(print "Battery 0: " (battery-state "BAT0") "%")