Created
January 9, 2014 01:43
-
-
Save kahagon/8327993 to your computer and use it in GitHub Desktop.
REPL setup for firmata testing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use PHPMake\SerialPort as SerialPort; | |
| define('START_SYSEX', 0xf0); | |
| define('END_SYSEX', 0xf7); | |
| define('QUERY_FIRMWARE', 0x79); | |
| $p = new SerialPort('/dev/ttyACM0'); | |
| $p->setBaudRate(57600)->setCanonical(false)->setVTime(1)->setVMin(0); | |
| $receivedData=array(); | |
| function dp() { | |
| global $p, $receivedData; | |
| while($d=$p->read(1024)) { | |
| $receivedData[]=$d; | |
| var_dump($d); | |
| } | |
| } | |
| function voidBuffer() { | |
| global $p; | |
| while ($r=$p->read(1024)) | |
| ; | |
| } | |
| function getBuffer() { | |
| global $p; | |
| $r = ''; | |
| while ($_r=$p->read(1024)) { | |
| $r .= $_r; | |
| } | |
| return $r; | |
| } | |
| function setPinMode($pin, $mode) { | |
| global $p; | |
| $p->write(pack('CCC', 0xF4, $pin, $mode)); | |
| } | |
| function digitalWrite($pin, $value) { | |
| global $p; | |
| $value = $value ? 1 : 0; | |
| $port = 0x90 + portNumberForPin($pin); | |
| $_pin = pinDestInPort($pin); | |
| $first = ($value << $_pin) & 0b01111111; | |
| $second = (0b10000000 >> $_pin) & $value; | |
| //printf('$port: 0x%02x'.PHP_EOL, $port); | |
| //printf('$first: 0b%08b'.PHP_EOL, $first); | |
| //printf('$second: 0b%08b'.PHP_EOL, $second); | |
| $p->write(pack('CCC', $port, $first, $second)); | |
| } | |
| function pinDestInPort($pin) { | |
| return $pin%8; | |
| } | |
| function portNumberForPin($pin) { | |
| return floor($pin/8); | |
| } | |
| function queryPinState($pin) { | |
| global $p; | |
| voidBuffer(); | |
| $p->write(pack('CCCC', 0xf0, 0x6d, $pin, 0xf7)); | |
| $r = getBuffer(); | |
| $pinState = (object)array( | |
| 'pin' => unpack('C', substr($r, 2, 1))[1], | |
| 'mode' => unpack('C', substr($r, 3, 1))[1], | |
| 'state' => unpack('C', substr($r, 4, 1))[1], | |
| ); | |
| return $pinState; | |
| } | |
| function queryFirmware() { | |
| global $p; | |
| voidBuffer(); | |
| $p->write(pack('CCC', START_SYSEX, QUERY_FIRMWARE, END_SYSEX)); | |
| $r = getBuffer(); | |
| $index = 'data'; | |
| $format = 'H' . (strlen($r)*2) . $index; | |
| //$d = unpack($format, $r)[$index]; | |
| $firmware = (object)array( | |
| 'name' => substr($r, 4, strlen($r)-5), | |
| 'majorVersion' => unpack('H2', substr($r, 2, 1))[1], | |
| 'minorVersion' => unpack('H2', substr($r, 3, 1))[1], | |
| ); | |
| return $firmware; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment