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

Adding protection for to the GPIO to only allow edge detection and call back on the XIO pins, clean up of the PWM, updated test code

This commit is contained in:
Robert Wolterman
2016-04-02 19:48:57 -05:00
parent cea44c287d
commit c2ab013a5d
3 changed files with 84 additions and 22 deletions

View File

@ -268,6 +268,12 @@ static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject
if (get_gpio_number(channel, &gpio))
return NULL;
// check to ensure gpio is on the expanded pins as those are the only ones that allow for edge settings
if (gpio < 408) {
PyErr_SetString(PyExc_RuntimeError, "Callbacks currently available on XIO-P0 to XIO-P7 only");
return NULL;
}
// check channel is set up as an input
if (!module_setup || gpio_direction[gpio] != INPUT)
{
@ -309,6 +315,12 @@ static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *k
if (get_gpio_number(channel, &gpio))
return NULL;
// check to ensure gpio is on the expanded pins as those are the only ones that allow for edge settings
if (gpio < 408) {
PyErr_SetString(PyExc_RuntimeError, "Edge Detection currently available on XIO-P0 to XIO-P7 only");
return NULL;
}
// check channel is set up as an input
if (!module_setup || gpio_direction[gpio] != INPUT)
{
@ -357,6 +369,12 @@ static PyObject *py_remove_event_detect(PyObject *self, PyObject *args)
if (get_gpio_number(channel, &gpio))
return NULL;
// check to ensure gpio is on the expanded pins as those are the only ones that allow for edge settings
if (gpio < 408) {
PyErr_SetString(PyExc_RuntimeError, "Edge Detection currently available on XIO-P0 to XIO-P7 only");
return NULL;
}
// remove all python callbacks for gpio
while (cb != NULL)
{
@ -413,6 +431,12 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
if (get_gpio_number(channel, &gpio))
return NULL;
// check to ensure gpio is on the expanded pins as those are the only ones that allow for edge settings
if (gpio < 408) {
PyErr_SetString(PyExc_RuntimeError, "Edge Detection currently available on XIO-P0 to XIO-P7 only");
return NULL;
}
// check channel is setup as an input
if (!module_setup || gpio_direction[gpio] != INPUT)
{

View File

@ -165,7 +165,7 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
}
static const char moduledocstring[] = "PWM functionality of a CHIP using Python";
static const char moduledocstring[] = "Hardware PWM functionality of a CHIP using Python";
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 'PWM0', or 'U13_18'"},
@ -178,7 +178,7 @@ PyMethodDef pwm_methods[] = {
};
#if PY_MAJOR_VERSION > 2
static struct PyModuleDef bbpwmmodule = {
static struct PyModuleDef chippwmmodule = {
PyModuleDef_HEAD_INIT,
"PWM", // name of module
moduledocstring, // module documentation, may be NULL
@ -196,7 +196,7 @@ PyMODINIT_FUNC initPWM(void)
PyObject *module = NULL;
#if PY_MAJOR_VERSION > 2
if ((module = PyModule_Create(&bbpwmmodule)) == NULL)
if ((module = PyModule_Create(&chippwmmodule)) == NULL)
return NULL;
#else
if ((module = Py_InitModule3("PWM", pwm_methods, moduledocstring)) == NULL)

View File

@ -1,35 +1,73 @@
#!/usr/bin/python
import CHIP_IO.GPIO as GPIO
import time, os
import time, os, threading
print "SETUP CSIDO"
def myfuncallback(channel):
print "CALLBACK LIKE DRAKE IN HOTLINE BLING"
def loopfunction():
print "LOOP FUNCTION"
for i in xrange(20):
if i % 2:
print "SETTING CSID0 LOW"
GPIO.output("CSID0",GPIO.LOW)
else:
print "SETTING CSID0 HIGH"
GPIO.output("CSID0",GPIO.HIGH)
print "SLEEPING"
time.sleep(1)
print "SETUP XIO-P0"
GPIO.setup("XIO-P0", GPIO.IN)
print "SETUP CSID0"
GPIO.setup("CSID0", GPIO.OUT)
#print os.path.exists('/sys/class/gpio/gpio132')
# VERIFY SIMPLE FUNCTIONALITY
print "VERIFY SIMPLE FUNCTIONALITY"
print "SETUP XIO-P1"
GPIO.setup("XIO-P1", GPIO.IN)
#GPIO.setup("U14_13", GPIO.IN)
print "READING XIO-P1"
print "READING XIO-PI"
GPIO.output("CSID0", GPIO.HIGH)
print "HIGH", GPIO.input("XIO-P1")
print "HIGH", GPIO.input("XIO-P0")
GPIO.output("CSID0", GPIO.LOW)
GPIO.output("CSID0", GPIO.LOW)
time.sleep(1)
print "LOW", GPIO.input("XIO-P1")
print "LOW", GPIO.input("XIO-P0")
GPIO.output("CSID0", GPIO.HIGH)
GPIO.output("CSID0", GPIO.HIGH)
print "HIGH", GPIO.input("XIO-P1")
time.sleep(1)
# ==============================================
# EDGE DETECTION - EXPANDED GPIO
print "SETTING UP EDGE DETECTION"
GPIO.add_event_detect("XIO-P0",GPIO.FALLING,myfuncallback)
GPIO.output("CSID0", GPIO.LOW)
GPIO.output("CSID0", GPIO.LOW)
print "LOW", GPIO.input("XIO-P1")
print "VERIFYING EDGE DETECT"
f = open("/sys/class/gpio/gpio408/edge","r")
edge = f.read()
f.close()
print "EDGE: %s" % edge
# LOOP WRITING ON CSID0 TO HOPEFULLY GET CALLBACK TO WORK
print "WAITING FOR CALLBACKS"
loopfunction()
print "PRESS CONTROL-C TO EXIT IF SCRIPT GETS STUCK"
GPIO.remove_event_detect("XIO-P0")
try:
# WAIT FOR EDGE
t = threading.Thread(target=loopfunction)
t.start()
print "WAITING FOR EDGE"
GPIO.wait_for_edge("XIO-P0",GPIO.FALLING)
print "WE'VE FALLEN LIKE COOLIO'S CAREER"
print "ATTEMPTING TO CANCEL THE TIMER"
t.cancel()
except:
pass
print "TESTING ERRORS THROWN WHEN SPECIFYING EDGE DETECTION ON UNAUTHORIZED GPIO"
GPIO.setup("CSID1",GPIO.IN)
GPIO.add_event_detect("CSID1",GPIO.FALLING,myfuncallback)
print "CLEANUP"
GPIO.remove_event_detect("XIO-P0")
GPIO.cleanup()