Skip to content

Commit 1603739

Browse files
committed
Added software PWM write ability and example PHP script.
Updated version to 0.2.0.
1 parent 14973ec commit 1603739

File tree

8 files changed

+95
-26
lines changed

8 files changed

+95
-26
lines changed

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
"name": "PHPiWire",
5656
"description": "Control the RaspberryPi GPIO via PHP (with wiringPi)",
5757
"author": "Andrew Collington (andy@amnuts.com)",
58-
"version": "0.1.0",
58+
"version": "0.2.0",
5959
"verbose": false,
6060
"requires": {
6161
"extensions": []
6262
},
63-
"extra-libs": "-lwiringPi"
63+
"extra-libs": "-lwiringPi -pthreads"
6464
}

example/blink.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Example of blinking an LED connected to pin 0
77
*
88
* @author Andrew Collington, andy@amnuts.com
9-
* @version 0.1.0
9+
* @version 0.2.0
1010
* @link https://github.com/amnuts/phpiwire
1111
* @license MIT, http://acollington.mit-license.org/
1212
*/

example/board.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Get the board information
77
*
88
* @author Andrew Collington, andy@amnuts.com
9-
* @version 0.1.0
9+
* @version 0.2.0
1010
* @link https://github.com/amnuts/phpiwire
1111
* @license MIT, http://acollington.mit-license.org/
1212
*/

example/pin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Checking pin status
77
*
88
* @author Andrew Collington, andy@amnuts.com
9-
* @version 0.1.0
9+
* @version 0.2.0
1010
* @link https://github.com/amnuts/phpiwire
1111
* @license MIT, http://acollington.mit-license.org/
1212
*/

example/pulse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Example of using hardware PWM to pulse an LED.
77
*
88
* @author Andrew Collington, andy@amnuts.com
9-
* @version 0.1.0
9+
* @version 0.2.0
1010
* @link https://github.com/amnuts/phpiwire
1111
* @license MIT, http://acollington.mit-license.org/
1212
*/

example/softpwm.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Phpiwire: A PHP wrapper for wiringPi
5+
*
6+
* Example of using software PWM to pulse an LED
7+
*
8+
* @author Andrew Collington, andy@amnuts.com
9+
* @version 0.2.0
10+
* @link https://github.com/amnuts/phpiwire
11+
* @license MIT, http://acollington.mit-license.org/
12+
*/
13+
14+
namespace Phpiwire;
15+
16+
if (PHP_SAPI !== 'cli') {
17+
echo 'Sorry, you can only use this via the command line.';
18+
return;
19+
}
20+
21+
set_time_limit(0);
22+
23+
echo "Raspberry Pi pulse - use ^C to stop\n";
24+
25+
$pi = new Board();
26+
$p = $pi->getPin(0)->mode(Pin::SOFT_PWM_OUT);
27+
28+
$sleep = 20000;
29+
$pwmValue = 100; // 0 min, 100 max
30+
31+
while (true) {
32+
for ($i = 0; $i <= $pwmValue; ++$i) {
33+
$p->softPwmWrite($i);
34+
usleep($sleep);
35+
}
36+
for ($i = $pwmValue; $i > 0; --$i) {
37+
$p->softPwmWrite($i);
38+
usleep($sleep);
39+
}
40+
}

phpiwire/Board.zep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Phpiwire: A PHP wrapper for wiringPi
33
*
44
* @author Andrew Collington, andy@amnuts.com
5-
* @version 0.1.0
5+
* @version 0.2.0
66
* @link https://github.com/amnuts/phpiwire
77
* @license MIT, http://acollington.mit-license.org/
88
*

phpiwire/Pin.zep

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Phpiwire: A PHP wrapper for wiringPi
33
*
44
* @author Andrew Collington, andy@amnuts.com
5-
* @version 0.1.0
5+
* @version 0.2.0
66
* @link https://github.com/amnuts/phpiwire
77
* @license MIT, http://acollington.mit-license.org/
88
*
@@ -24,21 +24,23 @@ namespace Phpiwire;
2424
class Pin
2525
{
2626
// read/write types
27-
const DIGITAL = 0;
28-
const ANALOG = 1;
29-
const PWM = 2;
27+
const DIGITAL = 0;
28+
const ANALOG = 1;
29+
const PWM = 2;
30+
const SOFT_PWM = 3;
3031
// pin modes
31-
const INPUT = 0;
32-
const OUTPUT = 1;
33-
const PWM_OUT = 2;
34-
const CLOCK = 3;
32+
const INPUT = 0;
33+
const OUTPUT = 1;
34+
const PWM_OUT = 2;
35+
const CLOCK = 3;
36+
const SOFT_PWM_OUT = 4;
3537
// pud modes
36-
const OFF = 0;
37-
const DOWN = 1;
38-
const UP = 2;
38+
const OFF = 0;
39+
const DOWN = 1;
40+
const UP = 2;
3941
// values
40-
const LOW = 0;
41-
const HIGH = 1;
42+
const LOW = 0;
43+
const HIGH = 1;
4244

4345
protected id { get };
4446
protected board { get };
@@ -53,10 +55,11 @@ class Pin
5355
let this->id = pin;
5456
let this->board = board;
5557
let this->modeName = [
56-
self::INPUT : "Input",
57-
self::OUTPUT : "Output",
58-
self::PWM_OUT : "PWM output",
59-
self::CLOCK : "GPIO clock"
58+
self::INPUT : "Input",
59+
self::OUTPUT : "Output",
60+
self::PWM_OUT : "Hardware PWM output",
61+
self::SOFT_PWM_OUT : "Software PWM output",
62+
self::CLOCK : "GPIO clock"
6063
];
6164
}
6265

@@ -79,15 +82,28 @@ class Pin
7982
*/
8083
public function mode(int! mode)
8184
{
82-
if !in_array(mode, [self::INPUT, self::OUTPUT, self::PWM_OUT, self::CLOCK]) {
85+
if !in_array(mode, [self::INPUT, self::OUTPUT, self::PWM_OUT, self::SOFT_PWM_OUT, self::CLOCK]) {
8386
throw new \Exception("Pin mode not supported");
8487
}
8588

8689
%{
8790
zval *pinnum;
8891
zephir_read_property_this(&pinnum, this_ptr, SL("id"), PH_NOISY_CC);
89-
pinMode(Z_LVAL_P(pinnum), mode);
9092
}%
93+
if mode == self::SOFT_PWM_OUT {
94+
var scheme;
95+
let scheme = this->board->getPinScheme();
96+
if scheme["scheme"] == Board::SYSTEM {
97+
throw new \Exception("Cannot use soft PWM with system number pin layout");
98+
}
99+
%{
100+
softPwmCreate (Z_LVAL_P(pinnum), 0, 100);
101+
}%
102+
} else {
103+
%{
104+
pinMode(Z_LVAL_P(pinnum), mode);
105+
}%
106+
}
91107

92108
let this->mode = mode;
93109
return this;
@@ -207,6 +223,9 @@ class Pin
207223
case self::PWM:
208224
%{ pwmWrite(pin, value); }%
209225
break;
226+
case self::SOFT_PWM:
227+
%{ softPwmWrite(pin, value); }%
228+
break;
210229
}
211230

212231
return this;
@@ -241,4 +260,14 @@ class Pin
241260
{
242261
return this->write(value, self::PWM);
243262
}
263+
264+
/**
265+
* Set PWM pin value
266+
*
267+
* @return var
268+
*/
269+
public function softPwmWrite(int! value)
270+
{
271+
return this->write(value, self::SOFT_PWM);
272+
}
244273
}

0 commit comments

Comments
 (0)