Python Shorts 04: Tuple of One

We’ve seen that we can create a tuple by using brackets to “squish” things together:

x = (“Rob”,21)

Tuples are very useful if you want to return multiple values from a function:

return (“Rob”,21)

This lets me return name and age values. However, sometimes you might want to return just one item in a tuple. You might think you do this:

x = (1)

However, this doesn’t work. Python thinks that the brackets are part of an expression, gets rid of them and puts the value 1 in x. If you want to make it clear to Python that you are making a tuple, put a “spare” comma on the end:

x = (1,)

This would “tupleise” the value.

Python Shorts 03: Tuples

Tuples should be called “lumps”. You can squish things together to make a lump. Tuples let you squish data together.

x = ("Jim",25)

The statement above creates a tuple called x which contains name and age information. Tuples can contain lots of data. The best way to unpick them is to use a cunning assignment:

(name,age)=x

This creates variables called name and age which hold “Jim” and 25 respectively.  Another way to get values out of a tuple is to index it:

name=x[0]

age=x[1]

The item at the start of the tuple has index value 0. Tuples are read only:

x[0]="James"

The statement above tries to change the in the x tuple. This will not work. Tuples are great for passing lumps of data around a program but no good for things you might want to modify. If you want to be able to change things in a lump of data you need use a list.

I find tuples very useful, but I do worry that the thing making the tuple and the thing using the tuple have to agree on the order of the elements. As we saw with name and age in function parameters this might lead to confusion. If you want both ends of a conversation to be absolutely clear about which data means what, you can use dictionaries to do that, but they are coming later.

Python Shorts 02: Default parameter values in functions and methods

We’ve seen how to make parameters clearer in calls, now let’s add some magic to the function itself. Sometimes we might only have the name of the person, can we make an add_person which will handle only one input? Yes, we can.

def add_person(name, age=3):
   print("Adding a person")

The code above creates an add_person function which has name and age parameters. The age parameter is set to 3 if it is missed out. Programmers call the value 3 the “default” value.

add_person(name="Simon")

This would add a person with the name Simon with age 3. You may be wondering why I picked 3 as the default. If we were writing a system to control access to fairground rides, we want to make it as safe as possible. Simon would only be able to go on rides which are suitable for 3-year-olds. If they want to go on the bigger rides, they must give their age.

add_person(name="Rob")

Note that if you want to use default values like these you must put the parameters with the default values at the end of the parameter list when the function is defined.

Get to sleep using your kitchens

If you are having bother getting to sleep, spend some time walking down memory lane thinking about kitchens. Works for me. Thinking about stuff that hasn’t happened yet gets the brain whizzing with plans and things to do. But thinking about stuff in the past is quite different (at least for me). So at the moment I’m working my way through the various kitchens we’ve had over the years. The memories are all good ones - like when we used to have a high chair and baby food all over the floor.

if you’re having trouble getting to sleep think about something that you liked doing in he past and see if it works for you..

Python Shorts 01: Named parameters in functions and methods

Welcome to the first “Python Sort”. I hope you find it useful. Let me know what you think in the comments…..

Python functions and methods can accept parameters that feed data into them. But once you have more than one parameter it can get tricky. For example, perhaps we have a function called add_person which adds a person to our system. The function accepts the name and the age of the person:

add_person("Jim",12)

This would add Jim to the system. But what happens if I get the call wrong?

add_person(12,"Fred")

The code and the name have been entered in the wrong order.  We are making someone with the name “21” and the age “Fred”. This will end in tears. The good news is that Python lets you specify the parameter name in the call:

add_person(name="Jim",age=21)

Now the destinations are explicitly defined and there is no chance of error and the code is much clearer.

Old tapes and scopes at the Hardware Meetup

Well, that was fun. Another retro-themed Hardware Meetup. Ross turned up with an oscilloscope and a tape recorder. After a bit of fiddling we got the tape recorder playing music from the 1970’s. Then we took the top off.

Old devices had a huge number of moving parts in them. Most of which still moved.

The oscilloscope worked too. So we are calling the evening a success. I’ll post details for the next meetup when we’ve recovered from the excitement of this one….

Python Shorts

I’m trying something new for April. I’ve got a whole bunch of tiny posts about Python which I’m going to be releasing over the next couple of weeks. Each one of them reveals a facet of the language that you might or might not know. If you know it, never mind - there’ll be another one along the next day. They start on Friday.

If there are any specific Python topics you’d like to know more about, post in the comments and I’ll see what I can do.

M5Stack Large Language Model

On the left is the Rabbit, then the LLM and then the Core2

For the price of a video game you can now get a device running a Large Language Model. Its made by M5Stack who seem to specialise in taking interesting technology and packaging it up for us to play with. This means you can have a self-contained unit the size of a matchbox which can understand and answer your questions without needing a network connection. Everything happens on the device. Embedded ChatGPT is becoming a thing. Although it is not quite as good yet.

