-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirdrsbeep.py
116 lines (99 loc) · 3.23 KB
/
irdrsbeep.py
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
#!python3
import argparse
import configparser
import irsdk
import time
import functools
try:
import winsound
except ImportError:
import os
def beep(frequency,duration):
os.system('beep -f %s -l %s' % (frequency,duration))
else:
def beep(frequency,duration):
winsound.Beep(frequency,duration)
VERSION = '0.2.0'
# no auto-flushing in windows
print = functools.partial(print, flush=True)
class State:
connected = False
drs = -1
def check_iracing():
if state.connected and not (ir.is_initialized and ir.is_connected):
state.connected = False
state.drs = -1
ir.shutdown()
print('irsdk disconnected')
elif not state.connected:
if not ir.startup():
pass
elif ir.is_connected:
state.connected = True
print('irsdk connected')
else:
print('not connected')
def beep_upcoming():
frequency = config.getint('drs', 'upcoming_frequency', fallback=500)
duration = config.getint('drs', 'upcoming_duration', fallback=100)
if frequency == 0:
return
beep(frequency, duration)
def beep_available():
frequency = config.getint('drs', 'available_frequency', fallback=1500)
duration = config.getint('drs', 'available_duration', fallback=200)
if frequency == 0:
return
beep(frequency, duration)
def loop():
# freeze for consisten per-frame data
ir.freeze_var_buffer_latest()
drs = ir['DRS_Status']
if drs is None:
return
# DrsStatus: 0 = inactive, 1 = can be activated in next DRS zone, 2 = can be activated now, 3 = active.
if state.drs == 0 and drs == 1:
if args.verbose:
print('[verbose] upcoming beep')
beep_upcoming()
if state.drs == 1 and (drs == 2 or drs == 3):
beep_available()
if args.verbose:
print('[verbose] available beep')
if drs != state.drs:
state.drs = drs
if args.verbose:
print('[verbose] drs update = ', drs)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='drs beep for iRacing v%s' % VERSION)
parser.add_argument('-v', '--version', action='version', version='v%s' % VERSION, help='show version and exit')
parser.add_argument('--beep', action='store_true', help='play "upcoming" and "available" drs beep sounds, then exit')
parser.add_argument('--verbose', action='store_true', help='verbose output for debugging')
parser.add_argument('--tick', type=float, default=0.05, help=argparse.SUPPRESS)
args = parser.parse_args()
print('drs beep for iRacing v%s' % VERSION)
print(' github.com/rndstr/irdrsbeep')
print()
config = configparser.ConfigParser()
config.read('irdrsbeep.ini')
if args.beep:
print('"drs upcoming" beep')
beep_upcoming()
time.sleep(1)
print('"drs available" beep')
beep_available()
quit()
ir = irsdk.IRSDK()
state = State()
print("waiting for iracing...")
try:
while True:
check_iracing()
if state.connected:
loop()
time.sleep(args.tick)
else:
time.sleep(1)
except KeyboardInterrupt:
# press ctrl+c to exit
pass