| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "internal.h" |
| 5 |
|
| 6 |
typedef struct { |
| 7 |
PyObject_HEAD; |
| 8 |
struct gpiod_line_settings *settings; |
| 9 |
} line_settings_object; |
| 10 |
|
| 11 |
static int set_error(void) |
| 12 |
{ |
| 13 |
Py_gpiod_SetErrFromErrno(); |
| 14 |
return -1; |
| 15 |
} |
| 16 |
|
| 17 |
static int |
| 18 |
line_settings_init(line_settings_object *self, PyObject *args, PyObject *kwargs) |
| 19 |
{ |
| 20 |
static char *kwlist[] = { |
| 21 |
"direction", |
| 22 |
"edge_detection", |
| 23 |
"bias", |
| 24 |
"drive", |
| 25 |
"active_low", |
| 26 |
"debounce_period", |
| 27 |
"event_clock", |
| 28 |
"output_value", |
| 29 |
NULL |
| 30 |
}; |
| 31 |
|
| 32 |
enum gpiod_line_clock event_clock; |
| 33 |
enum gpiod_line_direction direction; |
| 34 |
enum gpiod_line_value output_value; |
| 35 |
unsigned long debounce_period; |
| 36 |
enum gpiod_line_drive drive; |
| 37 |
enum gpiod_line_edge edge; |
| 38 |
enum gpiod_line_bias bias; |
| 39 |
int ret, active_low; |
| 40 |
|
| 41 |
ret = PyArg_ParseTupleAndKeywords(args, kwargs, "IIIIpkII", kwlist, |
| 42 |
&direction, &edge, &bias, &drive, &active_low, |
| 43 |
&debounce_period, &event_clock, &output_value); |
| 44 |
if (!ret) |
| 45 |
return -1; |
| 46 |
|
| 47 |
self->settings = gpiod_line_settings_new(); |
| 48 |
if (!self->settings) { |
| 49 |
Py_gpiod_SetErrFromErrno(); |
| 50 |
return -1; |
| 51 |
} |
| 52 |
|
| 53 |
ret = gpiod_line_settings_set_direction(self->settings, direction); |
| 54 |
if (ret) |
| 55 |
return set_error(); |
| 56 |
|
| 57 |
ret = gpiod_line_settings_set_edge_detection(self->settings, edge); |
| 58 |
if (ret) |
| 59 |
return set_error(); |
| 60 |
|
| 61 |
ret = gpiod_line_settings_set_bias(self->settings, bias); |
| 62 |
if (ret) |
| 63 |
return set_error(); |
| 64 |
|
| 65 |
ret = gpiod_line_settings_set_drive(self->settings, drive); |
| 66 |
if (ret) |
| 67 |
return set_error(); |
| 68 |
|
| 69 |
gpiod_line_settings_set_active_low(self->settings, active_low); |
| 70 |
gpiod_line_settings_set_debounce_period_us(self->settings, |
| 71 |
debounce_period); |
| 72 |
|
| 73 |
ret = gpiod_line_settings_set_edge_detection(self->settings, edge); |
| 74 |
if (ret) |
| 75 |
return set_error(); |
| 76 |
|
| 77 |
ret = gpiod_line_settings_set_output_value(self->settings, |
| 78 |
output_value); |
| 79 |
if (ret) |
| 80 |
return set_error(); |
| 81 |
|
| 82 |
ret = gpiod_line_settings_set_event_clock(self->settings, event_clock); |
| 83 |
if (ret) |
| 84 |
return set_error(); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static void line_settings_finalize(line_settings_object *self) |
| 90 |
{ |
| 91 |
if (self->settings) |
| 92 |
gpiod_line_settings_free(self->settings); |
| 93 |
} |
| 94 |
|
| 95 |
PyTypeObject line_settings_type = { |
| 96 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 97 |
.tp_name = "gpiod._ext.LineSettings", |
| 98 |
.tp_basicsize = sizeof(line_settings_object), |
| 99 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 100 |
.tp_new = PyType_GenericNew, |
| 101 |
.tp_init = (initproc)line_settings_init, |
| 102 |
.tp_finalize = (destructor)line_settings_finalize, |
| 103 |
.tp_dealloc = (destructor)Py_gpiod_dealloc, |
| 104 |
}; |
| 105 |
|
| 106 |
struct gpiod_line_settings *Py_gpiod_LineSettingsGetData(PyObject *obj) |
| 107 |
{ |
| 108 |
line_settings_object *settings; |
| 109 |
PyObject *type; |
| 110 |
|
| 111 |
type = PyObject_Type(obj); |
| 112 |
if (!type) |
| 113 |
return NULL; |
| 114 |
|
| 115 |
if ((PyTypeObject *)type != &line_settings_type) { |
| 116 |
PyErr_SetString(PyExc_TypeError, |
| 117 |
"not a gpiod._ext.LineSettings object"); |
| 118 |
Py_DECREF(type); |
| 119 |
return NULL; |
| 120 |
} |
| 121 |
Py_DECREF(type); |
| 122 |
|
| 123 |
settings = (line_settings_object *)obj; |
| 124 |
|
| 125 |
return settings->settings; |
| 126 |
} |