This article assumes that you have ev3dev installed on your EV3 and have an SSH connection. If you have not built the environment, please refer to this article.
mindstorm-Let's control EV3 with Linux! Install ev3dev OS and SSH connection
In ev3dev, I will explain how to control the LCD of EV3 with a figure of sample code. The python library ev3dev-lang-python provides APIs that allow you to easily control the LCD, but this time we will use those libraries. The purpose is to understand how LCD control works without using it.
The EV3's LCD has a monochrome screen of 178 x 128 pixels.
The video driver is provided by the Linux framebuffer interface, which allows you to control the LCD by writing data to the device file / dev / fb0.
Let's check the framebuffer information by executing the fbset -i
command.
robot@ev3dev:~$ fbset -i
mode "178x128"
geometry 178 128 178 128 1
timings 0 0 0 0 0 0 0
rgba 1/0,1/0,1/0,0/0
endmode
Frame buffer device information:
Name : ST7586
Address : 0xc39f9000
Size : 3072
Type : PACKED PIXELS
Visual : MONO01
XPanStep : 0
YPanStep : 0
YWrapStep : 0
LineLength : 24
Accelerator : No
You can see that the LCD is composed of 178 x 128 pixels, and the pixel information in a horizontal row is represented by 24 bytes. (Figure below)
Since the LCD is monochrome (not color), each minimum pixel is 0 or 1 binary data. Since 1byte = 8bit, 178/8 = 22.25 (byte) is enough to express 178px. However, in reality, the pixel information in a horizontal row is represented by 24 bytes. In other words, not all 24-byte data is displayed on the screen.
To control the LCD, 24 bytes x 128 (rows) = 3072 bytes of data must be written to the device file. The sample code to display the following figure is as follows.
#!/usr/bin/env python
# Hard coding these values is not a good idea because the values could
# change. But, since this is an example, we want to keep it short.
SCREEN_WIDTH = 178 # pixels
SCREEN_HEIGHT = 128 # pixels
LINE_LENGTH = 24 # bytes
SIZE = 3072 # bytes
import os
import array
def main():
#Define an array with 3072 elements
buf = [0] * SIZE
#Draw a vertical line on the 100th column
for row in range(0, SCREEN_HEIGHT):
buf[row * LINE_LENGTH + int(100 / 8)] = 1 << (100 % 8)
#Draw a horizontal line in the 64th column
for col in range(0, LINE_LENGTH):
buf[64 * LINE_LENGTH + col] = 0xff
import math
#Center point(40,40),Draw a circle with a radius of 20
for x in range(0, 20):
y = math.sqrt(20 * 20 - x * x)
buf[(40 + int(y)) * LINE_LENGTH + int((40 + x) / 8)] = 1 << ((40 + x) % 8)
buf[(40 - int(y)) * LINE_LENGTH + int((40 + x) / 8)] = 1 << ((40 + x) % 8)
buf[(40 + int(y)) * LINE_LENGTH + int((40 - x) / 8)] = 1 << ((40 - x) % 8)
buf[(40 - int(y)) * LINE_LENGTH + int((40 - x) / 8)] = 1 << ((40 - x) % 8)
#Write to device file
f = os.open('/dev/fb0', os.O_RDWR)
s = array.array('B', buf).tostring()
os.write(f, s)
os.close(f)
if __name__ == '__main__':
main()
When you save and execute the code, the figure will be displayed on the LCD screen. If you want to control a complicated LCD that is not defined in the library, it will be helpful.
Screen capture [Ev3dev] Create a program to capture LCD (screen) using python
Recommended Posts