-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.c
257 lines (204 loc) · 5.74 KB
/
gpio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
+----------------------------------------------------------------------+
| GPIO Extension |
+----------------------------------------------------------------------+
| Copyright (c) 2014 Francis Besset |
+----------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met (BSD-3-Clause). |
+----------------------------------------------------------------------+
| Author: Francis Besset <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_gpio.h"
#include "wiringPi.h"
#ifdef COMPILE_DL_GPIO
ZEND_GET_MODULE(gpio)
#endif
#ifndef TRUE
#define TRUE (1==1)
#define FALSE (1==2)
#endif
static int isInitialized = FALSE;
static int wpMode = WPI_MODE_PINS;
PHP_MINIT_FUNCTION(gpio)
{
/* wp modes */
REGISTER_LONG_CONSTANT("GPIO_MODE_PINS", WPI_MODE_PINS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("GPIO_MODE_GPIO", WPI_MODE_GPIO, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("GPIO_MODE_GPIO_SYS", WPI_MODE_GPIO_SYS, CONST_CS | CONST_PERSISTENT);
/* values */
REGISTER_LONG_CONSTANT("GPIO_LOW", LOW, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("GPIO_HIGH", HIGH, CONST_CS | CONST_PERSISTENT);
/* pin modes */
REGISTER_LONG_CONSTANT("GPIO_INPUT", INPUT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("GPIO_OUTPUT", OUTPUT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("GPIO_PWM", PWM_OUTPUT, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
ZEND_BEGIN_ARG_INFO(arginfo_gpio_mode, 0)
ZEND_ARG_INFO(0, mode)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_gpio_pin_mode, 0)
ZEND_ARG_INFO(0, pin)
ZEND_ARG_INFO(0, mode)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_gpio_pwm, 0)
ZEND_ARG_INFO(0, pin)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_gpio_read, 0)
ZEND_ARG_INFO(0, pin)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_gpio_write, 0)
ZEND_ARG_INFO(0, pin)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
zend_function_entry gpio_functions[] = {
PHP_FE(gpio_mode, arginfo_gpio_mode)
PHP_FE(gpio_pin_mode, arginfo_gpio_pin_mode)
PHP_FE(gpio_pwm, arginfo_gpio_pwm)
PHP_FE(gpio_read, arginfo_gpio_read)
PHP_FE(gpio_write, arginfo_gpio_write)
{NULL, NULL, NULL}
};
zend_module_entry gpio_module_entry = {
STANDARD_MODULE_HEADER,
"gpio",
gpio_functions,
PHP_MINIT(gpio),
NULL,
NULL,
NULL,
NULL,
PHP_GPIO_VERSION,
STANDARD_MODULE_PROPERTIES
};
PHP_FUNCTION(gpio_mode)
{
// return the current mode
ZVAL_LONG(return_value,wpMode);
// call function without args (read current mode)
if (ZEND_NUM_ARGS() == 0) {
return;
}
long newMode;
// call function with 1 arg (set new mode)
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &newMode) == FAILURE) {
return;
}
if (newMode == wpMode) {
return; // do nothing
}
switch (newMode) {
case WPI_MODE_GPIO:
wpMode = WPI_MODE_GPIO;
break;
case WPI_MODE_GPIO_SYS:
wpMode = WPI_MODE_GPIO_SYS;
break;
case WPI_MODE_PINS:
wpMode = WPI_MODE_PINS;
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid GPIO mode");
return;
}
isInitialized = FALSE;
}
PHP_FUNCTION(gpio_pin_mode)
{
long pin, mode;
char fGpio[128];
FILE *pFile;
if(zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &pin, &mode) == FAILURE) {
return;
} else if (!_wp_setup_mode()) {
return;
}
if (mode == PWM_OUTPUT) {
softPwmCreate((int) pin, 0, 100);
} else if (wpMode != WPI_MODE_GPIO_SYS) {
pinMode((int) pin, (int) mode);
} else {
sprintf(fGpio, "/sys/class/gpio/gpio%ld/value", pin);
// check if gpio is not exported
if (! (pFile = fopen(fGpio, "r"))) {
pFile = fopen ("/sys/class/gpio/export", "a");
fprintf(pFile, "%ld\n", pin);
}
fclose(pFile);
sprintf(fGpio, "/sys/class/gpio/gpio%ld/direction", pin);
do {
pFile = fopen (fGpio, "a");
} while (pFile == NULL);
// set new direction
if (mode == INPUT) {
fprintf(pFile, "in\n");
} else {
fprintf(pFile, "out\n");
}
fclose (pFile);
// force reinitialized to save the new status of GPIO
isInitialized = FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(gpio_pwm)
{
long pin, value;
if(zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &pin, &value) == FAILURE) {
return;
} else if (!_wp_setup_mode()) {
return;
}
softPwmWrite((int) pin, (int) value);
RETURN_TRUE;
}
PHP_FUNCTION(gpio_read)
{
long pin, result;
if(zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pin) == FAILURE) {
return;
} else if (!_wp_setup_mode()) {
return;
}
result = (long) digitalRead((int) pin);
{
ZVAL_LONG(return_value,result);
}
return;
}
PHP_FUNCTION(gpio_write)
{
long pin, value;
if(zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &pin, &value) == FAILURE) {
return;
} else if (!_wp_setup_mode()) {
return;
}
digitalWrite((int) pin, (int) value);
RETURN_TRUE;
}
int _wp_setup_mode(void)
{
if (isInitialized) {
return 1;
} else if (wpMode == WPI_MODE_GPIO_SYS) {
wiringPiSetupSys();
} else if (geteuid() != 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must be root to use the current mode. (Did you forget sudo?)");
return 0;
} else if (wpMode == WPI_MODE_GPIO) {
wiringPiSetupGpio();
} else {
wiringPiSetup();
}
isInitialized = TRUE;
return 1;
}