summaryrefslogtreecommitdiff
path: root/scripts/global.lua
blob: 867e73e5df4b36911bfc9c56a4c7ea95184cd68f (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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
-- SLIMax Mgr Lua Script v3.9
-- PART OF SLIMAX Manager pkg
-- Copyright (c)2012-2013 by Zappadoc - All Rights Reserved.
-- last change by Zappadoc - 2013-11

mSMX_VERSION = 3.9

-- see slimax_script_readme.txt for implementation

-- load left and right functions tools
require "scripts/functions_tools"

-- ============================================================
-- GLOBAL FLAGS
-- set this flags to change the std behaviors of SLIMax Mgr
-- ============================================================

-- true to show the total delta splits sector 1 + sector 2
-- false to show the delta of sector 2 only
mDelta_total_split_sectors = true

-- delta time delay
mDeltaTimeBackup = 0
mDeltaTimeOldTicks = 0
mDeltaTimeAlternateOldTicks = 0
-- change the delay below to increase or decrease the update of delta time
mDeltaTimeDelay = 200
-- change the delay below to increase or decrease the update of lap time
mRefreshLapTimeRate = 50

-- get feedback on display when you turn encoder or press button assigned to internal functions, see control.lua
-- true if you want to display switch pos and value
mSwitchFeedbackAllowed = false

-- Fanatec Digits Status
mFanatecDigitsFeedbackAllowed = true
-- Fanatec speedlimiter method seleted
mFanatecSpeedLimiterFeedbackAllowed = true
-- Fanatec osp method seleted
mFanatecOSPFeedbackAllowed = true
-- Fanatec shiftlights method seleted
mFanatecShiftlightsFeedbackAllowed = true

-- Low Fuel status
mLowFuelFeedbackAllowed = true

-- Left and Right Digits status
mLeftDigitsFeedbackAllowed = false
mRightDigitsFeedbackAllowed = false

-- display KPH or KMH
mUnitFeedbackAllowed = true

-- show the current OSP Factor
mOSPFeedbackAllowed = true

-- cool global brightness feedback
mBrightnessFeedbackAllowed = true

-- show the current value of maxgear option
mMaxGearFeedbackAllowed = true

-- activate or deactivate lap dump telemetry
-- using button 9 by default
mDumpLapAllowed = false
mDumpLapButton = 9

-- dump telemetry
TelemetryTools("dumplastlap", false);

-- mapping to keystroke demo script
-- see the support forum for more info on key mapping
mDemoMapToKeyAllowed = false
mDemoMapToKeySwitch = 5

-- ============================================================
-- GLOBAL VAR
-- ============================================================
-- SMXMessageBox Styles
MB_OK                       = 0x00000000
MB_OKCANCEL                 = 0x00000001
MB_ABORTRETRYIGNORE         = 0x00000002
MB_YESNOCANCEL              = 0x00000003
MB_YESNO                    = 0x00000004
MB_RETRYCANCEL              = 0x00000005
MB_CANCELTRYCONTINUE        = 0x00000006

MB_ICONHAND                 = 0x00000010
MB_ICONQUESTION             = 0x00000020
MB_ICONEXCLAMATION          = 0x00000030
MB_ICONASTERISK             = 0x00000040

MB_USERICON                 = 0x00000080
MB_ICONWARNING              = MB_ICONEXCLAMATION
MB_ICONERROR                = MB_ICONHAND

MB_ICONINFORMATION          = MB_ICONASTERISK
MB_ICONSTOP                 = MB_ICONHAND

-- MB result
mIDOK                = 1
mIDCANCEL            = 2
mIDABORT             = 3
mIDRETRY             = 4
mIDIGNORE            = 5
mIDYES               = 6
mIDNO                = 7

-- scripts settings
mCustomScriptsFileName = ""

-- new in: 2.2
-- init left and right digits panel text
mLeftSpdLmtText = "      "
mRightSpdLmtText = "      "
-- 4 digits F1 panel
mF1LeftSpdLmtText = "    "
mF1RightSpdLmtText = "    "

-- new: 2.1
-- RPM threshold values in percentage used with Shiftlights method 2
-- and configured with SLIMax Manager II Advanced Options Panel
-- default values:
RPM_PERCENT_VALUES = {50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 97, 98, 98, 99, 99 }
RPM_ABSOLUTE_VALUES = {15452, 15545, 15823, 16354, 16410, 16675, 17252, 17545, 17823, 18354, 18510, 18655, 18670, 18675, 18677 }

-- Fanatec text using for slowdown update
mFanatecText = ""
-- SIMRACE DLX text using for slowdown update
mSRDlxLeftText = ""
mSRDlxRightText = ""
-- SIMRACE DLX text using for slowdown update
mSRF1LeftText = ""
mSRF1RightText = ""

-- ============================================================

-- init globals flag
isGlobalInitialized = 0

-- 15 RPM leds
mRPMLedTable = {
    RPM0=0,
    RPM1=0,
    RPM2=0,
    RPM3=0,
    RPM4=0,
    RPM5=0,
    RPM6=0,
    RPM7=0,
    RPM8=0,
    RPM9=0,
    RPM10=0,
    RPM11=0,
    RPM12=0,
	RPM13=0,
    RPM14=0
}


-- SLI device type
-- mDeviceType no longer avaialable use GetDeviceType(devID) see 2.3 API


-- init globals
if mOld_ls1 == nil then
	mOld_bs1 =  0.00000
	mOld_bs2 =  0.00000
	mOld_bt =  0.00000
	mDelta_lpt = 0.00000
	mDelta_lptb = 0.00000
	mOld_ls1 =  0.00000
	mOld_ls2 =  0.00000
	mOld_lt =  0.00000
	mDelta_lpt = 0.00000
	mDelta_lptb = 0.00000
	mDelta_Sect1 = 0.0000
	mDelta_Sect1b = 0.0000
end


-- --------------------------------------------
-- KERS functions
-- --------------------------------------------

function GetKersPercent()
	-- get kers value
	local kers_level = GetCarInfo("kers")
	local k_percent = 0
	if kers_level ~= nil and kers_level > 0 then
	k_percent = round((kers_level/1000) / 4 )
	end
	return k_percent
end


-- Init all globals
function InitGlobals()
	-- default blink time delay
	mBlinkTime = 32
	mOSPBlinkTime = 8

	-- ticks stuff
	mOSPBlink = 0
	mSpdLmtBlink = 0
	mOldTickCount = 0
	mOldOSPTickCount = 0

	gRedZone = 0
	gOSPLimit = 0
	mBU0710Leds = 0
	mG27Leds = 0
	mOSPMethod = 0
	mFanatecLeds = 0
end

-- set RPM threshold value in percentage ( SLIMax Manager 2.1 )
function SetRPMPercentValue(index, value)
	if index <1 or index>15 then
	return
	end
	if value <0 or value >100 then
	return
	end

	RPM_PERCENT_VALUES[index] = value
end

-- set RPM threshold value ( SLIMax Manager 2.1 )
function SetRPMAbsoluteValue(index, value)
	if index <1 or index>15 then
	return
	end
	if value <0 or value >20000 then
	return
	end

	RPM_ABSOLUTE_VALUES[index] = value
end

-- get all global preferences set in general_default.sli
function GetSLIMaxInfo()
	-- get RPM Only flag
	mSpdLmtRPMLedOnly = GetContextInfo("spdlmtrpmledonlyflag")
	if mSpdLmtRPMLedOnly == nil then mSpdLmtRPMLedOnly = false end

	-- get blink delay value*
	mBlinkTime = GetContextInfo("blinktime")
	if mBlinkTime == nil then mBlinkTime = 32 end
	if mBlinkTime < 1 or  mBlinkTime > 48 then mBlinkTime = 32 end

	-- get osp blink delay value
	mOSPBlinkTime = GetContextInfo("ospblinktime")
	if mOSPBlinkTime == nil then mOSPBlinkTime = 32 end
	if mOSPBlinkTime < 1 or  mOSPBlinkTime > 48 then mOSPBlinkTime = 32 end

	-- get current speed Limiter LED index
	mSpeedLimiterLED = GetContextInfo("ledspeedlimiterindex")
	if mSpeedLimiterLED == nil then mSpeedLimiterLED = 5 end
	if mSpeedLimiterLED < 1 or  mSpeedLimiterLED > 6 then mSpeedLimiterLED = 5 end

	-- get current osp LEDs index
	mOSPLED1 = GetContextInfo("ospled1")
	if mOSPLED1 == nil then mOSPLED1 = 1 end
	if mOSPLED1 < 1 or  mOSPLED1 > 11 then mOSPLED1 = 1 end

	mOSPLED2 = GetContextInfo("ospled2")
	if mOSPLED2 == nil then mOSPLED2 = 1 end
	if mOSPLED2 < 1 or  mOSPLED2 > 11 then mOSPLED2 = 1 end

	-- blinking allowed?
	mNoBlink = GetContextInfo("noblinkflag")
	if mNoBlink == nil then mNoBlink = false end

	-- get limiter char flag
	mLimiterChar = GetContextInfo("limitercharflag")
	if mLimiterChar == nil then mLimiterChar = false end

	-- is OSP with first gear allowed?
	mOSPWithFirstGear = GetContextInfo("ospwithfirstgear")
	if mOSPWithFirstGear == nil then mOSPWithFirstGear = false end

end

-- get cpu ticks
 function GetTicks()
	local tcks = GetContextInfo("ticks")
	if tcks == nil then tcks  = 0 end
	return tcks
end

-- reset table function
function initLedTable(ibl, value)
	if ibl ~= nil then
	for k, v in pairs(ibl) do
	ibl[k] = value
	end
	end
end

-- toggle all led state
function toggleAllLed(val)
	initLedTable(mRPMLedTable, val)
	for i = 1, 6 do SetWarnLed(i, val) end
	-- 7 external leds is the max value used in SIM RACE F1 and Emulator
	for i = 1, 7 do SetExtLed(i, val) end
	SetRPMLed("mRPMLedTable")
end

-- The following function rounds a number
-- lua doesn't return the same rounded value as C/C++
function round(num)
    if num == nil then num = 0 end
    local i = 0
    if num >= 0 then
        i = math.floor(num+.5)
    else
        i = math.ceil(num-.5)
    end
    return i
end


-- return speed in KPH or MPH
function speed(spd, selector)
    if spd == nil then spd = 0 end
	if selector then
	-- MPH
	return ( spd * 2.237)
	else
	return (spd * 3.6)
	end
end

-- Fahrenheit to Celsius
function FtoC(f)
	return ((f - 32) * (5/9))
end

-- Celsius to Fahrenheit
function CtoF(c)
	return (c * (9/5) + 32)
end

-- Kelvin to Celsius
function KtoC(k)
	return ( k - 273.313)
end

-- liters to gallons
function LtoG(liters)
 return (liters * 0.264172052 )
end

-- gallons to liters
function GtoL(gallons)
 return (gallons * 3.78541178 )
end

-- return fuel in liters or gallons
function GetFuel(fl, selector)
    if fl == nil then fl = 0 end
	if selector then
	-- gallons
	return LtoG( fl)
	else
	return fl
	end
end

-- return celcius or fahrenheit
function GetTemp(tmp, selector)
    if tmp == nil then tmp = 0 end
	if selector then
	-- gallons
	return CtoF( tmp)
	else
	return tmp
	end
end

-- set refresh lap time rate in ms
function SetRefreshLapTimeRate(delay)
    if delay == nil or delay < 0 or delay > 5000 then delay = 50 end
	mRefreshLapTimeRate = delay
end

-- set delta time delay in ms
function SetDeltaTimeDelay(delay)
    if delay == nil or delay < 0 or delay > 5000 then delay = 200 end
	mDeltatimeDelay = delay
end

-- get current gear
function GetCurrentGear()
	local g = GetCarInfo("gear")
	-- get neutral
	local n = GetContextInfo("neutral")
	-- get reverse char and convert to string
	local r = string.char(GetContextInfo("reverse"))
	-- get state of custom Optimal Shift Point (OSP) records
	local ospcustom_on = GetContextInfo("ospcustom")

	-- set neutral, reverse or current gear
	local result = g

	if g == 0 then
	-- if neutral and using custom OSP record then add a dot to gear digit
	if ospcustom_on  then n = n + 128 end
	result = string.char(n)
	elseif g < 0 then
	-- reverse
	result = r
	end

	return result
end

function isAppIRacing(sim)
	if sim == "iRacingSim.exe" or sim == "iRacingSim64.exe" then
	return true
	end
	return false
end
function isAppRFactor(sim)
	if sim == "rFactor.exe" or sim == "rFactor2.exe" then
	return true
	end
	return false
end

-- force display text on left or/and right panel during delay in ms
function LeftRightMessageText(delay, left_text, right_text)
	-- disallow Mgr to display info on digits
	SetDigitsAllowed(false)

	-- set timeout
	local oldTcks = GetTicks() + delay

	-- loop until timeout
	while(oldTcks > GetTicks()) do
	SLISleep(1)
	if left_text ~= "" then
	SetLeftDigits(left_text)
	end
	if right_text ~= "" then
	SetRightDigits(right_text)
	end
	SLISendReport(0)
	end

	-- cleanup device
	UpdateDigits("      ", "      ")
	SLISendReport(0)

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


-- The following function calculate hr, mn, sec, hd, ms
-- param: time in meter/sec
function timeDispatcher( tt)
    if tms == nil then tms = 0 end
	local tms = math.abs(tt)
	local t_hr = 0
	local t_mn = 0
	local t_sc = 0
	local t_ms = 0
	local t_hd = 0

	if tms > 0 then
	    t_hr, n = math.modf(tms/3600)
        t_mn, c = math.modf(n*60)
        t_sc, s = math.modf(c*60)
        t_hd, h = math.modf(s*100)
        t_ms, m = math.modf(s*1000)
	end
--	print( tt, t_hr, t_mn, t_sc, t_hd, t_ms)
	return t_hr, t_mn, t_sc, t_hd, t_ms
end

function DisplayFormatTime(lptime, delay, dec, deltaflag, numDigits)
   local ticks = GetTicks()

   if dft_old_time == nil  or (ticks - dft_old_time) > delay then
	-- global to backup ticks (ms)
	dft_old_time = ticks

	local hr = 0
	local mn = 0
	local sc = 0
	local ms = 0
	local hd = 0
	local panel = ""
	local prefix = " "

	if lptime < 0 then
	prefix = "-"
	end
	-- explod time
	hr, mn, sc, hd, ms = timeDispatcher(lptime)

	if deltaflag == 1 then
	--display delta time
	if lptime == -1 or (mn + sc + ms) == 0.0 then
	if numDigits == 3 then
	Panel =  "-.--"
	else
	Panel =  "  -.-- "
	end
	elseif mn > 0 then
	if numDigits == 3 then
	if mn < 100 then
	Panel =  string.format( "%s%02d.", prefix, mn)
	else
	Panel = "   ."
	end

	else
	Panel =  string.format( " %s%1d.%02d ", prefix, mn, sc)
	end
	else

	if numDigits == 3 then
	if sc < 10 then
	if prefix == "-" then
	Panel =  string.format( "%s%1d.%01d ", prefix, sc, hd)
	else
	Panel =  string.format( "%1d.%02d ", sc, hd)
	end
	else
	Panel = " .  "
	end
	else
	if Ndig == 2 then
	Panel =  string.format( " %s%1d.%02d ", prefix, sc, hd)
	else
	Panel =  string.format( " %s%1d.%03d", prefix, sc, ms)
	end
	end

	end

	elseif deltaflag == 0 then
	--display laptime
	if lptime == -1 or (mn + sc + ms) == 0.0 then
	if numDigits == 3 then
	Panel =  "-.--"
	else
	Panel =  "-:--.---"
	end

	elseif mn < 10 then
	if numDigits == 3 then
	Panel =  string.format( "%1d.%02d", mn, sc)
	else
	Panel =  string.format( "%1d:%02d.%03d", mn, sc, ms)
	end
	elseif hr > 0 then
	if numDigits == 3 then
	Panel =  "  . "
	else
	Panel =  string.format( " %02d.%02d ", hr, mn)
	end
	else
	if numDigits == 3 then
	Panel =  string.format( " %02d.", mn)
	else
	Panel =  string.format( " %02d.%02d.%01d", mn, sc, ms)
	end
	end
	end
	end

	return Panel
end


-- return delta time vs last time + sector diff
function GetDeltaLastTime(isRF)

	local lpt = 0.0

	local sector = GetCarInfo("sector")

	if mDiffSectorDelay == nil or mDiffOldSector == nil or mDiffOldSector ~= sector then
	-- 8s delay, increase it if needed
	mDiffSectorDelay = GetTicks() + 8000
	mDiffOldSector = sector

	if sector == 1  then
	-- diff lap time 3
	local lt = GetTimeInfo("lastlaptime")

	if lt > 0 and mOld_lt > 0 then
	-- diff
	mDelta_lpt = lt - mOld_lt
	end
	-- backup lt
	mOld_lt =  lt
	-- save bt for delta vs best function
	mOld_bt =  GetTimeInfo("bestlaptime")


	elseif sector == 2 then
	-- diff sector 1
	local ls1 = GetTimeInfo("lastsector1")
	if isRF then
	ls1 = GetTimeInfo("sector1")
	end

	if ls1 > 0 and mOld_ls1 > 0 then
	mDelta_lpt = ls1 - mOld_ls1
	mDelta_Sect1 = mDelta_lpt
	end

	-- backup
	mOld_ls1 = ls1
	-- save bt for delta vs best function
	mOld_bs =  GetTimeInfo("bestsector1")

	else
	-- diff sector 2
	local ls2 = GetTimeInfo("lastsector2")
	if isRF then
	ls2 = GetTimeInfo("sector2")
	end


	if ls2 > 0 and mOld_ls2 > 0 then
	if mDelta_total_split_sectors then
	mDelta_lpt = mDelta_Sect1 + (ls2 - mOld_ls2)
	else
	mDelta_lpt =  ls2 - mOld_ls2
	end
	end
	-- backup value
	mOld_ls2 = ls2
	-- save bt for delta vs best function below
	mOld_bs2 =  GetTimeInfo("bestsector2")

	end
	end

	-- display delta by default
	lpt = GetTimeInfo("realdifflast")

	if mDiffSectorDelay > GetTicks() then
	lpt = mDelta_lpt
	end

	return lpt
end

-- return delta time vs best time + sector diff
function GetDeltaBestTime(isRF)
	local lpt = 0.0

	-- get sector
	local sector = GetCarInfo("sector")

	-- set delay
	if mDiffSectorDelay == nil or mDiffOldSector == nil or mDiffOldSector ~= sector then
	-- 8s delay, increase it if needed
	mDiffSectorDelay = GetTicks() + 8000
	mDiffOldSector = sector

	-- display sector diff during delay
	if sector == 1 then

	-- diff last lap time vs best
	local bt = GetTimeInfo("lastlaptime")

	-- save lt for delta vs last function above
	mOld_lt = bt

	-- compare
	if bt > 0 and mOld_bt > 0 then
	-- delta
	mDelta_lptb = bt - mOld_bt
	end
	-- backup
	mOld_bt =  GetTimeInfo("bestlaptime")


	elseif sector == 2 then
	-- diff sector 1
	local bs1 = GetTimeInfo("lastsector1")
	if isRF then
	bs1 = GetTimeInfo("sector1")
	end
	-- save lt for delta vs last function
	mOld_ls1 = bs1

	-- compare
	if bs1 > 0  and mOld_bs1 > 0 then
	mDelta_lptb =  bs1 - mOld_bs1
	mDelta_Sect1b = mDelta_lptb
	end

	mOld_bs1 =  GetTimeInfo("bestsector1")

	else
	-- diff sector 2
	local bs2 = GetTimeInfo("lastsector2")
	if isRF then
	bs2 = GetTimeInfo("sector2")
	end
	-- save lt for delta vs last function
	mOld_ls2 = bs2

	-- compare with old value
	if bs2 > 0  and mOld_bs2 > 0 then
	if mDelta_total_split_sectors then
	mDelta_lptb = mDelta_Sect1b + (bs2 - mOld_bs2)
	else
	mDelta_lptb = bs2 - mOld_bs2
	end
	end
	-- backup
	mOld_bs2 =  GetTimeInfo("bestsector2")
	end
	end

	-- display delta by default
	lpt = GetTimeInfo("realdiffbest")

	if mDiffSectorDelay > GetTicks() then
	lpt = mDelta_lptb
	end

	return lpt
end

--==============================================
require "scripts/slidevice"