Saturday, 1 December 2018

Raspberry Pi

For 12 years before retiring to the Forest of Dean I was a secondary school governor. Probably because my full-time work was technical, they made me the governor responsible for ICT - Information and Communications Technology. It was the nearest that the school got to teaching computing.

Many times I regaled the head teacher and anyone else who would listen, pointing out that all they were teaching children was how to use Microsoft Office, and doubtless enhancing that corporation's profits too. "We have to teach the syllabus," I was told. "Then the syllabus is wrong!" I would reply.

Within a couple of years of my leaving, everything changed with the introduction of the palm-sized Raspberry Pi. Here, at last, was a computer on which children, and grown-ups too, could learn real programming; a computer that could be connected to switches, relays, lights, buzzers and more. O how I wish that I could be back at that school to say "I told you so!"

The British-made Rasberry Pi was a runaway success. According to Wikipedia:
    More than 5 million Raspberry Pis were sold by February 2015, making it the best-selling British Computer. By November 2016 they had sold 11 million units and 12.5 million by March 2017, making it the third best-selling "general purpose computer". In March 2018, sales reached 19 million.

19 million Raspberry Pi computers... and I didn't have one. The shame of it! So earlier this year I bought a RasPi B+ and set about resurrecting my old programming skills. In truth, this was my second attempt at resurrection, for two years ago I confidently wrote this post, in which I declared my intention to learn a programming language called Visual Basic. I failed. Somewhere around lesson 6 I lost interest because I didn't have any hardware that I could program to actually DO something – turn lights on and off, ring bells, or even control the model railway.

Control the model railway? Now there was an interesting challenge, and one well within the capabilities of the Raspberry Pi. But could I learn to program it?

The favoured language for the Pi is Python so, as in 2016, I bought a book and avidly started reading and programming.  The language lacked the simplicity of the BASIC that I'd used at work, but learning it was a dream and before long I had the confidence to start writing a program for that model railway.

It was just like being back at work in the R&D unit – work that I loved.

Precisely what this program will do, I'll hopefully describe in a future post after it's all working, but here's a little bit of the code that I'm pleased to say is all my own work. It would probably make professional Python programmers weep, but I'm rather proud of it.

def timetable_main_display():
    tt=open('whereto.txt','r') # whereto stores pointer to last train working
    tp=int(tt.readline()) +1   # moves pointer to next train working
    tt.close()
    
    wkg = 0
    trow = 0
    noterow = 0

    import csv    
    with open('StPetrock timetable.csv') as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
            wkg = int(row[0])
                                   
            if wkg > (tp-1) and wkg < (tp+5):
                # calculate timetable time in minutes
                if wkg == tp: 
                    ttime = row[1]  
                    if len(ttime) == 4:
                        tclockhr = ttime[0]
                        tclockmin = ttime[2:4]
                        tclock_in_mins = (int(tclockhr)*60)+ int(tclockmin)
                    if len(ttime) == 5:
                        tclockhr = ttime[0:2]
                        tclockmin = ttime[3:5]
                        tclock_in_mins = (int(tclockhr)*60)+ int(tclockmin)
                        
                trow += 1
                twkg = row[0]
                ttime = row[1]
                tfrom = row[2]
                tto = row[3]
                tclass = row[4]
                tupdown = row[7]
                tstore = row[8]
                if trow == 1:
                    tclass_next_train = tclass
                    tupdown_next_train = tupdown
                    
                display_timetable_line(trow,twkg,ttime,tfrom,tto,tclass,tstore)





2 comments:

  1. I'm happy to take it on trust that this is all sheer brilliance. Otherwise, gobbledegook to me, I'm afraid, although I'd be interested to know why so many words in the program start with 't'.

    Lucy



    ReplyDelete
    Replies
    1. It's my abbreviation for anything concerning the timetable. The top line defines this as a function for the timetable display, so there'll be lots of 't's - timetable row, timetable from, timetable to (where trains come from and to), timetable class (passenger, freight, etc).

      Delete