summaryrefslogtreecommitdiff
path: root/scripts/controls.lua
blob: 3fa3de3e2f32c5e179e9200e66c40d63af1cfb96 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
-- SLIMax Mgr Lua Script v3.1.4
-- Copyright (c)2012-2013 by Zappadoc - All Rights Reserved.
-- last change by Zappadoc - 2013-10

-- param deviceType = (see GetDeviceType() to get device name )
-- param ctrlType = type of ctrl, switch (0) or button (1)
-- param ctrlPos = ctrl index, switch from 1 to 6 and button from 1 to n
-- param value = ctrl value, button down (>0) or up (==0) and switch from 1 to 12
-- param funcIndex = mapped to internal function or -1
-- param targetDevice  = the device targeted, should be equal to deviceType above or different
--                       if mapped to other device (see GetDeviceType() to get device name )
-- return 0 to give the control to SLIMax Mgr
-- return 1 to force the update of device
function controlsEvent(deviceType, ctrlType, ctrlPos, value, funcIndex, targetDevice)
	-- call custom script
	local result = custom_controlsEvent(deviceType, ctrlType, ctrlPos, value, funcIndex, targetDevice)
	-- if result = 0 bypass the script below and return 0
	-- if result = 1 bypass the script below and return 1
	if result <= 1 then return result end
	-- if result >= 2 continue

	-- call global custom script
	result = global_custom_controlsEvent(deviceType, ctrlType, ctrlPos, value, funcIndex, targetDevice)
	-- if result = 0 bypass the script below and return 0
	-- if result = 1 bypass the script below and return 1
	if result <= 1 then return result end
	-- if result >= 2 continue

	local loopFlag = false
	local dev = ""
	dev = GetDeviceType(deviceType)
	if dev == nil then dev = "none" end

	local targetdev = ""
	targetdev = GetDeviceType(targetDevice)
	if targetdev == nil then targetdev = "none" end

	-- switches and buttons
	-- ctrl change event
	local state = 0
	local ftecStr = ""
	local leftStr = ""
	local rightStr = ""

	local oldTcks = GetTicks()

	local delay = 600
	if funcIndex ~= -1 then
	if IsSLIFunction("maxgear", funcIndex) and mMaxGearFeedbackAllowed then
	-- max gear switch
	-- value from 4 to 7
	local mxgear = 0
	mxgear = GetCarInfo("maxgear")

	leftStr = "GEAR  "
	rightStr = rightStr.format( "MAX%1d  ", mxgear)

	-- set digits
	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif IsSLIFunction("brightness", funcIndex) and mBrightnessFeedbackAllowed then
	-- brightness feedback (switch or buttons)
	BrightnessFeedBack(targetdev)

	leftStr = "888888"
	rightStr = "888888"

	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif IsSLIFunction("ospfactor", funcIndex) and mOSPFeedbackAllowed then
	-- OSP Factor feedback
	leftStr = "OSP   "

	local ospf = GetContextInfo("ospfactor")
	rightStr = rightStr.format(" %03d  ", ospf)

	-- set digits
	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif IsSLIFunction("speedmetric", funcIndex) and mUnitFeedbackAllowed then
	-- metric KPH or MPH
	local spdm = false
	spdm = GetContextInfo("speedmetric")

	leftStr =  "SPD   "
	rightStr = " KPH  "
	if spdm then rightStr  = " MPH  " end

	-- set digits
	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif dev == "FANATEC" and IsSLIFunction("ospmethod", funcIndex) and mFanatecOSPFeedbackAllowed then
	-- fanatec speedlimitermethod feedback
	local info = GetContextInfo("ospmethod")
	ftecStr = ftecStr.format("P%2d", info+1)

	-- set digits
	SetFanatecDigits(ftecStr)
	FanatecSendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay
	elseif dev == "FANATEC" and IsSLIFunction("shiftlightsmethod", funcIndex) and mFanatecShiftlightsFeedbackAllowed then
	-- fanatec speedlimitermethod feedback
	local info = GetContextInfo("shiftlightsmethod")
	ftecStr = ftecStr.format("P%2d", info+1)

	-- set digits
	SetFanatecDigits(ftecStr)
	FanatecSendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay
	elseif dev == "FANATEC" and IsSLIFunction("speedlimitermethod", funcIndex) and mFanatecSpeedLimiterFeedbackAllowed then
	-- fanatec speedlimitermethod feedback
	local info = GetContextInfo("speedlimitermethod")
	ftecStr = ftecStr.format("P%2d", info+1)

	-- set digits
	SetFanatecDigits(ftecStr)
	FanatecSendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif IsSLIFunction("fanatecdigits", funcIndex) and mFanatecDigitsFeedbackAllowed then
	-- fanatecdigits feedback
	local info = GetContextInfo("fanatecdigits")
	ftecStr = ftecStr.format("P%2d", info+1)

	-- set digits
	SetFanatecDigits(ftecStr)
	FanatecSendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif IsSLIFunction("rightdigits", funcIndex) and mRightDigitsFeedbackAllowed then
	-- rightdigits feedback
	local info = GetContextInfo("rightdigits")

	if ctrlType == 1 then
	leftStr = leftStr.format("B%02d   ", ctrlPos)
	else
	leftStr = leftStr.format("S%1d    ", ctrlPos)
	end
	rightStr = rightStr.format("P%02d   ", info+1)

	-- set digits
	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif IsSLIFunction("leftdigits", funcIndex) and mLeftDigitsFeedbackAllowed then
	-- leftdigits feedback
	local info = GetContextInfo("leftdigits")
	rightStr = leftStr.format("P%02d   ", info+1)

	if ctrlType == 1 then
	leftStr = rightStr.format("B%02d   ", ctrlPos)
	else
	leftStr = rightStr.format("S%1d    ", ctrlPos)
	end

	-- set digits
	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay

	elseif IsSLIFunction("lowfuel", funcIndex) and mLowFuelFeedbackAllowed then
	-- lowfuel feedback
	-- get the option
	local info = GetContextInfo("lowfuel")

	-- set digits
	leftStr = leftStr.format("FUEL  ", ctrlPos)
	rightStr = rightStr.format("L %02d  ", info)

	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay
	end
	else

	if ctrlType == 0 then
	-- switch

	if mDemoMapToKeyAllowed and ctrlPos == mDemoMapToKeySwitch then
	-- demo script
	-- mapping a keystroke (see global.lua to set the targeted switch)
	local key = ""
	if value == 2 then key = "v" end
	-- if value == 3 then key = "K" end
	-- if value == 4 then key = "v" end
	-- if value == 5 then key = "F5" end
	if key ~= "" then
	-- params: key, delay, modifier
	SetKeystroke(key, 100, "")
	end
	end

	if mSwitchFeedbackAllowed then
	-- general switch feedback (see global.lua to activate)
	leftStr = leftStr.format("S%1d    ", ctrlPos)
	rightStr = rightStr.format("P%02d   ", value)

	-- set digits
	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay
	end
	elseif ctrlType == 1 then
	if ctrlPos == mDumpLapButton  and dev == "SLI-PRO" and value>0 and mDumpLapAllowed then
	-- sample script using button of SLI-PRO to
	-- toggle telemetry dumplastlap flag (true or false)
	-- telemetry logs are stored in telemetry directory
	-- after each completed lap
	local dlap = GetContextInfo("dumplastlap")
	local flag = not dlap;
	TelemetryTools("dumplastlap", flag);
	-- set digits
	leftStr = "tELENN"
	if flag then
	rightStr = "SAVE  "
	else
	rightStr = "FALSE "
	end
	UpdateDigits(leftStr, rightStr)
	SLISendReport(0)

	state = 1
	-- set timeout
	oldTcks = GetTicks() + delay
	end
	end
	end

	if state >= 1 then
	-- loop until timeout
	local newtcks = GetTicks()
	loopFlag = true
	while(oldTcks > newtcks and loopFlag ) do
	SLISleep(10)
	newtcks = GetTicks()
	SetLeftDigits(leftStr)
	SetRightDigits(rightStr)
	SLISendReport(0)
	end

	-- cleanup device
	oldTcks = 0
	state = 0
	toggleAllLed(0)
	UpdateDigits("      ", "      ")
	SLISendReport(0)

	-- allow Mgr to display info on digits
	SetDigitsAllowed(true)
	end

	-- skip
	return 0
end

-- set leds, digits, parts to cleanup and timeout
function UpdateDigits(leftInfo, rightInfo)
	-- do not refresh digits until timeout
	SetDigitsAllowed(false)
	mF1LeftSpdLmtText = string.sub(leftInfo,4)
	mF1RightSpdLmtText = string.sub(rightInfo,4)

	-- set digits string
	SetLeftDigits(leftInfo)
	SetRightDigits(rightInfo)

end

-- brightness feedback function
function BrightnessFeedBack(device)
	-- set all leds
	toggleAllLed(1)

	if device == "SLI-PRO" then
	-- SLI-PRO Device
	-- set digits
	UpdateDigits("888888", "888888")
	end
end