PHP code example of phpdafruit / ssd1306-php

1. Go to this page and download the library: Download phpdafruit/ssd1306-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpdafruit / ssd1306-php example snippets




use PhpdaFruit\SSD1306\Builder\DisplayFactory;
use PhpdaFruit\SSD1306\Services\TextRenderer;
use PhpdaFruit\SSD1306\Effects\TypewriterText;

// Create display using factory
$display = DisplayFactory::standard('/dev/i2c-7');

// Render text with typewriter effect
$textRenderer = new TextRenderer($display);
$effect = new TypewriterText();

for ($i = 0; $i < 10; $i++) {
    $display->clearDisplay();
    $effect->render($display, 'Hello World!', 10, 12, $i / 10);
    $display->display();
    usleep(100000);
}

use PhpdaFruit\SSD1306\Builder\DisplayBuilder;
use PhpdaFruit\SSD1306\Builder\DisplayFactory;

// Manual configuration
$display = (new DisplayBuilder())
    ->withDisplay(128, 32, '/dev/i2c-7')
    ->withBrightness(200)
    ->withRotation(0)
    ->build();

// Or use presets
$display = DisplayFactory::standard('/dev/i2c-7');
$display = DisplayFactory::highContrast('/dev/i2c-7');
$display = DisplayFactory::dimmed('/dev/i2c-7');

use PhpdaFruit\SSD1306\Services\TextRenderer;
use PhpdaFruit\SSD1306\Effects\{ScrollingText, TypewriterText, MarqueeText, FadeText};

$renderer = new TextRenderer($display);

// Scrolling text
$scroll = new ScrollingText('horizontal', 2);
$scroll->render($display, 'Scrolling...', 0, 10, 0.5);

// Typewriter effect
$typewriter = new TypewriterText();
$typewriter->render($display, 'Typing...', 10, 10, 0.75);

// Positioned text
$renderer->leftAlign('Left', 0, 0);
$renderer->centerAlign('Center', 16);
$renderer->rightAlign('Right', 24);

use PhpdaFruit\SSD1306\Services\ShapeRenderer;
use PhpdaFruit\SSD1306\Shapes\{ProgressBar, Gauge, RoundedBox, Icon};

$shapeRenderer = new ShapeRenderer($display);

// Progress bar
$progressBar = new ProgressBar(10, 10, 100, 8);
$progressBar->setValue(75);
$shapeRenderer->renderProgressBar($progressBar);

// Gauge
$gauge = new Gauge(64, 16, 15);
$gauge->setValue(65)->setRange(0, 100);
$shapeRenderer->renderGauge($gauge);

// Rounded box
$box = new RoundedBox(5, 5, 118, 22, 3);
$shapeRenderer->renderRoundedBox($box);

// Icons
Icon::initializeBuiltIns();
$icon = Icon::get('wifi');
$shapeRenderer->renderIcon($icon, 50, 12);

use PhpdaFruit\SSD1306\Services\AnimationEngine;

$animation = new AnimationEngine($display);

// Add frames
for ($x = 0; $x < 100; $x += 10) {
    $animation->addFrame(function($disp, $progress) use ($x) {
        $disp->fillCircle($x, 16, 5, 1);
    }, 100); // 100ms per frame
}

// Control playback
$animation->loop(true);
$animation->play();

use PhpdaFruit\SSD1306\StateMachine\{StateMachine, Transition};
use PhpdaFruit\SSD1306\StateMachine\States\{IdleState, AlertState, DashboardState};

$stateMachine = new StateMachine($display);

// Register states
$stateMachine->addState('idle', new IdleState($display));
$stateMachine->addState('alert', new AlertState($display, 'Warning!'));
$stateMachine->addState('dashboard', new DashboardState($display));

// Set initial state
$stateMachine->setInitialState('idle');

// Transition with fade effect
$transition = new Transition(0.5, Curve::easeInOut(...), 'fade');
$stateMachine->transition('alert', $transition);

// Update and render
$stateMachine->update(0.016); // Delta time
$stateMachine->render();

use PhpdaFruit\SSD1306\UI\{Menu, Notification, Dashboard};
use PhpdaFruit\SSD1306\UI\Widgets\{TextWidget, ProgressWidget, IconWidget, GraphWidget};

// Menu
$menu = new Menu($display);
$menu->addItem('Settings', fn() => loadSettings())
     ->addItem('Display', fn() => displayMenu())
     ->addItem('About', fn() => showAbout());
     
$menu->selectNext();
$menu->render();

// Notification
$notification = Notification::info($display, 'System Ready', 3.0);
$notification->show();

// Dashboard with widgets
$dashboard = new Dashboard($display, 2, 2);

$cpu = new ProgressWidget($display);
$cpu->setLabel('CPU')->setValue(65);

$mem = new ProgressWidget($display);
$mem->setLabel('MEM')->setValue(78);

$dashboard->addWidget($cpu, 0, 0)
          ->addWidget($mem, 0, 1);
          
$dashboard->render();

use PhpdaFruit\SSD1306\Math\{Vector2D, Matrix2D, Curve};

// Vector operations
$v1 = new Vector2D(10, 20);
$v2 = new Vector2D(5, 10);
$sum = $v1->add($v2);
$magnitude = $v1->magnitude();

// Matrix transformations
$matrix = Matrix2D::rotation(M_PI / 4);
$transformed = $matrix->transform($v1);

// Easing functions
$eased = Curve::easeInOut(0.5);
$bezier = Curve::cubicBezier($p0, $p1, $p2, $p3, 0.5);

use PhpdaFruit\SSD1306\Concerns\{HasAnimations, HasEffects, Renderable};

class MyWidget implements Renderable {
    use HasAnimations;
    use HasEffects;
    
    public function render(): void {
        // Render logic
    }
    
    // Interface  $p) => /* frame render */, 500, true);
$widget->withEffect(new ScrollingText());

// ✅ Good - Single instance
$display = DisplayFactory::standard('/dev/i2c-7');
$textRenderer = new TextRenderer($display);
$shapeRenderer = new ShapeRenderer($display);

// ❌ Bad - Multiple instances can cause segfaults
$display1 = new SSD1306Display(128, 32, '/dev/i2c-7');
$display2 = new SSD1306Display(128, 32, '/dev/i2c-7'); // Don't do this!

$display->clearDisplay();
// ... draw operations ...
$display->display();

$lastTime = microtime(true);
while (true) {
    $currentTime = microtime(true);
    $deltaTime = $currentTime - $lastTime;
    $lastTime = $currentTime;
    
    $stateMachine->update($deltaTime);
    $stateMachine->render();
}
bash
composer