Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 290f820

Browse files
committed
first version
1 parent 210ed28 commit 290f820

File tree

9 files changed

+74111
-0
lines changed

9 files changed

+74111
-0
lines changed

‎bin/phpunit-4.8.26.phar‎

Lines changed: 73927 additions & 0 deletions
Large diffs are not rendered by default.

‎composer.json‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "marabesi/arduino-php-wrapper",
3+
"authors": [
4+
{
5+
"name": "Marabesi",
6+
"email": "matheus.marabesi@gmail.com"
7+
}
8+
],
9+
"autoload": {
10+
"psr-4": {
11+
"Arduino\\": "src/"
12+
}
13+
},
14+
"require": {},
15+
"autoload-dev": {
16+
"classmap": [
17+
"test/ArduinoTestCase.php"
18+
]
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "~4.8",
22+
"mikey179/vfsStream": "~1"
23+
}
24+
}

‎phpunit.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="Arduino wrapper">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

‎src/Reader.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Arduino;
4+
5+
6+
class Reader
7+
{
8+
9+
}

‎src/Wrapper.php‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Arduino;
4+
5+
class Wrapper
6+
{
7+
8+
private static $wrapperName = 'arduino';
9+
private $path;
10+
11+
public function __construct()
12+
{
13+
self::register();
14+
}
15+
16+
public function stream_open($path, $mode, $options = null, &$opened_path = null)
17+
{
18+
$realPath = str_replace('arduino://', '', $path);
19+
20+
if (!file_exists($realPath)) {
21+
throw new \InvalidArgumentException('Could not find Arduino connection in ' . $realPath);
22+
}
23+
24+
$this->path = fopen($realPath, 'r+');
25+
26+
return true;
27+
}
28+
29+
public function stream_read($count)
30+
{
31+
return fread($this->path, $count);
32+
}
33+
34+
public function stream_write($data)
35+
{
36+
return fwrite($this->path, $data);
37+
}
38+
39+
public function stream_eof()
40+
{
41+
return fclose($this->path);
42+
}
43+
44+
public static function register()
45+
{
46+
// if we already defined the wrapper just return false
47+
foreach (stream_get_wrappers() as $wrapper) {
48+
if ($wrapper == self::$wrapperName) {
49+
return false;
50+
}
51+
}
52+
53+
stream_wrapper_register(self::$wrapperName, self::class);
54+
}
55+
}

‎src/Writer.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Arduino;
4+
5+
class Writer
6+
{
7+
private $wrappers;
8+
9+
public function __construct(Wrapper $wrapper)
10+
{
11+
$this->wrappers = $wrapper;
12+
}
13+
14+
public function out($to, $data)
15+
{
16+
$this->wrappers->stream_open($to, 'r+');
17+
$bytes = $this->wrappers->stream_write($data);
18+
19+
return ($bytes != 0);
20+
}
21+
}

‎test/ArduinoTest.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
class ArduinoTest extends \ArduinoTestCase
4+
{
5+
6+
public function testShouldDefineArduinoWrapperToBeUsed()
7+
{
8+
$arduino = new \Arduino\Wrapper();
9+
10+
$has = false;
11+
foreach (stream_get_wrappers() as $wrapper) {
12+
if ($wrapper == 'arduino') {
13+
$has = true;
14+
break;
15+
}
16+
}
17+
18+
$this->assertTrue($has);
19+
}
20+
21+
public function testShoudlSendDataToArduino()
22+
{
23+
$resource = fopen('arduino://' . $this->fakeUsbPath, 'r+');
24+
fwrite($resource, 'data to arduino');
25+
26+
$this->assertTrue(is_resource($resource));
27+
}
28+
29+
/**
30+
* @expectedException \InvalidArgumentException
31+
*/
32+
public function testShouldHandlerErrorWhenTheUsbIsNotAvailable()
33+
{
34+
fopen('arduino:///foo/bar/tty_fake_usb', 'r+');
35+
}
36+
}

‎test/ArduinoTestCase.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class ArduinoTestCase extends PHPUnit_Framework_TestCase
4+
{
5+
6+
protected $fakeArduinoUsbResource;
7+
protected $fakeUsbPath = 'ttyUSB0_fake';
8+
9+
public function setUp()
10+
{
11+
$this->fakeArduinoUsbResource = fopen($this->fakeUsbPath, 'w');
12+
}
13+
14+
public function tearDown()
15+
{
16+
fclose($this->fakeArduinoUsbResource);
17+
unlink($this->fakeUsbPath);
18+
}
19+
}

‎test/WriterTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
class WriterTest extends \ArduinoTestCase
4+
{
5+
6+
public function testShouldWriteToArduinoUsingOOPStyle()
7+
{
8+
$writer = new Arduino\Writer(new Arduino\Wrapper());
9+
$bytes = $writer->out($this->fakeUsbPath, 'from oop');
10+
11+
$this->assertNotEmpty($bytes);
12+
}
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /