Skip to content

Commit

Permalink
Implement Aura Sync API
Browse files Browse the repository at this point in the history
  • Loading branch information
SaifAqqad committed May 20, 2023
1 parent 4e23e4d commit 98b20d5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
135 changes: 135 additions & 0 deletions src/Lib/AuraSync.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#Requires AutoHotkey v1.1.35+

class AuraSync {
static CLSID := "aura.sdk.1"

__New(){
this.sdk := ComObjCreate(AuraSync.CLSID)

this.isControlled := false
this.devices := []

this.releaseMethod := ObjBindMethod(this, "releaseControl")
OnExit(this.releaseMethod)

; Cache devices
for device in this.sdk.Enumerate(0)
this.devices.Push(new AuraSync.Device(device, this))

if (this.devices.Length() > 0)
this.setAllDevicesColor(this.devices[1].lights[1].getColor(), 1)
}

takeControl(){
if(this.isControlled)
return
this.sdk.SwitchMode()
this.isControlled := true
}

releaseControl(){
if(!this.isControlled)
return
this.sdk.ReleaseControl(0)
this.isControlled := false
}

setAllDevicesColor(color, releaseDelay:= 0){
_r := this.releaseMethod
SetTimer, % _r, Off

for i, device in this.devices {
for i, light in device.lights
light.setColor(color)
device.apply()
}

if (releaseDelay > 0)
SetTimer, % _r, % -releaseDelay
}

isInstalled(){
local auraSdk

try {
auraSdk := ComObjCreate(AuraSync.CLSID)
ObjRelease(auraSdk)
} Catch {
return false
}

return true
}

rgbToBgr(r, g, b){
return (b << 16) | (g << 8) | r
}

hexToBgr(hex){
hex := StrReplace(hex, "#", "")

red := Format("{:d}","0x" SubStr(hex, 1, 2))
green := Format("{:d}","0x" SubStr(hex, 3, 2))
blue := Format("{:d}","0x" SubStr(hex, 5, 2))

return (blue << 16) | (green << 8) | red
}

bgrToRgb(color){
red := color & 0xFF
green := (color >> 8) & 0xFF
blue := (color >> 16) & 0xFF

return [red, green, blue]
}

bgrToHex(color){
red := color & 0xFF
green := (color >> 8) & 0xFF
blue := (color >> 16) & 0xFF

return Format("#{:02X}{:02X}{:02X}", red, green, blue)
}

class Device {
__New(raw, sdk){
this.sdk := sdk
this.raw := raw
this.name := raw.Name
this.type := raw.Type
this.width := raw.Width
this.height := raw.Height

; Cache device's lights
this.lights:= []
for light in raw.Lights
this.lights.Push(new AuraSync.Light(light, sdk))
}

apply(){
if (!this.sdk.isControlled)
this.sdk.takeControl()
this.raw.Apply()
}
}

class Light {
__New(raw, sdk){
this.sdk := sdk
this.raw := raw
this.Name := raw.Name
}

setColor(color){
if (!this.sdk.isControlled)
this.sdk.takeControl()
this.raw.Color := color
}

getColor(){
if(!this.sdk.isControlled)
this.sdk.takeControl()
return this.raw.Color
}
}
}
10 changes: 6 additions & 4 deletions src/MicMute.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SetWorkingDir %A_ScriptDir%
#Include, <StackSet>
#Include, <SoundPlayer>
#Include, <B64>
#Include, <AuraSync>

; ahkpm
#Include, %A_ScriptDir%\..\ahkpm-modules\github.com\
Expand Down Expand Up @@ -122,8 +123,6 @@ if(!arg_noUI)
UI_create(Func("reloadMicMute"))
; initilize micmute
initilizeMicMute(arg_profile)
; export the processed config object
config_obj.exportConfig()
OnExit(Func("exitMicMute"), -1)
; listen for sys theme changes
OnMessage(WM_SETTINGCHANGE, "updateSysTheme")
Expand All @@ -138,7 +137,7 @@ A_startupTime:= A_TickCount - A_startupTime
util_log("[Main] MicMute startup took " A_startupTime "ms")


initilizeMicMute(default_profile:=""){
initilizeMicMute(default_profile:="", exportConfig:=1){
util_log("[Main] Initilizing MicMute")
;make sure hotkeys are disabled before reinitilization
if(mic_controllers)
Expand Down Expand Up @@ -168,6 +167,9 @@ initilizeMicMute(default_profile:=""){
if(profile.LinkedApp)
watched_profiles.Push(profile)
}
; export the processed config object
if(exportConfig)
config_obj.exportConfig()
;enable linked apps timer
SetTimer, checkLinkedApps, % watched_profiles.Length()? 3000 : "Off"
;enable checkConfigDiff timer
Expand Down Expand Up @@ -386,7 +388,7 @@ checkConfigDiff(){
util_log("[Main] Detected changes to config file")
last_modif_time:= ""
setTimer, checkConfigDiff, Off
initilizeMicMute(current_profile.ProfileName)
initilizeMicMute(current_profile.ProfileName, false)
}
last_modif_time:= modif_time
}
Expand Down
2 changes: 2 additions & 0 deletions src/config/Config.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Config {
VoicemeeterIntegration:=0

__New(p_DefaultProfile:=""){
; Set Json options
JSON.EmptyObjectsAsArrays:= 1
if(!FileExist(A_ScriptDir "\config.json") || util_IsFileEmpty(A_ScriptDir "\config.json")){
if(FileExist(A_ScriptDir "\config.ini")){
isFirstLaunch:=0
Expand Down

0 comments on commit 98b20d5

Please sign in to comment.