In the UK you can get one from PiHut. At time of writing they had six left. Search the store for LLM. Things get a slightly more expensive when you realise that you also need an M5Stack Core module to control the LLM, but you might already have one of those lying around. I got myself a Core 2 device because most of my M5Stack Core modules are pretty old. The Core sits on top of the LLM and provides a touch screen and display. The LLM also has a microphone and speaker with “wake word” support.

The LLM device contains a fairly powerful CPU (which apparently came from some night vision goggles) paired with 4G of RAM and 32G of storage. It delivers 3.2TOPs (whatever that means) and comes with the Qwen2.5-0.5B LLM pre-installed. Other models are promised for later.

The M5Core device fits on top of the LLM and connects to a standard M5STack multi-pin connector which sticks out of the LLM. As the name implies you can stack other layers underneath if you want to. I’ve seen pictures of a base for the LLM which contains a wired Ethernet port but I’ve not seen it for sale anywhere. The LLM will unit will run freestanding, although I’ve no idea how you would get it started. You tell the LLM what to do via a serial connection. The M5Core device on top can run Arduino or UIFlow programs to send commands.

I had all kinds of fun getting the fancy UiFlow programming environment to work on my M5Core 2 so that I could use it to tell the LLM what to do. First I had to flash the firmware for Flow 2.0 into the Core 2. Then I had to bind it to my account, then I had to browse for it in the IDE, then I had to find that the device wasn’t recognised, then I had to scratch my head for a while and then I had to install the earlier version of the firmware to finally get it to work.

Then I had to enter the UiFlow block programming code. For some reason the sample programs are tightly bound to the M5 S3 version of the Core and if you try to change this it throws everything away. Very frustrating. Then I found that simply by connecting Thonny to the device and deleting boot.py from it you can get to write and deploy MicroPython and it just works (although resetting it can be a pain). The UiFlow environment is just a wrapper for a bunch of MicroPython. You can ask the IDE to show you the Python source, copy it out of the web editor, paste it into Thonny and away you go. The only change I had to make was to configure the serial connection to the LLM module:

llm_0 = LlmModule(2, tx=14, rx=13)

These are the numbers that worked for a Core2 device. When I finally got it running the results are pretty impressive. The model is small and it doesn’t know a huge amount. It thinks the earth is one million miles from the sun. However, it does work and response times are fine. I’ve printed a base from here and fixed it to the bottom of the stack so it now looks fully formed. I have to power it from a USB power supply but it works fine from that. At the moment I’m running a very simple Python script in the Core2 but I plan to add a bit of richness.

Core built with the LLM

So, around a year from getting the Rabbit (which uses a network connection to a big server) I now have a tiny battery powered device which does everything internally. No network. Blimey. Much more fun than a video game.

Just present

Something to remember: Presenting in front of people is a skill. Give an engineer a new thing to work with and ask them to use it and they will just do that. So treat presenting as a tool you need to learn how to use. Apply yourself to it in the same way. Have a go, learn from your mistakes and then have another go. You don’t have to look very far to find someone to present in front of. You could take it in turns next time you get together with a bunch of like minded souls.

Don’t make the mistake of thinking that presenting in front of people is a skill that you are born with. It is something that you can learn. And if you are born to be good at presenting (lucky you), applying yourself to your craft has got to be a good idea.

Long articles are easier to write

I’ve got an 4,000 word article to finish by the end of the week. I’d much rather it was 20,000 words. It turns out that longer articles are much easier to write. You just have to churn out the words. For a shorter article, where you want to get the same points out there, you have to write the longer one first and then figure out which bits can be thrown away.

Chord Keyboard Trainer

ChatGPT is officially amazing. I wanted to make a chord trainer for my PICO powered chord keyboard. So I pasted the Python source for the device into ChatGPT and asked it to make a JavaScript program that could train a user on the chords. And, after a fair bit of discussion we’ve managed to make the program I wanted.

We did have some fun. ChatGPT was determined to put linefeed characters into string literals rather than ‘\n’, which made the program break a few times. The worst bug we found was that when you are entering text into the game and press the space bar this can re-trigger the “Start New Game” button to very confusing effect.

However, it now works. If you want to test your Micro Writing skills you can have a go. If you type a letter quickly, before the help layout appears, you get 5 points. Otherwise you only get one. You can use the other buttons to get a complete list of the keys.

You can find it on GitHub here. You can have a go with it here.

PICO Musicbox

Say hello to the PICO Musicbox. It contains a Raspberry Pi PICO and uses an M5 Stack Synth module to make the sounds. There’s also an OLED display in the mix along with a rotary encoder. And of course some neopixels. You can see inside the box here. I’ve spent the last couple of days dropping out Circuit Python to make it all work together. If you fancy building one I’ll have the circuit diagram and software available soon, along with the STL files for the case.