SWIFTUINO — Arduino Circuit Simulator for iPad in Swift Playgrounds
Before writing the story of how it came to be I should first stick to what it is :) As the title states it is an Arduino simulator (with true and full simulation capabilities) built for iOS, built in Swift and adapted for the Swift Playgrounds app. Considering the likely readership there’s no need to explain Swift being the alternative programming language besides Objective-C for iOS applications. It’s also been open sourced but this is not our story here. Our story is more the essence of Swift Playgrounds being a Mac OS and more importantly an iPad app that is capable of running arbitrary code that you can change and tweak and compose and that recompiles and reruns on the fly. Apple being so attentive to security even at times reducing developers’ choices and taking away convenience features to always improve security and protect users a tool like Swift Playgrounds was something I personally didn’t expect from Apple. On the other hand there’s obviously a more important and equally strong force from Apple to create a developer tool for young developers to educate and inspire them to understand what coding and app development is about and even to usher them into the world of hardware programming, IoT and of course robots. Because robots are the ultimate attractor. After all, who doesn’t like robots. Swift Playgrounds is a very innovative initiative from Apple. I’d rather quote them instead of trying to paraphrase
Swift Playgrounds is a revolutionary app for iPad that makes learning Swift interactive and fun. It requires no coding knowledge, so it’s perfect for students just starting out. Solve puzzles to master the basics using Swift — a powerful programming language created by Apple and used by the pros to build today’s most popular apps. Then take on a series of challenges and step up to more advanced playgrounds designed by Apple and other leading developers.
To bring it into some perspective Swift Playgrounds has the ability to annotate your code so it renders like a book, reads like a book, acts like a book with chapters and pages (lessons) but compiles and executes like code. True compiled code, not a web based emulator like so many other tools that teach programming. Web based teaching of coding has its advantages like being very accessible and visual at the same time but personally I find some magic about compiled coding on the fly with its true unbounded nature.
OK, so that out of the way let’s talk about hardware development and programming. Hardware is hard they say and with good reason. Let’s see how you learn hardware. Someone took some hardware components including a microcontroller and created a schematic and made it into a PCB and made a development board. They chose hardware components that they tested on a breadboard with carefully chosen other components to maximize the chance of those components working together seamlessly. But this was just half of what they had to do. Microcontrollers need software too or as more often called firmware. Microcontroller vendors used to create their own software development environments or software development kits that enabled you to create firmware. Often times they required developers to sign up as a developer, commit resources and maybe even pay license and other fees for the privilege of writing software for that microcontroller environment. This was somewhat prohibitive for individual tinkerers and hobbyists to get started with hardware development. Then came along Arduino. They did everything required to help people get started easily with hardware development. There are genuine Arduino development boards as well as Arduino compatible boards for different kinds of microcontrollers. But what brings all those together is the Arduino integrated development environment or IDE. All microcontrollers work according to the same principle. As far as development goes you initialize once (in Arduino with setup() function) and then keep repeating the same code ( loop() function ) where you read values and do your logic and write some values. This basic principle is universally true with caveats like callback functions that do asynchronous processing of events that can occur at any time and require immediate attention from the microcontroller but those can very well be handled in the Arduino environment as well. The other great thing about Arduino is that it is not a higher level scripting language on top of the real development language but it is essentially C/C++. It just simplifies the functions that you need to fill with your own code in order to do something. Since Arduino has already had many years of history the community has created a lot of libraries that take away the complexity of having to know all the details of sensors and other components or to do complicated calculations like sensor fusion. The libraries again simplify what you need to do as a developer in order to achieve something useful.
It all sounds increasingly better and better however if you ever tried to replicate what’s written in an Arduino example with real hardware and the IDE it’s still likely that you’ve encountered a disappointing experience. You tried your best to follow the instructions to connect the components, entered the exact code into the IDE, compiled and uploaded your sketch but in the end it refused to work, didn’t do what was expected or even created some magic smoke if a polarity happened to end up reversed. If you haven’t tried it this should not discourage you from trying. Just know that the positive outcome is far from guaranteed. There are so many little things that can go wrong with hardware development even with the simplest examples that the road ahead is definitely winding. And looking back from multiple years of experience it does not really get better as you approach more complex challenges. It remains a struggle although a very rewarding one. It’s hard to imagine the feeling of something physical actually doing what you wanted it to do. It’s a lot more elevating than just software.
You get the picture. It can be very rewarding in the end but it will also cause a lot of grief along the way. Not all of us are willing to hold out and more importantly it’s easy for the beginner no matter with how much resolve and commitment to get discouraged very early on. And this brings me back to my original point of how to successfully teach hardware. You need to satisfy a couple of things that are hard to satisfy. Short attention span, need for instant gratification, fear of failure are all likely traits of a beginner learner. On the other end if success can be guaranteed then inspiration and gratification can take a beginner learner a long way in overcoming challenges. So if anyone can say “fight fire with fire” then allow me to say: Don’t teach hardware with hardware. Arduino took it to an extreme on the road of almost guaranteeing success. Let’s take it one step further actually guaranteeing it.
Swiftuino didn’t come to be as the result of this train of thought. Reality is not like that. There are a lot of random factors at play. The actual factors were.
- Swift is roughly a simplified C/C++ much like Arduino so it was easy to see the parallel
- Although iOS is not as freely multi-threaded as you’d hope there are ways to run things like loop() and simulation at different speeds
- For arbitrary code execution, change, recompile, rerun Swift Playgrounds unexpectedly turned out to be a near perfect tool
- Swift playgrounds’ interactive living book concept added a lot of extra dimensions to my original idea and implementation of an Arduino simulator for iOS
Here’s a bit lengthy video of 4 lessons from Arduino built in examples