-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandaloneCopAssist.py
More file actions
151 lines (132 loc) · 5.86 KB
/
StandaloneCopAssist.py
File metadata and controls
151 lines (132 loc) · 5.86 KB
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
import pyinsim
class CopAssist:
def __init__(self):
self.started = False
self.siren = False
self.siren_fast = False
self.strobe = False
self.use_extra_light = True
self.use_indicators = True
self.use_light = True
self.use_fog_light = True
self.strobe_cycle = 0
self.insim = pyinsim.insim(b'127.0.0.1', 29999, Admin=b'', Flags=pyinsim.ISF_MCI, Interval=200)
def strobe_func(self):
if self.strobe_cycle == 0:
self.strobe_cycle = 1
if self.use_extra_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_EXTRA | pyinsim.LCL_Mask_Extra)
if self.use_indicators:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_SIGNALS | pyinsim.LCL_Mask_Left)
if self.use_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_LIGHTS)
elif self.strobe_cycle == 1:
self.strobe_cycle = 2
if self.use_indicators:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_SIGNALS | pyinsim.LCL_Mask_Right)
if self.use_fog_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_FOG_REAR | pyinsim.LCL_Mask_FogRear)
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_FOG_FRONT)
elif self.strobe_cycle == 2:
self.strobe_cycle = 3
if self.use_extra_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_EXTRA)
if self.use_indicators:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_SIGNALS)
if self.use_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_LIGHTS | pyinsim.LCL_Mask_HighBeam)
elif self.strobe_cycle == 3:
self.strobe_cycle = 0
if self.use_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_LIGHTS)
if self.use_fog_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_FOG_REAR)
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL,
UVal=pyinsim.LCL_SET_FOG_FRONT | pyinsim.LCL_Mask_FogFront)
def stop_strobe(self):
self.strobe = False
if self.use_extra_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_EXTRA)
if self.use_indicators:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_SIGNALS)
if self.use_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_LIGHTS)
if self.use_fog_light:
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_FOG_REAR)
self.insim.send(pyinsim.ISP_SMALL, SubT=pyinsim.SMALL_LCL, UVal=pyinsim.LCL_SET_FOG_FRONT)
def stop_siren(self):
self.insim.send(pyinsim.ISP_MST, Msg=b"/siren off")
def start_siren(self):
self.insim.send(pyinsim.ISP_MST, Msg=b"/siren slow")
def toggle_siren(self):
self.siren = not self.siren
if not self.siren:
self.stop_siren()
else:
self.start_siren()
self.strobe = True
self.send_buttons()
def toggle_strobe(self):
self.strobe = not self.strobe
if not self.strobe:
self.stop_strobe()
self.siren = False
self.stop_siren()
self.send_buttons()
def send_buttons(self):
self.send_button(1, pyinsim.ISB_DARK | pyinsim.ISB_CLICK, 100, 0, 12, 5,
"^4Strobe" if self.strobe else "Strobe")
self.send_button(2, pyinsim.ISB_DARK | pyinsim.ISB_CLICK, 105, 0, 12, 5,
"^4Siren" if self.siren else "Siren")
def get_car_data(self, insim, mci):
if not self.started:
self.started = True
self.send_buttons()
self.insim.send(pyinsim.ISP_MSL, Msg=b"Type '/o siren' or '/o strobe' to toggle siren or strobe.")
if self.strobe:
self.strobe_func()
def button_click(self, insim, btc):
if btc.ClickID == 1:
self.toggle_strobe()
elif btc.ClickID == 2:
self.toggle_siren()
def send_button(self, click_id, style, t, l, w, h, text, inst=0, typeIn=0):
"""
This method checks if a button is already on the screen and if not, it sends a new button to LFS.
It makes sending buttons easier than always sending the entire thing to lfs.
"""
if type(text) == str:
text = text.encode()
self.insim.send(
pyinsim.ISP_BTN,
ReqI=255,
ClickID=click_id,
BStyle=style | 3,
Inst=inst,
T=t,
L=l,
W=w,
H=h,
Text=text,
TypeIn=typeIn)
def message_handling(self, insim, mso):
if mso.Msg == b'siren':
self.toggle_siren()
elif mso.Msg == b'strobe':
self.toggle_strobe()
def run(self):
self.insim.bind(pyinsim.ISP_MCI, self.get_car_data)
self.insim.bind(pyinsim.ISP_BTC, self.button_click)
self.insim.bind(pyinsim.ISP_MSO, self.message_handling)
pyinsim.run()
# Example in Test.py
if __name__ == "__main__":
Strobe = CopAssist()
Strobe.run()