Smashed
/It turns out that broken windows are surprisingly hard to photograph.
Rob Miles on the web. Also available in Real Life (tm)
I don’t know much about art. But I do know what I like. Liz Dees makes great art. And she’s got a show on at the moment in Hull. You can find out more here. We went along yesterday to take a look.
This is the venue.
Ian put the artwork on the walls. I think the fire extinguishers were there already.
Art as a Puzzle
They have free sweets too!
The work is up for sale at very reasonable prices, bearing in mind that you’re getting something that is individual and unique. If you are in the Hull area over the next couple of days you really should go along and take a look. And don’t forget to ask why one of the pictures appears to have graffiti all over it………
A few weekdays of lovely weather and then the weekend brings this. Oh well, at least the clouds make the sky look interesting.
How was your day?
If you want to find more Python Shorts you can find them here.
Making a CircuitPython program talk to a usb serial device is quite easy to do:
import usb_cdc
# Send a line of text to the host
usb_cdc.data.write(b"Hello!\n")
# Wait for a line of text from the host
while True:
if usb_cdc.data.connected:
try:
line = usb_cdc.data.readline()
if line:
print("Received:", line.decode("utf-8").strip())
except Exception as e:
print("Error reading:", e)
Howwever, when using CircuitPython on boards like the Raspberry Pi Pico, it exposes two USB serial ports over USB:
usb_cdc.console
→ used for the REPL (interactive shell)usb_cdc.data
→ intended for raw programmatic communicationWhen you use a browser that supports Web Serial (like Chrome or Edge), it might show both ports as separate choices. It's easy to accidentally connect to the console
port instead of the data
port. Your CircuitPython code ends up listening on usb_cdc.data
, but the browser is sending to usb_cdc.console
.
There are two ways you can fix this:
# boot.py
import usb_cdc
usb_cdc.disable(console=True)
Note that if you disable the console port you will not be able to interact with the device using tools such as Thonny. You will have to erase the EEPROM in the PICO and the re-flash it to get back control of the device.
We had a great hardware meetup tonight. Not much hardware, although I did bring my light panel. However, a lot of great techy chat. The next meetup will be in two weeks.
Another nice part of Scarborough
Just a reminder that we are doing the hardware thing this Wednesday starting around 5:00 pm in Hull MakerSpace in the Central Library. It would be lovely to see you there.
Perlin noise for the win!
I mentioned a while back that you could get 3 8x8 neopixel panels from Amazon for nine quid. You still can (at least until I’ve bought them all). I’ve started building a 24*8 pixel panel with them. I’ve got the box printed and wired up one panel.
Excuse my soldering
I’ve got it working and it turns out that you can get over 30fps driving all the panels flat out. This means that smooth fades are very doable. The next step is to buy six more panels and see how fast I can drive them in a 16*24 array. The plan is to use separate processor output pins to drive each panel which should make for super-smooth updates too. Such fun.
I’m doing this for an article I’m writing for Raspberry Pi Magazine. The article is about device setting storage. I thought I’d make a device which needs setting storage and it seems to have got a bit out of hand……..
We went to Scarborough today to visit Sci-Fi Scarborough. It was super. I took the big camera and grabbed some pictures.
What a welcome committee
The town was looking good too
They had some amazing film props and costumes
The spa is a great place
They had stuff for sale, authors, illustrators, props, cosplay, talks and tabletop gaming. We had lunch at the restaurant at the Spa and it was superb. Pro tip - have the fish and chips.
The event is open tomorrow (Sunday 27th). You really should go..
I’ve always liked Visio. It’s a great drawing tool. If you want a copy of your own for the mouth-wateringly low price of 17 pounds (or 20 dollars) you can get it here. The installation is a network download. Your purchase from stack social is a licence key that you can use to run the program. I’m using alongside my Office 365 installation - I’m not sure if it would work if I didn’t have Office 365. You’re tied to the 2021 version, but I don’t see that as a huge problem.
They also do Windows 11 Professional licenses for 15 dollars. This is an upgrade licence, you have to be running Windows 10 or 11 at the moment, but with the imminent demise of Windows 10 this might be worth a look. It is also worth buying this if you are running Windows 11 Home on your machine, because the Pro has some features which are very useful, including virtualization.
The latest issue of Raspberry Pi Magazine is now out. You can find it here. It’s got a couple of articles by me in it, which is rather nice. The first one is all about maximising your coding skills by learning new things about Python and taking a look at other languages - JavaScript and Rust. The second one describes how to build a Raspberry Pi PICO powered cable release with built in self-destruction detector. The magazine is a great read, and the new layout is really nice. Well worth a read. You can buy it online or you can find it in newsagents.
You might be wondering about the best way to use ChatGPT (other large language models are available) to learn how to write programs. Here’s what I think.
This is a great idea if you know how to do a job but are just too lazy to do it yourself. If is a bad idea if you want to learn from what ChatGPT will produce.
In my experience requests to make code usually result in something around 95% right. You can fix the first few faults by telling ChatGPT what it has got wrong. But there comes a point where this no longer works. ChatGPT will claim it has fixed things, but the program still won’t work. A skilled coder can have fun finding the remaining issues and fixing them. A learning coder will just get frustrated.
You are on much more solid ground if you ask ChatGPT to explain something you don’t understand. Ask it what “NaN” means in JavaScript, and you will get a good explanation, some examples and suggestions of things that you might want to know about next. This can make for great learning journeys. And at any time, you can ask it to summarize what you have learnt so far or ask you some questions you can use to check on your progress.
When ChatGPT is asked to explain something, it just has to find the information it has obtained from lots of sources - including my writings ☹ - and then summarize it. ChatGPT doesn’t have to make anything original. You can be a lot more confident that what it says is correct, since it hasn’t really had to originate anything.
You can use ChatGPT to test your understanding of something. This is very useful for two reasons. Firstly it gets you used to explaining what you know. Secondly it provides useful confirmation or critique of what you understand and how you expressed it. Start with “As I understand it….” and then go on to set out what you think you know. If you have a question about this, add it on the end. This gives the AI plenty of raw materials to come up with a useful response.
It turns out that ChatGPT is very good at figuring out what code does. If you want some comments on your approach or better ways to do things, paste the lot into ChatGPT and see what it says. You can then ask ChatGPT to expand on its comments and make more suggestions. If you want to learn a new graphical framework or library (or even language), paste your code into ChatGPT and ask it to generate a new program with the required changes. You can even get ChatGPT to write documentation for you. Or at least produce a first draft you can fiddle with.
Took the big camera to Beverley today. I’m not unhappy with the results.
The next Hardware Meetup will be on Wednesday 30th April starting at 5:00pm in the Hull Makerspace in the Central Library in Hull. I’ll be bringing some bits and bobs along. If you have something you want to show off, or got questions about something you want to make, it would be lovely to see you there.
I’ve been away for the last couple of weeks having the full theme park experience. Above is a picture of an installation at the resort where we stayed for the first week. The various hotels were all themed, this one was all about the 1990’s and was adorned with a huge laptop from the era.
They also had a rather large Mickey Mouse telephone. It was a great holiday. Expect slightly fewer Python posts and more Disney/Universal ones going forward.
The heading is doing most of the heavy lifting on this post. But I’d just like to add that the session starts at 6:30 pm and ends at 8:30 and is in the Brynmor Jones Library at Hull University. There are two talks, one from Ben Foster about integrating large AI models with smart home systems and another from Alistair Kennedy about Cybersecurity.
You can sign up here. The last session I went to was excellent.
We can use Python formatted string literals to easily drop values into strings of text. But we can also have even more fun with them when we add extra information to control the formatting process. This is particularly useful when we are creating messages to be displayed on devices with particular dimensions.
For example. perhaps I want to display the time on an LCD panel. I want the hour and the minute to have leading zeroes (so that they look like a digital clock). I could write some cunning code to add the zeroes if the values are less than 10, or I could use a formatted string:
hour=5
minute=6
These are our time values. If we want to make a digital clock out of them we just need to do this:
time = f"{hour:02}:{minute:02}"
Note that each expression to be output is now followed by a colon and a couple of digits. The first digit means "Pad the number with zeroes". The second digit means "display the value in two spaces". So the time string is set to:
05:06
If we don't want to pad the number with zeros, but use spaces instead, we just omit the leading zero.
f"{0:10}"
This string literal ends up being 9 spaces followed by a single zero.
We can add a decimal point to our formatting values to control the number of decimal places displayed when using floating point values:
x=f"{3.141592653:10.3f}"
This would create a 10 character long string with a three decimal place value of the high-precision value of Pi:
' 3.142'
Note that the f after the 3 in the fornmat (the number of decimal places) tells Python that you are outputting a floating point number and it must have three decimal places. If you leave out the f Python assumes you are outputting a general value and will just display three digits (3.14).
Things get even better when we start using control characters to select the text alignment:
f"{'Menu':^10}"
This would centre the word Menu in a 10 character string. Jolly useful if you want to line things up for a particular sized LCD panel. You can use < and > to left or right justify the string inside the field. And it gets even funner (if that is a word). You can even use variables to control this formatting behaviour:
display_width=16
banner=f"{'Menu':^{display_width}}"
The above statements make a banner which is 16 characters long and contains the word Menu centred in it. If you want to change the size of the display you are using, just modify display_width
It is worth getting good with format strings, they can save an awful lot of work.
Formatted string literals are amazingly useful. You can make your Python output look really good, whether you are printing on the terminal or assembling a message to be displayed on an embedded display.
There are several ways to create formatted literals. I like the “f-string” approach which works in later versions of the language, including MicroPython and CircuitPython.
Let’s start with some values we might want to output:
name="Rob"
age=21
We can create a formatted string to display this values as follows:
message = f"Name:{name} Age:{age}"
The f in front of the string tells Python to scan the string and look for braces (curly brackets) that identify expression values to be included in the string. The values of name and age are inserted into the string in the specified positions. If you actually want to put braces into your text you have to enter two of them:
age_braces = f"{{{age}}}"
This would create a string containing the value of age enclosed in braces.
A variable is a box you can name to put something in. In Python you can just make one:
age=21
Python decides it needs to make a variable (age is not a word Python knows), looks at 21, decides that it needs an integer shaped box, makes one, puts 21 in it and then paints the word age on the box so it can find age later. So far, so normal. Let’s make a function:
def brokenFunc():
print(age)
There’s a clue here that bad things are about to happen. If you run this you get:
UnboundLocalError: cannot access local variable 'age' where it is not associated with a value
This is because the body of a function (the indented bit after def) is a separate variable scope. The scope of a variable is those areas of your code where statements can work with it. Any variables created in a function body are local to the function and disappear when the function completes. Variables are either global (declared outside all functions) or local (declared inside the body of a function). If you want to be able to access global variables inside a function body you need to tell Python
def mendedFunc():
global age
print(age)
Code in the body of mendedFunc function can now access age (and also change it). Forcing programmers to explicitly state when they are using global resources in their code helps prevent errors which would be caused if the same variable name is used in more then one context. If one part of the program is manipulating the age of a person, and another part of the program is manipulating the age of the house they want to buy, we don’t want these getting mixed up when the program runs.
Rob Miles is technology author and educator who spent many years as a lecturer in Computer Science at the University of Hull. He is also a Microsoft Developer Technologies MVP. He is into technology, teaching and photography. He is the author of the World Famous C# Yellow Book and almost as handsome as he thinks he is.