# Tram Tracker# by Luke Jamieson# For Raspberry Pi with multiplexing 7 Segment displays# Python 3# June 2019# Display code adapted from: https://www.picmicrolab.com/multiplexing-7-segment-display-with-raspberry-pi/
import RPi.GPIO as GPIOimport urllib.requestimport urllib.parseimport mathimport timeimport datetime
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT) # D1GPIO.setup(11,GPIO.OUT) # D2GPIO.setup(13,GPIO.OUT) # D3GPIO.setup(15,GPIO.OUT) # D4GPIO.setup(16,GPIO.OUT) # D5GPIO.setup(18,GPIO.OUT) # D6GPIO.setup(22,GPIO.IN) #Wake Button
Bit7 = 37 #DPBit6 = 29 #GBit5 = 31 #FBit4 = 40 #EBit3 = 38 #DBit2 = 35 #CBit1 = 33 #BBit0 = 32 #A
Bits = [Bit0,Bit1,Bit2,Bit3,Bit4,Bit5,Bit6,Bit7]Digits = [0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E]
Tens = 0Ones = 0Tens2 = 0Ones2 = 0Tens3 = 0Ones3 = 0i = 1999 # counter between data fetchingwake = 6000 # wake timerstat1 = Falsestat2 = Falsestat3 = False
def InitIO(): for bit in Bits: GPIO.setup(bit,GPIO.OUT)
def SetBits(num,value): GPIO.output(Bits[num],value)
def OutputBinary(value): for bit in range(8): mask = 1<<bit SetBits(bit,value & mask)
InitIO()
while True:# ------------ Check Local Time -------------# Display only active between 6am and 1pm now = datetime.datetime.now() hour = int(now.strftime("%H")) if hour >= 6 and hour <= 12: wake = 6000# ------------ Check Button ----------# Display can be turned on manually with button press if GPIO.input(22)==1: wake = 6000# ------------ Not awake --------------# Turn off display outside of wake hours. if wake == 0: GPIO.output(7,GPIO.HIGH) GPIO.output(11,GPIO.HIGH) GPIO.output(13,GPIO.HIGH) GPIO.output(15,GPIO.HIGH) GPIO.output(16,GPIO.HIGH) GPIO.output(18,GPIO.HIGH) xtime1 = 60 xtime2 = 60 xtime3 = 60# ------------------- Display -----------------------# wake variable greater than zero = Run while wake > 0: #bitwise anding to turn on Dot Point if stat1 == True: and1 = 0x7F else: and1 = 0xFF
if stat2 == True: and2 = 0x7F else: and2 = 0xFF
if stat3 == True: and3 = 0x7F else: and3 = 0xFF# ----------------- First Time ----------------------- GPIO.output(7,GPIO.LOW) # D1 GPIO.output(11,GPIO.HIGH) GPIO.output(13,GPIO.HIGH) GPIO.output(15,GPIO.HIGH) GPIO.output(16,GPIO.HIGH) GPIO.output(18,GPIO.HIGH) if Tens == 0: OutputBinary(0xFF) elif xtime1 > 59: OutputBinary(0xBF) else: OutputBinary(Digits[Tens])
time.sleep(0.001) OutputBinary(0xFF)
GPIO.output(7,GPIO.HIGH) GPIO.output(11,GPIO.LOW) # D2 GPIO.output(13,GPIO.HIGH) GPIO.output(15,GPIO.HIGH) GPIO.output(16,GPIO.HIGH) GPIO.output(18,GPIO.HIGH) if Tens == 0 and Ones == 0: OutputBinary((0xAB & and1)) # n for now elif xtime1 > 59: OutputBinary(0xBF) else: OutputBinary((Digits[Ones] & and1))
time.sleep(0.001) OutputBinary(0xFF)# ----------------- Second Time ---------------------- GPIO.output(7,GPIO.HIGH) GPIO.output(11,GPIO.HIGH) GPIO.output(13,GPIO.LOW) # D3 GPIO.output(15,GPIO.HIGH) GPIO.output(16,GPIO.HIGH) GPIO.output(18,GPIO.HIGH) if Tens2 == 0: OutputBinary(0xFF) elif xtime2 > 59: OutputBinary(0xBF) else: OutputBinary(Digits[Tens2])
time.sleep(0.001) OutputBinary(0xFF)
GPIO.output(7,GPIO.HIGH) GPIO.output(11,GPIO.HIGH) GPIO.output(13,GPIO.HIGH) GPIO.output(15,GPIO.LOW) # D4 GPIO.output(16,GPIO.HIGH) GPIO.output(18,GPIO.HIGH) if Tens2 == 0 and Ones2 == 0: OutputBinary((0xAB & and2)) # n for now elif xtime2 > 59: OutputBinary(0xBF) else: OutputBinary((Digits[Ones2] & and2))
time.sleep(0.001) OutputBinary(0xFF)# ------------------ Third Time --------------------- GPIO.output(7,GPIO.HIGH) GPIO.output(11,GPIO.HIGH) GPIO.output(13,GPIO.HIGH) GPIO.output(15,GPIO.HIGH) GPIO.output(16,GPIO.LOW) # D5 GPIO.output(18,GPIO.HIGH) if Tens3 == 0: OutputBinary(0xFF) elif xtime3 > 59: OutputBinary(0xBF) else: OutputBinary(Digits[Tens3])
time.sleep(0.001) OutputBinary(0xFF)
GPIO.output(7,GPIO.HIGH) GPIO.output(11,GPIO.HIGH) GPIO.output(13,GPIO.HIGH) GPIO.output(15,GPIO.HIGH) GPIO.output(16,GPIO.HIGH) GPIO.output(18,GPIO.LOW) # D6 if Tens3 == 0 and Ones3 == 0: OutputBinary((0xAB & and3)) # n for now elif xtime3 > 59: OutputBinary(0xBF) else: OutputBinary((Digits[Ones3] & and3))
time.sleep(0.001) OutputBinary(0xFF)# -----------------------Get data------------------- i+=1 wake-=1 if(i == 2000): i = 0 try: strx = urllib.request.urlopen('http://www.tramtracker.com/Controllers/GetNextPredictionsForStop.ashx?stopNo=1306&routeNo=59&isLowFloor=false') tram = str(strx.read())
next1 = tram.find('\/Date') next2 = tram.find('\/Date', next1 + 1) next3 = tram.find('\/Date', next2 + 1) reqd = tram.find('\/Date', next3 + 1) recd = tram.find('\/Date', reqd + 1)# ------------------------ destination and distruption find ----- dest1 = tram.find("Destination") dest2 = tram.find("Destination", dest1 + 1) dest3 = tram.find("Destination", dest2 + 1) dist1 = tram.find("HasDisruption") dist2 = tram.find("HasDisruption", dist1 + 1) dist3 = tram.find("HasDisruption", dist2 + 1)# ----------------------- destination flag ----------------------- desf1 = tram[dest1 + 14:dest1 + 17] desf2 = tram[dest2 + 14:dest2 + 17] desf3 = tram[dest3 + 14:dest3 + 17]
if desf1 == 'Fli': destlg1 = False else: destlg1 = True if desf2 == 'Fli': destlg2 = False else: destlg2 = True if desf3 == 'Fli': destlg3 = False else: destlg3 = True# ----------------------- disruption flag ----------------------- disf1 = tram[dist1 + 15:dist1 + 19] disf2 = tram[dist2 + 15:dist2 + 19] disf3 = tram[dist3 + 15:dist3 + 19]
if disf1 == 'true': distlg1 = True else: distlg1 = False if disf2 == 'true': distlg2 = True else: distlg2 = False if disf3 == 'true': distlg3 = True else: distlg3 = False# ------------------- status flag -------------------------------- stat1 = destlg1 or distlg1 stat2 = destlg2 or distlg2 stat3 = destlg3 or distlg3# ------------------- calculate time -------------------------- time1 = int(tram[(next1 + 7):(next1 + 17)]) time2 = int(tram[(next2 + 7):(next2 + 17)]) time3 = int(tram[(next3 + 7):(next3 + 17)]) timerc = int(tram[(recd + 7):(recd + 17)]) xtime1 = math.floor((time1 - timerc) / 60) xtime2 = math.floor((time2 - timerc) / 60) xtime3 = math.floor((time3 - timerc) / 60)
Ones = int(xtime1 % 10) Tens = int(xtime1 / 10) Ones2 = int(xtime2 % 10) Tens2 = int(xtime2 / 10) Ones3 = int(xtime3 % 10) Tens3 = int(xtime3 / 10) except: Ones = int(15) Tens = int(15) Ones2 = int(15) Tens2 = int(15) Ones3 = int(15) Tens3 = int(15)