Skip to content

Instantly share code, notes, and snippets.

@hbradio
Created May 5, 2016 15:22
Show Gist options
  • Save hbradio/83213091a581b76bd3639128b5af0f11 to your computer and use it in GitHub Desktop.
Save hbradio/83213091a581b76bd3639128b5af0f11 to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2016 Robert Wolterman
Original BBIO Author Justin Cooper
Modified for CHIP_IO Author Robert Wolterman
This file incorporates work covered by the following copyright and
permission notice, all modified code adopts the original license:
Copyright (c) 2013 Adafruit
Author: Justin Cooper
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include "c_pwm.h"
#include "common.h"
#include "event_gpio.h"
#include "Python.h"
#define KEYLEN 7
#define PERIOD 0
#define DUTY 1
int pwm_initialized = 0;
struct pwm_params
{
float duty;
float freq;
bool enabled;
bool stop_flag;
int polarity;
};
struct softpwm
{
char key[KEYLEN+1]; /* leave room for terminating NUL byte */
struct pwm_params params;
pthread_mutex_t* params_lock;
pthread_t thread;
struct softpwm *next;
};
struct softpwm *exported_pwms = NULL;
struct softpwm *lookup_exported_pwm(const char *key)
{
struct softpwm *pwm = exported_pwms;
while (pwm != NULL)
{
if (strcmp(pwm->key, key) == 0) {
return pwm;
}
pwm = pwm->next;
}
return NULL; /* standard for pointers */
}
int softpwm_set_frequency(const char *key, float freq) {
struct softpwm *pwm;
if (freq <= 0.0)
return -1;
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
}
pthread_mutex_lock(pwm->params_lock);
pwm->params.freq = freq;
pthread_mutex_unlock(pwm->params_lock);
return 1;
}
int softpwm_set_polarity(const char *key, int polarity) {
struct softpwm *pwm;
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
}
if (polarity < 0 || polarity > 1) {
return -1;
}
pthread_mutex_lock(pwm->params_lock);
pwm->params.polarity = polarity;
pthread_mutex_unlock(pwm->params_lock);
return 0;
}
int softpwm_set_duty_cycle(const char *key, float duty) {;
struct softpwm *pwm;
if (duty < 0.0 || duty > 100.0)
return -1;
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
}
pthread_mutex_lock(pwm->params_lock);
pwm->params.duty = duty;
pthread_mutex_unlock(pwm->params_lock);
return 0;
}
void *softpwm_thread_toggle(void *key)
{
struct softpwm *pwm;
unsigned int gpio;
struct timespec tim_on;
struct timespec tim_off;
unsigned int sec;
unsigned int period_ns;
unsigned int on_ns;
unsigned int off_ns;
/* Used to determine if something has
* has changed
*/
unsigned int freq_local = 0;
unsigned int duty_local = 0;
unsigned int polarity_local = 0;
bool stop_flag_local = false;
bool enabled_local = false;
bool recalculate_timing = false;
PySys_WriteStderr("DEBUG; softpwm_thread_toggle start\n");
get_gpio_number(key, &gpio);
pwm = lookup_exported_pwm((char*)key);
while (!stop_flag_local) {
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling\n");
pthread_mutex_lock(pwm->params_lock);
if ((freq_local != pwm->params.freq) || (duty_local != pwm->params.duty)) {
recalculate_timing = true;
}
freq_local = pwm->params.freq;
duty_local = pwm->params.duty;
enabled_local = pwm->params.enabled;
stop_flag_local = pwm->params.stop_flag;
polarity_local = pwm->params.polarity;
pthread_mutex_unlock(pwm->params_lock);
/* If freq or duty has been changed, update the
* sleep times
*/
if (recalculate_timing) {
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling recalculating\n");
period_ns = (unsigned long)(1e9 / freq_local);
on_ns = (unsigned long)(period_ns * (duty_local/100));
off_ns = period_ns - on_ns;
sec = (unsigned int)(on_ns/1e9); /* Intentional truncation */
tim_on.tv_sec = sec;
tim_on.tv_nsec = on_ns - (sec*1e9);
sec = (unsigned int)(off_ns/1e9); /* Intentional truncation */
tim_off.tv_sec = sec;
tim_off.tv_nsec = off_ns - (sec*1e9);
recalculate_timing = false;
}
if (enabled_local)
{
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling enabled\n");
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling enabled setting gpio\n");
/* Set gpio */
if (polarity_local)
gpio_set_value(gpio, HIGH);
else
gpio_set_value(gpio, LOW);
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling enabled sleeping 1\n");
nanosleep(&tim_on, NULL);
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling enabled waking 1\n");
/* Unset gpio */
if (polarity_local)
gpio_set_value(gpio, LOW);
else
gpio_set_value(gpio, HIGH);
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling enabled sleeping 2\n");
nanosleep(&tim_off, NULL);
PySys_WriteStderr("DEBUG; softpwm_thread_toggle toggling enabled waking 2\n");
}
}
/* This pwm has been disabled */
PySys_WriteStderr("DEBUG; softpwm_thread_toggle end\n");
pthread_exit(NULL);
}
int softpwm_start(const char *key, float duty, float freq, int polarity)
{
struct softpwm *new_pwm, *pwm;
pthread_t new_thread;
pthread_mutex_t *new_params_lock;
unsigned int gpio;
int ret;
struct timespec tim;
get_gpio_number(key, &gpio);
gpio_export(gpio);
gpio_set_direction(gpio, OUTPUT);
PySys_WriteStderr("DEBUG; soft_pwm start\n");
// add to list
new_pwm = malloc(sizeof(struct softpwm));
if (new_pwm == 0) {
return -1; // out of memory
}
PySys_WriteStderr("DEBUG; soft_pwm 5\n");
new_params_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if (new_pwm == 0) {
return -1; // out of memory
}
pthread_mutex_init(new_params_lock, NULL);
PySys_WriteStderr("DEBUG; soft_pwm 10\n");
strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */
new_pwm->key[KEYLEN] = '\0'; /* terminate string */
new_pwm->params.enabled = false;
new_pwm->params.stop_flag = false;
new_pwm->params_lock = new_params_lock;
new_pwm->next = NULL;
PySys_WriteStderr("DEBUG; soft_pwm 15\n");
if (exported_pwms == NULL)
{
// create new list
exported_pwms = new_pwm;
} else {
// add to end of existing list
pwm = exported_pwms;
while (pwm->next != NULL)
pwm = pwm->next;
pwm->next = new_pwm;
}
PySys_WriteStderr("DEBUG; soft_pwm 20\n");
softpwm_set_duty_cycle(new_pwm->key, duty);
softpwm_set_frequency(new_pwm->key, freq);
softpwm_set_polarity(new_pwm->key, polarity);
PySys_WriteStderr("DEBUG; soft_pwm 22\n");
// create thread for pwm
ret = pthread_create(&new_thread, NULL, softpwm_thread_toggle, (void *)new_pwm->key);
PySys_WriteStderr("DEBUG; soft_pwm pthread_create ret is %d\n", ret);
if (ret) {
printf("ERROR; return code from pthread_create() is %d\n", ret);
PySys_WriteStderr("DEBUG; soft_pwm ERROR IN pthread_create\n");
exit(-1);
}
tim.tv_sec = 3;
tim.tv_nsec = 0;
nanosleep(&tim, NULL);
PySys_WriteStderr("DEBUG; soft_pwm 25\n");
new_pwm->thread = new_thread;
pthread_mutex_lock(new_params_lock);
new_pwm->params.enabled = true;
pthread_mutex_unlock(new_params_lock);
tim.tv_sec = 20;
tim.tv_nsec = 0;
nanosleep(&tim, NULL);
PySys_WriteStderr("DEBUG; soft_pwm end\n");
return 1;
}
int softpwm_disable(const char *key)
{
struct softpwm *pwm, *temp, *prev_pwm = NULL;
unsigned int gpio = 0;
// Disable the PWM
softpwm_set_frequency(key, 0);
softpwm_set_polarity(key, 0);
softpwm_set_duty_cycle(key, 0);
// remove from list
pwm = exported_pwms;
while (pwm != NULL)
{
if (strcmp(pwm->key, key) == 0)
{
pthread_mutex_lock(pwm->params_lock);
pwm->params.stop_flag = true;
pthread_mutex_unlock(pwm->params_lock);
get_gpio_number(key, &gpio);
gpio_unexport(gpio);
if (prev_pwm == NULL)
{
exported_pwms = pwm->next;
prev_pwm = pwm;
} else {
prev_pwm->next = pwm->next;
}
temp = pwm;
pwm = pwm->next;
free(temp->params_lock);
free(temp);
} else {
prev_pwm = pwm;
pwm = pwm->next;
}
}
return 0;
}
void softpwm_cleanup(void)
{
while (exported_pwms != NULL) {
softpwm_disable(exported_pwms->key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment