20
20
# data_txt_as_list = list of instances of DataText, used to print data on the screen.
21
21
# ----------------------------------------------------------------------------------------------------------------------
22
22
23
- import os , sys , time , datetime , random , binascii , struct , json , socket
24
- from dash_support import *
25
- from colours import *
23
+ import random , struct , json , socket
24
+ # from dash_support import *
25
+ # from colours import *
26
26
from gauges_text import *
27
27
from pygame .locals import *
28
28
from candata import *
29
29
from types import SimpleNamespace
30
30
31
+
31
32
def pygame_setup (cfg ):
32
- pygame .init () # Init pygame
33
- pygame .font .init () # Init pygame fonts
34
- pygame .mixer .quit () # BUG FIX, Quitting mixer stops it from hogging cpu!!!
33
+ pygame .init () # Init pygame
34
+ pygame .font .init () # Init pygame fonts
35
+ pygame .mixer .quit () # BUG FIX, Quitting mixer stops it from hogging cpu!!!
35
36
36
- POLLCAN = pygame .USEREVENT + 1 # Pygame event for CANBus
37
- pygame .time .set_timer (POLLCAN , cfg .Timers ['PollCAN_schedule' ]) # weird still required!
38
- clock = pygame .time .Clock () # Setup PG clock, critical component to runtime.
37
+ POLLCAN = pygame .USEREVENT + 1 # Pygame event for CANBus
38
+ pygame .time .set_timer (POLLCAN , cfg .Timers ['PollCAN_schedule' ]) # weird still required!
39
+ clock = pygame .time .Clock () # Setup PG clock, critical component to runtime.
39
40
40
41
# sort display surface
41
42
gameDisplay = pygame .display .set_mode ((cfg .Display ['width' ], cfg .Display ['height' ]))
@@ -63,13 +64,14 @@ def pygame_setup(cfg):
63
64
return hack_font , rpmFont , labelFont , dataFont , gameDisplay , windowSurface , clock
64
65
65
66
66
- def demo_rpm (demo_rpm_val ):
67
+ def demo_rpm (demo_rpm_val , max_rpm ):
67
68
if demo_rpm_val < max_rpm :
68
69
demo_rpm_val += 25
69
70
else :
70
71
demo_rpm_val = 0
71
72
return demo_rpm_val
72
73
74
+
73
75
def draw_screen_borders (windowSurface ):
74
76
middle_line = 355
75
77
d1 = 300
@@ -103,17 +105,18 @@ def draw_screen_borders(windowSurface):
103
105
pygame .draw .line (windowSurface , MAIN_BORDER_COLOUR , (x1 , y4 ), (display_width , y4 ), 1 )
104
106
return
105
107
108
+
106
109
def processing_loop (sock , cfg ):
107
110
hack_font , rpmFont , labelFont , dataFont , gameDisplay , windowSurface , clock = pygame_setup (cfg )
108
111
109
112
unpacker = struct .Struct (cfg .UDP_Dash ['fmt_struct' ])
110
113
111
114
# Control parameters
112
115
pending_data = True
113
- keep_running = True # ensures continued operation, set false in flow to stop
114
- demo_loop = False # runs demo data, set through keyboard
115
- random_loop = False # runs random data, set through keyboard
116
- demo_rpm_val = 0 # initial value for demo data.
116
+ keep_running = True # ensures continued operation, set false in flow to stop
117
+ demo_loop = False # runs demo data, set through keyboard
118
+ random_loop = False # runs random data, set through keyboard
119
+ demo_rpm_val = 0 # initial value for demo data.
117
120
118
121
# setup screen layout, borders etc
119
122
draw_screen_borders (windowSurface )
@@ -123,7 +126,7 @@ def processing_loop(sock, cfg):
123
126
([RPM_LSB_TXT , TEXT_BG ]), [420 , 160 ])
124
127
125
128
# declare rpm gauge instance and display bar for zero value
126
- rpm_bar = DisplayBarGauge ("rpm" , 0 , cfg .RPM ['max_rpm' ], windowSurface ,
129
+ rpm_bar = DisplayBarGauge ("rpm" , 0 , ( cfg .RPM ['shift_bar_lower_rpm' ], cfg . RPM [ ' max_rpm' ]) , windowSurface ,
127
130
([cfg .Images ['rev_image1' ], cfg .Images ['rev_image2' ], cfg .Images ['rev_image1' ],
128
131
cfg .Images ['rev_image_shift' ]]), GEN_BACKGROUND , ([10 , 15 ]),
129
132
(cfg .RPM ['band1' ], cfg .RPM ['band2' ], cfg .RPM ['band3' ]))
@@ -136,18 +139,18 @@ def processing_loop(sock, cfg):
136
139
False , True )
137
140
138
141
# --------------------------------------------------------- __ Display related lists __
139
- reference_display_table_labels = [] # Simple reference lookup table, easy check to
140
- # see if the table has been seen before.
141
- display_table_labels = [] # List of label objects from class.
142
- display_table_readings = [] # List of text objects relating to the data displayed
142
+ reference_display_table_labels = [] # Simple reference lookup table, easy check to
143
+ # see if the table has been seen before.
144
+ display_table_labels = [] # List of label objects from class.
145
+ display_table_readings = [] # List of text objects relating to the data displayed
143
146
# ----------------------------------------------------------------------------------------------------------------
144
- data_points = [] # List of Can_vals per value type received. List
145
- # attributes are updated to reflect each new packet
147
+ data_points = [] # List of Can_vals per value type received. List
148
+ # attributes are updated to reflect each new packet
146
149
147
- rpm_reading = Rpmval ("rpm" , 0 ) # Instantiate RPM reading (Can_val) object.
148
- # RPM operates at a higher frequency and as a result has been separated for ease.
150
+ rpm_reading = Rpmval ("rpm" , 0 ) # Instantiate RPM reading (Can_val) object.
151
+ # RPM operates at a higher frequency and as a result has been separated for ease.
149
152
150
- while keep_running : # Now start core loop used during reception.
153
+ while keep_running : # Now start core loop used during reception.
151
154
for event in pygame .event .get ():
152
155
if event .type == pygame .QUIT :
153
156
pygame .quit ()
@@ -171,7 +174,7 @@ def processing_loop(sock, cfg):
171
174
if event .key == K_RIGHT :
172
175
random_loop = False
173
176
demo_loop = False
174
- rpm_reading .wipe () # needs to fixed!!!!!
177
+ rpm_reading .wipe () # needs to fixed!!!!!
175
178
if event .key == K_LSHIFT :
176
179
random_loop = False
177
180
demo_loop = True
@@ -183,11 +186,10 @@ def processing_loop(sock, cfg):
183
186
rpm_dial_gauge .draw_wiper_arc ()
184
187
185
188
if demo_loop :
186
- rpm_reading .rx_val = demo_rpm (rpm_reading .rx_val )
189
+ rpm_reading .rx_val = demo_rpm (rpm_reading .rx_val , cfg . RPM [ 'max_rpm' ] )
187
190
188
191
if random_loop :
189
- rpm_reading .rx_val = random .randint (1 , 7700 )
190
-
192
+ rpm_reading .rx_val = random .randint (1 , cfg .RPM ['max_rpm' ])
191
193
192
194
# bug fix to stop zero values from the keyboard. -- code improvement needed
193
195
if rpm_reading .rx_val < 0 :
@@ -218,49 +220,50 @@ def processing_loop(sock, cfg):
218
220
data_label = str (unpacked_data [1 ].decode ("utf-8" )).rstrip ()
219
221
data_value = int (unpacked_data [2 ])
220
222
221
- if data_label == "Engine Speed" : # is data engine speed, if so
222
- rpm_reading .set_change (unpacked_data [2 ]) # update dedicated engine speed
223
+ if data_label == "Engine Speed" : # is data engine speed, if so
224
+ rpm_reading .set_change (unpacked_data [2 ]) # update dedicated engine speed
223
225
224
226
else :
225
- # if len(reference_display_table_labels) < 10: # Capture the labels from the stream of data
226
- if data_label not in reference_display_table_labels : # if not in list
227
+ # if len(reference_display_table_labels) < 10:
228
+ # Capture the labels from the stream of data
229
+ if data_label not in reference_display_table_labels : # if not in list
227
230
228
231
# create the labels here for each value here.
229
- reference_display_table_labels .append (data_label ) # add to list.
232
+ reference_display_table_labels .append (data_label ) # add to list.
230
233
x = cfg .Tables ['table_start_x' ]
231
234
y = cfg .Tables ['table_start_y' ] + (len (reference_display_table_labels ) * cfg .Tables ['table_inc_y' ])
232
235
display_table_labels .append (LabelText (data_label , windowSurface , data_label ,
233
- DARK_GREEN , TEXT_BG , labelFont , x , y ))
236
+ DARK_GREEN , TEXT_BG , labelFont , x , y ))
234
237
235
238
# repeat similar process here, for the data values.
236
239
display_table_readings .append (DataText (data_label , windowSurface , data_value , GREEN , TEXT_BG ,
237
- dataFont , (x + cfg .Tables ['table_value_offset' ]), y ))
240
+ dataFont , (x + cfg .Tables ['table_value_offset' ]), y ))
238
241
239
- data_points .append (Can_val (data_label , data_value )) # instantiate instance of class for new data label
242
+ data_points .append (Can_val (data_label , data_value )) # instantiate instance of class for new data label
240
243
241
244
# we have a full list of labels and prior data, so we need to update the screen value with the current
242
245
# data value
243
246
244
- for item in display_table_readings : # run through display table readings and look for
245
- if item .name == data_label : # instance name matching current data
246
- item .update (data_value ) # if match update the screen label.
247
+ for item in display_table_readings : # run through display table readings and look for
248
+ if item .name == data_label : # instance name matching current data
249
+ item .update (data_value ) # if match update the screen label.
247
250
248
- for item in data_points : # add new data point to appropriate Can_val instance in list
251
+ for item in data_points : # add new data point to appropriate Can_val instance in list
249
252
if item .name == data_label :
250
253
item .set_change (data_value )
251
- pygame .display .update () # Update the Pygame display.
254
+ pygame .display .update () # Update the Pygame display.
252
255
return
253
256
254
- def main ():
255
257
256
- with open ('server_cfg.txt' ) as json_data_file : # Open configuration file
257
- cfg_vals = json .load (json_data_file ) # load json data
258
- cfg = SimpleNamespace (** cfg_vals ) # namespace object from json
258
+ def main ():
259
+ with open ('server_cfg.txt' ) as json_data_file : # Open configuration file
260
+ cfg_values = json .load (json_data_file ) # load json data
261
+ cfg = SimpleNamespace (** cfg_values ) # namespace object from json
259
262
260
263
print (cfg .App ['name' ])
261
264
262
- sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM ) # create server socket
263
- svr_addr = (cfg .UDP_Dash ['host' ], cfg .UDP_Dash ['port' ]) # bind socket to port
265
+ sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM ) # create server socket
266
+ svr_addr = (cfg .UDP_Dash ['host' ], cfg .UDP_Dash ['port' ]) # bind socket to port
264
267
try :
265
268
sock .bind (svr_addr )
266
269
print (str (sock ))
@@ -270,5 +273,6 @@ def main():
270
273
if sock :
271
274
processing_loop (sock , cfg )
272
275
276
+
273
277
if __name__ == '__main__' :
274
278
main ()
0 commit comments