diff --git a/README.rst b/README.rst index 699675f..9064f56 100644 --- a/README.rst +++ b/README.rst @@ -6,7 +6,7 @@ Manual:: sudo ntpdate pool.ntp.org 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 cd CHIP_IO sudo python setup.py install @@ -65,6 +65,12 @@ Detecting events:: if GPIO.event_detected("XIO-P0"): print "event detected!" +**GPIO Cleanup** + +To clean up the GPIO when done, do the following:: + + GPIO.cleanup() + **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.:: - opkg update && opkg install python-compiler - #Either pip or easy_install - pip install -U pytest - easy_install -U pytest + sudo apt-get install python-pytest Execute the following in the root of the project:: diff --git a/source/common.c b/source/common.c index 91f8d58..3b1bbef 100644 --- a/source/common.c +++ b/source/common.c @@ -40,6 +40,12 @@ SOFTWARE. #include #include #include "common.h" +#include +#include +#include +#include +#include +#include int setup_error = 0; int module_setup = 0; @@ -137,12 +143,77 @@ pins_t table[] = { { 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) { pins_t *p; for (p = table; p->key != NULL; ++p) { if (strcmp(p->key, key) == 0) { - return p->gpio; + // 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 0; @@ -153,7 +224,17 @@ int lookup_gpio_by_name(const char *name) pins_t *p; for (p = table; p->name != NULL; ++p) { if (strcmp(p->name, name) == 0) { - return p->gpio; + // 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 0; @@ -187,20 +268,20 @@ int lookup_ain_by_name(const char *name) } } return -1; -} - -int copy_key_by_key(const char *input_key, char *key) -{ - pins_t *p; - for (p = table; p->key != NULL; ++p) { - if (strcmp(p->key, input_key) == 0) { - strncpy(key, p->key, 7); - key[7] = '\0'; - return 1; - } - } - return 0; -} +} + +int copy_key_by_key(const char *input_key, char *key) +{ + pins_t *p; + for (p = table; p->key != NULL; ++p) { + if (strcmp(p->key, input_key) == 0) { + strncpy(key, p->key, 7); + key[7] = '\0'; + return 1; + } + } + return 0; +} int copy_pwm_key_by_key(const char *input_key, char *key) { @@ -217,22 +298,22 @@ int copy_pwm_key_by_key(const char *input_key, char *key) } } return 0; -} - -int get_key_by_name(const char *name, char *key) -{ - pins_t *p; - for (p = table; p->name != NULL; ++p) { - if (strcmp(p->name, name) == 0) { - strncpy(key, p->key, 7); - key[7] = '\0'; - return 1; - } - } - return 0; -} +} -int get_pwm_key_by_name(const char *name, char *key) +int get_key_by_name(const char *name, char *key) +{ + pins_t *p; + for (p = table; p->name != NULL; ++p) { + if (strcmp(p->name, name) == 0) { + strncpy(key, p->key, 7); + key[7] = '\0'; + return 1; + } + } + return 0; +} + +int get_pwm_key_by_name(const char *name, char *key) { pins_t *p; for (p = table; p->name != NULL; ++p) { @@ -258,16 +339,16 @@ int get_gpio_number(const char *key, unsigned int *gpio) } return 0; -} - -int get_key(const char *input, char *key) -{ - if (!copy_key_by_key(input, key)) { - return get_key_by_name(input, key); - } - - return 1; -} +} + +int get_key(const char *input, char *key) +{ + if (!copy_key_by_key(input, key)) { + return get_key_by_name(input, key); + } + + return 1; +} int get_pwm_key(const char *input, char *key) { @@ -276,8 +357,8 @@ int get_pwm_key(const char *input, char *key) } return 1; -} - +} + int get_adc_ain(const char *key, unsigned int *ain) { *ain = lookup_ain_by_key(key); diff --git a/source/common.h b/source/common.h index 9e97d21..d012856 100644 --- a/source/common.h +++ b/source/common.h @@ -50,6 +50,7 @@ int gpio_direction[120]; char ctrl_dir[35]; char ocp_dir[25]; +int get_xio_base(); int get_gpio_number(const char *key, unsigned int *gpio); int get_pwm_key(const char *input, char *key); int get_key(const char *input, char *key);