Programming Projects: March 2021
Tags: programming, python, bbcmicro, c
Published: 2022-10-25 23:41:16

---
title: Programming Projects: March 2021
published: true
date: 2021-04-04 21:48:06 UTC
tags: programming,python,BBCMicro,C
canonical_url: https://www.ericsbinaryworld.com/2021/04/04/programming-projects-march-2021/
---

I started off the month thinking it was going to be Python heavy and ended up doing a lot more micro-controller programming. To be fair, I was mostly programming in CircuitPython, but it definitely came out of nowhere.

## Python

### Civilization VI Webhook in FastAPI

Last month I created a webhook program in Flask to generate notifications when it was someone’s turn in our multiplayer Civilization games. When I posted about it on reddit, someone suggested that I would be better off using FastAPI instead of Flask. I’d been hearing about FastAPI for months on _Talk Python to Me_ and _Python Bytes_, but I always got the impression that it would only be useful if I was writing my website to be async. 

Instead what I got was a much cleaner interface, an automated documentation system, and better use of types to validate data. I’m only using a fraction of what’s nice about FastAPI and I find it much nicer to use for this use case than Flask. After I got it working as well as when I was using Flask, I started working on enhancements and bug fixes. 

The work that drove me nuts until I figured it out was how to clean up my code (that is, refactor to reduce code reuse) and still maintain the ability to not have duplicate notifications from the Civilization Play By Cloud service. It turned out that I needed to be faster than 0.01 seconds to handle the code. So I had to write the new turn to the dictionary that’s functioning as a sort of bootleg database BEFORE posting to Matrix. It was quite satisfying to figure that out.

I then focused on moving some items (like usernames) out to config files so that it would be easy for someone to take my code and use it for themselves. I’m almost done with that. Afterwards I’ll create a PyPi package.

[The github repo is here](https://github.com/djotaku/Civilization_VI_Play_By_Cloud_Webhook_with_FastAPI).

### Django 2 by Example and Prophecy Practicum Django Rewrite

On the other end of the Python web frameworks, I also did some work with Django. When I first tried to learn Django a few years ago it was just too complex for me to understand. I was trying to learn too many concepts at once. Now that I understand decorators and, thanks to my work with Flask and FastAPI, the concepts of routes, it’s a lot easier for me to learn Django. The MVC framework still separates things a lot, causing a higher cognitive load. However, I can see the beauty in how it separates things and makes it easier for teams to work on the code. 

Once I had enough of the basics under my belt, I took another look at the Prophecy Practicum app I was writing for a friend. My work on creating it with Flask ground to a halt under the weight of all the extra work I was doing to develop things like an admin view. Eventually, I realized that Django may be a heavy framework for the way a lot of web development is done nowadays (feed APIs to phone apps and Javascript libraries), but for what I wanted to do, it would reduce a lot of the work I was doing because it came with those libraries built in. [So I’ve been able to almost get to a working first stab at things with only a couple hours’ work.](https://github.com/djotaku/prophecypracticum_django) It’s a lot nicer to focus on what I want the app to do rather than having to build up a bunch of scaffolding. Also, I love the way it handles creating databases from classes.

## CircuitPython

### Play Dough Piano

When reading through the Adafruit Python on Microcontrollers mailing list, I found out about a [Play Dough Piano](https://docs.google.com/presentation/d/e/2PACX-1vR6tV6hCx5HcGsltV0YrDIwP1oSciHhdDAZ-lyFkfz3qhXPqN4qbQrmDQF5ltQnnvsGcLeEBWj9-TP5/pub?start=false&loop=false&delayms=3000#slide=id.gbeb10f6ca8_0_103) (unfortunately I couldn’t figure out how to download that presentation). I copied their CircuitPython code since I don’t have any experience with CircuitPython HID. But I developed the Scratch code on my own thanks to all the experience I gained last year with Scratch. I also used my recent experience with soldering to add the pins to the [QTPy](https://www.adafruit.com/product/4600). If you decide to do this yourself, it’s important to note that you cannot use a breadboard because the capacitive touch pins on the QTPy reads as a key being pressed. The kids had a lot of fun and it finally gave me an idea of what to make with a QTPy. I bought 2 of them when they were half off during John Park’s show on Adafruit. [The code is available here](https://github.com/djotaku/scratch_qtpy_circuitpython_piano).

<iframe loading="lazy" title="MIT Scratch and Adafruit QTPy Play Dough Piano" width="640" height="360" src="https://www.youtube.com/embed/RZwRG4lCDbw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

### QTPy Streamdeck

![](http://www.ericsbinaryworld.com/wp-content/uploads/2021/04/PXL_20210321_152518089_v2-small_for_github.jpg)
_I don’t need a slick Streamdeck, just the ability to be able to take a few actions while in-game._

Working on the Circuit Python Piano with Sam combined with seeing someone make a Streamdeck using Cherry MX keys make me consider that perhaps I could use my [QTPy](https://www.adafruit.com/product/4600) to create a Streamdeck. I’d previously been focused on recreating the ElGato Streamdeck, so I was going to use a [PyPortal](https://www.adafruit.com/product/4116). However, a PyPortal is $50-ish and I don’t have a 3D printer to make an enclosure. Meanwhile I already had the QTPy and some buttons. I didn’t need the buttons to change or have images on them, so I went ahead and created a nice, simple Streamdeck. My first version only worked if I had OBS in focus which defeated the purpose. I was using key.send. Afterwards speaking to someone in the Adafruit Discord, I learned that I had to do press and release. Now it works awesomely and I can start recording and pause/unpause the recording without leaving the game.

[The repo is available here](https://github.com/djotaku/qtpy_streamdeck).

## Microsoft MakeCode

### Sam’s car

I got tired of waiting for the [BBC Micro:Bit V2](https://www.adafruit.com/product/4781) to be in stock at Adafruit. It’s the same price as the [BBC Micro:Bit V1](https://www.adafruit.com/product/3530), but with more features, so it felt silly to get the earlier version. But I got tired of not being able to build the [CuteBot](https://www.adafruit.com/product/4575) with Sam, so I bit the bullet and bought the version 1. It was very easy to program in Microsoft MakeCode because there are special blocks for programming the car. We were able to program the car to “dance like a bee”, avoid obstacles, and follow a path. The latter was particularly impressive to the wife. The kids were amused that their stuffed penguins were invisible to the car’s eyes. That is, when in obstacle avoidance mode, it would still crash into the penguins. I thought that was a little odd myself. But then I did some research and realized the “eyes” were an ultrasonic sensor. Since it’s using echo-location, it doesn’t work on furry objects – the same reason that padding is used to improve the acoustics in a room by dampening the noise. The object needed to be reasonably solid to let the car know it was there. I still have some more tinkering to do with Sam to add some more options to the examples the car’s manufacturer printed in the manual.

<video controls src="http://www.ericsbinaryworld.com/wp-content/uploads/2021/04/Cutebot.mp4"></video>

## C#

### GameDev.Tv Multiplayer class

[I finished up the online multiplayer video game class](https://github.com/djotaku/RealTimeStrategy_2) I was taking through GameDev.Tv. As I’ve said in previous updates – I now truly appreciate the work that goes into an online multiplayer game. It’ll be a while before I add multiplayer to the game I’ve been working on, but I’ll definitely be studying what I did in this class when the time comes. I highly recommend it if you’re considering making an online multiplayer game for the first time – especially if you want to do it over Steam.