Introduction: Onion Omega2 and Golang
Recently I bought an Onion Omega2 Plus. I might say that it is a nice piece of engineering.
It has 32 MB for filesystem (and the Omega2 Plus also has the possibility to add an SD card) and 64 MB RAM (for Omega2) or 128 MB RAM (for Omega2 Plus).
It has both advantages and disadvantages when compared with Raspberry for example:
1. It runs an OpenWRT system, thus you have integrated support for networking. (of course you also have it with Raspberry, but fiddling with it, can be easier in OpenWRT).
2. Its OS cannot be corrupted by power problems like it is the case with Raspberry
3. Lower power consumption
4. Smaller size
1. You cannot easily install development tools inside the board
2. You do not have all the options for development that you have with Raspberry
3. You will find it more difficult to find information about this board as it is not that popular as RaspberryPi is
Of course there might also be other advantages or disadvantages, but this is not the purpose of this instructable.
The first step I took when i received the board was to start it, since I did not read carefully, I did not know that this board does not have a standard port for power (like an USB one for example), so the final solution for me (because I did not have the patience to wait for a shield), was the picture you can see here.
A power regulator that takes the current from 4.5-9V to 3.3 V. You can also see in the picture a Saleae Logic Analyzer which was used for this material and a breadboard with a LED and a 100 Ohm resistor.
The board can be programmed with C, Python, PHP or Nodejs, pretty much out of the box, but I want speed and I also want features. Now, C would certainly give the speed I want, but it is kind of hard to create and maintain a webs server in C. The other languages, while perfectly good, do not have the speed I want.
Now, just to make this clear, all these languages are more than enough for a lot of projects, and I did not really wanted too much speed, but this is the reason I used to start on my Golang endeavor.
So, now, that I found myself a reason, I started looking for a good library in GO for Onion Omega2. The only good one I could easily find was a library that offers general functionality for filesystem mapped GPIO. But that is not very speedy and as I have already stated, I am a speed adict :).
I also wanted to give something back to the community so, I've decided to give it a try and create a fast-gpio library in Golang for Onion Omega2.
The result is: https://github.com/adiclepcea/go-omega2gpio
This library will offer you the possibility to access the GPIO pins in your Onion Omega2 directly from Golang. So now you can be fast with your pins and also have an easy to use embedded web server. Yeee.
There are some points here, the main one being that while fast, Golang still has some overhead as compared with C and only from Go 1.8 (i believe) it also supports MIPS as a target platform. Other than that, is much easier to use and it has a lot of integrated features that make it the ideal IoT alternative.
That being said, I have implemented a small PWM comparison with the native C implementation of fast-gpio and my implementation in Golang.
For both tests I have used a 220 HZ with 20 Duty Cycle PWM.
In the first image you can see that the C implementation obtained a frequency of 214.3 Hz as compared with the 208 Hz Golang implementation speed.
There is also the issue that the C implementation can only be stopped when the process stops. (after you run the command, if you run `ps` on the command prompt you can see that it still runs), while for Golang I have implemented StopPwm method. This method however add some overhead as it must check for a Stop request.
Also you must not consider this as final or official results because this processes will normally run in parallel with other processes and there is only one processor in this board.
Step 2: Small Demo
After all this technical details, let's see a small demonstration.
This is a demo I have made that shows a Go program running in the Onion Omega2.
Hope you like it.