From 8c3dab1ecc0419f85bcf0991985eb52219a107d8 Mon Sep 17 00:00:00 2001 From: Robert Wolterman Date: Tue, 24 Jan 2017 05:23:04 +0000 Subject: [PATCH] Fix and close #53. Start of implementation for #55 --- CHANGELOG.rst | 5 ++++ setup.py | 2 +- source/c_pwm.c | 62 +++++++++++++++++++++------------------------- source/c_pwm.h | 2 +- source/common.c | 19 ++++++++++++-- source/common.h | 2 ++ source/constants.c | 2 +- source/py_pwm.c | 2 +- 8 files changed, 56 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2288e20..e3a91af 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +0.3.2 +---- +* Fixing issue #53 to handle the return values of the set functions in pwm_enable. +* Start of whole library debug for #55 + 0.3.1 ---- * Fixing issue #50 where I broke GPIO.cleanup() and SOFTPWM.cleanup() when no input is specified. diff --git a/setup.py b/setup.py index 6b385a2..71f69cf 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ classifiers = ['Development Status :: 3 - Alpha', 'Topic :: System :: Hardware'] setup(name = 'CHIP_IO', - version = '0.3.1', + version = '0.3.2', author = 'Robert Wolterman', author_email = 'robert.wolterman@gmail.com', description = 'A module to control CHIP IO channels', diff --git a/source/c_pwm.c b/source/c_pwm.c index 56aa761..46ab864 100644 --- a/source/c_pwm.c +++ b/source/c_pwm.c @@ -47,9 +47,7 @@ SOFTWARE. // Global variables int pwm_initialized = 0; -int DEBUG = 0; -//int ENABLE = 1; -//int DISABLE = 0; +//int DEBUG = 0; // pwm devices (future chip pro use) struct pwm_dev @@ -150,17 +148,16 @@ int pwm_set_frequency(const char *key, float freq) { period_ns = (unsigned long)(1e9 / freq); if (pwm->enable) { - if (DEBUG) - printf(" ** IN pwm_set_frequency: pwm_initialized = %d\n", pwm_initialized); if (period_ns != pwm->period_ns) { pwm->period_ns = period_ns; len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer); - if (DEBUG) - printf(" ** pwm_set_frequency: buffer: %s\n", buffer); ssize_t s = write(pwm->period_fd, buffer, len); //ASSRT(s == len); - if (DEBUG) + if (DEBUG) { + printf(" ** IN pwm_set_frequency: pwm_initialized = %d\n", pwm_initialized); + printf(" ** pwm_set_frequency: buffer: %s\n", buffer); printf(" ** IN pwm_set_frequency: s = %d, len = %d\n", s, len); + } if (s != len) { rtnval = -1; } else { @@ -193,8 +190,6 @@ int pwm_set_polarity(const char *key, int polarity) { } if (pwm->enable) { - if (DEBUG) - printf(" ** IN pwm_set_polarity: pwm_initialized = %d\n", pwm_initialized); if (polarity == 0) { len = snprintf(buffer, sizeof(buffer), "%s", "normal"); BUF2SMALL(buffer); } @@ -202,11 +197,12 @@ int pwm_set_polarity(const char *key, int polarity) { { len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); BUF2SMALL(buffer); } - if (DEBUG) - printf(" ** pwm_set_poliarity: buffer: %s\n", buffer); ssize_t s = write(pwm->polarity_fd, buffer, len); //ASSRT(s == len); - if (DEBUG) + if (DEBUG) { + printf(" ** IN pwm_set_polarity: pwm_initialized = %d\n", pwm_initialized); + printf(" ** pwm_set_poliarity: buffer: %s\n", buffer); printf(" ** IN pwm_set_polarity: s = %d, len = %d\n", s, len); + } if (s != len) { rtnval = -1; } else { @@ -237,14 +233,13 @@ int pwm_set_duty_cycle(const char *key, float duty) { pwm->duty = (unsigned long)(pwm->period_ns * (duty / 100.0)); if (pwm->enable) { - if (DEBUG) - printf(" ** IN pwm_set_duty_cycle: pwm_initialized = %d\n", pwm_initialized); len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer); - if (DEBUG) - printf(" ** pwm_set_duty_cycle: buffer: %s\n", buffer); ssize_t s = write(pwm->duty_fd, buffer, len); //ASSRT(s == len); - if (DEBUG) + if (DEBUG) { + printf(" ** IN pwm_set_duty_cycle: pwm_initialized = %d\n", pwm_initialized); + printf(" ** pwm_set_duty_cycle: buffer: %s\n", buffer); printf(" ** IN pwm_set_duty_cycle: s = %d, len = %d\n", s, len); + } if (s != len) { rtnval = -1; } else { @@ -336,7 +331,7 @@ int pwm_start(const char *key, float duty, float freq, int polarity) printf(" ** IN pwm_start: enable_path: %s\n", enable_path); printf(" ** IN pwm_start: period_path: %s\n", period_path); printf(" ** IN pwm_start: duty_path: %s\n", duty_path); - printf(" **IN pwm_start: polarity_path: %s\n", polarity_path); + printf(" ** IN pwm_start: polarity_path: %s\n", polarity_path); } //add period and duty fd to pwm list @@ -393,13 +388,17 @@ int pwm_start(const char *key, float duty, float freq, int polarity) int rtnval = 0; rtnval = pwm_set_enable(key, ENABLE); - rtnval = 0; - rtnval = pwm_set_frequency(key, freq); - rtnval = 0; - //rtnval = pwm_set_polarity(key, polarity); - //rtnval = 0; - rtnval = pwm_set_duty_cycle(key, duty); - + // Fix for issue #53 + if (rtnval != -1) { + rtnval = 0; + rtnval = pwm_set_frequency(key, freq); + if (rtnval != -1) { + rtnval = 0; + //rtnval = pwm_set_polarity(key, polarity); + //rtnval = 0; + rtnval = pwm_set_duty_cycle(key, duty); + } + } return rtnval; } @@ -433,6 +432,9 @@ int pwm_disable(const char *key) { if (strcmp(pwm->key, key) == 0) { + if (DEBUG) { + printf(" ** IN pwm_disable: unexporting %s\n", key); + } //close the fd close(pwm->enable_fd); close(pwm->period_fd); @@ -464,11 +466,3 @@ void pwm_cleanup(void) } } -void pwm_toggle_debug(void) -{ - if (DEBUG) { - DEBUG = 0; - } else { - DEBUG = 1; - } -} diff --git a/source/c_pwm.h b/source/c_pwm.h index dd29844..64378e1 100644 --- a/source/c_pwm.h +++ b/source/c_pwm.h @@ -35,4 +35,4 @@ int pwm_set_frequency(const char *key, float freq); int pwm_set_duty_cycle(const char *key, float duty); int pwm_set_enable(const char *key, int enable); void pwm_cleanup(void); -void pwm_toggle_debug(void); +//void pwm_toggle_debug(void); diff --git a/source/common.c b/source/common.c index dcb5f20..2da5b38 100644 --- a/source/common.c +++ b/source/common.c @@ -49,6 +49,9 @@ SOFTWARE. int setup_error = 0; int module_setup = 0; +// Library Debug +int DEBUG = 0; + pins_t pins_info[] = { { "GND", "GND", "U13_1", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED}, { "CHG-IN", "CHG-IN", "U13_2", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED}, @@ -214,6 +217,14 @@ int get_xio_base(void) return xio_base_address; } /* get_xio_base */ +void toggle_debug(void) +{ + if (DEBUG) { + DEBUG = 0; + } else { + DEBUG = 1; + } +} int gpio_number(pins_t *pin) { @@ -351,12 +362,16 @@ int get_pwm_key_by_name(const char *name, char *key) pins_t *p; for (p = pins_info; p->name != NULL; ++p) { if (strcmp(p->name, name) == 0) { - printf("## FOUND PWM KEY, VALIDATING MUX MODE ##\n"); + if (DEBUG) { + printf(" ** get_pwm_key_by_name: FOUND PWM KEY, VALIDATING MUX MODE **\n"); + } //validate it's a valid pwm pin if (p->pwm_mux_mode == -1) return 0; - printf("## PWM KEY IS VALID ##\n"); + if (DEBUG) { + printf(" ** get_pwm_key_by_name: PWM KEY IS VALID ##\n"); + } strncpy(key, p->key, 7); key[7] = '\0'; return 1; diff --git a/source/common.h b/source/common.h index 76c08f1..aeee0ba 100644 --- a/source/common.h +++ b/source/common.h @@ -86,6 +86,7 @@ typedef struct dyn_int_array_s dyn_int_array_t; int setup_error; int module_setup; +int DEBUG; int get_xio_base(void); int gpio_number(pins_t *pin); @@ -110,3 +111,4 @@ void dyn_int_array_delete(dyn_int_array_t **in_array); void clear_error_msg(void); char *get_error_msg(void); void add_error_msg(char *msg); +void toggle_debug(void); diff --git a/source/constants.c b/source/constants.c index ff0c565..4cbbe9e 100644 --- a/source/constants.c +++ b/source/constants.c @@ -85,6 +85,6 @@ void define_constants(PyObject *module) bcm = Py_BuildValue("i", BCM); PyModule_AddObject(module, "BCM", bcm); - version = Py_BuildValue("s", "0.3.1"); + version = Py_BuildValue("s", "0.3.2"); PyModule_AddObject(module, "VERSION", version); } diff --git a/source/py_pwm.c b/source/py_pwm.c index 3912a33..98605aa 100644 --- a/source/py_pwm.c +++ b/source/py_pwm.c @@ -47,7 +47,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args) static PyObject *py_toggle_debug(PyObject *self, PyObject *args) { // toggle debug printing - pwm_toggle_debug(); + toggle_debug(); Py_RETURN_NONE; }