CIRCUITPYTHON FOR C HEADS: PARDON THE INTERRUPTS
- It presents the board as a regular mass storage device, so that development is as simple as using a thumb drive and a text editor. No intermediary programmer board or interface other than USB is needed.
- It handles variable types for you, so there's no need to declare a uint8_t or an int or a float or a double. Removing the need to consider the overall range of the number you're storing before you start handling it is a bit easier for beginners, hopefully leading to fewer overflow problems.
- The syntax is different - no more curly braces or semicolons. (We feel this is a debatable "pro" - some beginners do better with the structure that semi-colons and curly braces provide.)
- It handles strings & text parsing a heck of a lot better, without pointers. Pointers are a pretty abstract concept that takes a while to wrap your head around, and an even longer while to become second-nature. A downfall of C is how early they're needed to handle pretty mundane tasks that any beginning programmer wants to do - like display words on LCDs, or save and recall data.
Accessibility and approachability are important to us at Alpenglow, so for our next big project (currently codenamed Minimum Viable Cat…!) we took a look at approaching it from a beginner-friendly CircuitPython perspective. But we’re mostly oldhead C folks, so some concepts have taken us a hot minute to grasp or adjust to. And one such momentary mindfuck was something we’ve long taken for granted in C-world: Interrupts.
As you do, we started by searching for “circuitpython interrupts”, expecting helpful Stack Overflow pages or maybe some random yet magical forum threads. Instead we found a bunch of lengthy discussions in GitHub issues with titles like “no interrupts?” and “Asynchronous events: what are your use cases?” – not explaining how to use interrupts, but rather: confirming their absence. The feeling of confusion about the lack of this MCU mainstay was echoed in other discussions, such as “Anyone have any guidance on working without interrupts?” on Reddit. Numerous references to interrupts in MicroPython were forthcoming, and CircuitPython is just a fork of that, right, so … ? But the CP docs confirmed:
Concurrency within Python is not well supported. Interrupts and threading are disabled. async/await keywords are available on some boards for cooperative multitasking.
under “Differences from MicroPython.” Reading all the way to the bottom of issue 4542, however, we found a link to Cooperative Multitasking in CircuitPython with asyncio! asyncio brings cooperative multitasking with the async and await keywords paradigm found in other languages such as JavaScript and C#. While we don’t claim to be Python experts by any stretch of the imagination, the concept of event-style tasks was familiar from front end frameworks and other paradigms. Here’s how we used async/await to effectively run a servo and vibrator motor concurrently.
note that the adafruit_motor, asyncio, and adafruit_ticks libraries need to be added to the lib folder in order to run the above cody.py