Writing Your Odroid Project in Bash

I’ve purchased my Odroid-N2+ and installed the Ubuntu MATE operating system on an SD card. It works great!

One part of the SBC (Single Board Computer) is the GPIO (General Purpose Input & Output) pins, that can be used for sending and receiving signals to the devices the user develop.

An easy project for begginers can be found in Odroid Wiki wehn you select from the tree on the left side of your browser’s screen:

odroid-## -> application note -> gpio -> wiringpi

(replace ‘##’ by your Odroid model)

Or click here to go to the page.

Start by installing the ‘odroid-wiringpi’ as described at the beggining of the page.

Once you got to the example relevant to your device, you’ll see an image that will help you to locate the relevant pins on your device.

Note: the physical pin number differs from the WiringPi number.

The program you will find for turning the LED on and on is in C. With some changes I’ve made it looks as follows:

    #include <stdio.h>
    #include <stdlib.h>
    #include <wiringPi.h>
    #include <signal.h>
    #include <unistd.h>
    
    void sig_handler(int signo)
    {
        if (signo==SIGINT)
        {
            printf("\n\nExit\n");
            digitalWrite(0,LOW);
            exit(0);
        }
    }
    int main(void)
    {
        /* Connect to signal-handler, so the LED will not stay on
         * when the user presses <Ctrl>+C */
         
        if(signal(SIGINT, sig_handler)==SIG_ERR)
            printf("Cannot catch SIGINT\n");
            
        wiringPiSetup();
        pinMode(0, OUTPUT);
     
        for (;;)
        {
            digitalWrite(0, HIGH); 
            delay(1000);
            digitalWrite(0, LOW); 
            delay(1000);
        }
        return 0;
    }
 
    

You can use the same pin handling functions for other devices such as Raspberry Pi and Arduino.

Now, the hint for translating your program into Bash (GNU Bourne Again SHell) is the command found on the Odroid Wiki page:

gpio readall

The output of that bash commands looks as follows:

In the output you can see the mapping of each physical pin to the corresponding WiringPi(wPi) number to be used in a program, and its mode (for example Input or output). The table can help you decide what WiringPi pin number to choose if you translate a program written for another device into yours. For example, Raspberry PI uses BCM numbering, that does not appear in the table.

While trying to find how to access the pins from a Bash script, I found many examples with files under the ‘/sys’ folder. We don’t need them here. If the command ‘gpio’ takes parameters, then check if you can use others to set the pin mode adn to write to it using the command:

gpio -h

From the output, you can learn that you can set the pin mode, read from the pin or write to the pin using the ‘gpio’ command.

Following is the full script to blink the LED, and turn it off when the user presses

<ctrl>+C:

#!/bin/bash

function brexit () {
  gpio write 0 0
  exit 0
}

echo "Hello, world"
trap brexit SIGINT

gpio mode 0 out
while 
  true
do 
  gpio write 0 1
  sleep 1
  gpio write 0 0
  sleep 1
done

The ‘brexit’ function turns the LED off, and the ‘trap’ command associates it with the signal SIGINT (user break).

Command ‘gpio write <pin-number> <value>‘ sets the pin value to

  • 1 – HIGH to turn the LED on.
  • 0 – LOW to turn the LED 0

Advertisement

Making C Programmer-Friendly with GNU Libraries

Does anybody remember how to program in C? Well, its syntax is similar to that of languages such as Java, PHP and others, but C uses a data type named pointer, and an array is a pointer, too. When you define an array, the system allocates enough space for it, but when you use the array nobody keeps you from accessing elements out of that array. You will not get a “Subscript out of range” error with the file’s name and line number. What you’ll get instead is a segmentation fault or another variable’s value will change. Use a debugger to find where it happens.
The library GLib has functions that perform operations on data structures like in modern object oriented languages. For example:
Use GArray, and you can enjoy the function ‘g_array_sort’ that sorts using a user-defined comparison function. This structure also allows you to increase the size of your array without calling ‘realloc’ implicitly.
GSList and GList are the data types of singly-linked and doubly-linked respectively, and a ‘foreach’ functions are defined to perform the same operation on each of their elements.
Other data structures are hash-tables, balanced binary trees, N-ary trees, etc.

An interesting utility is the command line option parser, that will check the validity of values passed via the command line and pass the values to function and global variables defined by the user. In addition, this utility will define the ‘–help’ flag, which will print your program’s command line flags and their use.

Of course, this is not everything you can do with GLib, GLib also supports threads and processes, events, etc.

Read more here.

If you want to distribute products you developed using GLib, you might be interested in the license. GLib is distributed under the Lesser GPL license, which means that you can distribute proprietary and open-source software developed with this library.