Commit da41f9f8 authored by EJ Finneran's avatar EJ Finneran
Browse files

Merge branch 'sample-code' into 'master'

Sample code

See merge request !1
parents ec9468e4 c898db94
all: led_test.c gpio-utils.c gpio-utils.h
cc led_test.c gpio-utils.c -o led_test
Example showing how Vesta Gateway's user button and LEDs can be
controlled from a C application running on the Gateway.
See the section at the https://developer.rigado.com/docsets/Working-with-the-Rigado-Vesta-Gateway/v1.0.0 web stie titled "Accessing the Gateway's User Button and LED from C Code" for more information about how to build and run this code, as well as an explanation of how it works.
/* Copyright (c) 2011, RidgeRun
* All rights reserved.
*
* Contributors include:
* Todd Fischer
* Brad Lu
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the RidgeRun.
* 4. Neither the name of the RidgeRun nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
/****************************************************************
* Constants
****************************************************************/
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64
/****************************************************************
* gpio_export
****************************************************************/
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* gpio_unexport
****************************************************************/
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* gpio_set_dir
****************************************************************/
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/direction");
return fd;
}
if (out_flag)
write(fd, "out", 4);
else
write(fd, "in", 3);
close(fd);
return 0;
}
/****************************************************************
* gpio_set_value
****************************************************************/
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-value");
return fd;
}
if (value)
write(fd, "1", 2);
else
write(fd, "0", 2);
close(fd);
return 0;
}
/****************************************************************
* gpio_get_value
****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF];
char ch;
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1);
if (ch != '0') {
*value = 1;
} else {
*value = 0;
}
close(fd);
return 0;
}
/****************************************************************
* gpio_set_edge
****************************************************************/
int gpio_set_edge(unsigned int gpio, char *edge)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-edge");
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
/****************************************************************
* gpio_fd_open
****************************************************************/
int gpio_fd_open(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0) {
perror("gpio/fd_open");
}
return fd;
}
/****************************************************************
* gpio_fd_close
****************************************************************/
int gpio_fd_close(int fd)
{
return close(fd);
}
/* Copyright (c) 2011, RidgeRun
* All rights reserved.
*
* Contributors include:
* Todd Fischer
* Brad Lu
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the RidgeRun.
* 4. Neither the name of the RidgeRun nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __GPIOUTIL_H
#define __GPIOUTIL_H
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64
int gpio_export(unsigned int gpio);
int gpio_unexport(unsigned int gpio);
int gpio_set_dir(unsigned int gpio, unsigned int out_flag);
int gpio_set_value(unsigned int gpio, unsigned int value);
int gpio_get_value(unsigned int gpio, unsigned int *value);
int gpio_set_edge(unsigned int gpio, char *edge);
int gpio_fd_open(unsigned int gpio);
int gpio_fd_close(int fd);
#endif
/* Copyright (C) 2017 Rigado
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include "gpio-utils.h"
#include <errno.h>
#define USER_BUTTON 138
#define TIMEOUT -1 //forever
#define redled_dc "/sys/class/pwm/pwmchip5/pwm0/duty_cycle"
#define redled_period "/sys/class/pwm/pwmchip5/pwm0/period"
#define greenled_dc "/sys/class/pwm/pwmchip1/pwm0/duty_cycle"
//duty cycle in nanoseconds
const char period_1sec[] = "1000000000";
const char led_flash_1[] = "500000000"; // .5sec
const char led_flash_2[] = "950000000"; // .05sec
const char led_off[] = "1000000000"; // 1 sec
const char led_on[] = "0000000000"; // 0 sec
const char period_tenth_sec[] = "100000000"; // .1 sec
const char led_flash_rapid[] = "95000000"; // .005 sec
void next_led_pattern()
{
int fd_red_dc;
int fd_green_dc;
int fd_red_period;
static int state = 0;
fd_red_dc = open(redled_dc, O_WRONLY);
fd_red_period = open(redled_period, O_WRONLY);
fd_green_dc = open(greenled_dc, O_WRONLY);
if( fd_red_dc == -1 || fd_green_dc == -1 || fd_red_period == -1)
{
perror("Cannot open output file\n");
close(fd_red_dc);
close(fd_red_period);
close(fd_green_dc);
exit(1);
}
if(state == 0)
{
printf("Button pressed.\n pwm1 period=1 sec, duty cycle=100%\n pwm5 period=1 sec, duty cycle=50%\n");
write(fd_green_dc, led_off, sizeof(led_off));
write(fd_red_dc, led_flash_1, sizeof(led_flash_1));
state = 1;
} else if(state == 1)
{
printf("Button pressed.\n pwm1 period=1 sec, duty cycle=50%\n pwm5 period=1 sec, duty cycle=100%\n");
write(fd_red_dc, led_off, sizeof(led_off));
write(fd_green_dc, led_flash_1, sizeof(led_flash_1));
state = 2;
} else if(state == 2)
{
printf("Button pressed.\n pwm1 period=1 sec, duty cycle=100%\n pwm5 period=1 sec, duty cycle=95%\n");
write(fd_green_dc, led_off, sizeof(led_off));
write(fd_red_dc, led_flash_2, sizeof(led_flash_2));
state = 3;
} else if(state == 3)
{
printf("Button pressed.\n pwm1 period=1 sec, duty cycle=100%\n pwm5 period=.1 sec, duty cycle=95%\n");
write(fd_red_dc, led_flash_rapid, sizeof(led_flash_rapid));
write(fd_red_period, period_tenth_sec, sizeof(period_tenth_sec));
state = 4;
} else if(state==4) {
printf("Button pressed.\n pwm1 period=1 sec, duty cycle=0%\n pwm5 period=1 sec, duty cycle=50%\n");
write(fd_red_period, period_1sec, sizeof(period_1sec));
write(fd_red_dc, led_flash_1, sizeof(led_flash_1));
write(fd_green_dc, led_on, sizeof(led_on));
state = 5;
} else if(state == 5)
{
printf("Button pressed.\n pwm1 period=1 sec, duty cycle=0%\n pwm5 period=1 sec, duty cycle=100%\n");
write(fd_red_dc, led_off, sizeof(led_off));
write(fd_green_dc, led_on, sizeof(led_on));
state = 0;
}
close(fd_red_dc);
close(fd_green_dc);
close(fd_red_period);
}
int main()
{
struct pollfd fd_poll;
int gpio_fd, rc;
char buf[MAX_BUF];
unsigned int gpio;
int len;
int count = 0;
gpio_export(USER_BUTTON);
gpio_set_dir(USER_BUTTON, 0);
gpio_set_edge(USER_BUTTON, "rising"); // Can be rising, falling or both
gpio_fd = gpio_fd_open(USER_BUTTON);
printf("\nLED test. Pressing gateway's button will change LED pattern\n\n");
while ( count < 6 ) {
fd_poll.fd = gpio_fd;
fd_poll.events = POLLPRI;
rc = poll(&fd_poll, 1, TIMEOUT);
if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}
if (fd_poll.revents & POLLPRI) {
lseek(fd_poll.fd, 0, SEEK_SET); // Read from the start of the file
len = read(fd_poll.fd, buf, MAX_BUF);
if(buf[0] == '1') {
next_led_pattern();
count++;
}
}
fflush(stdout);
}
printf("\nLED test done\n");
gpio_fd_close(gpio_fd);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment