-
Notifications
You must be signed in to change notification settings - Fork 3
/
OmegaOnion.php
305 lines (249 loc) · 7.56 KB
/
OmegaOnion.php
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
namespace ageeweb\php_onion;
/**
* @package php_onion
*
* @copyright Copyright (C) 2015 AGeeWeb.nl - All rights reserved.
* @license MIT
*/
/**
* class for use with Onion Omega
* sets GPIO direction / on / off
* sets relay on / off
* sets onboard dock LED
* how to use see HowToUse.php
*/
/*
* setting up php: (in command line mode)
*
* >opkg update
* >opkg install php5 php5-cgi zoneinfo-europe nano
* nano=simpel editor for on the command line, but you can use the editor from Onion Console or vi
* zoneinfo-europe=timezone package
*
* php.ini set timezone:
* [Date]
* date.timezone = "Europe/Amsterdam" (or something else, use timezone package for your area)
*
*
* add line
* list interpreter ".php=/usr/bin/php-cgi"
* to: /etc/config/uhhtpd
*
* and type in the command:
* /etc/init.d/uhttpd restart
* to restart the http demon
*
* place this file into:
* /www/ or a subdirectory of it
* and use it in your php programs
*/
//see also example from Rinus van Weert:
// https://community.onion.io/topic/39/simple-php-web-gpio-example-switching-leds
class OmegaOnion
{
//format:
// %u = unsigned decimal number
// %b = integer binary
// %s = string
// %d = (signed) decimal number
const IO_LOGFILE = 'IO.LOG';
const FASTGPIO = 'fast-gpio';
//fast-gpio set-{in/out}put <gpio>
const FASTGPIO_SETDIRECTION = 'fast-gpio set-%sput %u';
//fast-gpio get-direction <gpio>
const FASTGPIO_GETDIRECTION = 'fast-gpio get-direction %u';
//fast-gpio set <gpio> <value: 0 or 1>
const FASTGPIO_SETPIN = 'fast-gpio set %u %b';
//fast-gpio read <gpio>
const FASTGPIO_READPIN = 'fast-gpio read %u';
//fast-gpio pwm <gpio> <freq in Hz> <duty cycle percentage>
const FASTGPIO_PWMPIN = 'fast-gpio pwm %u %u %u';
//need RGB color code (output more than 1 row gives problems?)
const EXP_LED = 'expled %s>/dev/null';
//relay-exp -s <dipswitch=000> -i;
const EXP_RELAY_INIT = 'relay-exp -s %s -i';
//relay-exp -s <dipswitch=000> <channel:0 or 1 or all> <value:0 or 1>
const EXP_RELAY_SET = 'relay-exp -s %s %s %u';
protected $logFileName;
protected $log;
public static function now()
{
return date("Y-m-d h:m:s");
}
public static function nowInt()
{
return strtotime(self::now());
}
function __construct( $fileName = self::IO_LOGFILE )
{
//write output to log
if ($fileName === FALSE ) {
$this->logFileName = FALSE;
} else {
$this->logFileName = $fileName;
$this->log = fopen($this->logFileName,"a");
fwrite( $this->log, self::now() . "\n" );
}
}
function __destruct()
{
//close logfile
if ( ( $this->logFileName != FALSE ) ) {
fclose( $this->log );
}
}
public function writeLog( $var )
{
if ( ( $this->logFileName != FALSE ) and isset($var) ) {
if ( is_array($var) ) {
foreach ( $var as $line ) {
fwrite( $this->log, $line . "\n");
}
} else {
fwrite( $this->log, $var . "\n");
}
}
}
function execCommand( $command )
{
$this->writeLog( $command );
$result = exec( $command );
$this->writeLog( $result );
return $result;
}
//define functions
/* setGpioDirection
* use setGpioDirection( GPIO-pin, direction )
* direction:string
* 'in'
* 'out'
*/
// we will use the onion gpio function to control GPIO pins
public function setGpioDirection( $GPIO, $direction )
{
if ( $direction == 'in' or $direction == 'out') {
$command = sprintf( self::FASTGPIO_SETDIRECTION, $direction, $GPIO);
$this->execCommand( $command );
}
}
/* getGpioDirection
* use getGpioDirection( GPIO-pin )
* returns:string
* 'in'
* 'out'
*/
// we will use the onion gpio function to control GPIO pins
public function getGpioDirection( $GPIO )
{
$command = sprintf( self::FASTGPIO_READPIN, $GPIO);
$readOutput = $this->execCommand( $command );
$result = ( strpos( $readOutput, 'input') === FALSE ? 'out' : 'in' );
return $result;
}
/* readGpio
* use readGpio( GPIO-pin )
* returns:int
* 1: on
* 0: off
*/
// we will use the onion gpio function to control GPIO pins
public function readGpio( $GPIO )
{
$command = sprintf( self::FASTGPIO_READPIN, $GPIO);
$readOutput = $this->execCommand( $command );
if ( isset( $readOutput )) {
$result = ( ( substr( rtrim( $readOutput ), -1 ) === '1' ) === FALSE ? 0 : 1 );
}
return $result;
}
/* writeGpio
* use writeGpio( GPIO-pin, newValue )
* newValue:int
* 1: on
* 0: off
*/
// we will use the onion gpio function to control GPIO pins
public function writeGpio( $GPIO, $newValue )
{
$result = 0;
if ( $newValue < 2) {
$command = sprintf( self::FASTGPIO_SETPIN, $GPIO, ($newValue === 1 ? 1 : 0 ) );
$readOutput = $this->execCommand( $command );
return $result;
}
}
/* pwmGpio
* use pwmGpio( GPIO-pin, time, percentage )
* time:int = interval time
* percentage:int = percentage on
*/
// we will use the onion gpio function to control GPIO pins
public function pwmGpio( $GPIO, $time, $perc )
{
$result = 0;
if ( $newValue < 2) {
$command = sprintf( self::FASTGPIO_PWMPIN, $GPIO, $time, $perc );
$readOutput = $this->execCommand( $command );
}
}
/* wait
* use wait( milliSec )
*/
public function wait( $milliSec )
{
$this->writeLog( "wait for $milliSec" );
if ( $milliSec > 999 ) {
sleep( $milliSec / 1000 );
} else {
usleep( $milliSec * 1000 );
}
}
/* setRGBled
* use setRGBled( [value] )
* value = hex RGB color
* or string "off" to switch Led complete off (default)
*/
// we will use the onion gpio function to control GPIO pins
public function setRGBled( $value = "off" )
{
if ( $value == "off" ) {
$this->writeLog( 'setoff LED on dock' );
//write 1="on" to 15/16/17 sets off the rgb led
$this->writeGpio( 15, 1);
$this->writeGpio( 16, 1);
$this->writeGpio( 17, 1);
} else {
$command = sprintf( self::EXP_LED, $value);
$this->writeLog( 'set LED on dock' );
$this->execCommand( $command );
}
}
/* initRelay
* use initRelay( [dipSwitch] )
* dipSwitch = if you have more than 1 relay, you can insert the dipSwitch values
* default = 000
*/
// we will use the onion relay_exp function to control relay
function initRelay( $dipSwitch = "000" ){ // init Relay expansion
$command = sprintf( self::EXP_RELAY_INIT, $dipSwitch);
return $this->execCommand( $command );
}
/* writeRelay
* use writeRelay( [channel], [newValue], [dipSwitch] )
* channel:int
* 0 & 1: channel
* 2: for both (default)
* newValue:int
* 1: on
* 0: off (default)
* dipSwitch:string
* if you have more than 1 relay, you can insert the dipSwitch values (example:010 or 100)
* default = 000
*/
// we will use the onion relay_exp function to control relay
function writeRelay( $channel = 2, $newValue = 0, $dipSwitch = "000" ){
$command = sprintf( self::EXP_RELAY_SET, $dipSwitch, ($channel === 2 ? "all" : $channel ), ($newValue === 1 ? 1 : 0 ) );
return $this->execCommand( $command );
}
}