Skip to content

Commit f29e4e0

Browse files
committed
Issue #4 fixed.
1 parent 84f70eb commit f29e4e0

File tree

3 files changed

+67
-54
lines changed

3 files changed

+67
-54
lines changed

dash.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@
2020
# data_txt_as_list = list of instances of DataText, used to print data on the screen.
2121
# ----------------------------------------------------------------------------------------------------------------------
2222

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 *
2626
from gauges_text import *
2727
from pygame.locals import *
2828
from candata import *
2929
from types import SimpleNamespace
3030

31+
3132
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!!!
3536

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.
3940

4041
# sort display surface
4142
gameDisplay = pygame.display.set_mode((cfg.Display['width'], cfg.Display['height']))
@@ -63,13 +64,14 @@ def pygame_setup(cfg):
6364
return hack_font, rpmFont, labelFont, dataFont, gameDisplay, windowSurface, clock
6465

6566

66-
def demo_rpm(demo_rpm_val):
67+
def demo_rpm(demo_rpm_val, max_rpm):
6768
if demo_rpm_val < max_rpm:
6869
demo_rpm_val += 25
6970
else:
7071
demo_rpm_val = 0
7172
return demo_rpm_val
7273

74+
7375
def draw_screen_borders(windowSurface):
7476
middle_line = 355
7577
d1 = 300
@@ -103,17 +105,18 @@ def draw_screen_borders(windowSurface):
103105
pygame.draw.line(windowSurface, MAIN_BORDER_COLOUR, (x1, y4), (display_width, y4), 1)
104106
return
105107

108+
106109
def processing_loop(sock, cfg):
107110
hack_font, rpmFont, labelFont, dataFont, gameDisplay, windowSurface, clock = pygame_setup(cfg)
108111

109112
unpacker = struct.Struct(cfg.UDP_Dash['fmt_struct'])
110113

111114
# Control parameters
112115
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.
117120

118121
# setup screen layout, borders etc
119122
draw_screen_borders(windowSurface)
@@ -123,7 +126,7 @@ def processing_loop(sock, cfg):
123126
([RPM_LSB_TXT, TEXT_BG]), [420, 160])
124127

125128
# 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,
127130
([cfg.Images['rev_image1'], cfg.Images['rev_image2'], cfg.Images['rev_image1'],
128131
cfg.Images['rev_image_shift']]), GEN_BACKGROUND, ([10, 15]),
129132
(cfg.RPM['band1'], cfg.RPM['band2'], cfg.RPM['band3']))
@@ -136,18 +139,18 @@ def processing_loop(sock, cfg):
136139
False, True)
137140

138141
# --------------------------------------------------------- __ 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
143146
# ----------------------------------------------------------------------------------------------------------------
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
146149

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.
149152

150-
while keep_running: # Now start core loop used during reception.
153+
while keep_running: # Now start core loop used during reception.
151154
for event in pygame.event.get():
152155
if event.type == pygame.QUIT:
153156
pygame.quit()
@@ -171,7 +174,7 @@ def processing_loop(sock, cfg):
171174
if event.key == K_RIGHT:
172175
random_loop = False
173176
demo_loop = False
174-
rpm_reading.wipe() #needs to fixed!!!!!
177+
rpm_reading.wipe() # needs to fixed!!!!!
175178
if event.key == K_LSHIFT:
176179
random_loop = False
177180
demo_loop = True
@@ -183,11 +186,10 @@ def processing_loop(sock, cfg):
183186
rpm_dial_gauge.draw_wiper_arc()
184187

185188
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'])
187190

188191
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'])
191193

192194
# bug fix to stop zero values from the keyboard. -- code improvement needed
193195
if rpm_reading.rx_val < 0:
@@ -218,49 +220,50 @@ def processing_loop(sock, cfg):
218220
data_label = str(unpacked_data[1].decode("utf-8")).rstrip()
219221
data_value = int(unpacked_data[2])
220222

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
223225

224226
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
227230

228231
# 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.
230233
x = cfg.Tables['table_start_x']
231234
y = cfg.Tables['table_start_y'] + (len(reference_display_table_labels) * cfg.Tables['table_inc_y'])
232235
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))
234237

235238
# repeat similar process here, for the data values.
236239
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))
238241

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
240243

241244
# we have a full list of labels and prior data, so we need to update the screen value with the current
242245
# data value
243246

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.
247250

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
249252
if item.name == data_label:
250253
item.set_change(data_value)
251-
pygame.display.update() # Update the Pygame display.
254+
pygame.display.update() # Update the Pygame display.
252255
return
253256

254-
def main():
255257

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
259262

260263
print(cfg.App['name'])
261264

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
264267
try:
265268
sock.bind(svr_addr)
266269
print(str(sock))
@@ -270,5 +273,6 @@ def main():
270273
if sock:
271274
processing_loop(sock, cfg)
272275

276+
273277
if __name__ == '__main__':
274278
main()

gauges_text.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,19 @@ class DisplayBarGauge(object):
213213
This bar works horizontally.
214214
"""
215215

216-
def __init__(self, name, data_value, max_data_value, wsurface, image_list, mask_colour, image_xy_list, limits):
217-
216+
def __init__(self, name, data_value, range, wsurface, image_list, mask_colour, image_xy_list, limits):
218217
self.name = name # A arbitrary name for the object.
219218
self.data = data_value # Data value to be plotted
220-
self.max_data_value = max_data_value # The maximum value observed in the data value
219+
self.start_value = range[0]
220+
self.max_data_value = range[1] # The maximum value observed in the data value
221221
self.windowsSurface = wsurface # Target Pygame surface
222222
self.image_list = image_list # Supplied list of images to use
223223
self.mask_colour = mask_colour # Mask colour should always be background colour
224224
self.image_xy = image_xy_list # Location of where to draw the rect/images
225225
self.limits = limits
226+
227+
self.adjusted_range = self.max_data_value - self.start_value
228+
226229
# Load the listed images into a list of loaded images for use.
227230
# Use of images would be .... self.windowsSurface.blit(self.loaded_images[2], (100, 100))
228231
self.loaded_images = []
@@ -240,9 +243,15 @@ def __init__(self, name, data_value, max_data_value, wsurface, image_list, mask_
240243
self.updatebar(self.data)
241244

242245
def updatebar(self, data):
243-
self.data = data
244-
# calculate the mask_end_x as data/max_data
245-
mask_end_x = 0 - (self.mask_rect[0] - ((self.data/self.max_data_value) * self.mask_rect[0]))
246+
if data > self.start_value:
247+
adjusted_data = data - self.start_value
248+
self.data = data
249+
# calculate the mask_end_x as data/max_data
250+
mask_end_x = 0 - (self.mask_rect[0] - ((adjusted_data / self.adjusted_range) * self.mask_rect[0]))
251+
252+
else: # basically igore condition, but requires mask_end_x setting to -790 or similar.
253+
mask_end_x = 0 - (self.mask_rect[0] - ((0/self.max_data_value) * self.mask_rect[0]))
254+
246255
# set mask rectangle coordinates
247256
mask_rectangle = Rect(self.mask_start_x, self.mask_start_y, mask_end_x, self.mask_end_y)
248257
# Set which image is displayed based on data value

server_cfg.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
"RPM": {
6464
"max_rpm": 7700,
65-
"rpm_delay": 2000,
65+
"shift_bar_lower_rpm": 6000,
6666
"band1":2500,
6767
"band2":6600,
6868
"band3":7500,

0 commit comments

Comments
 (0)