forked from ArmanG/MyoU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyouMyo.lua
165 lines (133 loc) · 4.14 KB
/
youMyo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
scriptId = 'com.youtube'
function pauseOrPlay()
myo.keyboard("space", "press")
end
function framestepForwards()
myo.keyboard("right_arrow", "press")
end
function framestepBackwards()
myo.keyboard("left_arrow", "press")
end
function scrollUp()
myo.keyboard("up_arrow","press")
end
function scrollDown()
myo.keyboard("down_arrow","press")
end
function resetFist()
fistMade = false
referenceRoll = myo.getRoll()
currentRoll = referenceRoll
-- myo.keyboard("up_arrow","up")
-- myo.keyboard("down_arrow","up")
end
-- Makes use of myo.getArm() to swap wave out and wave in when the armband is being worn on
-- the left arm. This allows us to treat wave out as wave right and wave in as wave
-- left for consistent direction. The function has no effect on other poses.
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 unlock()
unlocked = true
extendUnlock()
end
function extendUnlock()
unlockedSince = myo.getTimeMilliseconds()
end
-- All timeouts in milliseconds
UNLOCKED_TIMEOUT = 4000 -- Time since last activity before we lock
function onPeriodic()
local now = myo.getTimeMilliseconds()
-- ...
-- Lock after inactivity
if unlocked then
-- If we've been unlocked longer than the timeout period, lock.
-- Activity will update unlockedSince, see extendUnlock() above.
if now - unlockedSince > UNLOCKED_TIMEOUT then
unlocked = false
end
end
currentRoll = myo.getRoll()
if myo.getXDirection() == "towardElbow" then
currentRoll = currentRoll * -1
extendUnlock()
end
if unlocked and fistMade then -- Moves page when fist is held and Myo is rotated
extendUnlock()
subtractive = currentRoll - referenceRoll
if subtractive > 0.2 then
scrollUp()
elseif subtractive < -0.2 then
scrollDown()
end
end
end
function onPoseEdge(pose, edge)
-- ...
-- Forward/backward and shuttle.
if pose == "thumbToPinky" then
if edge == "off" then
-- Unlock when pose is released in case the user holds it for a while.
unlock()
elseif edge == "on" and not unlocked then
-- Vibrate twice on unlock.
-- We do this when the pose is made for better feedback.
myo.vibrate("short")
myo.vibrate("short")
extendUnlock()
end
end
if pose == "waveIn" or pose == "waveOut" or pose == "fist" then
local now = myo.getTimeMilliseconds()
if unlocked and edge == "on" then
-- Deal with direction and arm.
pose = conditionallySwapWave(pose)
-- Determine direction based on the pose.
if pose == "waveIn" then
framestepBackwards()
elseif pose == "fist" then -- Sets up fist movement
pauseOrPlay()
if not fistMade then
referenceRoll = myo.getRoll()
fistMade = true
if myo.getXDirection() == "towardElbow" then -- Adjusts for Myo orientation
referenceRoll = referenceRoll * -1
end
end
elseif pose == "waveOut" then
framestepForwards()
end
if pose ~= "fist" then -- Reset call
resetFist()
end
-- Initial burst and vibrate
myo.vibrate("short")
extendUnlock()
end
end
end
function onForegroundWindowChange(app, title)
-- Here we decide if we want to control the new active app.
local wantActive = false
if app == "com.google.Chrome" then
wantActive = true
activeApp = "Chrome"
end
return wantActive
end
function activeAppName()
-- Return the active app name determined in onForegroundWindowChange
return activeApp
end
function onActiveChange(isActive)
if not isActive then
unlocked = false
end
end