Skip to content

Commit

Permalink
Add more hammerspoon helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeebswihart committed Jan 29, 2020
1 parent ee9fbc9 commit 42f2c50
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
19 changes: 19 additions & 0 deletions hammerspoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,22 @@ hs.dockicon.hide()
local global = require 'global'
local keepit = require 'keepit'
local mail = require 'mail'

local sleepwake = require 'sleepwake'
local wifi = require 'wifi'
local usb = require 'usb'
local wifi = require 'wifi'

function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
homewatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")
Empty file added hammerspoon/reload.lua
Empty file.
27 changes: 27 additions & 0 deletions hammerspoon/sleepwake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "string"

local module = {}

function checkBluetoothResult(rc, stderr, stderr)
if rc ~= 0 then
print(string.format("Unexpected result executing `blueutil`: rc=%d stderr=%s stdout=%s", rc, stderr, stdout))
end
end

function bluetooth(power)
print("Setting bluetooth to " .. power)
local t = hs.task.new("/usr/local/bin/blueutil", checkBluetoothResult, {"--power", power})
t:start()
end

function f(event)
if event == hs.caffeinate.watcher.systemWillSleep then
bluetooth("off")
elseif event == hs.caffeinate.watcher.screensDidWake then
bluetooth("on")
end
end

module.watcher = hs.caffeinate.watcher.new(f)
module.watcher:start()
return module
16 changes: 16 additions & 0 deletions hammerspoon/usb.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local module = {}

local evtAdd = "added"
local evtRemove = "removed"

function usbDeviceCallback(data)
-- TODO set audio output to the FiiO DAC
-- TODO set audio input to the blue snowball
pn = data["productName"]
evt = data["eventType"]
end

module.watcher = hs.usb.watcher.new(usbDeviceCallback)
module.watcher:start()

return module
23 changes: 23 additions & 0 deletions hammerspoon/wifi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local module = {}
module.watcher = nil
local homeSSID = "chrowireless"
local lastSSID = hs.wifi.currentNetwork()

function ssidChangedCallback()
newSSID = hs.wifi.currentNetwork()

if newSSID == homeSSID and lastSSID ~= homeSSID then
-- We just joined our home WiFi network
hs.audiodevice.defaultOutputDevice():setVolume(25)
elseif newSSID ~= homeSSID and lastSSID == homeSSID then
-- We just departed our home WiFi network
hs.audiodevice.defaultOutputDevice():setVolume(0)
end

lastSSID = newSSID
end

module.watcher = hs.wifi.watcher.new(ssidChangedCallback)
module.watcher:start()

return module

0 comments on commit 42f2c50

Please sign in to comment.