127 lines
11 KiB
Plaintext
127 lines
11 KiB
Plaintext
|
|
Episode: 2933
|
|||
|
|
Title: HPR2933: A walk through my PifaceCAD Python code – Part 1
|
|||
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2933/hpr2933.mp3
|
|||
|
|
Transcribed: 2025-10-24 13:29:59
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
It's Wednesday the 30th of October 2019 and this is HPR episode 2933 entitled, I walk
|
|||
|
|
through my PiFaceCAD Python code part 1. It's part of a series a little bit of Python
|
|||
|
|
and it's the 50th anniversary show for MrX. The show itself is about 50 minutes long and
|
|||
|
|
carries an explicit fact. The summary is, in this series I do a whirlwind tour of the
|
|||
|
|
Python code I developed for my PiFaceCAD board. This episode of HPR is brought to you by
|
|||
|
|
an honesthost.com. Get 15% discount on all shared hosting with the offer code HPR15. That's
|
|||
|
|
HPR15. Better web hosting that's honest and fair at An Honesthost.com.
|
|||
|
|
Hello and welcome hacker public radio audience. My name is MrX and welcome to this podcast.
|
|||
|
|
I'd like to start as usual by thanking the people HPR for making this service available
|
|||
|
|
to us all. It really is a truly invaluable service to bring to all of us. HPR is a community
|
|||
|
|
led podcast provided by the community for the community. That means you can contribute to
|
|||
|
|
just pick up a microphone, MP3 player, mobile phone, computer if you've got one, hit record
|
|||
|
|
enough you go and then send it in simple as that. Well it's been a while since I've recorded
|
|||
|
|
anything and I'm feeling decidedly rusty. I wasn't sure if I was going to record this episode.
|
|||
|
|
I'm hummed and hooded about it for quite a while. So in this episode I thought I'd do a quick run
|
|||
|
|
through try to keep it quick of my Python program which I use on my PiFace command and display
|
|||
|
|
board which sits on top of my Raspberry Pi which I use to control a mock P on another Raspberry Pi
|
|||
|
|
upstairs. It allows me to play and pause a track playing on mock P music and command line player
|
|||
|
|
and allows me to go to the next track, previous track and so on and so forth. I explained all that
|
|||
|
|
in a previous episode hopefully. I've just turned it on and it says there are only 23 days to a free
|
|||
|
|
HPR slot and the blinks on the side is glowing green which means this is an adequate, I want to say
|
|||
|
|
plenty, it's never plenty, adequate number of shows in the queue. Ken would never want to admit
|
|||
|
|
that. There's never adequate, there's always space for more. So why don't you contribute something
|
|||
|
|
and give us some shows? I've got contributed to some shows. More than we know what to do with.
|
|||
|
|
So yes, one with the shoe. So this is going to be a bit off the cuff.
|
|||
|
|
So I've got, I'll just move screens here and I'll just quit the program with a queue and I'm
|
|||
|
|
logged into my Raspberry Pi with the PiFace board installed. So I've got a copy here open and
|
|||
|
|
of caddas. So where am I? What was I saying? So this script has been in development for quite a
|
|||
|
|
long time and I mean, nowadays I hardly ever tweak it. It can go months without change. I did do
|
|||
|
|
some changes recently, which I'll talk about later on as I come to them. My most recent changes.
|
|||
|
|
But I'm just looking back at the date stamps on the script. So I've got a backup script which
|
|||
|
|
backs up my scripts directory as it's up. Well, tj, tj, tjz it. And let me just see. Yeah, so
|
|||
|
|
so the very earliest version of my scripts folder goes back to 2014. So I should, does that mean
|
|||
|
|
I've been working with it? So it might be in 2014 when I started working on it. I'm not quite
|
|||
|
|
sure, but it goes back a long way anyway. And the most recent one was that most recent change was
|
|||
|
|
on the 5th of December 2018, just for him to sick. So the program's called caddas menu.py.
|
|||
|
|
It's a Python program. Basically, so I'll just do I have that open? Let's just see. Yeah,
|
|||
|
|
yes, here we go. So I had a few, but some, a few problems with an update. And I had to go back
|
|||
|
|
from Python 3 to Python 2. But I'll explain that in another podcast. Can I ignore that for now?
|
|||
|
|
Hopefully, I won't tweak it too much. I did wonder, as I said, I wondered whether to release this or not
|
|||
|
|
because it's always nerve-wracking when you think about releasing code into the wild. You know,
|
|||
|
|
it's going to show how rubbish programmers are and all the mistakes and terrible things. And
|
|||
|
|
I'm actually just spilling. And I always have been so, Dave will have a Canadian when he sees the
|
|||
|
|
a devil spelling in this script, in English. But then I suppose, well, I'm not a professional
|
|||
|
|
programmer, I'm a hobby programmer. It's a good thing, it's a curse, but Python's so easy
|
|||
|
|
to play with, you can pull code together really quickly and really easily. Probably you can
|
|||
|
|
write quite nasty code quite easily as well, of course. But I was kind of impatient just
|
|||
|
|
to build this thing up, and it kind of morphed from very simple thing. It wasn't well planned
|
|||
|
|
out like a good software, but software should be, you know, was, oh, what they want to do
|
|||
|
|
well. Of course, the first thing I wanted to do, too, was to able to pause and play, pause
|
|||
|
|
and pause and play audio playback from my Raspberry Pi upstairs. It was running mock P, mock
|
|||
|
|
P being the command line player, which plays audio. And I wanted to run, I wanted to be able
|
|||
|
|
to, at the very least, play and pause, toggle between play and pause on that, that was
|
|||
|
|
really all I needed to achieve. And that was probably the very first thing that I managed.
|
|||
|
|
You know, mock P can be told, I think it's a dash capital G, I think, mock P is based
|
|||
|
|
dash G, dash capital G, toggle between play and pause. And that's all you needed to send
|
|||
|
|
to the PI upstairs, and that would, that would do it. And click to the Raspberry PI upstairs
|
|||
|
|
as a set of cordless headphones. I've probably explained all this before, so maybe I have
|
|||
|
|
to go into all that. So anyway, I've got a section at the front here, which starts things
|
|||
|
|
to do, and I hope I have stuff in there, which, if you're bored, you can read over. Because
|
|||
|
|
I'm quite a long time, actually, I've got a section code starts here, at line 140.
|
|||
|
|
So the really clever thing is, is a PI face, CAD module, which come with the PI face board.
|
|||
|
|
That's what all the clever stuff is, really. I'm just taking advantage of this module
|
|||
|
|
that was written by the PI face people. And of course, there's a new version, I think
|
|||
|
|
of the newer version of PI. And this is, of course, some of the weaknesses, you know,
|
|||
|
|
the distribution updates, and then the drivers don't work in things, and then you lose compatibility
|
|||
|
|
and all this sort of thing. So, that's a problem. It's getting a bit long in the tooth now.
|
|||
|
|
And I've barely had the will and inclination to get it working after the last deb being
|
|||
|
|
upgrade, but that's another story. So, yeah, so the whole pilot module is, I import into
|
|||
|
|
the Python project. I won't be rushing through that. The most recent thing I did was below
|
|||
|
|
that, I've got a section for global variables. And I had littered throughout the code, I had
|
|||
|
|
the address for my internal IP address, the Raspberry Pi, which I was controlling. And
|
|||
|
|
all littered throughout the code was, you know, Pi at 192.168.1.Whatever, you know. And what
|
|||
|
|
I really should have had was a global variable called remote, well, a global variable basically
|
|||
|
|
so that you could just refer to that, and I would say, if you ever needed to change that
|
|||
|
|
IP address, you would change it in one place and not 15 places or whatever. So, that was
|
|||
|
|
I think I did very recently, and I called that variable remote device. So, remote device
|
|||
|
|
equals, in my case, Pi at 192.168.1.Thirteen. So, I probably rely, well, I definitely rely
|
|||
|
|
too heavily on global variables. And I'm not, I don't think I've been terribly consistent
|
|||
|
|
with global variables either, because, from documentation I read online, I was under
|
|||
|
|
the understanding that you needed to use a command to allow you to use global variables
|
|||
|
|
in a function. Now, what I'm just trying to think now, you know, of course, I'm, it's
|
|||
|
|
not long since I've done any programming and Python, and I can't even know what the command
|
|||
|
|
was. Let me just see now. What was that command to allow you? Oh, global, that was it. So,
|
|||
|
|
for example, I've got, I've got a line here, it says global space menu. So, that allows
|
|||
|
|
you to use the global variable menu. Now, what I didn't actually understand was that if
|
|||
|
|
you, I, I, and I'm still not, I haven't done enough, I haven't done any experiments to
|
|||
|
|
really prove this, but it must be the case that if you don't use, if you've got a global
|
|||
|
|
variable, so you've got, maybe, okay, so the difference between that, a private variable
|
|||
|
|
and a global, private variable and not a normal variable and a global variable is that a
|
|||
|
|
normal variable is a scope. If you've got a normal variable inside a, a function in you,
|
|||
|
|
then other functions outside, outside that can't see that, that variable, picking this
|
|||
|
|
very complicated answer, but a global variable of it's at the top of the code, then outside
|
|||
|
|
all the functions in every day, all the functions can see that global variable. But the way
|
|||
|
|
it works is, I think is if you don't use the command global, then you can still access
|
|||
|
|
that global variable, you just can't change it. So, in some cases, I've got, I'm using
|
|||
|
|
the command global institutions where I don't actually need to change that global variable.
|
|||
|
|
So in reality, for safety, I shouldn't be using the word global, so you just ignore it
|
|||
|
|
altogether and just, I can read the contents of the global variable, but I don't need to
|
|||
|
|
change it. I think that's how that basically works. How did I get into that? I don't know.
|
|||
|
|
Anyway, so I've got too many global variables. You should really, my understanding is that
|
|||
|
|
you should really feed values in and out of functions rather than pass values into functions
|
|||
|
|
rather than relying on global variables. I remember a tutor explaining to me many, many years
|
|||
|
|
ago when I was doing a course on what was it? It's not Python, what was it? Pascal, that
|
|||
|
|
was it. I need to say, well, it's a bit like, I imagine you're outside a field and you
|
|||
|
|
want to cross a field to get to the other side and there's a bull in the middle of the field
|
|||
|
|
as well. You can either walk around outside edge where it's nice and safe or you can jump
|
|||
|
|
over the fence and try and run to the other side and I say, well, it's a bit like that.
|
|||
|
|
If you're using global variables, then another function could come go in and change that
|
|||
|
|
of variable to something else that you're not aware of and mark up your code, basically,
|
|||
|
|
whereas if you're passing the value in then you're not affecting the global variable.
|
|||
|
|
Anyway, whatever that will come from. I better watch my tingle how long has been going for?
|
|||
|
|
15 minutes. Oh, this is a disaster. This could be far too long. Okay, I think that's about
|
|||
|
|
enough for this episode. I think it's a good place to stop it. If you want to contact me,
|
|||
|
|
then I can be contacted at MrX at hpr.googlemail.com. That's mrx80hpr.com. So tune in next time for
|
|||
|
|
an exciting episode of Hacker Public Radio.
|
|||
|
|
If you have comments on today's show, please email the host directly, leave a comment on
|
|||
|
|
the website or record a follow-up episode yourself. Unless otherwise stated, today's show
|
|||
|
|
is released on the creative comments, attribution, share a light 3.0 license.
|