a friend and i are making a digital dashboard on a rasspberry pi but we keep running into problem when combining the obd code and the pygame. sepertaly they wwork but together it is either pygame or obd that doesn't work. origanaly we tried doing it by asking the data this way:
import obd
from obd import OBDStatus
connection=obd.OBD("/dev/rfcomm0")
obd.logger.setLevel(obd.logging.DEBUG)
print (obd.OBD(fast=False , timeout =30,protocol = 0))
print(OBDStatus.ELM_CONNECTED)
print(OBDStatus.OBD_CONNECTED)
print(OBDStatus.CAR_CONNECTED)
cmd=obd.commands.SPEED
response=connection.query(cmd)
print(response.value)
connection.close()
but we ran into problems with speed that the ignition was off. now we do it the watch command from obd and now obd part works but pygame stopped working because of it. we don't realy know is going wrong as we stopped getting error's in the debug screen
this is the combined code:
import pygame
from pygame.locals import *
import obd
pygame.init()
def rotate(surface, angle, pivot, offset):
rotated_image = pygame.transform.rotozoom(surface, -angle, 1)
rotated_offset = offset.rotate(angle)
rect = rotated_image.get_rect(center=pivot+rotated_offset)
return rotated_image, rect
def get_speed(s):
global speed
if not s.is_null():
speed = int(s.value.magnitude) #for kph
print(speed)
def get_rpm(r):
global rpm
if not r.is_null():
rpm = int(r.value.magnitude)
print(rpm)
def get_load(l):
global load
if not l.is_null():
load = int(l.value.magnitude)
def angle_up(angle):
angle += 1
return angle
def angle_down(angle):
angle -= 1
return angle
connection = obd.Async(fast=False)
connection.watch(obd.commands.SPEED, callback=get_speed)
connection.watch(obd.commands.RPM, callback=get_rpm)
connection.watch(obd.commands.ENGINE_LOAD, callback=get_load)
connection.start()
hoek1 = -225
hoek2 = -225
hoek3 = -226
speed = 0
rpm = 0
load = 0
SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080
offset = pygame.math.Vector2(162.5, 0)
center1 = int(SCREEN_WIDTH/5), int(SCREEN_HEIGHT/2)
center2 = int(SCREEN_WIDTH*(3/5)), int(SCREEN_HEIGHT/2)
center3 = SCREEN_WIDTH, int(SCREEN_HEIGHT/2)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
running = True
Background = pygame.image.load('./graphics/Background.png').convert_alpha()
surf1 = pygame.image.load('./graphics/Wijzer.png').convert_alpha()
surf2 = pygame.image.load('./graphics/Wijzer.png').convert_alpha()
surf3 = pygame.image.load('./graphics/Wijzer.png').convert_alpha()
UPDATE =pygame.USEREVENT + 1
pygame.time.set_timer(UPDATE, 250)
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
connection.stop()
running = False
elif event.type == QUIT:
connection.stop()
running = False
elif event.type == UPDATE:
if speed > hoek1:
hoek1 = angle_up(hoek1)
if speed < hoek1:
hoek1 = angle_down(hoek1)
if rpm > hoek2:
hoek2 = angle_up(hoek2)
if rpm < hoek2:
hoek2 = angle_down(hoek2)
if load > hoek3:
hoek3 = angle_up(hoek3)
if load < hoek3:
hoek3 = angle_down(hoek3)
if hoek1 > 45:
hoek1 = 45
if hoek2 > 45:
hoek2 = 45
if hoek3 > -135:
hoek3 = -135
if hoek1 < -225:
hoek1 = -225
if hoek2 < -225:
hoek2 = -225
if hoek3 < -225:
hoek3 = -225
surf1_rotated, surf_center1_rotated = rotate(surf1, hoek1, center1, offset)
surf2_rotated, surf_center2_rotated = rotate(surf2, hoek2, center2, offset)
surf3_rotated, surf_center3_rotated = rotate(surf3, hoek3, center3, offset)
screen.blit(Background, (0,0))
screen.blit(surf1_rotated, surf_center1_rotated)
screen.blit(surf2_rotated, surf_center2_rotated)
screen.blit(surf3_rotated, surf_center3_rotated)
pygame.display.flip()
clock.tick(30)
pygame.quit()
the code is on this github link: https://github.com/EGO-mercenary/Dashboard