1
0
mirror of https://github.com/xtacocorex/CHIP_IO synced 2025-07-20 12:53:22 +00:00

Fixing the code to handle pulling the right XIO base number due to the change in the CHIP OS 1.1 with the 4.4 kernel, thanks to howientc and fordsfords for their help

This commit is contained in:
Robert Wolterman
2016-06-18 14:34:41 -05:00
parent 133453c57e
commit 818be5202e
3 changed files with 133 additions and 48 deletions

View File

@ -6,7 +6,7 @@ Manual::
sudo ntpdate pool.ntp.org sudo ntpdate pool.ntp.org
sudo apt-get update sudo apt-get update
sudo apt-get install build-essential python-dev python-pip -y sudo apt-get install git build-essential python-dev python-pip -y
git clone git://github.com/xtacocorex/CHIP_IO.git git clone git://github.com/xtacocorex/CHIP_IO.git
cd CHIP_IO cd CHIP_IO
sudo python setup.py install sudo python setup.py install
@ -65,6 +65,12 @@ Detecting events::
if GPIO.event_detected("XIO-P0"): if GPIO.event_detected("XIO-P0"):
print "event detected!" print "event detected!"
**GPIO Cleanup**
To clean up the GPIO when done, do the following::
GPIO.cleanup()
**PWM**:: **PWM**::
import CHIP_IO.PWM as PWM import CHIP_IO.PWM as PWM
@ -106,10 +112,7 @@ Use SOFTPWM at low speeds (hundreds of Hz) for the best results. Do not use for
Install py.test to run the tests. You'll also need the python compiler package for py.test.:: Install py.test to run the tests. You'll also need the python compiler package for py.test.::
opkg update && opkg install python-compiler sudo apt-get install python-pytest
#Either pip or easy_install
pip install -U pytest
easy_install -U pytest
Execute the following in the root of the project:: Execute the following in the root of the project::

View File

@ -40,6 +40,12 @@ SOFTWARE.
#include <dirent.h> #include <dirent.h>
#include <time.h> #include <time.h>
#include "common.h" #include "common.h"
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <assert.h>
int setup_error = 0; int setup_error = 0;
int module_setup = 0; int module_setup = 0;
@ -137,14 +143,79 @@ pins_t table[] = {
{ NULL, NULL, 0 } { NULL, NULL, 0 }
}; };
// CREDIT FOR THIS FUNCTION DUE TO HOWIE KATZ OF NTC AND STEVE FORD
// THIS WILL FIND THE PROPER XIO BASE SYSFS NUMBER
// PORTED TO C FORM HOWIE'S PYTHON CODE WITH THE HELP OF STEVE:
// https://gist.github.com/howientc/606545e0ff47e2cda61f14fca5c46eee
// HAT TIP TO:
// http://stackoverflow.com/questions/8149569/scan-a-directory-to-find-files-in-c
// http://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c
#define GPIO_PATH "/sys/class/gpio"
#define EXPANDER "pcf8574a\n"
#define OLDXIOBASE 408
int get_xio_base()
{
int xiobase = -1;
char label_file[80];
FILE *label_fp;
char base_file[80];
FILE *base_fp;
char input_line[80];
DIR *dir;
struct dirent *ent;
struct stat sbuf;
if ((dir = opendir (GPIO_PATH)) != NULL) {
while ((ent = readdir (dir)) != NULL) {
lstat(ent->d_name,&sbuf);
if (S_ISDIR(sbuf.st_mode)) {
if (strcmp(".",ent->d_name) == 0 || strcmp("..",ent->d_name) == 0) {
continue;
}
//printf ("%s\n", ent->d_name);
sprintf(label_file, "%s/%s/label", GPIO_PATH, ent->d_name);
//printf("label_file='%s'\n", label_file);
label_fp = fopen(label_file, "r");
if (label_fp != NULL) {
fgets(input_line, sizeof(input_line), label_fp); assert(strlen(input_line) < sizeof(input_line)-1);
fclose(label_fp);
if (strcmp(input_line, EXPANDER) == 0) {
/* Found the expander, get the contents of base */
sprintf(base_file, "%s/%s/base", GPIO_PATH, ent->d_name);
base_fp = fopen(base_file, "r"); assert(base_fp != NULL);
fgets(input_line, sizeof(input_line), base_fp); assert(strlen(input_line) < sizeof(input_line)-1);
fclose(base_fp);
xiobase = atoi(input_line);
break; /* Found it, exit the while */
}
}
}
}
closedir (dir);
}
return xiobase;
}
int lookup_gpio_by_key(const char *key) int lookup_gpio_by_key(const char *key)
{ {
pins_t *p; pins_t *p;
for (p = table; p->key != NULL; ++p) { for (p = table; p->key != NULL; ++p) {
if (strcmp(p->key, key) == 0) { if (strcmp(p->key, key) == 0) {
// FIGURE OUT IF WE'RE AN XIO PIN USING THE DEFAULT PINS
if (p->gpio >= 408) {
int gbase = get_xio_base();
if (gbase != -1) {
return gbase - (p->gpio - OLDXIOBASE);
} else {
return 0;
}
} else {
return p->gpio; return p->gpio;
} }
} }
}
return 0; return 0;
} }
@ -153,9 +224,19 @@ int lookup_gpio_by_name(const char *name)
pins_t *p; pins_t *p;
for (p = table; p->name != NULL; ++p) { for (p = table; p->name != NULL; ++p) {
if (strcmp(p->name, name) == 0) { if (strcmp(p->name, name) == 0) {
// FIGURE OUT IF WE'RE AN XIO PIN USING THE DEFAULT PINS
if (p->gpio >= 408) {
int gbase = get_xio_base();
if (gbase != -1) {
return gbase - (p->gpio - OLDXIOBASE);
} else {
return 0;
}
} else {
return p->gpio; return p->gpio;
} }
} }
}
return 0; return 0;
} }

View File

@ -50,6 +50,7 @@ int gpio_direction[120];
char ctrl_dir[35]; char ctrl_dir[35];
char ocp_dir[25]; char ocp_dir[25];
int get_xio_base();
int get_gpio_number(const char *key, unsigned int *gpio); int get_gpio_number(const char *key, unsigned int *gpio);
int get_pwm_key(const char *input, char *key); int get_pwm_key(const char *input, char *key);
int get_key(const char *input, char *key); int get_key(const char *input, char *key);