Hardware Reference
In-Depth Information
Listing 16-4 continued
ser.flushInput()
# tell the arduino to start sending
running = True
ser.write('3')
ser.write('G')
def checkInput(b):
# see if the bytes have been received in the correct order
correct = True
for i in range(0,4):
#print i,” - “ # ,hex(ord(b[i]))
if (ord(b[i]) >> 6) != i :
correct = False
return correct
def getData():
global reading, running
if running :
a = ser.read(4)
if checkInput(a) :
reading[0] = ((ord(a[0]) & 0x1f)<< 5) | ;
(ord(a[1]) &0x1f)
reading[1] = ((ord(a[2]) & 0x1f)<<5) | ;
(ord(a[3]) &0x1f)
#print reading[0],” - “,reading[1]
else:
correct = False
while correct == False : # resynchronise
print “lost sync “,ser.inWaiting()
b = ser.read(1)
t = a[1] + a[2] + a[3] + b[0]
a = t
correct = checkInput(a)
def blank_screen():
screen.fill((255,255,255)) # blank screen
pygame.display.update()
def terminate(): # close down the program
print (“Closing down please wait”)
# tell the arduino to stop sending
Search WWH ::




Custom Search