The mraa functions that you need to make use of a pin in output mode are very simple.
mraa_gpio_init (mraa_pin)
To set the pin up using mraa number.
mraa_gpio_dir (pin,dir)
To set the pin to output.
Finally we have:
mraa_gpio_write (pin, value)
which sets the line to high or low
Connecting our Galileo board to drive external LEDs.
Things you will need
– Breadboard
– Some Wire
– LED
– Resistor 330 Ohms
Connect your 3 LEDS to Intel Galileo digital pins:
10, 9, 8 & the GND from the supply pins.
Now we can start to write a program, Create a file, called gpiodemo.c
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa/gpio.h>
sig_atomic_t volatile isrunning = 1;
void sig_handler(int signum);
int main ()
{
signal(SIGINT, &sig_handler);
mraa_init();
mraa_gpio_context led1;
mraa_gpio_context led2;
mraa_gpio_context led3;
led1 = mraa_gpio_init(10);
led2 = mraa_gpio_init(9);
led3 = mraa_gpio_init(8);
mraa_gpio_dir(led1, MRAA_GPIO_OUT);
mraa_gpio_dir(led2, MRAA_GPIO_OUT);
mraa_gpio_dir(led3, MRAA_GPIO_OUT);
while (isrunning) {
fprintf(stdout, “LED 1\n”);
mraa_gpio_write(led1,1);
mraa_gpio_write(led2,0);
mraa_gpio_write(led3,0);
sleep(1);
fprintf(stdout, “LED 2\n”);
mraa_gpio_write(led1,0);
mraa_gpio_write(led2,1);
mraa_gpio_write(led3,0);
sleep(1);
fprintf(stdout, “LED 3\n”);
mraa_gpio_write(led1,0);
mraa_gpio_write(led2,0);
mraa_gpio_write(led3,1);
sleep(1);
}
// reset all LEDS to 0
mraa_gpio_write(led1,0);
mraa_gpio_write(led2,0);
mraa_gpio_write(led3,0);
// close all gpio pins
mraa_gpio_close(led1);
mraa_gpio_close(led2);
mraa_gpio_close(led3);
fprintf(stdout, “\nDone\n”);
return MRAA_SUCCESS;
}
void sig_handler(int signum) {
if (signum == SIGINT)
isrunning = 0;
}
Compile the code using the Galileo toolchain:
$ gcc -lmraa —o gpiodemo gpiodemo.c
Next, we will try to execute the resulting binaries
$ ./gpiodemo
The program will enter into a forever while loop, the only way to exit from it would be to send a SIGINT to it. You can either use the *Nix $kill or simply pressing CTRL+C on the active program terminal will issue a Interrupt signal.
Leave A Comment?