I always wanted to build something with mruby. After building mruby on my Raspberry Pi, I wanted to use the serialport mrbgem to instruct my Texas TM4C123G with ARM Cortex M4. I used to program the controller using Keil IDE on windows. After getting a mac, I have been writing program into it using energia. It is as simple as programming an arduino. The pin diagram with numbers are in the figure for the use in energia.
It has a built in led connected to the pins 1, 2, 3 and 4 of Port F (PF1, PF2, PF3, PF4). Each one contributes a single unique color to the led. So, when we give power to more than one of these pins, we get different colors. It’s fun working with these colors. So I have been using a serial communication program in the board so that it glows different leds with different combinations of PF1, PF2, PF3 and PF4.
The code is here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
After this, I went for mruby to play with the serialport mrbgem to instruct the controller. For this we have to install mruby first by following commands.
git clone http://github.com/mruby/mruby.git cd mruby make
Then we have to add the path variable to .bashprofile file in the home of Raspberry Pi. Just add the line at the end of .bashprofile file.
Then after restarting the terminal, we can have all the necessary command line tools for mruby as mirb, mruby, mrbc. Let’s install the serialport and sleep mrbgems. First of all we need to install mgem (command line utility for mrbgems).
gem install mgem
Now, we need to add mruby-serialport and mruby-sleep to the known mrbgems.
mgem add mruby-serialport mgem add mruby-sleep
Then we need to generate our content of build_config.rb in the root folder.
Right after this command, we need to give inputs to the required field and after that it will generate the content of buildconfig.rb. Then just create a file called buildconfig.rb and paste the content generated into it. Like my build_config.rb looks like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
I have commented the mruby-string-utf8 as it created some problems while installing. Then to build this in the directory, just hit rake.
Then it will download the mebgems from git and will build it in the build directory. Before building, there are a few changes to be made in the mrbgems files. I found answers to my queries from the github repo of mruby-serialport. Just have a look here and set everything accordingly. After that use rake to build everyhting.
If it gets successful, we can write program (serialport.rb) to instruct our controller.
From a Ruby File
1 2 3 4 5 6 7 8 9 10 11
Provided you own the serialport file ttyUSB0 for the controller. Otherwise, do this.
sudo chown pi /dev/ttyUSB0
I am sending the characters from the list because I have programmed my board to receive these characters to glow certain leds. The Sleep::sleep(2) adds a delay of 2 seconds between consecutive writes of characters. To run this use the following.
And enjoy different colors glowing on the same led with a halt of 2 seconds.
From an Executable made using a C file
Then I wanted to run it with an executable built using the tool mrbc.
mrbc -Bserialport serialport.rb
It will create a file called serialport.c with an array called serialport. Just add the following at the end o f the serialport.c file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
After this run the following commands to make an executable.
gcc -I/home/pi/mruby/src -I/home/pi/mruby/include -c serialport.c -o serialport.o gcc -o serialport serialport.o -lm /home/pi/mruby/build/host/lib/libmruby.a
The above command creates an executable serialport. Just run it and it will do the same that the ruby code was doing before.
Happy Hacking. :)
P.S. - make sure the baud_rate from both the sides are same. :P