Dec-19-2018, 06:26 AM
Hello guys. I have a python 2.7 script for my raspberry pi. My problem is When I run code, Raspberry pi use %20 of CPU. I think, wrong something in my code. I saw else program bigger than mine(Also it runs video and render image) but it use %5 of CPU. I think my algorithm is wrong but I couldn't find any solution. My code this. If you can helping to me, I will happy. Thanks your time.
Program details is: Rpi connect with uart to stm. Stm transmit data to Rpi. Rpi read incoming data and it shows on the GUI. Also there is image on GUI. My python program runs for Rpi.
Program details is: Rpi connect with uart to stm. Stm transmit data to Rpi. Rpi read incoming data and it shows on the GUI. Also there is image on GUI. My python program runs for Rpi.
#! /usr/bin/python
# -*-coding: utf-8 -*-
#-Library setting-#
import RPi.GPIO as gpio
import time
import serial
from Tkinter import *
from ttk import Frame, Style, Button
from PIL import ImageTk, Image
import subprocess
from playsound import playsound
gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(33, gpio.IN)
#-Buffer definition-#
dataarray=''
data=''
uartData=''
dstflg=False
dspflg=False
dataflg=False
testflg=False
floorinfoflg=False
control_count=0
#-Serial port config-#
try:
stmSer=serial.Serial(port='/dev/ttyAMA0', baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0.00001
)
except serial.SerialException:
import traceback
traceback.print_exc()
print "Stmser except error"
L1=Label(root, text=(traceback.print_exc()))
L1.place(x=10, y=400)
#-GUI update according to data of 'CARSTAT'-#
def updateGUI(data):
global left_character
global right_character
global call_up
global call_down
global travel_up
global travel_down
global floorinfoflg
global control
try:
left_character=(data[10:12]).decode("hex")
right_character=(data[12:14]).decode("hex")
petra_status_1=format(ord(data[14:16].decode("hex")),'08b')
print "UpdateGUI"
travel_down=petra_status_1[4]
travel_up=petra_status_1[3]
katno.config(text=left_character+right_character, font=("Arial", 300))
print "UpdaaeGUI work"
if travel_up=="1":
image=Image.open("/home/pi/Petra/upArrow2.png")
photo=ImageTk.PhotoImage(image)
label.config(image=photo)
label.photo_ref=photo
if left_character=="1" or left_character=="2":
katno.config(text=left_character+right_character, font=("Arial", 210))
katno.place(x=0,y=40)
else:
katno.config(text=left_character+right_character, font=("Arial", 300))
katno.place(x=-90,y=-30)
elif travel_down=="1":
image=Image.open("/home/pi/Petra/downArrow2.png")
photo=ImageTk.PhotoImage(image)
label.config(image=photo)
label.photo_ref=photo
if left_character=="1" or left_character=="2":
katno.config(text=left_character+right_character, font=("Arial", 210))
katno.place(x=0,y=40)
else:
katno.config(text=left_character+right_character, font=("Arial", 300))
katno.place(x=-90,y=-30)
elif travel_up=="0" and travel_down=="0":
image=Image.open("/home/pi/Petra/upArrow2.png")
photo=ImageTk.PhotoImage(image)
label.config(image='')
label.photo_ref=photo
if left_character=="0":
katno.config(text=left_character+right_character, font=("Arial", 300))
katno.place(x=-150, y=-30)
elif left_character=="1" or left_character=="2":
katno.config(text=left_character+right_character, font=("Arial", 300))
katno.place(x=-150, y=-30)
else:
katno.config(text=left_character+right_character, font=("Arial", 300))
katno.place(x=-190, y=-30)
else:
print "travel conditions ERROR"
root.after(15,loop)
except:
import traceback
traceback.print_exc()
print "UpdateGUI Error"
prgloop()
L2=Label(root, text=(traceback.print_exc()))
L2.place(x=10, y=400)
###-Data Read -#
def readuart():
global dataread
global datalist
global dataarray
global dstflg
global dspflg
global testflg
global stmSer
while True:
try:
if stmSer.inWaiting()>0:
dataread=stmSer.read(1)
if testflg==True:
dstflg=True
dspflg=True
if dataread=='@' and dstflg==False:
dstflg=True
dspflg=False
dataarray=''
elif dataread=='$' and dspflg==False:
dspflg=True
dstflg=False
#print "dataarray: "+dataarray
return dataarray
dataarray=''
else:
if dstflg==True and dspflg==False:
dataarray=dataarray+''.join(dataread)
elif dstflg==True and dspflg==True:
print "Reset loop"
dataarray=''
uartData=''
writeda=''
testflg=False
dstflg=False
dspflg=False
root.after(5,readuart)
elif dstflg==False and dspflg==False:
print "Reset loop"
dataarray=''
uartData=''
writeda=''
testflg=False
dstflg=False
dspflg=False
root.after(5,readuart)
else:
print "ERROR!!"
input_value = gpio.input(33) ##Button Pgm_DW
if input_value == True:
testflg=True
dataarray=''
uartData=''
writeda=''
#print('The button has been pressed...')
#else:
#print "Data bekleniyor."
except:
import traceback
traceback.print_exc()
print "readUart Error"
L3=Label(root, text=(traceback.print_exc()))
L3.place(x=10, y=400)
print "readUart Error"
def prgloop():
print "Reset prgloop"
testflg=False
root.after(5,loop)
def render():
image2=Image.open('/home/pi/Petra/image_new.jpg')
photo2=ImageTk.PhotoImage(image2)
label2=Label(image=photo2, bg="#142F5E")
label2.image=photo2
label2.config(height=375, width=480)
label2.place(x=0, y=430)
#-Furcation to update function -#
def loop():
global dataread
global datalist
global dataarray
global dstflg
global uartData
global dspflg
global data
dspflg=False
try:
uartData=readuart()
if uartData[:8]=='CARSTAT,':
updateGUI(uartData)
#print "data: "+uartData
else:
#print "İnvalid Data"
root.after(5,loop)
except:
print "Update ERROR"
import traceback
traceback.print_exc()
L4=Label(root, text=(traceback.print_exc()))
L4.place(x=10, y=400)
prgloop()
#-Graphical Design-#
try:
root=Tk()
root.title('V0.8')
root.overrideredirect(0)
katno=Label(root, text='1', font=("Arial", 300), width=2, fg='white', bg='#2080FF')
katno.place(x=-90, y=-40)
katno.config(padx=170)
image=Image.open('/home/pi/Petra/upArrow2.png')
photo=ImageTk.PhotoImage(image)
label=Label(image=photo, bg="#2080FF")
label.image=photo
label.place(x=30, y=50)
root.configure(background="#2080FF")
root.geometry('480x400+0+0')
render()
loop()
root.mainloop()
except:
import traceback
traceback.print_exc()
L5=Label(root, text=(traceback.print_exc()))
L5.place(x=10, y=40)
print "GUI ERROR"
