mirror of
https://github.com/xtacocorex/CHIP_IO
synced 2025-07-20 12:53:22 +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:
@ -268,6 +268,12 @@ static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject
|
|||||||
if (get_gpio_number(channel, &gpio))
|
if (get_gpio_number(channel, &gpio))
|
||||||
return NULL;
|
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
|
// check channel is set up as an input
|
||||||
if (!module_setup || gpio_direction[gpio] != 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))
|
if (get_gpio_number(channel, &gpio))
|
||||||
return NULL;
|
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
|
// check channel is set up as an input
|
||||||
if (!module_setup || gpio_direction[gpio] != 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))
|
if (get_gpio_number(channel, &gpio))
|
||||||
return NULL;
|
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
|
// remove all python callbacks for gpio
|
||||||
while (cb != NULL)
|
while (cb != NULL)
|
||||||
{
|
{
|
||||||
@ -413,6 +431,12 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
|
|||||||
if (get_gpio_number(channel, &gpio))
|
if (get_gpio_number(channel, &gpio))
|
||||||
return NULL;
|
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
|
// check channel is setup as an input
|
||||||
if (!module_setup || gpio_direction[gpio] != INPUT)
|
if (!module_setup || gpio_direction[gpio] != INPUT)
|
||||||
{
|
{
|
||||||
|
@ -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[] = {
|
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'"},
|
{"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
|
#if PY_MAJOR_VERSION > 2
|
||||||
static struct PyModuleDef bbpwmmodule = {
|
static struct PyModuleDef chippwmmodule = {
|
||||||
PyModuleDef_HEAD_INIT,
|
PyModuleDef_HEAD_INIT,
|
||||||
"PWM", // name of module
|
"PWM", // name of module
|
||||||
moduledocstring, // module documentation, may be NULL
|
moduledocstring, // module documentation, may be NULL
|
||||||
@ -196,7 +196,7 @@ PyMODINIT_FUNC initPWM(void)
|
|||||||
PyObject *module = NULL;
|
PyObject *module = NULL;
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_MAJOR_VERSION > 2
|
||||||
if ((module = PyModule_Create(&bbpwmmodule)) == NULL)
|
if ((module = PyModule_Create(&chippwmmodule)) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
#else
|
#else
|
||||||
if ((module = Py_InitModule3("PWM", pwm_methods, moduledocstring)) == NULL)
|
if ((module = Py_InitModule3("PWM", pwm_methods, moduledocstring)) == NULL)
|
||||||
|
@ -1,35 +1,73 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import CHIP_IO.GPIO as GPIO
|
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)
|
GPIO.setup("CSID0", GPIO.OUT)
|
||||||
|
|
||||||
#print os.path.exists('/sys/class/gpio/gpio132')
|
# VERIFY SIMPLE FUNCTIONALITY
|
||||||
|
print "VERIFY SIMPLE FUNCTIONALITY"
|
||||||
|
|
||||||
print "SETUP XIO-P1"
|
print "READING XIO-PI"
|
||||||
GPIO.setup("XIO-P1", GPIO.IN)
|
|
||||||
#GPIO.setup("U14_13", GPIO.IN)
|
|
||||||
|
|
||||||
print "READING XIO-P1"
|
|
||||||
GPIO.output("CSID0", GPIO.HIGH)
|
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)
|
||||||
GPIO.output("CSID0", GPIO.LOW)
|
print "LOW", GPIO.input("XIO-P0")
|
||||||
time.sleep(1)
|
|
||||||
print "LOW", GPIO.input("XIO-P1")
|
|
||||||
|
|
||||||
GPIO.output("CSID0", GPIO.HIGH)
|
# ==============================================
|
||||||
GPIO.output("CSID0", GPIO.HIGH)
|
# EDGE DETECTION - EXPANDED GPIO
|
||||||
print "HIGH", GPIO.input("XIO-P1")
|
print "SETTING UP EDGE DETECTION"
|
||||||
time.sleep(1)
|
GPIO.add_event_detect("XIO-P0",GPIO.FALLING,myfuncallback)
|
||||||
|
|
||||||
GPIO.output("CSID0", GPIO.LOW)
|
print "VERIFYING EDGE DETECT"
|
||||||
GPIO.output("CSID0", GPIO.LOW)
|
f = open("/sys/class/gpio/gpio408/edge","r")
|
||||||
print "LOW", GPIO.input("XIO-P1")
|
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"
|
print "CLEANUP"
|
||||||
|
GPIO.remove_event_detect("XIO-P0")
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user