mirror of
https://github.com/xtacocorex/CHIP_IO
synced 2025-07-20 21:03:22 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
6bd2e61450 | |||
cd85e2b5eb | |||
6beacbb382 | |||
a498fc12bd | |||
2e2177da26 | |||
541d76f641 | |||
2b23e2d165 | |||
a839661c3b | |||
cb4e272a35 | |||
05b936ca1d |
@ -1,3 +1,27 @@
|
|||||||
|
0.4.0
|
||||||
|
---
|
||||||
|
* Software Servo code added
|
||||||
|
- Only works on the LCD and CSI pins
|
||||||
|
* Fixed cleanup() for the SOFTPWM and SERVO
|
||||||
|
- The per pin cleanup for SOFTPWM doesn't work as stop() clears up the memory for the pin used
|
||||||
|
- SERVO code was based on SOFTPWM, so it inherited this issue
|
||||||
|
|
||||||
|
0.3.5
|
||||||
|
---
|
||||||
|
* Merged in brettcvz's code to read a byte of data from the GPIO
|
||||||
|
- Cleaned the code up and expanded it (in the low level C code) to read up to 32 bits of data
|
||||||
|
- Presented 8 bit and 16 bits of data functions to the Python interface with brettcvz's read_byte() and my read_word()
|
||||||
|
* I think I finally fixed the GPIO.cleanup() code one and for all
|
||||||
|
|
||||||
|
0.3.4.1
|
||||||
|
---
|
||||||
|
* Quick fix as I borked XIO setup as inputs with the latest change that enabled PUD
|
||||||
|
|
||||||
|
0.3.4
|
||||||
|
---
|
||||||
|
* Pull Up/Pull Down resistor setting now available for the R8 GPIO.
|
||||||
|
* Some general cleanup
|
||||||
|
|
||||||
0.3.3
|
0.3.3
|
||||||
----
|
----
|
||||||
* Added Debug printing for all the capabilities with the toggle_debug() function
|
* Added Debug printing for all the capabilities with the toggle_debug() function
|
||||||
|
42
README.rst
42
README.rst
@ -19,7 +19,6 @@ For Python2.7::
|
|||||||
cd CHIP_IO
|
cd CHIP_IO
|
||||||
sudo python setup.py install
|
sudo python setup.py install
|
||||||
cd ..
|
cd ..
|
||||||
sudo rm -rf CHIP_IO
|
|
||||||
|
|
||||||
For Python3::
|
For Python3::
|
||||||
|
|
||||||
@ -34,7 +33,6 @@ For Python3::
|
|||||||
cd CHIP_IO
|
cd CHIP_IO
|
||||||
sudo python3 setup.py install
|
sudo python3 setup.py install
|
||||||
cd ..
|
cd ..
|
||||||
sudo rm -rf CHIP_IO
|
|
||||||
|
|
||||||
**Usage**
|
**Usage**
|
||||||
|
|
||||||
@ -191,6 +189,15 @@ Inputs work similarly to outputs.::
|
|||||||
import CHIP_IO.GPIO as GPIO
|
import CHIP_IO.GPIO as GPIO
|
||||||
GPIO.setup("CSID0", GPIO.IN)
|
GPIO.setup("CSID0", GPIO.IN)
|
||||||
|
|
||||||
|
Other options when setting up pins::
|
||||||
|
|
||||||
|
# Specify pull up/pull down settings on a pin
|
||||||
|
GPIO.setup("CSID0", GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
|
# Specify initial value for an output
|
||||||
|
GPIO.setup("CSID0", GPIO.OUT, initial=1)
|
||||||
|
|
||||||
|
Pull Up/Down values are only for pins that are provided by the R8, the XIO are not capable of this. The allowable values are: PUD_OFF, PUD_UP, and PUD_DOWN.
|
||||||
|
|
||||||
Polling inputs::
|
Polling inputs::
|
||||||
|
|
||||||
if GPIO.input("CSID0"):
|
if GPIO.input("CSID0"):
|
||||||
@ -198,6 +205,15 @@ Polling inputs::
|
|||||||
else:
|
else:
|
||||||
print("LOW")
|
print("LOW")
|
||||||
|
|
||||||
|
Read lots of data::
|
||||||
|
|
||||||
|
# Get 8 bits of data in one shot
|
||||||
|
mybyte = GPIO.read_byte("LCD-D3")
|
||||||
|
# Get 16 bits of data in one shot
|
||||||
|
myword = GPIO.read_word("XIO-P4")
|
||||||
|
|
||||||
|
This code was initially added by brettcvz and I cleaned it up and expanded it.
|
||||||
|
|
||||||
The edge detection code below only works for the AP-EINT1, AP-EINT3, and XPO Pins on the CHIP.
|
The edge detection code below only works for the AP-EINT1, AP-EINT3, and XPO Pins on the CHIP.
|
||||||
|
|
||||||
Waiting for an edge (GPIO.RISING, GPIO.FALLING, or GPIO.BOTH::
|
Waiting for an edge (GPIO.RISING, GPIO.FALLING, or GPIO.BOTH::
|
||||||
@ -269,10 +285,8 @@ Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to kn
|
|||||||
SPWM.set_frequency("XIO-P7", 10)
|
SPWM.set_frequency("XIO-P7", 10)
|
||||||
# To Stop SPWM
|
# To Stop SPWM
|
||||||
SPWM.stop("XIO-P7")
|
SPWM.stop("XIO-P7")
|
||||||
# Cleanup can have no argument to clean up all SoftPWM outputs
|
# Cleanup
|
||||||
SPWM.cleanup()
|
SPWM.cleanup()
|
||||||
# Or you can specify a single SoftPWM output to cleanup (keeping the rest intact)
|
|
||||||
SPWM.cleanup("XIO-P7")
|
|
||||||
#For specific polarity: this example sets polarity to 1 on start:
|
#For specific polarity: this example sets polarity to 1 on start:
|
||||||
SPWM.start("XIO-P7", 50, 2000, 1)
|
SPWM.start("XIO-P7", 50, 2000, 1)
|
||||||
|
|
||||||
@ -280,6 +294,24 @@ Use SOFTPWM at low speeds (hundreds of Hz) for the best results. Do not use for
|
|||||||
|
|
||||||
If using SOFTPWM and PWM at the same time, import CHIP_IO.SOFTPWM as SPWM or something different than PWM as to not confuse the library.
|
If using SOFTPWM and PWM at the same time, import CHIP_IO.SOFTPWM as SPWM or something different than PWM as to not confuse the library.
|
||||||
|
|
||||||
|
**SERVO**::
|
||||||
|
|
||||||
|
import CHIP_IO.SERVO as SERVO
|
||||||
|
# Enable/Disable Debug
|
||||||
|
SERVO.toggle_debug()
|
||||||
|
#SPWM.start(channel, angle=0, range=180)
|
||||||
|
#angle values are between +/- range/2)
|
||||||
|
#you can choose any pin except the XIO's
|
||||||
|
SERVO.start("CSID4", 50)
|
||||||
|
SERVO.set_angle("CSID4", 25.5)
|
||||||
|
SERVO.set_range("CSID4", 90)
|
||||||
|
# To Stop Servo
|
||||||
|
SERVO.stop("CSID4")
|
||||||
|
# Cleanup
|
||||||
|
SERVO.cleanup()
|
||||||
|
|
||||||
|
The Software Servo control only works on the LCD and CSI pins. The XIO is too slow to control.
|
||||||
|
|
||||||
**LRADC**::
|
**LRADC**::
|
||||||
|
|
||||||
The LRADC was enabled in the 4.4.13-ntc-mlc. This is a 6 bit ADC that is 2 Volt tolerant.
|
The LRADC was enabled in the 4.4.13-ntc-mlc. This is a 6 bit ADC that is 2 Volt tolerant.
|
||||||
|
5
setup.py
5
setup.py
@ -20,7 +20,7 @@ classifiers = ['Development Status :: 3 - Alpha',
|
|||||||
'Topic :: System :: Hardware']
|
'Topic :: System :: Hardware']
|
||||||
|
|
||||||
setup(name = 'CHIP_IO',
|
setup(name = 'CHIP_IO',
|
||||||
version = '0.3.3',
|
version = '0.4.0',
|
||||||
author = 'Robert Wolterman',
|
author = 'Robert Wolterman',
|
||||||
author_email = 'robert.wolterman@gmail.com',
|
author_email = 'robert.wolterman@gmail.com',
|
||||||
description = 'A module to control CHIP IO channels',
|
description = 'A module to control CHIP IO channels',
|
||||||
@ -32,5 +32,6 @@ setup(name = 'CHIP_IO',
|
|||||||
packages = find_packages(),
|
packages = find_packages(),
|
||||||
ext_modules = [Extension('CHIP_IO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
ext_modules = [Extension('CHIP_IO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
||||||
Extension('CHIP_IO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
Extension('CHIP_IO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
||||||
Extension('CHIP_IO.SOFTPWM', ['source/py_softpwm.c', 'source/c_softpwm.c', 'source/constants.c', 'source/common.c', 'source/event_gpio.c'], extra_compile_args=['-Wno-format-security'])]) #,
|
Extension('CHIP_IO.SOFTPWM', ['source/py_softpwm.c', 'source/c_softpwm.c', 'source/constants.c', 'source/common.c', 'source/event_gpio.c'], extra_compile_args=['-Wno-format-security']),
|
||||||
|
Extension('CHIP_IO.SERVO', ['source/py_servo.c', 'source/c_softservo.c', 'source/constants.c', 'source/common.c', 'source/event_gpio.c'], extra_compile_args=['-Wno-format-security'])]) #,
|
||||||
# Extension('CHIP_IO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
# Extension('CHIP_IO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
||||||
|
@ -33,6 +33,7 @@ SOFTWARE.
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "c_pwm.h"
|
#include "c_pwm.h"
|
||||||
@ -102,6 +103,7 @@ struct pwm_exp *lookup_exported_pwm(const char *key)
|
|||||||
|
|
||||||
int initialize_pwm(void)
|
int initialize_pwm(void)
|
||||||
{
|
{
|
||||||
|
int e_no;
|
||||||
if (!pwm_initialized) {
|
if (!pwm_initialized) {
|
||||||
int fd, len;
|
int fd, len;
|
||||||
char str_gpio[80];
|
char str_gpio[80];
|
||||||
@ -113,23 +115,37 @@ int initialize_pwm(void)
|
|||||||
printf(" ** initialize_pwm **\n");
|
printf(" ** initialize_pwm **\n");
|
||||||
if ((fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY)) < 0)
|
if ((fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY)) < 0)
|
||||||
{
|
{
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "initialize_pwm: could not open export file");
|
||||||
|
add_error_msg(err);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); BUF2SMALL(str_gpio);
|
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); BUF2SMALL(str_gpio);
|
||||||
ssize_t s = write(fd, str_gpio, len); ASSRT(s == len);
|
ssize_t s = write(fd, str_gpio, len); e_no = errno;
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
if (s != len) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "initialize_pwm: could not export pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
printf(" ** initialize_pwm: export pin: s = %d, len = %d\n", s, len);
|
printf(" ** initialize_pwm: export pin: s = %d, len = %d\n", s, len);
|
||||||
close(fd);
|
|
||||||
|
|
||||||
pwm_initialized = 1;
|
pwm_initialized = 1;
|
||||||
return 1;
|
return 1;
|
||||||
|
} else {
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** initialize_pwm: pwm is already initialized\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pwm_set_frequency(const char *key, float freq) {
|
int pwm_set_frequency(const char *key, float freq) {
|
||||||
int len;
|
int len, e_no;
|
||||||
int rtnval = -1;
|
int rtnval = -1;
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
unsigned long period_ns;
|
unsigned long period_ns;
|
||||||
@ -151,13 +167,16 @@ int pwm_set_frequency(const char *key, float freq) {
|
|||||||
pwm->period_ns = period_ns;
|
pwm->period_ns = period_ns;
|
||||||
|
|
||||||
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
|
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
|
||||||
ssize_t s = write(pwm->period_fd, buffer, len); //ASSRT(s == len);
|
ssize_t s = write(pwm->period_fd, buffer, len); e_no = errno;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf(" ** 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(" ** pwm_set_frequency: buffer: %s\n", buffer);
|
||||||
printf(" ** pwm_set_frequency: s = %d, len = %d\n", s, len);
|
printf(" ** pwm_set_frequency: s = %d, len = %d\n", s, len);
|
||||||
}
|
}
|
||||||
if (s != len) {
|
if (s != len) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_set_frequency: could not change frequency of pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
rtnval = -1;
|
rtnval = -1;
|
||||||
} else {
|
} else {
|
||||||
rtnval = 1;
|
rtnval = 1;
|
||||||
@ -173,7 +192,7 @@ int pwm_set_frequency(const char *key, float freq) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int pwm_set_period_ns(const char *key, unsigned long period_ns) {
|
int pwm_set_period_ns(const char *key, unsigned long period_ns) {
|
||||||
int len;
|
int len, e_no;
|
||||||
int rtnval = -1;
|
int rtnval = -1;
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
struct pwm_exp *pwm;
|
struct pwm_exp *pwm;
|
||||||
@ -191,13 +210,16 @@ int pwm_set_period_ns(const char *key, unsigned long period_ns) {
|
|||||||
pwm->period_ns = period_ns;
|
pwm->period_ns = period_ns;
|
||||||
|
|
||||||
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
|
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
|
||||||
ssize_t s = write(pwm->period_fd, buffer, len); //ASSRT(s == len);
|
ssize_t s = write(pwm->period_fd, buffer, len); e_no = errno;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf(" ** pwm_set_period_ns: pwm_initialized = %d\n", pwm_initialized);
|
printf(" ** pwm_set_period_ns: pwm_initialized = %d\n", pwm_initialized);
|
||||||
printf(" ** pwm_set_period_ns: buffer: %s\n", buffer);
|
printf(" ** pwm_set_period_ns: buffer: %s\n", buffer);
|
||||||
printf(" ** pwm_set_period_ns: s = %d, len = %d\n", s, len);
|
printf(" ** pwm_set_period_ns: s = %d, len = %d\n", s, len);
|
||||||
}
|
}
|
||||||
if (s != len) {
|
if (s != len) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_set_period_ns: could not change period of pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
rtnval = -1;
|
rtnval = -1;
|
||||||
} else {
|
} else {
|
||||||
rtnval = 1;
|
rtnval = 1;
|
||||||
@ -233,7 +255,7 @@ int pwm_get_period_ns(const char *key, unsigned long *period_ns) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int pwm_set_polarity(const char *key, int polarity) {
|
int pwm_set_polarity(const char *key, int polarity) {
|
||||||
int len;
|
int len, e_no;
|
||||||
int rtnval = -1;
|
int rtnval = -1;
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
struct pwm_exp *pwm;
|
struct pwm_exp *pwm;
|
||||||
@ -256,13 +278,16 @@ int pwm_set_polarity(const char *key, int polarity) {
|
|||||||
{
|
{
|
||||||
len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); BUF2SMALL(buffer);
|
len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); BUF2SMALL(buffer);
|
||||||
}
|
}
|
||||||
ssize_t s = write(pwm->polarity_fd, buffer, len); //ASSRT(s == len);
|
ssize_t s = write(pwm->polarity_fd, buffer, len); e_no = errno;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf(" ** pwm_set_polarity: pwm_initialized = %d\n", pwm_initialized);
|
printf(" ** pwm_set_polarity: pwm_initialized = %d\n", pwm_initialized);
|
||||||
printf(" ** pwm_set_polarity: buffer: %s\n", buffer);
|
printf(" ** pwm_set_polarity: buffer: %s\n", buffer);
|
||||||
printf(" ** pwm_set_polarity: s = %d, len = %d\n", s, len);
|
printf(" ** pwm_set_polarity: s = %d, len = %d\n", s, len);
|
||||||
}
|
}
|
||||||
if (s != len) {
|
if (s != len) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_set_polarity: could not change polarity of pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
rtnval = -1;
|
rtnval = -1;
|
||||||
} else {
|
} else {
|
||||||
rtnval = 1;
|
rtnval = 1;
|
||||||
@ -274,7 +299,7 @@ int pwm_set_polarity(const char *key, int polarity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int pwm_set_duty_cycle(const char *key, float duty) {
|
int pwm_set_duty_cycle(const char *key, float duty) {
|
||||||
int len;
|
int len, e_no;
|
||||||
int rtnval = -1;
|
int rtnval = -1;
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
struct pwm_exp *pwm;
|
struct pwm_exp *pwm;
|
||||||
@ -293,13 +318,16 @@ int pwm_set_duty_cycle(const char *key, float duty) {
|
|||||||
|
|
||||||
if (pwm->enable) {
|
if (pwm->enable) {
|
||||||
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
|
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
|
||||||
ssize_t s = write(pwm->duty_fd, buffer, len); //ASSRT(s == len);
|
ssize_t s = write(pwm->duty_fd, buffer, len); e_no = errno;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf(" ** 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(" ** pwm_set_duty_cycle: buffer: %s\n", buffer);
|
||||||
printf(" ** 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) {
|
if (s != len) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_set_duty_cycle: could not change duty cycle of pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
rtnval = -1;
|
rtnval = -1;
|
||||||
} else {
|
} else {
|
||||||
rtnval = 1;
|
rtnval = 1;
|
||||||
@ -312,7 +340,7 @@ int pwm_set_duty_cycle(const char *key, float duty) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int pwm_set_pulse_width_ns(const char *key, unsigned long pulse_width_ns) {
|
int pwm_set_pulse_width_ns(const char *key, unsigned long pulse_width_ns) {
|
||||||
int len;
|
int len, e_no;
|
||||||
int rtnval = -1;
|
int rtnval = -1;
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
struct pwm_exp *pwm;
|
struct pwm_exp *pwm;
|
||||||
@ -330,13 +358,16 @@ int pwm_set_pulse_width_ns(const char *key, unsigned long pulse_width_ns) {
|
|||||||
|
|
||||||
if (pwm->enable) {
|
if (pwm->enable) {
|
||||||
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
|
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
|
||||||
ssize_t s = write(pwm->duty_fd, buffer, len); //ASSRT(s == len);
|
ssize_t s = write(pwm->duty_fd, buffer, len); e_no = errno;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf(" ** pwm_set_pulse_width_ns: pwm_initialized = %d\n", pwm_initialized);
|
printf(" ** pwm_set_pulse_width_ns: pwm_initialized = %d\n", pwm_initialized);
|
||||||
printf(" ** pwm_set_pulse_width_ns: buffer: %s\n", buffer);
|
printf(" ** pwm_set_pulse_width_ns: buffer: %s\n", buffer);
|
||||||
printf(" ** pwm_set_pulse_width_ns: s = %d, len = %d\n", s, len);
|
printf(" ** pwm_set_pulse_width_ns: s = %d, len = %d\n", s, len);
|
||||||
}
|
}
|
||||||
if (s != len) {
|
if (s != len) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_set_pulse_width_ns: could not change pulse width of pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
rtnval = -1;
|
rtnval = -1;
|
||||||
} else {
|
} else {
|
||||||
rtnval = 1;
|
rtnval = 1;
|
||||||
@ -351,7 +382,7 @@ int pwm_set_pulse_width_ns(const char *key, unsigned long pulse_width_ns) {
|
|||||||
|
|
||||||
int pwm_set_enable(const char *key, int enable)
|
int pwm_set_enable(const char *key, int enable)
|
||||||
{
|
{
|
||||||
int len;
|
int len, e_no;
|
||||||
int rtnval = -1;
|
int rtnval = -1;
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
struct pwm_exp *pwm;
|
struct pwm_exp *pwm;
|
||||||
@ -372,7 +403,7 @@ int pwm_set_enable(const char *key, int enable)
|
|||||||
|
|
||||||
|
|
||||||
len = snprintf(buffer, sizeof(buffer), "%d", enable); BUF2SMALL(buffer);
|
len = snprintf(buffer, sizeof(buffer), "%d", enable); BUF2SMALL(buffer);
|
||||||
ssize_t s = write(pwm->enable_fd, buffer, len); //ASSRT(s == len);
|
ssize_t s = write(pwm->enable_fd, buffer, len); e_no = errno;
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
printf(" ** 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(" ** pwm_set_enable: buffer: %s\n", buffer);
|
||||||
@ -386,6 +417,9 @@ int pwm_set_enable(const char *key, int enable)
|
|||||||
pwm->enable = enable;
|
pwm->enable = enable;
|
||||||
rtnval = 0;
|
rtnval = 0;
|
||||||
} else {
|
} else {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_set_enable: could not enable/disable pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
rtnval = -1;
|
rtnval = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -432,11 +466,18 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//add period and duty fd to pwm list
|
//add period and duty fd to pwm list
|
||||||
if ((enable_fd = open(enable_path, O_WRONLY)) < 0)
|
if ((enable_fd = open(enable_path, O_WRONLY)) < 0) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_start: could not open enable file");
|
||||||
|
add_error_msg(err);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if ((period_fd = open(period_path, O_WRONLY)) < 0) {
|
if ((period_fd = open(period_path, O_WRONLY)) < 0) {
|
||||||
close(enable_fd);
|
close(enable_fd);
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_start: could not open period file");
|
||||||
|
add_error_msg(err);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -444,6 +485,9 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
|
|||||||
//error, close already opened period_fd.
|
//error, close already opened period_fd.
|
||||||
close(enable_fd);
|
close(enable_fd);
|
||||||
close(period_fd);
|
close(period_fd);
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_start: could not open duty cycle file");
|
||||||
|
add_error_msg(err);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -452,13 +496,19 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
|
|||||||
close(enable_fd);
|
close(enable_fd);
|
||||||
close(period_fd);
|
close(period_fd);
|
||||||
close(duty_fd);
|
close(duty_fd);
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_start: could not open polarity file");
|
||||||
|
add_error_msg(err);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add to list
|
// add to list
|
||||||
new_pwm = malloc(sizeof(struct pwm_exp));
|
new_pwm = malloc(sizeof(struct pwm_exp));
|
||||||
if (new_pwm == 0) {
|
if (new_pwm == 0) {
|
||||||
return -1; // out of memory
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_start: unable to allocate memory");
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
@ -503,7 +553,7 @@ int pwm_disable(const char *key)
|
|||||||
{
|
{
|
||||||
struct pwm_exp *pwm, *temp, *prev_pwm = NULL;
|
struct pwm_exp *pwm, *temp, *prev_pwm = NULL;
|
||||||
|
|
||||||
int fd, len;
|
int fd, len, e_no;
|
||||||
char str_gpio[80];
|
char str_gpio[80];
|
||||||
// Per https://github.com/NextThingCo/CHIP-linux/pull/4
|
// Per https://github.com/NextThingCo/CHIP-linux/pull/4
|
||||||
// we need to export 0 here to enable pwm0
|
// we need to export 0 here to enable pwm0
|
||||||
@ -517,12 +567,21 @@ int pwm_disable(const char *key)
|
|||||||
|
|
||||||
if ((fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY)) < 0)
|
if ((fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY)) < 0)
|
||||||
{
|
{
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_disable: could not open unexport file");
|
||||||
|
add_error_msg(err);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); BUF2SMALL(str_gpio);
|
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); BUF2SMALL(str_gpio);
|
||||||
ssize_t s = write(fd, str_gpio, len); ASSRT(s == len);
|
ssize_t s = write(fd, str_gpio, len); e_no = errno;
|
||||||
close(fd);
|
close(fd);
|
||||||
|
if (s != len) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "pwm_disable: could not unexport pwm (%s)", strerror(e_no));
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// remove from list
|
// remove from list
|
||||||
pwm = exported_pwms;
|
pwm = exported_pwms;
|
||||||
while (pwm != NULL)
|
while (pwm != NULL)
|
||||||
|
@ -35,10 +35,11 @@ SOFTWARE.
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include "c_pwm.h"
|
#include "c_softpwm.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "event_gpio.h"
|
#include "event_gpio.h"
|
||||||
|
|
||||||
|
355
source/c_softservo.c
Normal file
355
source/c_softservo.c
Normal file
@ -0,0 +1,355 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2017 Robert Wolterman
|
||||||
|
|
||||||
|
Using CHIP_IO servo code from Brady Hurlburt as a basis for the servo code
|
||||||
|
|
||||||
|
Copyright (c) 2016 Brady Hurlburt
|
||||||
|
|
||||||
|
Original BBIO Author Justin Cooper
|
||||||
|
Modified for CHIP_IO Author Brady Hurlburt
|
||||||
|
|
||||||
|
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 <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "c_softservo.h"
|
||||||
|
#include "common.h"
|
||||||
|
#include "event_gpio.h"
|
||||||
|
|
||||||
|
#define KEYLEN 7
|
||||||
|
|
||||||
|
#define PERIOD 0
|
||||||
|
#define DUTY 1
|
||||||
|
|
||||||
|
#define MSTONS 1000000
|
||||||
|
#define BLOCKNS 20 * MSTONS
|
||||||
|
#define FUDGEFACTOR 450
|
||||||
|
#define MSTOMICROS 1000
|
||||||
|
#define MICROSTONS 1000
|
||||||
|
#define MINMICROS 1000
|
||||||
|
#define NEUTRAL 1500
|
||||||
|
#define MAXMICROS 2000
|
||||||
|
|
||||||
|
int servo_initialized = 0;
|
||||||
|
|
||||||
|
struct servo_params
|
||||||
|
{
|
||||||
|
float range;
|
||||||
|
float min_angle;
|
||||||
|
float max_angle;
|
||||||
|
float current_angle;
|
||||||
|
bool enabled;
|
||||||
|
bool stop_flag;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct servo
|
||||||
|
{
|
||||||
|
char key[KEYLEN+1]; /* leave room for terminating NUL byte */
|
||||||
|
int gpio;
|
||||||
|
struct servo_params params;
|
||||||
|
pthread_mutex_t* params_lock;
|
||||||
|
pthread_t thread;
|
||||||
|
struct servo *next;
|
||||||
|
};
|
||||||
|
struct servo *exported_servos = NULL;
|
||||||
|
|
||||||
|
struct servo *lookup_exported_servo(const char *key)
|
||||||
|
{
|
||||||
|
struct servo *srv = exported_servos;
|
||||||
|
|
||||||
|
while (srv != NULL)
|
||||||
|
{
|
||||||
|
if (strcmp(srv->key, key) == 0) {
|
||||||
|
return srv;
|
||||||
|
}
|
||||||
|
srv = srv->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL; /* standard for pointers */
|
||||||
|
}
|
||||||
|
|
||||||
|
void *servo_thread_toggle(void *arg)
|
||||||
|
{
|
||||||
|
struct servo *srv = (struct servo *)arg;
|
||||||
|
int gpio = srv->gpio;
|
||||||
|
struct timespec tim_on;
|
||||||
|
struct timespec tim_off;
|
||||||
|
float on_time_microsec = 0;
|
||||||
|
unsigned int on_ns;
|
||||||
|
unsigned int off_ns;
|
||||||
|
|
||||||
|
/* Used to determine if something has
|
||||||
|
* has changed
|
||||||
|
*/
|
||||||
|
float angle_local = 0;
|
||||||
|
float range_local = 0;
|
||||||
|
bool stop_flag_local = false;
|
||||||
|
bool enabled_local = false;
|
||||||
|
bool recalculate_timing = false;
|
||||||
|
|
||||||
|
while (!stop_flag_local) {
|
||||||
|
/* Take a snapshot of the parameter block */
|
||||||
|
pthread_mutex_lock(srv->params_lock);
|
||||||
|
if ((angle_local != srv->params.current_angle) || (range_local != srv->params.range)) {
|
||||||
|
recalculate_timing = true;
|
||||||
|
}
|
||||||
|
angle_local = srv->params.current_angle;
|
||||||
|
range_local = srv->params.range;
|
||||||
|
enabled_local = srv->params.enabled;
|
||||||
|
stop_flag_local = srv->params.stop_flag;
|
||||||
|
pthread_mutex_unlock(srv->params_lock);
|
||||||
|
|
||||||
|
/* If freq or duty has been changed, update the
|
||||||
|
* sleep times
|
||||||
|
*/
|
||||||
|
if (recalculate_timing) {
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo updating timing: new angle: (%.2f)\n",angle_local);
|
||||||
|
on_time_microsec = (((MAXMICROS - MINMICROS) / range_local) * angle_local) + NEUTRAL;
|
||||||
|
on_ns = (unsigned long)(on_time_microsec * MICROSTONS - FUDGEFACTOR);
|
||||||
|
off_ns = BLOCKNS - on_ns;
|
||||||
|
tim_on.tv_sec = 0;
|
||||||
|
tim_on.tv_nsec = on_ns;
|
||||||
|
tim_off.tv_sec = 0;
|
||||||
|
tim_off.tv_nsec = off_ns;
|
||||||
|
recalculate_timing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (enabled_local)
|
||||||
|
{
|
||||||
|
/* Set gpio */
|
||||||
|
gpio_set_value(gpio, HIGH);
|
||||||
|
|
||||||
|
nanosleep(&tim_on, NULL);
|
||||||
|
|
||||||
|
/* Unset gpio */
|
||||||
|
gpio_set_value(gpio, LOW);
|
||||||
|
|
||||||
|
nanosleep(&tim_off, NULL);
|
||||||
|
//printf("AFTER SECOND NANOSLEEP\n");
|
||||||
|
} /* if enabled_local */
|
||||||
|
} /* while !stop_flag_local */
|
||||||
|
|
||||||
|
gpio_set_value(gpio, LOW);
|
||||||
|
|
||||||
|
/* This servo has been disabled */
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int servo_start(const char *key, float angle, float range)
|
||||||
|
{
|
||||||
|
struct servo *new_srv, *srv;
|
||||||
|
pthread_t new_thread;
|
||||||
|
pthread_mutex_t *new_params_lock;
|
||||||
|
int gpio;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (get_gpio_number(key, &gpio) < 0) {
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_start: invalid gpio specified **\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gpio_export(gpio) < 0) {
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "Error setting up servo on pin %d, maybe already exported? (%s)", gpio, get_error_msg());
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_start: %d exported **\n", gpio);
|
||||||
|
|
||||||
|
if (gpio_set_direction(gpio, OUTPUT) < 0) {
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_start: gpio_set_direction failed **\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("c_softservo.c: servo_start(%d,%.2f,%.2f)\n",gpio,angle,range);
|
||||||
|
|
||||||
|
// add to list
|
||||||
|
new_srv = malloc(sizeof(struct servo)); ASSRT(new_srv != NULL);
|
||||||
|
new_params_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
|
||||||
|
if (new_srv == 0) {
|
||||||
|
return -1; // out of memory
|
||||||
|
}
|
||||||
|
pthread_mutex_init(new_params_lock, NULL);
|
||||||
|
pthread_mutex_lock(new_params_lock);
|
||||||
|
|
||||||
|
strncpy(new_srv->key, key, KEYLEN); /* can leave string unterminated */
|
||||||
|
new_srv->key[KEYLEN] = '\0'; /* terminate string */
|
||||||
|
new_srv->gpio = gpio;
|
||||||
|
new_srv->params.enabled = true;
|
||||||
|
new_srv->params.stop_flag = false;
|
||||||
|
new_srv->params_lock = new_params_lock;
|
||||||
|
new_srv->next = NULL;
|
||||||
|
|
||||||
|
if (exported_servos == NULL)
|
||||||
|
{
|
||||||
|
// create new list
|
||||||
|
exported_servos = new_srv;
|
||||||
|
} else {
|
||||||
|
// add to end of existing list
|
||||||
|
srv = exported_servos;
|
||||||
|
while (srv->next != NULL)
|
||||||
|
srv = srv->next;
|
||||||
|
srv->next = new_srv;
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(new_params_lock);
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_enable: setting servo parameters **\n");
|
||||||
|
ASSRT(servo_set_range(new_srv->key, range) == 0);
|
||||||
|
ASSRT(servo_set_angle(new_srv->key, angle) == 0);
|
||||||
|
|
||||||
|
pthread_mutex_lock(new_params_lock);
|
||||||
|
// create thread for srv
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_enable: creating thread **\n");
|
||||||
|
ret = pthread_create(&new_thread, NULL, servo_thread_toggle, (void *)new_srv);
|
||||||
|
ASSRT(ret == 0);
|
||||||
|
|
||||||
|
new_srv->thread = new_thread;
|
||||||
|
|
||||||
|
pthread_mutex_unlock(new_params_lock);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int servo_disable(const char *key)
|
||||||
|
{
|
||||||
|
struct servo *srv, *temp, *prev_srv = NULL;
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** in servo_disable **\n");
|
||||||
|
// remove from list
|
||||||
|
srv = exported_servos;
|
||||||
|
while (srv != NULL)
|
||||||
|
{
|
||||||
|
if (strcmp(srv->key, key) == 0)
|
||||||
|
{
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_disable: found pin **\n");
|
||||||
|
pthread_mutex_lock(srv->params_lock);
|
||||||
|
srv->params.stop_flag = true;
|
||||||
|
pthread_mutex_unlock(srv->params_lock);
|
||||||
|
pthread_join(srv->thread, NULL); /* wait for thread to exit */
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_disable: unexporting %d **\n", srv->gpio);
|
||||||
|
gpio_unexport(srv->gpio);
|
||||||
|
|
||||||
|
if (prev_srv == NULL)
|
||||||
|
{
|
||||||
|
exported_servos = srv->next;
|
||||||
|
prev_srv = srv;
|
||||||
|
} else {
|
||||||
|
prev_srv->next = srv->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp = srv;
|
||||||
|
srv = srv->next;
|
||||||
|
free(temp->params_lock);
|
||||||
|
free(temp);
|
||||||
|
} else {
|
||||||
|
prev_srv = srv;
|
||||||
|
srv = srv->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int servo_set_range(const char *key, float range)
|
||||||
|
{
|
||||||
|
struct servo *srv;
|
||||||
|
float min_angle, max_angle;
|
||||||
|
|
||||||
|
srv = lookup_exported_servo(key);
|
||||||
|
|
||||||
|
if (srv == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the min and max angle
|
||||||
|
max_angle = range / 2.0;
|
||||||
|
min_angle = -max_angle;
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_set_range(%d,%.2f = %.2f,%.2f)\n",srv->gpio,range,min_angle,max_angle);
|
||||||
|
pthread_mutex_lock(srv->params_lock);
|
||||||
|
srv->params.range = range;
|
||||||
|
srv->params.min_angle = min_angle;
|
||||||
|
srv->params.max_angle = max_angle;
|
||||||
|
pthread_mutex_unlock(srv->params_lock);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int servo_set_angle(const char *key, float angle)
|
||||||
|
{
|
||||||
|
struct servo *srv;
|
||||||
|
|
||||||
|
srv = lookup_exported_servo(key);
|
||||||
|
|
||||||
|
if (srv == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure we're between the range of allowable angles
|
||||||
|
if (angle < srv->params.min_angle || angle > srv->params.max_angle) {
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "Angle specified (%.2f) for pin %d, is outside allowable range (%.2f,%.2f)", angle, srv->gpio, srv->params.min_angle,srv->params.max_angle);
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** servo_set_angle(%d,%.2f)\n",srv->gpio,angle);
|
||||||
|
pthread_mutex_lock(srv->params_lock);
|
||||||
|
srv->params.current_angle = angle;
|
||||||
|
pthread_mutex_unlock(srv->params_lock);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void servo_cleanup(void)
|
||||||
|
{
|
||||||
|
while (exported_servos != NULL) {
|
||||||
|
servo_disable(exported_servos->key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
40
source/c_softservo.h
Normal file
40
source/c_softservo.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2017 Robert Wolterman
|
||||||
|
|
||||||
|
Using CHIP_IO SoftPWM code from Brady Hurlburt as a basis for the servo code
|
||||||
|
|
||||||
|
Copyright (c) 2016 Brady Hurlburt
|
||||||
|
|
||||||
|
Original BBIO Author Justin Cooper
|
||||||
|
Modified for CHIP_IO Author Brady Hurlburt
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
int servo_start(const char *key, float angle, float range);
|
||||||
|
int servo_disable(const char *key);
|
||||||
|
int servo_set_range(const char *key, float range);
|
||||||
|
int servo_set_angle(const char *key, float angle);
|
||||||
|
void servo_cleanup(void);
|
264
source/common.c
264
source/common.c
@ -53,90 +53,89 @@ int module_setup = 0;
|
|||||||
int DEBUG = 0;
|
int DEBUG = 0;
|
||||||
|
|
||||||
pins_t pins_info[] = {
|
pins_t pins_info[] = {
|
||||||
{ "GND", "GND", "U13_1", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U13_1", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CHG-IN", "CHG-IN", "U13_2", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CHG-IN", "CHG-IN", "U13_2", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "VCC-5V", "VCC-5V", "U13_3", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "VCC-5V", "VCC-5V", "U13_3", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "GND", "GND", "U13_4", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U13_4", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "VCC-3V3", "VCC-3V3", "U13_5", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "VCC-3V3", "VCC-3V3", "U13_5", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "TS", "TS", "U13_6", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "TS", "TS", "U13_6", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "VCC-1V8", "VCC-1V8", "U13_7", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "VCC-1V8", "VCC-1V8", "U13_7", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "BAT", "BAT", "U13_8", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "BAT", "BAT", "U13_8", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "TWI1-SDA", "KPD-I2C-SDA", "U13_9", 48, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "TWI1-SDA", "KPD-I2C-SDA", "U13_9", 48, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "PWRON", "PWRON", "U13_10", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "PWRON", "PWRON", "U13_10", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "TWI1-SCK", "KPD-I2C-SCL", "U13_11", 47, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "TWI1-SCK", "KPD-I2C-SCL", "U13_11", 47, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "GND", "GND", "U13_12", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U13_12", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "X1", "X1", "U13_13", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "X1", "X1", "U13_13", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "X2", "X2", "U13_14", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "X2", "X2", "U13_14", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "Y1", "Y1", "U13_15", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "Y1", "Y1", "U13_15", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "Y2", "Y2", "U13_16", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "Y2", "Y2", "U13_16", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D2", "LCD-D2", "U13_17", 98, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D2", "LCD-D2", "U13_17", 98, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "PWM0", "PWM0", "U13_18", 34, BASE_METHOD_AS_IS, 0, -1, SPWM_DISABLED},
|
{ "PWM0", "PWM0", "U13_18", 34, BASE_METHOD_AS_IS, 0, -1, BOTH},
|
||||||
{ "LCD-D4", "LCD-D4", "U13_19", 100, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D4", "LCD-D4", "U13_19", 100, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D3", "LCD-D3", "U13_20", 99, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D3", "LCD-D3", "U13_20", 99, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D6", "LCD-D6", "U13_21", 102, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D6", "LCD-D6", "U13_21", 102, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D5", "LCD-D5", "U13_22", 101, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D5", "LCD-D5", "U13_22", 101, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D10", "LCD-D10", "U13_23", 106, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D10", "LCD-D10", "U13_23", 106, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D7", "LCD-D7", "U13_24", 103, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D7", "LCD-D7", "U13_24", 103, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D12", "LCD-D12", "U13_25", 108, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D12", "LCD-D12", "U13_25", 108, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D11", "LCD-D11", "U13_26", 107, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D11", "LCD-D11", "U13_26", 107, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D14", "LCD-D14", "U13_27", 110, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D14", "LCD-D14", "U13_27", 110, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D13", "LCD-D13", "U13_28", 109, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D13", "LCD-D13", "U13_28", 109, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D18", "LCD-D18", "U13_29", 114, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D18", "LCD-D18", "U13_29", 114, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D15", "LCD-D15", "U13_30", 111, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D15", "LCD-D15", "U13_30", 111, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D20", "LCD-D20", "U13_31", 116, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D20", "LCD-D20", "U13_31", 116, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D19", "LCD-D19", "U13_32", 115, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D19", "LCD-D19", "U13_32", 115, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D22", "LCD-D22", "U13_33", 118, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D22", "LCD-D22", "U13_33", 118, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D21", "LCD-D21", "U13_34", 117, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D21", "LCD-D21", "U13_34", 117, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-CLK", "LCD-CLK", "U13_35", 120, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-CLK", "LCD-CLK", "U13_35", 120, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-D23", "LCD-D23", "U13_36", 119, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-D23", "LCD-D23", "U13_36", 119, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-VSYNC", "LCD-VSYNC", "U13_37", 123, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-VSYNC", "LCD-VSYNC", "U13_37", 123, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-HSYNC", "LCD-HSYNC", "U13_38", 122, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-HSYNC", "LCD-HSYNC", "U13_38", 122, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "GND", "GND", "U13_39", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U13_39", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LCD-DE", "LCD-DE", "U13_40", 121, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "LCD-DE", "LCD-DE", "U13_40", 121, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "GND", "GND", "U14_1", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U14_1", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "VCC-5V", "VCC-5V", "U14_2", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "VCC-5V", "VCC-5V", "U14_2", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "UART1-TX", "UART-TX", "U14_3", 195, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "UART1-TX", "UART-TX", "U14_3", 195, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "HPL", "HPL", "U14_4", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "HPL", "HPL", "U14_4", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "UART1-RX", "UART-RX", "U14_5", 196, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "UART1-RX", "UART-RX", "U14_5", 196, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "HPCOM", "HPCOM", "U14_6", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "HPCOM", "HPCOM", "U14_6", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "FEL", "FEL", "U14_7", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "FEL", "FEL", "U14_7", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "HPR", "HPR", "U14_8", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "HPR", "HPR", "U14_8", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "VCC-3V3", "VCC-3V3", "U14_9", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "VCC-3V3", "VCC-3V3", "U14_9", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "MICM", "MICM", "U14_10", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "MICM", "MICM", "U14_10", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "LRADC", "ADC", "U14_11", -1, BASE_METHOD_AS_IS, -1, 0, SPWM_DISABLED},
|
{ "LRADC", "ADC", "U14_11", -1, BASE_METHOD_AS_IS, -1, 0, BOTH},
|
||||||
{ "MICIN1", "MICIN1", "U14_12", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "MICIN1", "MICIN1", "U14_12", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "XIO-P0", "XIO-P0", "U14_13", 0, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P0", "XIO-P0", "U14_13", 0, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "XIO-P1", "XIO-P1", "U14_14", 1, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P1", "XIO-P1", "U14_14", 1, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "XIO-P2", "GPIO1", "U14_15", 2, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P2", "GPIO1", "U14_15", 2, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "XIO-P3", "GPIO2", "U14_16", 3, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P3", "GPIO2", "U14_16", 3, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "XIO-P4", "GPIO3", "U14_17", 4, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P4", "GPIO3", "U14_17", 4, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "XIO-P5", "GPIO4", "U14_18", 5, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P5", "GPIO4", "U14_18", 5, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "XIO-P6", "GPIO5", "U14_19", 6, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P6", "GPIO5", "U14_19", 6, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "XIO-P7", "GPIO6", "U14_20", 7, BASE_METHOD_XIO, -1, -1, SPWM_ENABLED},
|
{ "XIO-P7", "GPIO6", "U14_20", 7, BASE_METHOD_XIO, -1, -1, CHIP},
|
||||||
{ "GND", "GND", "U14_21", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U14_21", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "GND", "GND", "U14_22", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U14_22", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "AP-EINT1", "KPD-INT", "U14_23", 193, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "AP-EINT1", "KPD-INT", "U14_23", 193, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "AP-EINT3", "AP-INT3", "U14_24", 35, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "AP-EINT3", "AP-INT3", "U14_24", 35, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "TWI2-SDA", "I2C-SDA", "U14_25", 50, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "TWI2-SDA", "I2C-SDA", "U14_25", 50, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "TWI2-SCK", "I2C-SCL", "U14_26", 49, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "TWI2-SCK", "I2C-SCL", "U14_26", 49, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSIPCK", "SPI-SEL", "U14_27", 128, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSIPCK", "SPI-SEL", "U14_27", 128, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSICK", "SPI-CLK", "U14_28", 129, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSICK", "SPI-CLK", "U14_28", 129, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSIHSYNC", "SPI-MOSI", "U14_29", 130, BASE_METHOD_AS_IS, 1, -1, SPWM_DISABLED},
|
{ "CSIHSYNC", "SPI-MOSI", "U14_29", 130, BASE_METHOD_AS_IS, 1, -1, BOTH},
|
||||||
{ "CSIVSYNC", "SPI-MISO", "U14_30", 131, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSIVSYNC", "SPI-MISO", "U14_30", 131, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSID0", "CSID0", "U14_31", 132, BASE_METHOD_AS_IS, 1, -1, SPWM_DISABLED},
|
{ "CSID0", "CSID0", "U14_31", 132, BASE_METHOD_AS_IS, 1, -1, BOTH},
|
||||||
{ "CSID1", "CSID1", "U14_32", 133, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSID1", "CSID1", "U14_32", 133, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSID2", "CSID2", "U14_33", 134, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSID2", "CSID2", "U14_33", 134, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSID3", "CSID3", "U14_34", 135, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSID3", "CSID3", "U14_34", 135, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSID4", "CSID4", "U14_35", 136, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSID4", "CSID4", "U14_35", 136, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSID5", "CSID5", "U14_36", 137, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSID5", "CSID5", "U14_36", 137, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSID6", "CSID6", "U14_37", 138, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSID6", "CSID6", "U14_37", 138, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "CSID7", "CSID7", "U14_38", 139, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "CSID7", "CSID7", "U14_38", 139, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "GND", "GND", "U14_39", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U14_39", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ "GND", "GND", "U14_40", -1, BASE_METHOD_AS_IS, -1, -1, SPWM_DISABLED},
|
{ "GND", "GND", "U14_40", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||||
{ NULL, NULL, NULL, -1, 0, -1, -1, -1}
|
{ NULL, NULL, NULL, -1, 0, -1, -1, -1}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// CREDIT FOR THIS FUNCTION DUE TO HOWIE KATZ OF NTC AND STEVE FORD
|
// CREDIT FOR THIS FUNCTION DUE TO HOWIE KATZ OF NTC AND STEVE FORD
|
||||||
// THIS WILL FIND THE PROPER XIO BASE SYSFS NUMBER
|
// THIS WILL FIND THE PROPER XIO BASE SYSFS NUMBER
|
||||||
// PORTED TO C FORM HOWIE'S PYTHON CODE WITH THE HELP OF STEVE:
|
// PORTED TO C FORM HOWIE'S PYTHON CODE WITH THE HELP OF STEVE:
|
||||||
@ -250,6 +249,22 @@ int gpio_number(pins_t *pin)
|
|||||||
return gpio_num;
|
return gpio_num;
|
||||||
} /* gpio_number */
|
} /* gpio_number */
|
||||||
|
|
||||||
|
int gpio_pud_capable(pins_t *pin)
|
||||||
|
{
|
||||||
|
int capable = -1;
|
||||||
|
|
||||||
|
switch (pin->base_method) {
|
||||||
|
case BASE_METHOD_AS_IS:
|
||||||
|
capable = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BASE_METHOD_XIO:
|
||||||
|
capable = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return capable;
|
||||||
|
}
|
||||||
|
|
||||||
int lookup_gpio_by_key(const char *key)
|
int lookup_gpio_by_key(const char *key)
|
||||||
{
|
{
|
||||||
@ -284,6 +299,39 @@ int lookup_gpio_by_altname(const char *altname)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lookup_pud_capable_by_key(const char *key)
|
||||||
|
{
|
||||||
|
pins_t *p;
|
||||||
|
for (p = pins_info; p->key != NULL; ++p) {
|
||||||
|
if (strcmp(p->key, key) == 0) {
|
||||||
|
return gpio_pud_capable(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lookup_pud_capable_by_name(const char *name)
|
||||||
|
{
|
||||||
|
pins_t *p;
|
||||||
|
for (p = pins_info; p->name != NULL; ++p) {
|
||||||
|
if (strcmp(p->name, name) == 0) {
|
||||||
|
return gpio_pud_capable(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lookup_pud_capable_by_altname(const char *altname)
|
||||||
|
{
|
||||||
|
pins_t *p;
|
||||||
|
for (p = pins_info; p->altname != NULL; ++p) {
|
||||||
|
if (strcmp(p->altname, altname) == 0) {
|
||||||
|
return gpio_pud_capable(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int lookup_ain_by_key(const char *key)
|
int lookup_ain_by_key(const char *key)
|
||||||
{
|
{
|
||||||
pins_t *p;
|
pins_t *p;
|
||||||
@ -397,6 +445,33 @@ int get_gpio_number(const char *key, int *gpio)
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int compute_port_pin(const char *key, int gpio, int *port, int *pin)
|
||||||
|
{
|
||||||
|
int capable = 0;
|
||||||
|
int rtn = -1;
|
||||||
|
|
||||||
|
capable = lookup_pud_capable_by_key(key);
|
||||||
|
if (capable < 0) {
|
||||||
|
capable = lookup_pud_capable_by_name(key);
|
||||||
|
if (capable < 0) {
|
||||||
|
capable = lookup_gpio_by_altname(key);
|
||||||
|
if (capable < 0) {
|
||||||
|
capable = 0; // default to false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (capable) {
|
||||||
|
// Method from:
|
||||||
|
// https://bbs.nextthing.co/t/chippy-gonzales-fast-gpio/14056/6?u=xtacocorex
|
||||||
|
*port = gpio / 32;
|
||||||
|
*pin = gpio % 32;
|
||||||
|
rtn = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rtn;
|
||||||
|
}
|
||||||
|
|
||||||
int get_key(const char *input, char *key)
|
int get_key(const char *input, char *key)
|
||||||
{
|
{
|
||||||
if (!copy_key_by_key(input, key)) {
|
if (!copy_key_by_key(input, key)) {
|
||||||
@ -455,33 +530,6 @@ int build_path(const char *partial_path, const char *prefix, char *full_path, si
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_spi_bus_path_number(unsigned int spi)
|
|
||||||
{
|
|
||||||
char path[FILENAME_BUFFER_SIZE];
|
|
||||||
char ocp_dir[FILENAME_BUFFER_SIZE];
|
|
||||||
|
|
||||||
build_path("/sys/devices", "ocp", ocp_dir, sizeof(ocp_dir)); BUF2SMALL(ocp_dir);
|
|
||||||
|
|
||||||
if (spi == 0) {
|
|
||||||
snprintf(path, sizeof(path), "%s/48030000.spi/spi_master/spi1", ocp_dir); BUF2SMALL(path);
|
|
||||||
} else {
|
|
||||||
snprintf(path, sizeof(path), "%s/481a0000.spi/spi_master/spi1", ocp_dir); BUF2SMALL(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
DIR* dir = opendir(path);
|
|
||||||
if (dir) {
|
|
||||||
closedir(dir);
|
|
||||||
//device is using /dev/spidev1.x
|
|
||||||
return 1;
|
|
||||||
} else if (ENOENT == errno) {
|
|
||||||
//device is using /dev/spidev2.x
|
|
||||||
return 2;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// We do not know at compile time how many GPIOs there are, so it is not safe
|
// We do not know at compile time how many GPIOs there are, so it is not safe
|
||||||
// to declare per-GPIO arrays with a static size. The "dyn_int_array_*"
|
// to declare per-GPIO arrays with a static size. The "dyn_int_array_*"
|
||||||
// functions implement a dynamic integer array which grows as needed at run
|
// functions implement a dynamic integer array which grows as needed at run
|
||||||
|
@ -56,6 +56,7 @@ SOFTWARE.
|
|||||||
#define BCM 11
|
#define BCM 11
|
||||||
#define CHIP 0
|
#define CHIP 0
|
||||||
#define CHIPPRO 1
|
#define CHIPPRO 1
|
||||||
|
#define BOTH 2
|
||||||
|
|
||||||
// In the pins_t structure, the "base_method" field tells how
|
// In the pins_t structure, the "base_method" field tells how
|
||||||
// the "gpio" field should be interpreted.
|
// the "gpio" field should be interpreted.
|
||||||
@ -68,11 +69,12 @@ typedef struct pins_t {
|
|||||||
const char *name;
|
const char *name;
|
||||||
const char *altname; /* alternate name as referenced on pocketchip pin header */
|
const char *altname; /* alternate name as referenced on pocketchip pin header */
|
||||||
const char *key;
|
const char *key;
|
||||||
|
//const char *altkey; /* alternate key for chip pro */
|
||||||
int gpio; /* port number to use under /sys/class/gpio */
|
int gpio; /* port number to use under /sys/class/gpio */
|
||||||
int base_method; /* modifier for port number; see BASE_METHOD_... */
|
int base_method; /* modifier for port number; see BASE_METHOD_... */
|
||||||
int pwm_mux_mode; /* pwm pin */
|
int pwm_mux_mode; /* pwm pin */
|
||||||
int ain; /* analog pin */
|
int ain; /* analog pin */
|
||||||
int spwm_allow; /* pin allowed for software pwm */
|
int sbc_type; /* which sbc pin is allowed */
|
||||||
} pins_t;
|
} pins_t;
|
||||||
|
|
||||||
|
|
||||||
@ -90,9 +92,13 @@ int DEBUG;
|
|||||||
|
|
||||||
int get_xio_base(void);
|
int get_xio_base(void);
|
||||||
int gpio_number(pins_t *pin);
|
int gpio_number(pins_t *pin);
|
||||||
|
int gpio_pud_capable(pins_t *pin);
|
||||||
int lookup_gpio_by_key(const char *key);
|
int lookup_gpio_by_key(const char *key);
|
||||||
int lookup_gpio_by_name(const char *name);
|
int lookup_gpio_by_name(const char *name);
|
||||||
int lookup_gpio_by_altname(const char *altname);
|
int lookup_gpio_by_altname(const char *altname);
|
||||||
|
int lookup_pud_capable_by_key(const char *key);
|
||||||
|
int lookup_pud_capable_by_name(const char *name);
|
||||||
|
int lookup_pud_capable_by_altname(const char *altname);
|
||||||
int lookup_ain_by_key(const char *key);
|
int lookup_ain_by_key(const char *key);
|
||||||
int lookup_ain_by_name(const char *name);
|
int lookup_ain_by_name(const char *name);
|
||||||
int copy_key_by_key(const char *input_key, char *key);
|
int copy_key_by_key(const char *input_key, char *key);
|
||||||
@ -104,7 +110,6 @@ int get_key(const char *input, char *key);
|
|||||||
int get_pwm_key(const char *input, char *key);
|
int get_pwm_key(const char *input, char *key);
|
||||||
int get_adc_ain(const char *key, unsigned int *ain);
|
int get_adc_ain(const char *key, unsigned int *ain);
|
||||||
int build_path(const char *partial_path, const char *prefix, char *full_path, size_t full_path_len);
|
int build_path(const char *partial_path, const char *prefix, char *full_path, size_t full_path_len);
|
||||||
int get_spi_bus_path_number(unsigned int spi);
|
|
||||||
void dyn_int_array_set(dyn_int_array_t **in_array, int i, int val, int initial_val);
|
void dyn_int_array_set(dyn_int_array_t **in_array, int i, int val, int initial_val);
|
||||||
int dyn_int_array_get(dyn_int_array_t **in_array, int i, int initial_val);
|
int dyn_int_array_get(dyn_int_array_t **in_array, int i, int initial_val);
|
||||||
void dyn_int_array_delete(dyn_int_array_t **in_array);
|
void dyn_int_array_delete(dyn_int_array_t **in_array);
|
||||||
@ -112,3 +117,4 @@ void clear_error_msg(void);
|
|||||||
char *get_error_msg(void);
|
char *get_error_msg(void);
|
||||||
void add_error_msg(char *msg);
|
void add_error_msg(char *msg);
|
||||||
void toggle_debug(void);
|
void toggle_debug(void);
|
||||||
|
int compute_port_pin(const char *key, int gpio, int *port, int *pin);
|
||||||
|
@ -88,6 +88,6 @@ void define_constants(PyObject *module)
|
|||||||
module_debug = Py_BuildValue("i", DEBUG ? Py_True: Py_False);
|
module_debug = Py_BuildValue("i", DEBUG ? Py_True: Py_False);
|
||||||
PyModule_AddObject(module, "DEBUG", module_debug);
|
PyModule_AddObject(module, "DEBUG", module_debug);
|
||||||
|
|
||||||
version = Py_BuildValue("s", "0.3.3");
|
version = Py_BuildValue("s", "0.4.0");
|
||||||
PyModule_AddObject(module, "VERSION", version);
|
PyModule_AddObject(module, "VERSION", version);
|
||||||
}
|
}
|
||||||
|
@ -38,17 +38,23 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include "event_gpio.h"
|
#include "event_gpio.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
const char *stredge[4] = {"none", "rising", "falling", "both"};
|
const char *stredge[4] = {"none", "rising", "falling", "both"};
|
||||||
|
|
||||||
|
// Memory Map for PUD
|
||||||
|
uint8_t *memmap;
|
||||||
|
|
||||||
// file descriptors
|
// file descriptors
|
||||||
struct fdx
|
struct fdx
|
||||||
{
|
{
|
||||||
@ -85,6 +91,63 @@ dyn_int_array_t *event_occurred = NULL;
|
|||||||
int thread_running = 0;
|
int thread_running = 0;
|
||||||
int epfd = -1;
|
int epfd = -1;
|
||||||
|
|
||||||
|
// Thanks to WereCatf and Chippy-Gonzales for the Memory Mapping code/help
|
||||||
|
int map_pio_memory()
|
||||||
|
{
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** map_pio_memory: opening /dev/mem **\n");
|
||||||
|
int fd = open("/dev/mem", O_RDWR|O_SYNC);
|
||||||
|
if(fd < 0) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "map_pio_memory: could not open /dev/mem (%s)", strerror(errno));
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// uint32_t addr = 0x01c20800 & ~(getpagesize() - 1);
|
||||||
|
//Requires memmap to be on pagesize-boundary
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** map_pio_memory: mapping memory **\n");
|
||||||
|
memmap = (uint8_t *)mmap(NULL, getpagesize()*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x01C20000);
|
||||||
|
if(memmap == NULL) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "map_pio_memory: mmap failed (%s)", strerror(errno));
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
//Set memmap to point to PIO-registers
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** map_pio_memory: moving to pio registers **\n");
|
||||||
|
memmap=memmap+0x800;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gpio_get_pud(int port, int pin)
|
||||||
|
{
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** gpio_get_pud: port %d, pin %d **\n", port, pin);
|
||||||
|
volatile uint32_t *pioMem32, *configRegister;
|
||||||
|
pioMem32=(uint32_t *)(memmap+port*0x24+0x1c); //0x1c == pull-register
|
||||||
|
configRegister=pioMem32+(pin >> 4);
|
||||||
|
return *configRegister >> ((pin & 15) * 2) & 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gpio_set_pud(int port, int pin, uint8_t value)
|
||||||
|
{
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** gpio_set_pud: port %d, pin %d, value %d **\n", port, pin, value);
|
||||||
|
value &= 3;
|
||||||
|
volatile uint32_t *pioMem32, *configRegister;
|
||||||
|
uint32_t mask;
|
||||||
|
pioMem32=(uint32_t *)(memmap+port*0x24+0x1c); //0x1c == pull-register
|
||||||
|
configRegister=pioMem32+(pin >> 4);
|
||||||
|
mask = ~(3 << ((pin & 15) * 2));
|
||||||
|
*configRegister &= mask;
|
||||||
|
*configRegister |= value << ((pin & 15) * 2);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int gpio_export(int gpio)
|
int gpio_export(int gpio)
|
||||||
{
|
{
|
||||||
@ -119,7 +182,7 @@ int gpio_export(int gpio)
|
|||||||
|
|
||||||
// add to list
|
// add to list
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
printf(" ** gpio_export: creating data struct **\n");
|
printf(" ** gpio_export: creating data struct **\n");
|
||||||
new_gpio = malloc(sizeof(struct gpio_exp)); ASSRT(new_gpio != NULL);
|
new_gpio = malloc(sizeof(struct gpio_exp)); ASSRT(new_gpio != NULL);
|
||||||
|
|
||||||
new_gpio->gpio = gpio;
|
new_gpio->gpio = gpio;
|
||||||
@ -423,8 +486,8 @@ int gpio_set_value(int gpio, unsigned int value)
|
|||||||
strncpy(vstr, "0", ARRAY_SIZE(vstr) - 1);
|
strncpy(vstr, "0", ARRAY_SIZE(vstr) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG)
|
//if (DEBUG)
|
||||||
printf(" ** gpio_set_value: writing %s **\n", vstr);
|
// printf(" ** gpio_set_value: writing %s **\n", vstr);
|
||||||
|
|
||||||
ssize_t s = write(fd, vstr, strlen(vstr)); e_no = errno;
|
ssize_t s = write(fd, vstr, strlen(vstr)); e_no = errno;
|
||||||
|
|
||||||
@ -443,8 +506,7 @@ int gpio_get_value(int gpio, unsigned int *value)
|
|||||||
int fd = fd_lookup(gpio);
|
int fd = fd_lookup(gpio);
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
if (!fd)
|
if (!fd) {
|
||||||
{
|
|
||||||
if ((fd = open_value_file(gpio)) == -1) {
|
if ((fd = open_value_file(gpio)) == -1) {
|
||||||
char err[256];
|
char err[256];
|
||||||
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d value file", gpio);
|
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d value file", gpio);
|
||||||
@ -484,6 +546,58 @@ int gpio_get_value(int gpio, unsigned int *value)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int gpio_get_more(int gpio, int bits, unsigned int *value)
|
||||||
|
{
|
||||||
|
int fd = fd_lookup(gpio);
|
||||||
|
char ch;
|
||||||
|
|
||||||
|
if (!fd) {
|
||||||
|
if ((fd = open_value_file(gpio)) == -1) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "gpio_get_more: could not open GPIO %d value file", gpio);
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop for our number of bits
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < bits; i++) {
|
||||||
|
|
||||||
|
if (lseek(fd, 0, SEEK_SET) < 0) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "gpio_get_more: could not seek GPIO %d (%s)", gpio, strerror(errno));
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ssize_t s = read(fd, &ch, sizeof(ch));
|
||||||
|
if (s < 0) {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "gpio_get_more: could not read GPIO %d (%s)", gpio, strerror(errno));
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == '1') {
|
||||||
|
*value |= (1 << i);
|
||||||
|
} else if (ch == '0') {
|
||||||
|
*value |= (0 << i);
|
||||||
|
} else {
|
||||||
|
char err[256];
|
||||||
|
snprintf(err, sizeof(err), "gpio_get_more: unrecognized read GPIO %d (%c)", gpio, ch);
|
||||||
|
add_error_msg(err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (DEBUG) {
|
||||||
|
printf(" ** gpio_get_more: %c **\n", ch);
|
||||||
|
printf(" ** gpio_get_more: current value: %u **\n", *value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int gpio_set_edge(int gpio, unsigned int edge)
|
int gpio_set_edge(int gpio, unsigned int edge)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -36,6 +36,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#define NO_EDGE 0
|
#define NO_EDGE 0
|
||||||
#define RISING_EDGE 1
|
#define RISING_EDGE 1
|
||||||
#define FALLING_EDGE 2
|
#define FALLING_EDGE 2
|
||||||
@ -54,6 +56,12 @@ SOFTWARE.
|
|||||||
#define PUD_DOWN 1
|
#define PUD_DOWN 1
|
||||||
#define PUD_UP 2
|
#define PUD_UP 2
|
||||||
|
|
||||||
|
extern uint8_t *memmap;
|
||||||
|
|
||||||
|
int map_pio_memory(void);
|
||||||
|
int gpio_get_pud(int port, int pin);
|
||||||
|
int gpio_set_pud(int port, int pin, uint8_t value);
|
||||||
|
|
||||||
int gpio_export(int gpio);
|
int gpio_export(int gpio);
|
||||||
int gpio_unexport(int gpio);
|
int gpio_unexport(int gpio);
|
||||||
void exports_cleanup(void);
|
void exports_cleanup(void);
|
||||||
@ -61,6 +69,7 @@ int gpio_set_direction(int gpio, unsigned int in_flag);
|
|||||||
int gpio_get_direction(int gpio, unsigned int *value);
|
int gpio_get_direction(int gpio, unsigned int *value);
|
||||||
int gpio_set_value(int gpio, unsigned int value);
|
int gpio_set_value(int gpio, unsigned int value);
|
||||||
int gpio_get_value(int gpio, unsigned int *value);
|
int gpio_get_value(int gpio, unsigned int *value);
|
||||||
|
int gpio_get_more(int gpio, int bits, unsigned int *value);
|
||||||
int fd_lookup(int gpio);
|
int fd_lookup(int gpio);
|
||||||
int open_value_file(int gpio);
|
int open_value_file(int gpio);
|
||||||
|
|
||||||
|
249
source/py_gpio.c
249
source/py_gpio.c
@ -58,14 +58,34 @@ struct py_callback
|
|||||||
};
|
};
|
||||||
static struct py_callback *py_callbacks = NULL;
|
static struct py_callback *py_callbacks = NULL;
|
||||||
|
|
||||||
|
// python function toggle_debug()
|
||||||
|
static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
// toggle debug printing
|
||||||
|
toggle_debug();
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
static int init_module(void)
|
static int init_module(void)
|
||||||
{
|
{
|
||||||
|
clear_error_msg();
|
||||||
|
|
||||||
|
if (map_pio_memory() < 0) {
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "init_module error (%s)", get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we make it here, we're good to go
|
||||||
|
if (DEBUG)
|
||||||
|
printf(" ** init_module: setup complete **\n");
|
||||||
module_setup = 1;
|
module_setup = 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void remember_gpio_direction(int gpio, int direction)
|
static void remember_gpio_direction(int gpio, int direction)
|
||||||
{
|
{
|
||||||
dyn_int_array_set(&gpio_direction, gpio, direction, -1);
|
dyn_int_array_set(&gpio_direction, gpio, direction, -1);
|
||||||
@ -93,14 +113,11 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The !channel fixes issues #50
|
// The !channel fixes issues #50
|
||||||
if (!channel || strcmp(channel, "") == 0) {
|
if (channel == NULL || strcmp(channel, "\0") == 0) {
|
||||||
event_cleanup();
|
event_cleanup();
|
||||||
} else {
|
} else {
|
||||||
if (get_gpio_number(channel, &gpio) < 0) {
|
if (get_gpio_number(channel, &gpio) < 0) {
|
||||||
char err[2000];
|
event_cleanup();
|
||||||
snprintf(err, sizeof(err), "Invalid channel %s. (%s)", channel, get_error_msg());
|
|
||||||
PyErr_SetString(PyExc_ValueError, err);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
gpio_unexport(gpio);
|
gpio_unexport(gpio);
|
||||||
}
|
}
|
||||||
@ -111,69 +128,87 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||||||
// python function setup(channel, direction, pull_up_down=PUD_OFF, initial=None)
|
// python function setup(channel, direction, pull_up_down=PUD_OFF, initial=None)
|
||||||
static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
int gpio;
|
int gpio;
|
||||||
char *channel;
|
char *channel;
|
||||||
int direction;
|
int direction;
|
||||||
int pud = PUD_OFF;
|
int pud = PUD_OFF;
|
||||||
int initial = 0;
|
int initial = 0;
|
||||||
static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL};
|
static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL};
|
||||||
|
|
||||||
clear_error_msg();
|
clear_error_msg();
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|ii", kwlist, &channel, &direction, &pud, &initial))
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|ii", kwlist, &channel, &direction, &pud, &initial))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!module_setup) {
|
if (!module_setup) {
|
||||||
init_module();
|
init_module();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (direction != INPUT && direction != OUTPUT)
|
||||||
if (direction != INPUT && direction != OUTPUT)
|
{
|
||||||
{
|
PyErr_SetString(PyExc_ValueError, "An invalid direction was passed to setup()");
|
||||||
PyErr_SetString(PyExc_ValueError, "An invalid direction was passed to setup()");
|
return NULL;
|
||||||
return NULL;
|
}
|
||||||
}
|
|
||||||
|
// Force pud to be off if we're configured for output
|
||||||
if (direction == OUTPUT)
|
if (direction == OUTPUT) {
|
||||||
pud = PUD_OFF;
|
pud = PUD_OFF;
|
||||||
|
}
|
||||||
if (pud != PUD_OFF && pud != PUD_DOWN && pud != PUD_UP)
|
|
||||||
{
|
if (pud != PUD_OFF && pud != PUD_DOWN && pud != PUD_UP)
|
||||||
PyErr_SetString(PyExc_ValueError, "Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN");
|
{
|
||||||
return NULL;
|
PyErr_SetString(PyExc_ValueError, "Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN");
|
||||||
}
|
return NULL;
|
||||||
|
}
|
||||||
if (get_gpio_number(channel, &gpio) < 0) {
|
|
||||||
char err[2000];
|
if (get_gpio_number(channel, &gpio) < 0) {
|
||||||
snprintf(err, sizeof(err), "Invalid channel %s. (%s)", channel, get_error_msg());
|
char err[2000];
|
||||||
PyErr_SetString(PyExc_ValueError, err);
|
snprintf(err, sizeof(err), "Invalid channel %s. (%s)", channel, get_error_msg());
|
||||||
return NULL;
|
PyErr_SetString(PyExc_ValueError, err);
|
||||||
}
|
return NULL;
|
||||||
|
}
|
||||||
if (gpio_export(gpio) < 0) {
|
|
||||||
char err[2000];
|
if (gpio_export(gpio) < 0) {
|
||||||
snprintf(err, sizeof(err), "Error setting up channel %s, maybe already exported? (%s)", channel, get_error_msg());
|
char err[2000];
|
||||||
PyErr_SetString(PyExc_RuntimeError, err);
|
snprintf(err, sizeof(err), "Error setting up channel %s, maybe already exported? (%s)", channel, get_error_msg());
|
||||||
return NULL;
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
}
|
return NULL;
|
||||||
if (gpio_set_direction(gpio, direction) < 0) {
|
}
|
||||||
char err[2000];
|
if (gpio_set_direction(gpio, direction) < 0) {
|
||||||
snprintf(err, sizeof(err), "Error setting direction %d on channel %s. (%s)", direction, channel, get_error_msg());
|
char err[2000];
|
||||||
PyErr_SetString(PyExc_RuntimeError, err);
|
snprintf(err, sizeof(err), "Error setting direction %d on channel %s. (%s)", direction, channel, get_error_msg());
|
||||||
return NULL;
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
}
|
return NULL;
|
||||||
if (direction == OUTPUT) {
|
}
|
||||||
if (gpio_set_value(gpio, initial) < 0) {
|
|
||||||
|
// Pull Up/Down
|
||||||
|
// Only if the pin we want is able to use it (R8 Owned, no XIO)
|
||||||
|
int port, pin;
|
||||||
|
if (compute_port_pin(channel, gpio, &port, &pin) == 0) {
|
||||||
|
// Set the PUD
|
||||||
|
gpio_set_pud(port, pin, pud);
|
||||||
|
// Check it was set properly
|
||||||
|
int pudr = gpio_get_pud(port, pin);
|
||||||
|
if (pudr != pud) {
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "Error setting pull up down %d on channel %s", pud, channel);
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (direction == OUTPUT) {
|
||||||
|
if (gpio_set_value(gpio, initial) < 0) {
|
||||||
char err[2000];
|
char err[2000];
|
||||||
snprintf(err, sizeof(err), "Error setting initial value %d on channel %s. (%s)", initial, channel, get_error_msg());
|
snprintf(err, sizeof(err), "Error setting initial value %d on channel %s. (%s)", initial, channel, get_error_msg());
|
||||||
PyErr_SetString(PyExc_RuntimeError, err);
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
remember_gpio_direction(gpio, direction);
|
remember_gpio_direction(gpio, direction);
|
||||||
|
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
} /* py_setup_channel */
|
} /* py_setup_channel */
|
||||||
|
|
||||||
|
|
||||||
@ -251,6 +286,83 @@ static PyObject *py_input_gpio(PyObject *self, PyObject *args)
|
|||||||
return py_value;
|
return py_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: Come up with a way to merge py_read_byte_gpio and py_read_word_gpio
|
||||||
|
// python function value = read_byte(channel)
|
||||||
|
static PyObject *py_read_byte_gpio(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
int gpio;
|
||||||
|
char *channel;
|
||||||
|
unsigned int value = 0;
|
||||||
|
PyObject *py_value;
|
||||||
|
|
||||||
|
clear_error_msg();
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "s", &channel))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (get_gpio_number(channel, &gpio)) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Invalid channel");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check channel is set up as an input or output
|
||||||
|
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
|
||||||
|
{
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel first");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only want to get a 8 bits here
|
||||||
|
if (gpio_get_more(gpio, 8, &value) < 0) {
|
||||||
|
char err[1024];
|
||||||
|
snprintf(err, sizeof(err), "Could not get 8 bits of data ('%s')", get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
py_value = Py_BuildValue("i", value);
|
||||||
|
|
||||||
|
return py_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// python function value = read_word(channel)
|
||||||
|
static PyObject *py_read_word_gpio(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
int gpio;
|
||||||
|
char *channel;
|
||||||
|
unsigned int value = 0;
|
||||||
|
PyObject *py_value;
|
||||||
|
|
||||||
|
clear_error_msg();
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "s", &channel))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (get_gpio_number(channel, &gpio)) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Invalid channel");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check channel is set up as an input or output
|
||||||
|
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
|
||||||
|
{
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel first");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only want to get a 8 bits here
|
||||||
|
if (gpio_get_more(gpio, 16, &value) < 0) {
|
||||||
|
char err[1024];
|
||||||
|
snprintf(err, sizeof(err), "Could not get 16 bits of data ('%s')", get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
py_value = Py_BuildValue("i", value);
|
||||||
|
|
||||||
|
return py_value;
|
||||||
|
}
|
||||||
|
|
||||||
static void run_py_callbacks(int gpio, void* data)
|
static void run_py_callbacks(int gpio, void* data)
|
||||||
{
|
{
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
@ -743,10 +855,6 @@ static PyObject *py_selftest(PyObject *self, PyObject *args)
|
|||||||
ASSRT(0 == build_path("/home", "ip", fp, sizeof(fp)));
|
ASSRT(0 == build_path("/home", "ip", fp, sizeof(fp)));
|
||||||
ASSRT(0 == build_path("/NOTFOUND", "ch", fp, sizeof(fp)));
|
ASSRT(0 == build_path("/NOTFOUND", "ch", fp, sizeof(fp)));
|
||||||
|
|
||||||
printf("Testing get_spi_bus_path_number\n");
|
|
||||||
ASSRT(2 == get_spi_bus_path_number(0)); /* doesn't really work on CHIP */
|
|
||||||
ASSRT(2 == get_spi_bus_path_number(1)); /* doesn't really work on CHIP */
|
|
||||||
|
|
||||||
printf("Testing error message buffer\n");
|
printf("Testing error message buffer\n");
|
||||||
clear_error_msg();
|
clear_error_msg();
|
||||||
ASSRT(0 == strlen(get_error_msg()));
|
ASSRT(0 == strlen(get_error_msg()));
|
||||||
@ -825,6 +933,8 @@ PyMethodDef gpio_methods[] = {
|
|||||||
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS | METH_KEYWORDS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
|
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS | METH_KEYWORDS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
|
||||||
{"output", py_output_gpio, METH_VARARGS, "Output to a GPIO channel\ngpio - gpio channel\nvalue - 0/1 or False/True or LOW/HIGH"},
|
{"output", py_output_gpio, METH_VARARGS, "Output to a GPIO channel\ngpio - gpio channel\nvalue - 0/1 or False/True or LOW/HIGH"},
|
||||||
{"input", py_input_gpio, METH_VARARGS, "Input from a GPIO channel. Returns HIGH=1=True or LOW=0=False\ngpio - gpio channel"},
|
{"input", py_input_gpio, METH_VARARGS, "Input from a GPIO channel. Returns HIGH=1=True or LOW=0=False\ngpio - gpio channel"},
|
||||||
|
{"read_byte", py_read_byte_gpio, METH_VARARGS, "Read a byte (8 bits) from a set of GPIO channels. Returns 8-bits of integer data\ngpio - gpio channel."},
|
||||||
|
{"read_word", py_read_word_gpio, METH_VARARGS, "Read a word (16 bits) from a set of GPIO channels. Returns 16-bits of integer data\ngpio - gpio channel."},
|
||||||
{"add_event_detect", (PyCFunction)py_add_event_detect, METH_VARARGS | METH_KEYWORDS, "Enable edge detection events for a particular GPIO channel.\nchannel - either board pin number or BCM number depending on which mode is set.\nedge - RISING, FALLING or BOTH\n[callback] - A callback function for the event (optional)\n[bouncetime] - Switch bounce timeout in ms for callback"},
|
{"add_event_detect", (PyCFunction)py_add_event_detect, METH_VARARGS | METH_KEYWORDS, "Enable edge detection events for a particular GPIO channel.\nchannel - either board pin number or BCM number depending on which mode is set.\nedge - RISING, FALLING or BOTH\n[callback] - A callback function for the event (optional)\n[bouncetime] - Switch bounce timeout in ms for callback"},
|
||||||
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\ngpio - gpio channel"},
|
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\ngpio - gpio channel"},
|
||||||
{"event_detected", py_event_detected, METH_VARARGS, "Returns True if an edge has occured on a given GPIO. You need to enable edge detection using add_event_detect() first.\ngpio - gpio channel"},
|
{"event_detected", py_event_detected, METH_VARARGS, "Returns True if an edge has occured on a given GPIO. You need to enable edge detection using add_event_detect() first.\ngpio - gpio channel"},
|
||||||
@ -836,6 +946,7 @@ PyMethodDef gpio_methods[] = {
|
|||||||
{"selftest", py_selftest, METH_VARARGS, "Internal unit tests"},
|
{"selftest", py_selftest, METH_VARARGS, "Internal unit tests"},
|
||||||
{"direction", (PyCFunction)py_set_direction, METH_VARARGS | METH_KEYWORDS, "Change direction of gpio channel. Either INPUT or OUTPUT\n" },
|
{"direction", (PyCFunction)py_set_direction, METH_VARARGS | METH_KEYWORDS, "Change direction of gpio channel. Either INPUT or OUTPUT\n" },
|
||||||
{"setmode", (PyCFunction)py_setmode, METH_VARARGS, "Dummy function that does nothing but maintain compatibility with RPi.GPIO\n" },
|
{"setmode", (PyCFunction)py_setmode, METH_VARARGS, "Dummy function that does nothing but maintain compatibility with RPi.GPIO\n" },
|
||||||
|
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
|
||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -73,14 +73,12 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (duty_cycle < 0.0 || duty_cycle > 100.0)
|
if (duty_cycle < 0.0 || duty_cycle > 100.0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frequency <= 0.0)
|
if (frequency <= 0.0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
|
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -90,8 +88,12 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!pwm_start(key, duty_cycle, frequency, polarity))
|
if (pwm_start(key, duty_cycle, frequency, polarity) < 0) {
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "Unable to start PWM: %s (%s)", channel, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_ValueError, err);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
@ -112,7 +114,12 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pwm_disable(key);
|
if (pwm_disable(key) < 0) {
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_ValueError, err);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
@ -130,8 +137,7 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
|||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &duty_cycle))
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &duty_cycle))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (duty_cycle < 0.0 || duty_cycle > 100.0)
|
if (duty_cycle < 0.0 || duty_cycle > 100.0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -142,7 +148,9 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pwm_set_duty_cycle(key, duty_cycle) == -1) {
|
if (pwm_set_duty_cycle(key, duty_cycle) == -1) {
|
||||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_ValueError, err);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,20 +178,20 @@ static PyObject *py_set_pulse_width_ns(PyObject *self, PyObject *args, PyObject
|
|||||||
|
|
||||||
// Get the period out of the data struct
|
// Get the period out of the data struct
|
||||||
int rtn = pwm_get_period_ns(key, &period_ns);
|
int rtn = pwm_get_period_ns(key, &period_ns);
|
||||||
if (rtn == -1)
|
if (rtn == -1) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "period unable to be obtained");
|
PyErr_SetString(PyExc_ValueError, "period unable to be obtained");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pulse_width_ns < 0.0 || pulse_width_ns > period_ns)
|
if (pulse_width_ns < 0.0 || pulse_width_ns > period_ns) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "pulse width must have a value from 0 to period");
|
PyErr_SetString(PyExc_ValueError, "pulse width must have a value from 0 to period");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pwm_set_pulse_width_ns(key, pulse_width_ns) == -1) {
|
if (pwm_set_pulse_width_ns(key, pulse_width_ns) < 0) {
|
||||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_ValueError, err);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,8 +211,7 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency))
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (frequency <= 0.0)
|
if (frequency <= 0.0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
|
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -214,8 +221,10 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pwm_set_frequency(key, frequency) == -1) {
|
if (pwm_set_frequency(key, frequency) < 0) {
|
||||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_ValueError, err);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,8 +244,7 @@ static PyObject *py_set_period_ns(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|k", kwlist, &channel, &period_ns))
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|k", kwlist, &channel, &period_ns))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (period_ns <= 0)
|
if (period_ns <= 0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "period must be greater than 0ns");
|
PyErr_SetString(PyExc_ValueError, "period must be greater than 0ns");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -246,8 +254,10 @@ static PyObject *py_set_period_ns(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pwm_set_period_ns(key, period_ns) == -1) {
|
if (pwm_set_period_ns(key, period_ns) < 0) {
|
||||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_ValueError, err);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
258
source/py_servo.c
Normal file
258
source/py_servo.c
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2017 Robert Wolterman
|
||||||
|
|
||||||
|
Using CHIP_IO servo code from Brady Hurlburt as a basis for the servo code
|
||||||
|
|
||||||
|
Copyright (c) 2016 Brady Hurlburt
|
||||||
|
|
||||||
|
Original BBIO Author Justin Cooper
|
||||||
|
Modified for CHIP_IO Author Brady Hurlburt
|
||||||
|
|
||||||
|
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 "Python.h"
|
||||||
|
#include "constants.h"
|
||||||
|
#include "common.h"
|
||||||
|
#include "c_softservo.h"
|
||||||
|
|
||||||
|
// python function toggle_debug()
|
||||||
|
static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
// toggle debug printing
|
||||||
|
toggle_debug();
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// python function cleanup()
|
||||||
|
static PyObject *py_cleanup(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
// unexport the Servo
|
||||||
|
servo_cleanup();
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// python function start(channel, angle, range)
|
||||||
|
static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
{
|
||||||
|
int gpio;
|
||||||
|
char key[8];
|
||||||
|
char *channel = NULL;
|
||||||
|
float angle = 0.0;
|
||||||
|
float range = 180.0;
|
||||||
|
static char *kwlist[] = {"channel", "angle", "range", NULL};
|
||||||
|
|
||||||
|
clear_error_msg();
|
||||||
|
|
||||||
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ff", kwlist, &channel, &angle, &range)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ASSRT(channel != NULL);
|
||||||
|
|
||||||
|
if (!get_key(channel, key)) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Invalid Servo key or name.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check to ensure gpio is one of the allowed pins
|
||||||
|
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||||
|
get_gpio_number(channel, &gpio);
|
||||||
|
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (servo_start(key, angle, range) < 0) {
|
||||||
|
printf("servo_start failed");
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "Error starting servo on pin %s (%s)", key, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// python function stop(channel)
|
||||||
|
static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
{
|
||||||
|
int gpio;
|
||||||
|
char key[8];
|
||||||
|
char *channel;
|
||||||
|
|
||||||
|
clear_error_msg();
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "s", &channel))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (!get_key(channel, key)) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Invalid key or name");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check to ensure gpio is one of the allowed pins
|
||||||
|
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||||
|
get_gpio_number(channel, &gpio);
|
||||||
|
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
servo_disable(key);
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// python method SERVO.set_range(channel, duty_cycle)
|
||||||
|
static PyObject *py_set_range(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
{
|
||||||
|
int gpio;
|
||||||
|
char key[8];
|
||||||
|
char *channel;
|
||||||
|
float range = 180.0;
|
||||||
|
static char *kwlist[] = {"channel", "range", NULL};
|
||||||
|
|
||||||
|
clear_error_msg();
|
||||||
|
|
||||||
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &range))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (range > 360.0) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "range can not be greater than 360.0");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!get_key(channel, key)) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Invalid key or name.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check to ensure gpio is one of the allowed pins
|
||||||
|
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||||
|
get_gpio_number(channel, &gpio);
|
||||||
|
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (servo_set_range(key, range) == -1) {
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, "You must start() the Servo channel first");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// python method SERVO.set_angle(channel, duty_cycle)
|
||||||
|
static PyObject *py_set_angle(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
{
|
||||||
|
int gpio;
|
||||||
|
char key[8];
|
||||||
|
char *channel;
|
||||||
|
float angle = 0.0;
|
||||||
|
static char *kwlist[] = {"channel", "angle", NULL};
|
||||||
|
|
||||||
|
clear_error_msg();
|
||||||
|
|
||||||
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &angle))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
//if (range > 360.0) {
|
||||||
|
// PyErr_SetString(PyExc_ValueError, "range can not be greater than 360.0");
|
||||||
|
// return NULL;
|
||||||
|
//}
|
||||||
|
|
||||||
|
if (!get_key(channel, key)) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Invalid key or name.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check to ensure gpio is one of the allowed pins
|
||||||
|
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||||
|
get_gpio_number(channel, &gpio);
|
||||||
|
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (servo_set_angle(key, angle) == -1) {
|
||||||
|
char err[2000];
|
||||||
|
snprintf(err, sizeof(err), "Error setting servo angle on pin %s (%s)", key, get_error_msg());
|
||||||
|
PyErr_SetString(PyExc_RuntimeError, err);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char moduledocstring[] = "Software Servo functionality of a CHIP using Python";
|
||||||
|
|
||||||
|
PyMethodDef servo_methods[] = {
|
||||||
|
{"start", (PyCFunction)py_start_channel, METH_VARARGS | METH_KEYWORDS, "Set up and start the Servo. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
||||||
|
{"stop", (PyCFunction)py_stop_channel, METH_VARARGS | METH_KEYWORDS, "Stop the Servo. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
||||||
|
{"set_range", (PyCFunction)py_set_range, METH_VARARGS, "Change the servo range\nrange - max angular range of the servo" },
|
||||||
|
{"set_angle", (PyCFunction)py_set_angle, METH_VARARGS, "Change the servo angle\nangle - angle of the servo between +/-(range/2)" },
|
||||||
|
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS, "Clean up by resetting All or one Servo that have been used by this program."},
|
||||||
|
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
|
||||||
|
{NULL, NULL, 0, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION > 2
|
||||||
|
static struct PyModuleDef chipservomodule = {
|
||||||
|
PyModuleDef_HEAD_INIT,
|
||||||
|
"SERVO", // name of module
|
||||||
|
moduledocstring, // module documentation, may be NULL
|
||||||
|
-1, // size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
|
||||||
|
servo_methods
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION > 2
|
||||||
|
PyMODINIT_FUNC PyInit_SERVO(void)
|
||||||
|
#else
|
||||||
|
PyMODINIT_FUNC initSERVO(void)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
PyObject *module = NULL;
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION > 2
|
||||||
|
if ((module = PyModule_Create(&chipservomodule)) == NULL)
|
||||||
|
return NULL;
|
||||||
|
#else
|
||||||
|
if ((module = Py_InitModule3("SERVO", servo_methods, moduledocstring)) == NULL)
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
define_constants(module);
|
||||||
|
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION > 2
|
||||||
|
return module;
|
||||||
|
#else
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
}
|
@ -34,29 +34,20 @@ SOFTWARE.
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "c_softpwm.h"
|
#include "c_softpwm.h"
|
||||||
|
|
||||||
// python function cleanup(channel=None)
|
// python function toggle_debug()
|
||||||
|
static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
// toggle debug printing
|
||||||
|
toggle_debug();
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// python function cleanup()
|
||||||
static PyObject *py_cleanup(PyObject *self, PyObject *args)
|
static PyObject *py_cleanup(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
// unexport the PWM
|
// unexport the PWM
|
||||||
char key[8];
|
softpwm_cleanup();
|
||||||
char *channel = NULL;
|
|
||||||
|
|
||||||
clear_error_msg();
|
|
||||||
|
|
||||||
// Channel is optional
|
|
||||||
if (!PyArg_ParseTuple(args, "|s", &channel))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
// The !channel fixes issue #50
|
|
||||||
if (!channel || strcmp(channel, "") == 0) {
|
|
||||||
softpwm_cleanup();
|
|
||||||
} else {
|
|
||||||
if (!get_key(channel, key)) {
|
|
||||||
PyErr_SetString(PyExc_ValueError, "Invalid SOFTPWM key or name.");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
softpwm_disable(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
@ -83,14 +74,12 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (duty_cycle < 0.0 || duty_cycle > 100.0)
|
if (duty_cycle < 0.0 || duty_cycle > 100.0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frequency <= 0.0)
|
if (frequency <= 0.0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
|
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -100,8 +89,7 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (softpwm_start(key, duty_cycle, frequency, polarity) < 0)
|
if (softpwm_start(key, duty_cycle, frequency, polarity) < 0) {
|
||||||
{
|
|
||||||
printf("softpwm_start failed");
|
printf("softpwm_start failed");
|
||||||
char err[2000];
|
char err[2000];
|
||||||
snprintf(err, sizeof(err), "Error starting softpwm on pin %s (%s)", key, get_error_msg());
|
snprintf(err, sizeof(err), "Error starting softpwm on pin %s (%s)", key, get_error_msg());
|
||||||
@ -146,8 +134,7 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
|||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &duty_cycle))
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &duty_cycle))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (duty_cycle < 0.0 || duty_cycle > 100.0)
|
if (duty_cycle < 0.0 || duty_cycle > 100.0) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -178,8 +165,7 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency))
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if ((frequency <= 0.0) || (frequency > 10000.0))
|
if ((frequency <= 0.0) || (frequency > 10000.0)) {
|
||||||
{
|
|
||||||
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0 and less than 10000.0");
|
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0 and less than 10000.0");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -200,11 +186,12 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
|||||||
static const char moduledocstring[] = "Software PWM functionality of a CHIP using Python";
|
static const char moduledocstring[] = "Software PWM functionality of a CHIP using Python";
|
||||||
|
|
||||||
PyMethodDef pwm_methods[] = {
|
PyMethodDef pwm_methods[] = {
|
||||||
{ "start", (PyCFunction)py_start_channel, METH_VARARGS | METH_KEYWORDS, "Set up and start the PWM channel. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
{"start", (PyCFunction)py_start_channel, METH_VARARGS | METH_KEYWORDS, "Set up and start the PWM channel. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
||||||
{ "stop", (PyCFunction)py_stop_channel, METH_VARARGS | METH_KEYWORDS, "Stop the PWM channel. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
{"stop", (PyCFunction)py_stop_channel, METH_VARARGS | METH_KEYWORDS, "Stop the PWM channel. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
||||||
{ "set_duty_cycle", (PyCFunction)py_set_duty_cycle, METH_VARARGS, "Change the duty cycle\ndutycycle - between 0.0 and 100.0" },
|
{"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)" },
|
{"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"},
|
{"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"},
|
||||||
|
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
|
||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import CHIP_IO.LRADC as ADC
|
import CHIP_IO.LRADC as ADC
|
||||||
|
|
||||||
# == ENABLE DEBUG ==
|
# == ENABLE DEBUG ==
|
71
test/servotest.py
Normal file
71
test/servotest.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import CHIP_IO.SERVO as SERVO
|
||||||
|
import CHIP_IO.GPIO as GPIO
|
||||||
|
import time
|
||||||
|
import datetime
|
||||||
|
import threading
|
||||||
|
|
||||||
|
class ServoTestReceiver(threading.Thread):
|
||||||
|
def __init__(self,gpio,key,maxcount=20,sleeptime=0.005):
|
||||||
|
self.gpio = gpio
|
||||||
|
self.key = key
|
||||||
|
self.counter = 0
|
||||||
|
self.maxcount = maxcount
|
||||||
|
self.sleeptime = sleeptime
|
||||||
|
self.dead = False
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
|
def kill(self):
|
||||||
|
self.dead = True
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
print("SETTING UP RECEIVER GPIO")
|
||||||
|
self.gpio.cleanup()
|
||||||
|
self.gpio.setup(self.key, self.gpio.IN)
|
||||||
|
print("STARTING RECEIVE LOOP")
|
||||||
|
try:
|
||||||
|
#while self.counter < self.maxcount:
|
||||||
|
while not self.dead:
|
||||||
|
pwmval = self.gpio.input(self.key)
|
||||||
|
print("SERVO VALUE: {0} @ {1}".format(pwmval, datetime.datetime.now()))
|
||||||
|
time.sleep(self.sleeptime)
|
||||||
|
self.counter += 1
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
self.gpio.cleanup(self.key)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# SETUP VARIABLES
|
||||||
|
SERVOGPIO = "CSID1" #"XIO-P7"
|
||||||
|
RECEIVERGPIO = "CSID7"
|
||||||
|
COUNT = 120
|
||||||
|
SLEEPTIME = 0.0001
|
||||||
|
RANGE = 180
|
||||||
|
|
||||||
|
# SETUP PWM
|
||||||
|
try:
|
||||||
|
print("SERVO START")
|
||||||
|
SERVO.toggle_debug()
|
||||||
|
SERVO.start(SERVOGPIO, 0, RANGE)
|
||||||
|
|
||||||
|
# SETUP SERVO RECEIVER
|
||||||
|
#rcvr = ServoTestReceiver(GPIO, RECEIVERGPIO, COUNT, SLEEPTIME)
|
||||||
|
#rcvr.start()
|
||||||
|
|
||||||
|
# LOOP THROUGH RANGE
|
||||||
|
for i in range(-(RANGE/2),(RANGE/2),5):
|
||||||
|
print("SETTING ANGLE: {0}".format(i))
|
||||||
|
SERVO.set_angle(SERVOGPIO, i)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
#rcvr.kill()
|
||||||
|
raw_input("PRESS ENTER WHEN DONE")
|
||||||
|
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
# CLEANUP
|
||||||
|
print("CLEANUP")
|
||||||
|
SERVO.stop(SERVOGPIO)
|
||||||
|
SERVO.cleanup(SERVOGPIO)
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user