Skip to content

Commit 706ef8c

Browse files
committed
Updated docs, begin I2C impl
1 parent d33222a commit 706ef8c

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ $board->getLoop()->run();
7575

7676
GPIO (input) is the default mode of the pin objects. Alternate functions can be accessed by using the ```->setFunction(PinFunction::x)``` method. It is
7777
recommended to use the function names as opposed to `ALT0..5` unless you know exactly what you're doing, as quite a lot are reserved.
78-
A few useful classes are also included for digital interaction.
78+
A few useful classes are also included for digital interaction. With the default python-mmap, you can expect a raw transition speed of ~15kHz, with the
79+
native extension, it's more like 50kHz on a Pi 3.
7980

8081

8182
### PWM

examples/benchmark.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* @package phpi
4+
* @author Michael Calcinai <[email protected]>
5+
*/
6+
7+
include __DIR__.'/../vendor/autoload.php';
8+
9+
use Calcinai\PHPi\Pin\PinFunction;
10+
11+
$board = \Calcinai\PHPi\Factory::create();
12+
13+
$pin = $board->getPin(18);
14+
$pin->setFunction(PinFunction::OUTPUT);
15+
16+
$start = microtime(true);
17+
18+
for($i=0; $i<1e4; $i++){
19+
$pin->high(true);
20+
$pin->low(true);
21+
}
22+
23+
$time = (microtime(true) - $start);
24+
$ms = $time * 1000;
25+
$writes = $i * 2;
26+
27+
$writes_sec = $writes / $time;
28+
29+
printf("%0.2dms for %d transitions (%d writes), %d writes/sec\n", $ms, $i, $writes, $writes_sec);

examples/i2c.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/**
3+
* @package phpi
4+
* @author Michael Calcinai <[email protected]>
5+
*/

src/PHPi/Board.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Calcinai\PHPi\Board\BoardInterface;
1111
use Calcinai\PHPi\Peripheral\Clock;
12+
use Calcinai\PHPi\Peripheral\I2C;
1213
use Calcinai\PHPi\Peripheral\PWM;
1314
use Calcinai\PHPi\Peripheral\Register;
1415
use Calcinai\PHPi\Peripheral\SPI;
@@ -76,6 +77,11 @@ abstract class Board implements BoardInterface {
7677
*/
7778
private $spis;
7879

80+
/**
81+
* @var I2C[]
82+
*/
83+
private $i2cs;
84+
7985

8086
public function __construct(LoopInterface $loop) {
8187
$this->loop = $loop;
@@ -138,6 +144,18 @@ public function getSPI($spi_number){
138144
return $this->spis[$spi_number];
139145
}
140146

147+
/**
148+
* @param $i2c_number
149+
* @return SPI
150+
*/
151+
public function getI2C($i2c_number){
152+
if(!isset($this->i2cs[$i2c_number])){
153+
$this->i2cs[$i2c_number] = new I2C($this, $i2c_number);
154+
}
155+
156+
return $this->i2cs[$i2c_number];
157+
}
158+
141159
/**
142160
* @return Register\GPIO
143161
*/

src/PHPi/Pin.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,11 @@ public function assertFunction(array $valid_functions){
209209
}
210210

211211

212-
public function high(){
213-
$this->assertFunction([PinFunction::OUTPUT]);
212+
public function high($fast_mode = false){
213+
214+
if(!$fast_mode){
215+
$this->assertFunction([PinFunction::OUTPUT]);
216+
}
214217

215218
$this->setInternalLevel(self::LEVEL_HIGH);
216219

@@ -220,8 +223,11 @@ public function high(){
220223
return $this;
221224
}
222225

223-
public function low(){
224-
$this->assertFunction([PinFunction::OUTPUT]);
226+
public function low($fast_mode = false){
227+
228+
if(!$fast_mode){
229+
$this->assertFunction([PinFunction::OUTPUT]);
230+
}
225231

226232
$this->setInternalLevel(self::LEVEL_LOW);
227233

0 commit comments

Comments
 (0)