The SmartEdge IIOT GatewaySmartEdge IIOT Gateway is a Raspberry Pi 3 Compute based industrial Linux box. It runs Raspbian with some additions. There's a set of industrial hardware extensions. In this blog I revisit the industrial I/O. I create a C program that controls the inputs and outputs. |
API
Use the Industrial inputs and outputs from a C program. Pretend to make a simple API.
It's an intermediate exercise in my far away goal to use these pins in OpenPLC.
The API:
static PINHANDLE openInOut(char *pin, PINDIRECTION direction) static void closeInOut(PINHANDLE fp) static void setHigh(PINHANDLE fp, bool high) static bool getHigh(PINHANDLE fp)
Four functions to deal with the pins.
- Use openInOut() to indicate that you want to use one of the industrial in- or outouts.
In the pin isn't set up yet, this will be done when you call this initialiser. - closeInOut() is the cleanup at the end. It releases resources.
It doesn't reset pin states though. Everything you altered will stay that way. - setHigh() drives output pins.
- getHigh() reads input pins.
Looking back I could have used read() and write() but it is what it is.
How to use the API?
Straightforward. You initialise the pins and use them.
This is my test program: When you push a switch attached to Input 1, a led connected to Output 1 will light up:
- Read an input (I1).
- Set output (O1) to the same value.
PINHANDLE pO1 = openInOut(GPIO_O1, OUTDIRECTION); PINHANDLE pI1 = openInOut(GPIO_I1, INDIRECTION); bool state = false; setHigh(pO1, state); while(!usleep(100000)) { if(state != getHigh(pI1)) { state = !state; setHigh(pO1, state); } }
You can see the result in the animation at the top of the blog post.
Full Code
To help understand the code, I'm summarising the Linux level commands that are used in the C source.
echo 200 >/sys/class/gpio/export echo 201 >/sys/class/gpio/export echo in > /sys/class/gpio/gpio200/direction echo out > /sys/class/gpio/gpio201/direction cat /sys/class/gpio/gpio201/value echo 0 > /sys/class/gpio/gpio201/value echo 1 > /sys/class/gpio/gpio201/value |
The same calls are done here, if/when needed.
This is just a few tens of lines.
#include<unistd.h> #include <fcntl.h> #include <dirent.h> #include <errno.h> #include <stdio.h> #include <string.h> #include <stdbool.h> #define GPIODIR "/sys/class/gpio" #define EXPORTFILE GPIODIR "/export" #define GPIODIR_PT2 "/gpio" #define GPIO_O1 "201" #define GPIO_I1 "200" #define PINHANDLE FILE * #define PINDIRECTION bool #define OUTDIRECTION true #define INDIRECTION false // longest string used + 1 #define BUFFLEN sizeof GPIODIR GPIODIR_PT2 GPIO_O1 "/directionX" /* * jc: this comes from the source of raspberry gpio utility * changeOwner: * Change the ownership of the file to the real userId of the calling * program so we can access it. ********************************************************************************* */ //static void changeOwner (char *file) //{ // uid_t uid = getuid () ; // uid_t gid = getgid () ; // // if (chown (file, uid, gid) != 0) // { // // todo error here // } //} static bool dirExists (char *dir) { bool retval = false; // check if our pin is exported DIR* d = opendir(dir); if (d) { /* Directory exists. */ closedir(d); retval = true; } return retval; } static PINHANDLE openInOut(char *pin, PINDIRECTION direction) { PINHANDLE retval = NULL; char buffer[BUFFLEN]; strcpy(buffer, GPIODIR); strcat(buffer, GPIODIR_PT2); strcat(buffer, pin); // check if our pin is exported if (!dirExists(buffer)) { FILE *fp; fp = fopen(EXPORTFILE, "w"); fputs(pin, fp); fclose(fp); } // get size of buffer at this point // this point will be used later to build // the path to the character driver too. int buflen = strlen(buffer); // set to direction { FILE *fp; strcat(buffer, "/direction"); fp = fopen(buffer, "w"); if (direction) { //output fputs("out", fp); } else { // input fputs("in", fp); } fclose(fp); } // get file handle to the character driver { char* start = buffer + buflen; strcpy(start, "/value"); if (direction) { // output retval = fopen(buffer, "w"); } else { // input retval = fopen(buffer, "r"); } // unbuffered, so that a read and write are direct // linked to the character driver setvbuf(retval, NULL, _IONBF, 0); } return retval; } static void closeInOut(PINHANDLE fp) { fclose(fp); } static void setHigh(PINHANDLE fp, bool high) { if(high) { fputs("1", fp); } else { fputs("0", fp); } // fflush(fp); I switched to an unbuffered stream. } static bool getHigh(PINHANDLE fp) { char buffer[2]; rewind(fp); fread(buffer, sizeof buffer[0], 1, fp); return (buffer[0] == '1'); } int main(void) { PINHANDLE pO1 = openInOut(GPIO_O1, OUTDIRECTION); PINHANDLE pI1 = openInOut(GPIO_I1, INDIRECTION); bool state = false; setHigh(pO1, state); while(!usleep(100000)) { if(state != getHigh(pI1)) { state = !state; setHigh(pO1, state); } } closeInOut(pI1); closeInOut(pO1); }
There are some little additional functions I didn't mention in my summary:
In the case that the pin is not in the right state, or never used before, the code does the management to set everything up.
The program does not need to be executed by root, but the user has to be part of the gpio group.