Skip to content

Commit 7244e10

Browse files
authored
Fix double events (#245) (#251)
Fix double events (#245)
2 parents 519a35c + 86860af commit 7244e10

File tree

3 files changed

+130
-129
lines changed

3 files changed

+130
-129
lines changed

src/SCRIPTS/BF/MSP/messages.lua

+85-85
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
1-
MSP_PID_FORMAT = {
2-
read = 112, -- MSP_PID
3-
write = 202, -- MSP_SET_PID
4-
minBytes = 8,
5-
fields = {
6-
-- P
7-
{ vals = { 1 } },
8-
{ vals = { 4 } },
9-
{ vals = { 7 } },
10-
-- I
11-
{ vals = { 2 } },
12-
{ vals = { 5 } },
13-
{ vals = { 8 } },
14-
-- D
15-
{ vals = { 3 } },
16-
{ vals = { 6 } },
17-
},
18-
}
19-
20-
MSP_PID_ADVANCED_FORMAT = {
21-
read = 94, -- MSP_PID_ADVANCED
22-
write = 95, -- MSP_SET_PID_ADVANCED
23-
minBytes = 23,
24-
fields = {
25-
-- weight
26-
{ vals = { 10 }, scale = 100 },
27-
-- transition
28-
{ vals = { 9 }, scale = 100 },
29-
},
30-
}
31-
32-
local INTRO_DELAY = 1600
33-
local READOUT_DELAY = 500
34-
35-
function extractMspValues(cmd, rx_buf, msgFormat, msgValues)
36-
if cmd == nil or rx_buf == nil then
37-
return
38-
end
39-
if cmd ~= msgFormat.read then
40-
return
41-
end
42-
if #(rx_buf) > 0 then
43-
msgValues.raw = {}
44-
for i=1,#(rx_buf) do
45-
msgValues.raw[i] = rx_buf[i]
46-
end
47-
48-
msgValues.values = {}
49-
for i=1,#(msgFormat.fields) do
50-
if (#(msgValues.raw) or 0) >= msgFormat.minBytes then
51-
local f = msgFormat.fields[i]
52-
if f.vals then
53-
local value = 0;
54-
for idx=1, #(f.vals) do
55-
local raw_val = msgValues.raw[f.vals[idx]]
56-
raw_val = bit32.lshift(raw_val, (idx-1)*8)
57-
value = bit32.bor(value, raw_val)
58-
end
59-
msgValues.values[i] = value/(f.scale or 1)
60-
end
61-
end
62-
end
63-
end
64-
end
65-
66-
function readoutMsp(msgFormat, msg)
67-
local t = getTime()
68-
if msg.lastTrigger == nil or msg.lastTrigger + INTRO_DELAY <= t then
69-
playFile(msg.intro)
70-
msg.lastTrigger = t
71-
elseif msg.reqTS == nil or msg.reqTS + READOUT_DELAY <= t then
72-
protocol.mspRead(msgFormat.read)
73-
msg.reqTS = t
74-
else
75-
local cmd, rx_buf = mspPollReply()
76-
extractMspValues(cmd, rx_buf, msgFormat, msg)
77-
if msg.raw then
78-
for i=1,#(msg.readoutValues) do
79-
playNumber(msg.values[msg.readoutValues[i]], 0)
80-
end
81-
msg.raw = nil
82-
end
83-
end
84-
mspProcessTxQ()
85-
end
1+
MSP_PID_FORMAT = {
2+
read = 112, -- MSP_PID
3+
write = 202, -- MSP_SET_PID
4+
minBytes = 8,
5+
fields = {
6+
-- P
7+
{ vals = { 1 } },
8+
{ vals = { 4 } },
9+
{ vals = { 7 } },
10+
-- I
11+
{ vals = { 2 } },
12+
{ vals = { 5 } },
13+
{ vals = { 8 } },
14+
-- D
15+
{ vals = { 3 } },
16+
{ vals = { 6 } },
17+
},
18+
}
19+
20+
MSP_PID_ADVANCED_FORMAT = {
21+
read = 94, -- MSP_PID_ADVANCED
22+
write = 95, -- MSP_SET_PID_ADVANCED
23+
minBytes = 23,
24+
fields = {
25+
-- weight
26+
{ vals = { 10 }, scale = 100 },
27+
-- transition
28+
{ vals = { 9 }, scale = 100 },
29+
},
30+
}
31+
32+
local INTRO_DELAY = 1600
33+
local READOUT_DELAY = 500
34+
35+
function extractMspValues(cmd, rx_buf, msgFormat, msgValues)
36+
if cmd == nil or rx_buf == nil then
37+
return
38+
end
39+
if cmd ~= msgFormat.read then
40+
return
41+
end
42+
if #(rx_buf) > 0 then
43+
msgValues.raw = {}
44+
for i=1,#(rx_buf) do
45+
msgValues.raw[i] = rx_buf[i]
46+
end
47+
48+
msgValues.values = {}
49+
for i=1,#(msgFormat.fields) do
50+
if (#(msgValues.raw) or 0) >= msgFormat.minBytes then
51+
local f = msgFormat.fields[i]
52+
if f.vals then
53+
local value = 0;
54+
for idx=1, #(f.vals) do
55+
local raw_val = msgValues.raw[f.vals[idx]]
56+
raw_val = bit32.lshift(raw_val, (idx-1)*8)
57+
value = bit32.bor(value, raw_val)
58+
end
59+
msgValues.values[i] = value/(f.scale or 1)
60+
end
61+
end
62+
end
63+
end
64+
end
65+
66+
function readoutMsp(msgFormat, msg)
67+
local t = getTime()
68+
if msg.lastTrigger == nil or msg.lastTrigger + INTRO_DELAY <= t then
69+
playFile(msg.intro)
70+
msg.lastTrigger = t
71+
elseif msg.reqTS == nil or msg.reqTS + READOUT_DELAY <= t then
72+
protocol.mspRead(msgFormat.read)
73+
msg.reqTS = t
74+
else
75+
local cmd, rx_buf = mspPollReply()
76+
extractMspValues(cmd, rx_buf, msgFormat, msg)
77+
if msg.raw then
78+
for i=1,#(msg.readoutValues) do
79+
playNumber(msg.values[msg.readoutValues[i]], 0)
80+
end
81+
msg.raw = nil
82+
end
83+
end
84+
mspProcessTxQ()
85+
end

src/SCRIPTS/BF/ui.lua

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ function run_ui(event)
352352
incValue(1)
353353
elseif event == userEvent.press.minus or event == userEvent.repeatPress.minus or event == userEvent.dial.left then
354354
incValue(-1)
355+
killEvents(event)
355356
end
356357
end
357358
local nextPage = currentPage

src/SCRIPTS/FUNCTIONS/pids.lua

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
SCRIPT_HOME = "/SCRIPTS/BF"
2-
3-
assert(loadScript(SCRIPT_HOME.."/MSP/messages.lua"))()
4-
5-
local msg_p = {
6-
intro = "p.wav",
7-
readoutValues = {1, 2, 3},
8-
}
9-
10-
local msg_i = {
11-
intro = "i.wav",
12-
readoutValues = {4, 5, 6},
13-
}
14-
15-
local msg_d = {
16-
intro = "d.wav",
17-
readoutValues = {7, 8},
18-
}
19-
20-
local msg_dsetpt = {
21-
intro = "dsetpt.wav",
22-
readoutValues = {1, 2},
23-
}
24-
25-
local pidSelectorField = nil
26-
27-
local function init_pids()
28-
pidSelectorField = getFieldInfo("trim-thr")
29-
end
30-
31-
local function run_pids()
32-
local pidSelector = getValue(pidSelectorField.id)
33-
if pidSelector > 33 and pidSelector < 99 then
34-
readoutMsp(MSP_PID_FORMAT, msg_p)
35-
elseif pidSelector > 99 and pidSelector < 165 then
36-
readoutMsp(MSP_PID_FORMAT, msg_i)
37-
elseif pidSelector > 165 and pidSelector < 231 then
38-
readoutMsp(MSP_PID_FORMAT, msg_d)
39-
elseif pidSelector > -99 and pidSelector < -33 then
40-
readoutMsp(MSP_PID_ADVANCED_FORMAT, msg_dsetpt)
41-
end
42-
end
43-
44-
return { init = init_pids, run = run_pids }
1+
SCRIPT_HOME = "/SCRIPTS/BF"
2+
3+
assert(loadScript(SCRIPT_HOME.."/MSP/messages.lua"))()
4+
5+
local msg_p = {
6+
intro = "p.wav",
7+
readoutValues = {1, 2, 3},
8+
}
9+
10+
local msg_i = {
11+
intro = "i.wav",
12+
readoutValues = {4, 5, 6},
13+
}
14+
15+
local msg_d = {
16+
intro = "d.wav",
17+
readoutValues = {7, 8},
18+
}
19+
20+
local msg_dsetpt = {
21+
intro = "dsetpt.wav",
22+
readoutValues = {1, 2},
23+
}
24+
25+
local pidSelectorField = nil
26+
27+
local function init_pids()
28+
pidSelectorField = getFieldInfo("trim-thr")
29+
end
30+
31+
local function run_pids()
32+
local pidSelector = getValue(pidSelectorField.id)
33+
if pidSelector > 33 and pidSelector < 99 then
34+
readoutMsp(MSP_PID_FORMAT, msg_p)
35+
elseif pidSelector > 99 and pidSelector < 165 then
36+
readoutMsp(MSP_PID_FORMAT, msg_i)
37+
elseif pidSelector > 165 and pidSelector < 231 then
38+
readoutMsp(MSP_PID_FORMAT, msg_d)
39+
elseif pidSelector > -99 and pidSelector < -33 then
40+
readoutMsp(MSP_PID_ADVANCED_FORMAT, msg_dsetpt)
41+
end
42+
end
43+
44+
return { init = init_pids, run = run_pids }

0 commit comments

Comments
 (0)