-
Notifications
You must be signed in to change notification settings - Fork 5
/
2048.lua
143 lines (135 loc) · 3.5 KB
/
2048.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
scriptId = 'com.thalmic.2048'
--Jake Chapeskie
--Commands
--Move up down left and right to move
--"thumbToPinky" toggle unlock
--"fist" to reset zero/dead zone
PITCH_MOTION_THRESHOLD = 6 -- degrees
YAW_MOTION_THRESHOLD = 6-- degrees
--Helper Functions
function getMyoYawDegrees()
local yawValue = math.deg(myo.getYaw())
return yawValue
end
function getMyoPitchDegrees()
local PitchValue = math.deg(myo.getPitch())
return PitchValue
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 conditionalPitch(pitch)
if myo.getXDirection()== "towardElbow" then
pitch=-pitch;
end
return pitch
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
--Control Functions
function moveLeft()
myo.keyboard("left_arrow", "press")
end
function moveRight()
myo.keyboard("right_arrow", "press")
end
function moveUp()
myo.keyboard("up_arrow", "press")
end
function moveDown()
myo.keyboard("down_arrow", "press")
end
--Toggle Unlock Functions
function lock()
enabled = false
myo.vibrate("short")
end
function unlock()
yawReference = getMyoYawDegrees()
pitchReference = getMyoPitchDegrees()
enabled = true
myo.vibrate("short")
myo.vibrate("short")
end
function onPoseEdge(pose, edge)
pose=conditionallySwapWave(pose)
local now = myo.getTimeMilliseconds()
--Hold to move activation
if pose == "fist" and enabled then
--moveActive = edge == "on"
yawReference = getMyoYawDegrees()
pitchReference = getMyoPitchDegrees()
moveSince = now
end
--Other shortcut control
if edge == "on" then
if pose == "thumbToPinky" then
if enabled then
lock()
else
unlock()
end
end
end
end
-- onPeriodic runs every ~10ms
function onPeriodic()
local now = myo.getTimeMilliseconds()
if enabled then
local relativeYaw = degreeDiff(getMyoYawDegrees(), yawReference)
if math.abs(relativeYaw)> YAW_MOTION_THRESHOLD then
if moveYawLatch == false then
if relativeYaw>0 then
moveLeft()
moveYawLatch=true
else
moveRight()
moveYawLatch=true
end
end
else
moveYawLatch=false
end
local relativePitch = degreeDiff(getMyoPitchDegrees(), pitchReference)
relativePitch=conditionalPitch(relativePitch)
if math.abs(relativePitch)> PITCH_MOTION_THRESHOLD then
if movePitchLatch == false then
if relativePitch>0 then
moveDown()
movePitchLatch=true
else
moveUp()
movePitchLatch=true
end
end
else
movePitchLatch=false
end
end
end
-- Only activate when using 2048
function onForegroundWindowChange(app, title)
enabled = true
movePitchLatch=false
movePitchLatch=false
yawReference = getMyoYawDegrees()
pitchReference = getMyoPitchDegrees()
moveSince = now
if string.match(title, "2048") then
return true
end
end