scriptId = 'com.jakechapeskie.media' minMyoConnectVersion ='0.8.1' scriptDetailsUrl = 'https://market.myo.com/app/54eb721ee4b07a1270f42ca2' scriptTitle = 'Global Media Keys' ROLL_MOTION_THRESHOLD = 7 -- degrees SLOW_MOVE_PERIOD = 20 ROLL_INTERVAL = 5 rollReference=0 rolls = 0 enabled=false myo.setLockingPolicy("standard") function onForegroundWindowChange(app, title) return true end function activeAppName() return scriptTitle end function getMyoRollDegrees() local RollValue = math.deg(myo.getRoll()) return RollValue end function onUnlock() enabled=false end function degreeDiff(value, base) local diff = value - base if diff > 180 then diff = diff - 360 elseif diff < -180 then diff = diff + 360 end return diff end function conditionallySwapWave(pose) if myo.getArm() == "left" then if pose == "waveIn" then pose = "waveOut" elseif pose == "waveOut" then pose = "waveIn" end end return pose end function onPoseEdge(pose, edge) pose=conditionallySwapWave(pose) local now = myo.getTimeMilliseconds() if (pose ~= "rest" and pose ~= "unknown") then -- hold if edge is on, timed if edge is off myo.unlock(edge == "off" and "timed" or "hold") end --Hold to activate if edge == "on" then if pose == "fist" then moveActive = edge == "on" rollReference = getMyoRollDegrees() rolls = 0 enabled=true elseif pose =="waveIn" then myo.mediaKey("previous") elseif pose =="waveOut" then myo.mediaKey("next") elseif pose =="fingersSpread" then myo.mediaKey("play_pause") else enabled=false end end end function onPeriodic() if (enabled) then local relativeRoll = degreeDiff(getMyoRollDegrees(), rollReference) local newRolls = math.floor(relativeRoll / ROLL_INTERVAL) local volumeKey = newRolls < rolls and "volume_down" or "volume_up" local deltaRolls = math.abs(newRolls - rolls) if (deltaRolls > 0) then for i=1,deltaRolls do myo.mediaKey(volumeKey); end end rolls = newRolls end end