Close

Reprogram the code in Golang

A project log for Prometheus Alarm Clock

Possessing both the beauty of the bygone analog age and the convenience of cutting edge tech, Prometheus is the ultimate alarm clock!

andrew-leeAndrew Lee 04/08/2017 at 19:220 Comments

Hey guys and gals!

So in order to be able to play music on my speakers through my Pi, I am installing and running a program called Shairport-Sync (Github). It hosts a Airplay server on the Raspberry Pi, and allows the playback of music via the network interfaces on the Pi. (I dropped the idea of using Bluetooth since the technologies didn't play too well on my Raspberry Pi).

However, then I started getting problems where my server wouldn't stay up for more than 24 hours. Initially, I thought it was my code (although today I figured out it was Shairport-Sync that was causing the issues since I was using the wireless interface to host both the UI for the clock and the Airplay server, and the server was preventing me from using any ssh/http/vnc services through whatever interface it was enabled for. Hence, the quick fix was to disable Shairport-Sync from using wireless connections and make it wired connection only, and now all wireless functionality is restored on my project while retaining the ability for me to play music via an ethernet cable. One caveat is that now I can't use SSH/VNC using a wired connection which is really unfortunate, but a worthy sacrifice)

Hence, through this misunderstanding, I decided to recode this project in golang. First a background for those of you that don't know about golang. It's a statically compiled modern language invented and used by Google since 2007. And all I can say is that it is AWSOME! Being used to the idiosyncrasies of C++, I was under the impression that all languages that are fast require idiomatic and low level handling. However, golang really removes alot of the formalities associated with compiled languages, and natively supports concurrency, easy syntax, and garbage collection (memory management) built in.

Hence, I was able to recode both the server functionality was well as about 80% of the hardware functionality in about a day!

Although I am having some issues, these are probably from my lack of understanding of the language, and I believe in due time I will be able to complete the project.

If you doubt the speed of golang versus a Python + Node.js setup, here are some benchmarks between the three languages.

Golang vs Python 3

Golang vs Node

Although Golang and Node's performances are comparable (Golang is about 3x faster in most cases) the real kicker is how much faster Go is compared with Python 3 (for most tasks Go is 20x-100x faster than Python!!). Now node is actually also compiled at runtime (Node being another brainchild of Google), but Golang's real performance increases come from the fact that it is compiled to machine code and uses native data types (much like C or C++) which is why an interpreted language like Python never had a chance!

Furthermore, although my previous code was "good", it was using 2 different programs to do 1 thing. Hence, by combining the functionality into one program, there would also be a performance boost. (In my previous set up, I used JSON files as the intermediate communication medium between 2 programs. Currently, I am only using the JSON files to save the alarm states when the program closes).

Although I am sure it is possible to write low level code to directly access the GPIO pins, to make my life a little easier, I utilized a package that creates GPIO structs that I can send High and Low commands to (corresponding to outputting true or false).

Furthermore, I modified the UI to have separate post calls on each On/Off toggle (rather than having to submit all changes in the bottom of the page).

Overall, I think this efficiency is a good thing (as it would allow me to use the limited computing resources of the Pi for other things on top of running the alarm clock).

Discussions