summaryrefslogtreecommitdiff
path: root/scripts/simracedlx.lua
blob: fe716eac93c1446bd11bd2fd9e0b2fd6bed5d975 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
-- SIMRACE DELUXE Lua Script v1.5
-- Copyright (c)2012-2013 by Zappadoc - All Rights Reserved.
-- this script builds all functions associated with 
-- left and right panels of SIMRACE-DLX device 
-- last change by Zappadoc - 2013-09

-- ============================================================
-- Left and Right Panel functions
-- ============================================================
function srdlxLeftDigitsEvent(swFunction)
	return srdlxDigitsEvent(swFunction, 0)
end
function srdlxRightDigitsEvent(swFunction)
	return srdlxDigitsEvent(swFunction, 1)
end
-- IN function of left panel
-- param: recieve from SLI Manager the current switch position 
function srdlxDigitsEvent(swFunction, side)
	swValue = swFunction + 1

	-- call custom script
	if side == 0 then
		local result = custom_srdlxLeftDigitsEvent(swValue)
		-- 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_srdlxLeftDigitsEvent(swValue)
		-- 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
    else
		local result = custom_srdlxRightDigitsEvent(swValue)
		-- 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_srdlxRightDigitsEvent(swValue)
		-- 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	
	end
	
	local hr = 0
	local mn = 0
	local sc = 0
	local ms = 0
	local hd = 0
	local lpt = 0.0
	local diffTimeFlag = false
	local timeFlag = false
	local systemflag = false
	local unit = false
    local inf = ""
    local spd = 0.0
	local isSlowUpdate = false
	local srdlxPanel = ""

	-- get speed in kph or mph (use "rawspeed" to get value in meter/sec)
	local spd = GetCarInfo("speed")
	-- get current display unit metric or imperial
	unit = GetContextInfo("speedmetric")
	
	-- lap finished, display lap time a few seconds
	local dlt = false
	dlt = GetContextInfo("displaylaptime")
	if dlt == nil then dlt = false end
	if dlt and side == 1 then
		swValue = 11
	end
	
	-- check if quick info button is down
	local qi = false
	qi = GetContextInfo("quickinfobutton")
	if qi == nil or qi == 0 then qi = false end
	if qi then
		-- get index for left and right panel
		local qiInf = 1
		if side == 0 then
			qiInf = GetContextInfo("f1quickinfoleft")
		else
			qiInf = GetContextInfo("f1quickinforight")
		end
		if qiInf == nil then qiInf = 1 end
		-- force position to match QI preference
		swValue = qiInf
	end
	
	-- check if PIT Feedback ON
	local pf = false
	pf = GetContextInfo("pitfeedback")
	if pf == nil or pf == 0 then pf = false end
	if pf then
		-- force position to PIT/SPEED function if car pits
		local pit = GetCarInfo("inpits")
		if pit > 0 then
			if side == 0 then
				swValue = 45
			else
				swValue = 1			
			end		
		end	
	end
	
	-- get current simulation name
	local sim = GetContextInfo("simulation")
	
	--print ( "swValue: " .. swValue .. "\n")

	-- check postion and compute left panel string
	if swValue == 1 then
		-- speed only
		srdlxPanel = string.format("%3.0f ", spd )

	elseif swValue == 2 then
		-- fuel
		local fuel = GetCarInfo("fuel")
		if fuel ~= nil then
			fuel = GetFuel(fuel, unit)
			if fuel >= 100 then
				srdlxPanel = string.format("F%3d", round(fuel))
			elseif fuel >= 10 then
				srdlxPanel = string.format("F%02d ", round(fuel))
			else
				srdlxPanel = string.format("F%1.1f ", fuel)
			end
		end
    elseif swValue == 3 then
		-- position
		inf = GetContextInfo("position")
		if inf ~= nil then
			if inf >= 100 then
				 srdlxPanel = string.format("P%3d", inf)
			else
				 srdlxPanel = string.format("P%02d ", inf)
			end		   
  		end
   elseif swValue == 4 then
		-- laps completed
		inf = GetContextInfo("laps")
		if inf ~= nil then
			-- if more then 99 laps
			if inf >= 100 then
				srdlxPanel = string.format("L%3d", inf)
			else
				srdlxPanel = string.format("L%02d ", inf)
			end
 		end
    elseif swValue == 5 then
		-- sector
		inf = GetCarInfo("sector")
		if inf ~= nil then
			-- check if sector > 9
			if inf >9 then
				srdlxPanel = string.format("S%02d ", inf)
			else
				srdlxPanel = string.format("S%01d ", inf)
			end
 		end
   elseif swValue == 6 then
		-- total laps if available
		local tl = GetContextInfo("lapscount")
		if tl < 1 then  tl = 0 end
		-- if more then 99 laps
		if tl >= 100 or tl >= 100 then
			srdlxPanel = string.format("%03d ", tl)
		else
			srdlxPanel = string.format("t%02d ", tl)
		end
		
    elseif swValue == 7 then
		-- water temp
		inf = GetCarInfo("watertemp")
		if inf ~= nil then
			inf = GetTemp(inf, unit)
		    srdlxPanel = string.format("%2.1f ", inf)
 		end
    elseif swValue == 8 then
		-- oil temp
		inf = GetCarInfo("oiltemp")
		if inf ~= nil then
			inf = GetTemp(inf, unit)
		    srdlxPanel = string.format("%2.1f ", inf)
 		end
		
    elseif swValue == 9 then
			-- best lap time 
		timeFlag = true
		lpt = GetTimeInfo("laptime")

 elseif swValue == 10 then
			-- best lap time 
		timeFlag = true
		lpt = GetTimeInfo("bestlaptime")

	elseif swValue == 11 then
		-- last lap time
		timeFlag = true
		lpt = GetTimeInfo("lastlaptime")
	
	elseif swValue >= 12 and swValue <= 18 and isAppIRacing(sim) then
		timeFlag = true
		diffTimeFlag = true
		-- iRacing partials
		local ts = GetContextInfo("partialcount")
		local sector = GetCarInfo("sector")
		if ts ~= nil and ts > 0 then
			if swValue == 12 then
			   lpt = GetPartialTimeInfo("currentpartial", sector)
			elseif swValue == 13 then
				lpt = GetPartialTimeInfo("vsbestlap", sector)
			elseif swValue == 14 then
				lpt = GetPartialTimeInfo("vsoptimallap", sector)
			elseif swValue == 15 then
				lpt = GetPartialTimeInfo("vsoptimalsector", sector)
			elseif swValue == 16 then
				lpt = GetPartialTimeInfo("vssessionbestlap", sector)
			elseif swValue == 17 then
				lpt = GetPartialTimeInfo("vssessionoptimallap", sector)
			elseif swValue == 18 then
				lpt = GetPartialTimeInfo("vssessionoptimalsector", sector)
			end
		else
			lpt = 0.0
		end
		
	elseif swValue == 19 then
		-- real time diff vs your best
		diffTimeFlag = true
		timeFlag = true
		lpt = GetTimeInfo("realdiffbest")

	elseif swValue == 20 then
		-- real time diff vs your last
		diffTimeFlag = true
		timeFlag = true
		lpt = GetTimeInfo("realdifflast")

	elseif swValue == 21 then
		-- get an average consumption of fuel per lap and gives the remaining laps
		local remainintank = GetCarInfo("remainlapsintank")
		srdlxPanel = string.format("L%3.0f",  math.floor(remainintank) )
			
	elseif swValue == 22 then
		-- rpm
		isSlowUpdate = true
		diffTimeFlag = true
		local rpm = GetCarInfo("rpm")
		local r = rpm / 10.0
		if r < 1000 then
			srdlxPanel =  string.format("%3.1f", r)	
		else
			srdlxPanel =  string.format("%4d.", round(r))	
		end
	elseif swValue == 23 then
		-- track size
		isSlowUpdate = true
		diffTimeFlag = true
		local trcksz = GetContextInfo("tracksize")
		local r = trcksz / 10
		if r < 1000 then
			srdlxPanel =  string.format("%3.1f", r)	
		else
			srdlxPanel =  string.format("%4d.", round(r))	
		end
				
	elseif swValue == 24 then
		-- distance percent
		local dist = GetContextInfo("lapdistance")
		-- track size
		local trcksz = GetContextInfo("tracksize")
		local p = round(dist / (trcksz / 100))
		srdlxPanel = string.format("%3d ", p )
		
	elseif swValue == 25 then
		-- kers
		local kers = GetCarInfo("kers")
		srdlxPanel = string.format("%3d ", round(kers/1000))
		
	elseif swValue == 26 then
		-- kers max 
		local kmx = GetCarInfo("kersmax")
		srdlxPanel = string.format("%3d ", round(kmx/1000))
		
	elseif swValue == 27 then
		-- drs
		local drs = GetCarInfo("drs")
		if drs == 1 then
			srdlxPanel = "ON " 
		else
			srdlxPanel = "OFF"
		end
		
	elseif swValue == 28 then
		-- kers percent
		local kers = GetCarInfo("kers")
		srdlxPanel = string.format("%3d ", round((kers/1000)/4))
		
    elseif swValue == 29 then
		-- wheels temp if available
		inf = GetCarInfo("wheeltempfrontleft")
		if inf ~= nil then
			-- if rFactor convert Kelvin to Celsius (see global.lua)
			if isAppRFactor(sim) then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
		
    elseif swValue == 30 then
		inf = GetCarInfo("wheeltempfrontright")
		if inf ~= nil then
			if isAppRFactor(sim) then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
		
    elseif swValue == 31 then
		inf = GetCarInfo("wheeltemprearleft")
		if inf ~= nil then
			if isAppRFactor(sim) then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
		
    elseif swValue == 32 then
		inf = GetCarInfo("wheeltemprearright")
		if inf ~= nil then
			if isAppRFactor(sim) then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
	
  elseif swValue == 33 then
		-- wheels pressure if available
		inf = GetCarInfo("wheelpressfrontleft")
		if inf ~= nil then
			-- convert to psi
			srdlxPanel = string.format("%2.1f ", inf / 6.88)
 		end
		
    elseif swValue == 34 then
		inf = GetCarInfo("wheelpressfrontright")
		if inf ~= nil then
		    srdlxPanel = string.format("%2.1f ", inf / 6.88)
 		end
		
    elseif swValue == 35 then
		inf = GetCarInfo("wheelpressrearleft")
		if inf ~= nil then
		    srdlxPanel = string.format("%2.1f ", inf / 6.88)
 		end
		
    elseif swValue == 36 then
		inf = GetCarInfo("wheelpressrearright")
		if inf ~= nil then
		    srdlxPanel = string.format("%2.1f ", inf / 6.88)
 		end
		
   elseif swValue == 37 then
		-- brakes temp if available
		inf = GetCarInfo("braketempfrontleft")
		if inf ~= nil then
			if isAppRFactor(sim) or sim == "GTR2.exe" then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
		
    elseif swValue == 38 then
		inf = GetCarInfo("braketempfrontright")
		if inf ~= nil then
			if isAppRFactor(sim) or sim == "GTR2.exe" then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
		
    elseif swValue == 39 then
		inf = GetCarInfo("braketemprearleft")
		if inf ~= nil then
			if isAppRFactor(sim) or sim == "GTR2.exe" then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
		
    elseif swValue == 40 then
		inf = GetCarInfo("braketemprearright")
		if inf ~= nil then
			if isAppRFactor(sim) or sim == "GTR2.exe" then  inf = KtoC(inf) end
		    srdlxPanel = string.format("%3.0f ", inf)
 		end
		
	elseif swValue == 41 then
		-- time remaining if available
		lpt = GetTimeInfo("timeremaining")
		timeFlag = true
	
	elseif swValue == 42 then
		-- PC system time
		systemflag = true
		lpt = GetTimeInfo("systemtime")
		timeFlag = true
		diffTimeFlag = true
	elseif swValue == 43 then
		-- time elapsed if available
		lpt = GetTimeInfo("timetotalelapsed")
		timeFlag = true

	elseif swValue == 44 then
		-- last sector 1, 2 and 3
		local sector = GetCarInfo("sector")
		timeFlag = true
		diffTimeFlag = true
		if sector == 1 then
			-- sector 3
			local ls1 = GetTimeInfo("lastsector1")
			local ls2 = GetTimeInfo("lastsector2") 
			local lt = GetTimeInfo("lastlaptime") 
			if ls1 > 0 and ls2 > 0 then
				lpt = lt - (ls1 + ls2)
			end
		elseif sector == 2 then
			local ls1 = GetTimeInfo("lastsector1")
			lpt = ls1
		else
			-- sector 3
			local ls2 = GetTimeInfo("lastsector2") 
			lpt = ls2
		end		
		
	elseif swValue == 45 then
		-- PIT
		srdlxPanel = "PIt "
		
	elseif swValue == 46 then
		-- sc
		local sc = GetContextInfo("safetycare")
		if sc == 1 then
			srdlxPanel = "SCAR"
		else
			srdlxPanel = "OFF "
		end			
		
	else
		srdlxPanel = "    "
	end
	
	local refreshRate = mRefreshLapTimeRate
	if diffTimeFlag then
		refreshRate = mDeltaTimeDelay
	end		
	
	if timeFlag and lpt ~= nil then
	
		-- set char of negative number
		local c = ""
		if lpt < 0 then c = "-" end
		
		-- explod time
		hr, mn, sc, hd, ms = timeDispatcher(lpt)
		
	--print("lpt: " .. lpt .. " m: " .. mn .. " - s: " .. sc .. " - ms: " .. ms .."\n" )
	
		-- update display every mDeltaTimeDelay time
		if GetTicks() > (refreshRate + mDeltaTimeOldTicks ) then
			mDeltaTimeOldTicks = GetTicks()
			
			if lpt == -1 or (mn + sc + ms) == 0.0000 then
				mDeltaTimeBackup =  "-.---" 
		
			elseif systemflag then
				mDeltaTimeBackup =  string.format( "%2d.%02d", mn, sc)
			-- > 9mn
			elseif mn > 9 then
				if c == "" then
					mDeltaTimeBackup =  string.format( "%2d.%2d", mn, sc)
				else
					mDeltaTimeBackup =  string.format( "%s%2d ", c, mn)
				end	
			
			-- < 10mn
			elseif mn > 0 and mn < 10 then
				if c == "" then
					 mDeltaTimeBackup =  string.format( "%1d.%02d.%01d", mn, sc, hd)
				 else
					mDeltaTimeBackup =  string.format( "%s%1d.%02d", c, mn, sc)
				end	

			else
				-- mn == 0 ; sc > 9
				if sc > 9 then
					if c == "" then
						mDeltaTimeBackup =  string.format( "0.%2d.%1d", sc, hd)
					else
						mDeltaTimeBackup =  string.format( "%s0.%2d.", c, sc)
					end	
				else
					-- sc < 10
					if c == "" then
						mDeltaTimeBackup =  string.format( "0.%01d.%02d", sc, hd)
					else
						mDeltaTimeBackup =  string.format( "%s0.%01d.%01d", c, sc, hd)
					end	
				end
			end
		end
		
		--print(srdlxPanel,mDeltaTimeBackup)
		srdlxPanel = mDeltaTimeBackup
	end
	
	if isSlowUpdate then
		
		if GetTicks() > ( refreshRate  + mDeltaTimeOldTicks ) then
			mDeltaTimeOldTicks = GetTicks()
			if side == 0 then
				mSRDlxLeftText = srdlxPanel
			else
				mSRDlxRightText = srdlxPanel
			end
			-- print(GetTicks() .. mSRDlxRightText .."\n")
		end
	else
		if side == 0 then
			mSRDlxLeftText = srdlxPanel
		else
			mSRDlxRightText = srdlxPanel
		end
	end

	-- print(mSRDlxLeftText, mSRDlxRightText, srdlxPanel)
	-- send string to sli manager
	if side == 0 then
		SetLeftDigits( mSRDlxLeftText ) 
	else
		SetRightDigits( mSRDlxRightText ) 
	end

	return 1
end