如何在linux启动结束后在小TFT屏幕上绘图(在rPI上)

问题描述 投票:0回答:1

我设法让 TFT 屏幕(160*128)在我的 PI 上工作(通过 SPI)

当系统启动时,我可以看到启动结束,在最后,“登录:”

我想要一个程序(文本终端,或图形程序,还不确定)在启动过程的最后启动,并使用 tft 屏幕作为其 STD 输出。我没有运行 X。

(创建了/dev/fb0,当我插入屏幕时,它变成了/dev/fb1,但我设法拥有了/dev/fbTFT)

1/我应该禁用 getty 吗? (如何 ?) 2/如何让Python(或C)程序在启动结束时启动并将控制台用作STDout? (我没有键盘) 3 我应该使用libcurses(文本模式)吗?

我可以使用显示图片 fbi -d /dev/fbTFT -T 1 -noverbose -a cat.jpg (显示一只猫)

python c pi directfb
1个回答
0
投票

为此,您可以直接将像素值转储到帧缓冲区对应的直接内存中。以下代码是我根据here

的答案为此用例创建的示例代码
import os
import sys  # for exit
import mmap # shared memory

frame_buffer_number = 'fb0'   # device created by the device driver

# get width and height
f = open(f"/sys/class/graphics/{frame_buffer_number}/virtual_size", "r")
width_and_height = f.read()
width_string, height_string = width_and_height.split(',')
width = int(width_string) # width
height = int(height_string) # height
f.close


# get bits per pixel
f = open(f"/sys/class/graphics/{frame_buffer_number}/bits_per_pixel", "r")
bpp = int(f.read())
if not bpp in (16, 32):
    print("Unsupported bpp")
    sys.exit()
f.close()

print(f"width: {width}, height: {height}, bpp: {bpp}")

# open framebuffer and map it onto a python bytearray
frame_buffer_device = open(f"/dev/{frame_buffer_number}", mode='r+b') # open R/W
frame_buffer_memory_map = mmap.mmap(frame_buffer_device.fileno(), width * height * bpp//8, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)


# Copy the image pixel data to the framebuffer
for y in range(height):
    for x in range(width):
        r, g, b = 120, 120, 200
        
        # Calculate the offset in framebuffer memory
        offset = (y * width + x) * 2

        # Convert RGB values to 16-bit code
        code = ((r & 0x1F) << 11) | ((g & 0x3F) << 5) | (b & 0x1F)

        # Write the 16-bit code to the framebuffer mmap
        frame_buffer_memory_map[offset] = code & 0xFF
        frame_buffer_memory_map[offset + 1] = (code >> 8) & 0xFF

        

#Close the framebuffer device and mmap
frame_buffer_memory_map.close()
frame_buffer_device.close()

一旦根据您的要求更改了代码,请将其放入

/etc/rc.local
中,如下所示,

python3 path/to/your/code &
exit0

这将直接写入帧缓冲区的内存并在其上绘制内容。

© www.soinside.com 2019 - 2024. All rights reserved.