Skip to content

Instantly share code, notes, and snippets.

@mgdm
Created July 6, 2015 19:21
Show Gist options
  • Save mgdm/f688fb16e28fc1e8f2a5 to your computer and use it in GitHub Desktop.
Save mgdm/f688fb16e28fc1e8f2a5 to your computer and use it in GitHub Desktop.
Using MFFI to drive a SureElec LCD display
<?php
namespace Sureelec;
use MFFI\Library;
use MFFI\Struct;
use MFFI\Type;
class DeviceInfo extends Struct {
public static function definition() {
return [
'width' => Type::TYPE_INT,
'height' => Type::TYPE_INT,
'rom_size' => Type::TYPE_INT,
'has_rx8025' => Type::TYPE_INT,
'has_light_sensor' => Type::TYPE_INT,
'has_thermal_sensor' => Type::TYPE_INT,
];
}
}
class Context extends Struct {
static function definition() {
return [
'fd' => Type::TYPE_INT,
'width' => Type::TYPE_INT,
'display_info' => DeviceInfo::byValue(),
'framebuffer' => Type::TYPE_STRING,
'framebuffer_size' => Type::TYPE_INT,
'display_state' => Type::TYPE_INT,
'contrast' => Type::TYPE_INT,
'brightness' => Type::TYPE_INT
];
}
}
class Display {
/** @var MFFI\Library */
protected $library;
/** @var callable */
protected $createContext;
/** @var Sureelec\Context */
private function createBindings()
{
$this->library = new Library('libsureelec.dylib');
$this->createContext = $this->library->bind('libsureelec_create',
[ Type::TYPE_STRING, Type::TYPE_INT ], Context::byReference());
}
public function __construct($device, $debug = false) {
$this->createBindings();
$this->context = call_user_func($this->createContext, $device, $debug ? 1 : 0);
}
}
$display = new Display('/dev/tty.SLAB_USBtoUART', true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment