Skip to content

Instantly share code, notes, and snippets.

@jrotello
Created July 20, 2013 02:26
Show Gist options
  • Save jrotello/6043594 to your computer and use it in GitHub Desktop.
Save jrotello/6043594 to your computer and use it in GitHub Desktop.
A PowerShell script that lights a blink(1) LED based upon the remaining battery level of the computer. The remaining battery level is polled and updated every 30 seconds. Requires a ThingM blink(1). Assumes that the blink1-tool.exe is the the system path.
$global:batteryMonitor = $null
function Enable-BatteryMonitor() {
if ((Get-WmiObject win32_battery) -eq $null) {
Write-Host "No battery found."
return
}
if ($global:batteryMonitor -eq $null) {
$global:batteryMonitor = New-Object Timers.Timer
$global:batteryMonitor.Interval = 30000
$global:batteryMonitor.Enabled = $true
Register-ObjectEvent -InputObject $global:batteryMonitor -SourceIdentifier BatteryMonitor -EventName Elapsed -Action {
$battery = Get-WmiObject win32_battery
if ($battery.EstimatedChargeRemaining -le 20) {
blink1-tool.exe --red -q
} elseif ($battery.EstimatedChargeRemaining -gt 20 -and $battery.EstimatedChargeRemaining -le 70) {
blink1-tool.exe --rgb "255,204,0" -q #yellow
} else {
blink1-tool.exe --green -q
}
} | Out-Null
$global:batteryMonitor.Start()
} else {
Write-Host "BatteryMonitor has already been enabled."
}
}
function Disable-BatteryMonitor() {
if ($global:batteryMonitor -ne $null) {
blink1-tool.exe --off -q
Unregister-Event -SourceIdentifier BatteryMonitor
$global:batteryMonitor.Stop()
$global:batteryMonitor.Dispose()
$global:batteryMonitor = $null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment