1
0
mirror of https://github.com/xtacocorex/CHIP_IO synced 2025-07-19 20:33:21 +00:00

More updates to the library to add debug printing

This commit is contained in:
Robert Wolterman
2017-01-26 05:55:30 +00:00
parent bd7f667041
commit 8221016c10
8 changed files with 67 additions and 40 deletions

View File

@ -78,7 +78,7 @@ def get_scale_factor():
# Debug
if DEBUG:
print("Current LRADC Scaling Factor: {0}".format(SCALE_FACTOR))
print("lradc.get_scale_factor: {0}".format(SCALE_FACTOR))
return SCALE_FACTOR
@ -103,7 +103,7 @@ def get_allowable_sample_rates():
# Debug
if DEBUG:
print("Allowable Sampling Rates:")
print("lradc.get_allowable_sample_rates:")
for rate in SAMPLE_RATES:
print("{0}".format(rate))
@ -125,7 +125,7 @@ def set_sample_rate(rate):
# Debug
if DEBUG:
print("Setting Sample Rate to: {0}".format(rate))
print("lradc.set_sample_rate: {0}".format(rate))
# Write the rate
f = open(LRADC_BASE_DEVICE_FILE+CURRENT_SAMPLE_RATE_FILE,"w")
@ -156,7 +156,7 @@ def get_sample_rate():
# Debug
if DEBUG:
print("Current Samplig Rate: {0}".format(dat))
print("lradc.get_sample_rate: {0}".format(dat))
return dat
@ -174,7 +174,7 @@ def get_chan0_raw():
# Debug
if DEBUG:
print("CHAN0 RAW: {0}".format(dat))
print("lradc.get_chan0_raw: {0}".format(dat))
return dat
@ -192,7 +192,7 @@ def get_chan1_raw():
# Debug
if DEBUG:
print("CHAN1 RAW: {0}".format(dat))
print("lradc.get_chan1_raw: {0}".format(dat))
return dat
@ -208,7 +208,7 @@ def get_chan0():
# Debug
if DEBUG:
print("CHAN0: {0}".format(dat))
print("lradc.get_chan0: {0}".format(dat))
return dat
@ -224,7 +224,7 @@ def get_chan1():
# Debug
if DEBUG:
print("CHAN1: {0}".format(dat))
print("lradc.get_chan1: {0}".format(dat))
return dat

View File

@ -111,7 +111,7 @@ int initialize_pwm(void)
int gpio = 0;
if (DEBUG)
printf(" ** EXPORTING PWM0 **\n");
printf(" ** initialize_pwm **\n");
if ((fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY)) < 0)
{
return -1;
@ -119,7 +119,7 @@ int initialize_pwm(void)
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); BUF2SMALL(str_gpio);
ssize_t s = write(fd, str_gpio, len); ASSRT(s == len);
if (DEBUG)
printf(" ** IN initialize_pwm: s = %d, len = %d\n", s, len);
printf(" ** initialize_pwm: export pin: s = %d, len = %d\n", s, len);
close(fd);
pwm_initialized = 1;
@ -154,9 +154,9 @@ int pwm_set_frequency(const char *key, float freq) {
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
ssize_t s = write(pwm->period_fd, buffer, len); //ASSRT(s == len);
if (DEBUG) {
printf(" ** IN pwm_set_frequency: pwm_initialized = %d\n", pwm_initialized);
printf(" ** 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);
printf(" ** pwm_set_frequency: s = %d, len = %d\n", s, len);
}
if (s != len) {
rtnval = -1;
@ -199,9 +199,9 @@ int pwm_set_polarity(const char *key, int polarity) {
}
ssize_t s = write(pwm->polarity_fd, buffer, len); //ASSRT(s == len);
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);
printf(" ** pwm_set_polarity: pwm_initialized = %d\n", pwm_initialized);
printf(" ** pwm_set_polarity: buffer: %s\n", buffer);
printf(" ** pwm_set_polarity: s = %d, len = %d\n", s, len);
}
if (s != len) {
rtnval = -1;
@ -236,9 +236,9 @@ int pwm_set_duty_cycle(const char *key, float duty) {
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
ssize_t s = write(pwm->duty_fd, buffer, len); //ASSRT(s == len);
if (DEBUG) {
printf(" ** IN pwm_set_duty_cycle: pwm_initialized = %d\n", pwm_initialized);
printf(" ** 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);
printf(" ** pwm_set_duty_cycle: s = %d, len = %d\n", s, len);
}
if (s != len) {
rtnval = -1;
@ -261,7 +261,7 @@ int pwm_set_enable(const char *key, int enable)
if (enable != 0 && enable != 1) {
if (DEBUG)
printf(" ** INVALID ENABLE OPTION, NEEDS TO BE EITHER 0 OR 1! **\n");
printf(" ** pwm_set_enable: enable needs to be 0 or 1! **\n");
return rtnval;
}
@ -269,7 +269,7 @@ int pwm_set_enable(const char *key, int enable)
if (pwm == NULL) {
if (DEBUG)
printf(" ** SOMETHING BAD HAPPEND IN pwm_set_enable, WE'RE EXITING WITH -1 NOW! **\n");
printf(" ** pwm_set_enable: pwm struct is null **\n");
return rtnval;
}
@ -277,15 +277,15 @@ int pwm_set_enable(const char *key, int enable)
len = snprintf(buffer, sizeof(buffer), "%d", enable); BUF2SMALL(buffer);
ssize_t s = write(pwm->enable_fd, buffer, len); //ASSRT(s == len);
if (DEBUG) {
printf(" ** IN pwm_set_enable: pwm_initialized = %d\n", pwm_initialized);
printf(" ** pwm_set_enable: pwm_initialized = %d\n", pwm_initialized);
printf(" ** pwm_set_enable: buffer: %s\n", buffer);
printf(" ** IN pwm_set_enable: s = %d, len = %d\n", s, len);
printf(" ** pwm_set_enable: s = %d, len = %d\n", s, len);
}
if (s == len)
{
if (DEBUG)
printf(" ** SETTING pwm->enable to %d\n", enable);
printf(" ** pwm_set_enable: pwm->enable to %d\n", enable);
pwm->enable = enable;
rtnval = 0;
} else {
@ -306,16 +306,16 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
struct pwm_exp *new_pwm, *pwm;
if (DEBUG)
printf(" ** IN pwm_start: pwm_initialized = %d\n", pwm_initialized);
printf(" ** pwm_start: pwm_initialized = %d\n", pwm_initialized);
if(!pwm_initialized) {
initialize_pwm();
} else {
if (DEBUG)
printf(" ** ALREADY INITIALIZED, CALLING CLEANUP TO START FRESH **");
printf(" ** pwm_start: pwm already initialized, cleaning up **");
pwm_cleanup();
}
if (DEBUG)
printf(" ** IN pwm_start: pwm_initialized = %d\n", pwm_initialized);
printf(" ** pwm_start: pwm_initialized = %d\n", pwm_initialized);
//setup the pwm base path, the chip only has one pwm
snprintf(pwm_base_path, sizeof(pwm_base_path), "/sys/class/pwm/pwmchip0/pwm%d", 0); BUF2SMALL(pwm_base_path);
@ -327,11 +327,11 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_base_path); BUF2SMALL(polarity_path);
if (DEBUG) {
printf(" ** IN pwm_start: pwm_base_path: %s\n", pwm_base_path);
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(" ** pwm_start: pwm_base_path: %s\n", pwm_base_path);
printf(" ** pwm_start: enable_path: %s\n", enable_path);
printf(" ** pwm_start: period_path: %s\n", period_path);
printf(" ** pwm_start: duty_path: %s\n", duty_path);
printf(" ** pwm_start: polarity_path: %s\n", polarity_path);
}
//add period and duty fd to pwm list
@ -365,7 +365,7 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
}
if (DEBUG)
printf(" ** IN pwm_start: IF WE MAKE IT HERE, THE FILES WERE SUCCESSFULLY OPEN **\n");
printf(" ** pwm_start: sysfs files opened successfully **\n");
strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */
new_pwm->key[KEYLEN] = '\0'; /* terminate string */
new_pwm->period_fd = period_fd;
@ -433,7 +433,7 @@ int pwm_disable(const char *key)
if (strcmp(pwm->key, key) == 0)
{
if (DEBUG) {
printf(" ** IN pwm_disable: unexporting %s\n", key);
printf(" ** pwm_disable: unexporting %s\n", key);
}
//close the fd
close(pwm->enable_fd);

View File

@ -35,4 +35,3 @@ 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);

View File

@ -96,6 +96,8 @@ int softpwm_set_frequency(const char *key, float freq) {
return -1;
}
if (DEBUG)
printf(" ** softpwm_set_frequency: %f **\n", freq);
pthread_mutex_lock(pwm->params_lock);
pwm->params.freq = freq;
pthread_mutex_unlock(pwm->params_lock);
@ -116,6 +118,8 @@ int softpwm_set_polarity(const char *key, int polarity) {
return -1;
}
if (DEBUG)
printf(" ** softpwm_set_polarity: %d **\n", polarity);
pthread_mutex_lock(pwm->params_lock);
pwm->params.polarity = polarity;
pthread_mutex_unlock(pwm->params_lock);
@ -135,6 +139,8 @@ int softpwm_set_duty_cycle(const char *key, float duty) {;
return -1;
}
if (DEBUG)
printf(" ** softpwm_set_duty_cycle: %f **\n", duty);
pthread_mutex_lock(pwm->params_lock);
pwm->params.duty = duty;
pthread_mutex_unlock(pwm->params_lock);
@ -194,7 +200,6 @@ void *softpwm_thread_toggle(void *arg)
if (enabled_local)
{
/* Force 0 duty cycle to be 0 */
if (duty_local != 0)
{
@ -238,16 +243,27 @@ int softpwm_start(const char *key, float duty, float freq, int polarity)
int gpio;
int ret;
if (get_gpio_number(key, &gpio) < 0)
if (get_gpio_number(key, &gpio) < 0) {
if (DEBUG)
printf(" ** softpwm_start: invalid gpio specified **\n");
return -1;
}
if (gpio_export(gpio) < 0) {
char err[2000];
snprintf(err, sizeof(err), "Error setting up softpwm on pin %d, maybe already exported? (%s)", gpio, get_error_msg());
add_error_msg(err);
return -1;
}
if (gpio_set_direction(gpio, OUTPUT) < 0)
if (DEBUG)
printf(" ** softpwm_start: %d exported **\n", gpio);
if (gpio_set_direction(gpio, OUTPUT) < 0) {
if (DEBUG)
printf(" ** softpwm_start: gpio_set_direction failed **\n");
return -1;
}
// add to list
new_pwm = malloc(sizeof(struct softpwm)); ASSRT(new_pwm != NULL);
@ -279,12 +295,16 @@ int softpwm_start(const char *key, float duty, float freq, int polarity)
}
pthread_mutex_unlock(new_params_lock);
if (DEBUG)
printf(" ** softpwm_enable: setting softpwm parameters **\n");
ASSRT(softpwm_set_duty_cycle(new_pwm->key, duty) == 0);
ASSRT(softpwm_set_frequency(new_pwm->key, freq) == 0);
ASSRT(softpwm_set_polarity(new_pwm->key, polarity) == 0);
pthread_mutex_lock(new_params_lock);
// create thread for pwm
if (DEBUG)
printf(" ** softpwm_enable: creating thread **\n");
ret = pthread_create(&new_thread, NULL, softpwm_thread_toggle, (void *)new_pwm);
ASSRT(ret == 0);
@ -299,17 +319,23 @@ int softpwm_disable(const char *key)
{
struct softpwm *pwm, *temp, *prev_pwm = NULL;
if (DEBUG)
printf(" ** in softpwm_disable **\n");
// remove from list
pwm = exported_pwms;
while (pwm != NULL)
{
if (strcmp(pwm->key, key) == 0)
{
if (DEBUG)
printf(" ** softpwm_disable: found pin **\n");
pthread_mutex_lock(pwm->params_lock);
pwm->params.stop_flag = true;
pthread_mutex_unlock(pwm->params_lock);
pthread_join(pwm->thread, NULL); /* wait for thread to exit */
if (DEBUG)
printf(" ** softpwm_disable: unexporting %d **\n", pwm->gpio);
gpio_unexport(pwm->gpio);
if (prev_pwm == NULL)

View File

@ -84,6 +84,9 @@ void define_constants(PyObject *module)
bcm = Py_BuildValue("i", BCM);
PyModule_AddObject(module, "BCM", bcm);
module_debug = Py_BuildValue("i", DEBUG ? Py_True: Py_False);
PyModule_AddObject(module, "DEBUG", module_debug);
version = Py_BuildValue("s", "0.3.2");
PyModule_AddObject(module, "VERSION", version);

View File

@ -13,5 +13,6 @@ PyObject *version;
PyObject *unknown;
PyObject *board;
PyObject *bcm;
PyObject *module_debug;
void define_constants(PyObject *module);

View File

@ -183,7 +183,6 @@ PyMethodDef pwm_methods[] = {
{"set_frequency", (PyCFunction)py_set_frequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 0.0)" },
{"cleanup", py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
//{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
{NULL, NULL, 0, NULL}
};

View File

@ -195,12 +195,11 @@ PyMethodDef pwm_methods[] = {
{ "set_duty_cycle", (PyCFunction)py_set_duty_cycle, METH_VARARGS, "Change the duty cycle\ndutycycle - between 0.0 and 100.0" },
{ "set_frequency", (PyCFunction)py_set_frequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 0.0)" },
{ "cleanup", (PyCFunction)py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
//{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION > 2
static struct PyModuleDef chippwmmodule = {
static struct PyModuleDef chipspwmmodule = {
PyModuleDef_HEAD_INIT,
"SOFTPWM", // name of module
moduledocstring, // module documentation, may be NULL
@ -218,7 +217,7 @@ PyMODINIT_FUNC initSOFTPWM(void)
PyObject *module = NULL;
#if PY_MAJOR_VERSION > 2
if ((module = PyModule_Create(&chippwmmodule)) == NULL)
if ((module = PyModule_Create(&chipspwmmodule)) == NULL)
return NULL;
#else
if ((module = Py_InitModule3("SOFTPWM", pwm_methods, moduledocstring)) == NULL)