Commit 19de7351 authored by Jakob Moser's avatar Jakob Moser
Browse files

Merge branch 'display' into 'master'

Add code to access display

See merge request moser/poolpay!23
parents 1130fb8c 72e60124
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
from dataclasses import dataclass
from pathlib import Path


@dataclass(frozen=True)
class Display:
    framebuffer_path: Path
    width: int
    height: int

    def show(self, picture: bytes) -> None:
        with self.framebuffer_path.open("wb") as f:
            f.write(picture)
+14 −0
Original line number Diff line number Diff line
# PoolPay Display Interface

This package contains code to interface with the LCD mounted to the Pi.

It forms part of the larger PoolPay application, but can also be run as a standalone module (primarily for experimenting purposes).

## Run as standalone module

Execute the following lines in the base repository directory:

```bash
source venv/bin/activate
python3 -m poolpay.display test
```
+0 −0

Empty file added.

+36 −0
Original line number Diff line number Diff line
import argparse
from pathlib import Path

from poolpay.display.Display import Display
from poolpay.display.get_test_picture import get_test_picture


def action_test(display: Display) -> None:
    test_picture = get_test_picture(display.width, display.height)
    display.show(test_picture)


ACTIONS = {"test": action_test}


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        prog="poolpay.display",
        description="Experimental util to interface with a Framebuffer display",
    )
    parser.add_argument(
        "action",
        choices=["test"],
        help="the action to be done (currently, must always be 'test')",
    )

    return parser.parse_args()


# Parse the command line arguments
args = parse_args()

display = Display(Path("/dev/fb0"), width=480, height=320)

# Call the selected action
ACTIONS[args.action](display)
+92 −0
Original line number Diff line number Diff line
/*
Stolen from https://gist.github.com/FredEckert/3425429#file-framebuffer-c with minimal modifications

The implementation of get_test_picture.py and Display.py is inspired by this code, however, this code
is much faster, as the integer operations take 10× longer in Python.

To execute:

gcc framebuffer.c -o framebuffer
./framebuffer
*/

/*
To test that the Linux framebuffer is set up correctly, and that the device permissions
are correct, use the program below which opens the frame buffer and draws a gradient-
filled red square:

retrieved from:
Testing the Linux Framebuffer for Qtopia Core (qt4-x11-4.2.2)

http://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2/qtopiacore-testingframebuffer.html
*/

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

int main()
{
    int fbfd = 0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0;
    char *fbp = 0;
    int x = 0, y = 0;
    long int location = 0;

    // Open the file for reading and writing
    fbfd = open("/dev/fb0", O_RDWR);
    if (fbfd == -1) {
        perror("Error: cannot open framebuffer device");
        exit(1);
    }
    printf("The framebuffer device was opened successfully.\n");

    // Get fixed screen information
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
        perror("Error reading fixed information");
        exit(2);
    }

    // Get variable screen information
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
        perror("Error reading variable information");
        exit(3);
    }

    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);

    // Figure out the size of the screen in bytes
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

    // Map the device to memory
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
    if ((int)fbp == -1) {
        perror("Error: failed to map framebuffer device to memory");
        exit(4);
    }
    printf("The framebuffer device was mapped to memory successfully.\n");
    const int xStart = 0;
    const int yStart = 0;
    // Figure out where in memory to put the pixel
    for (y = yStart; y < 320; y++)
        for (x = xStart; x < 480; x++) {
            location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
                       (y+vinfo.yoffset) * finfo.line_length;

            //assume 16bpp
            int b = 10;
            int g = (float)x*31.0/480.0;
            int r = (float)x*31.0/480.0;
            unsigned short int t = r<<11 | g << 5 | b;
            *((unsigned short int*)(fbp + location)) = t;
        }
    munmap(fbp, screensize);
    close(fbfd);
    return 0;
}
Loading