This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_fb.py
204 lines (157 loc) · 3.69 KB
/
test_fb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
"""Framebuffer test program
Usage: python test_fb.py [options]
Options:
-d ..., --device=... Framebuffer device to test, default: /dev/fb0
-h, --help show this help
"""
import sys
import getopt
from time import sleep
import struct
import termios, fcntl, sys, os
import colorsys
from fb import Framebuffer
from gfx import Rect
def keypressed():
try:
c = sys.stdin.read(1)
return True
except IOError:
return False
def pause(secs):
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
ctrlc = False
paused = False
t = secs/0.1
i = 0
while i<t:
if keypressed():
paused = True
break
sleep(0.1)
i += 1
if paused:
while True:
if keypressed():
break
sleep(0.1)
except KeyboardInterrupt:
ctrlc = True
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
if ctrlc:
sys.exit(1)
def msg(fb, s, c, max_size):
width = fb.str_width(s)
for i in range(max_size+1, 0, -1):
if fb.xres > i*width:
fb.putstr(-1, -1, s, c, i)
return True
return False
def show_name(fb, c):
fb.fill(0)
if not msg(fb, fb.name, c, 3):
msg(fb, 'Test', c, 3)
pause(2)
def show_done(fb, c):
fb.fill(0)
msg(fb, 'Done', c, 3)
pause(2)
def test_blank(fb, c):
print " Blanking"
fb.fill(0)
msg(fb, 'Blank ', c, 2)
pause(1)
fb.blank(1)
pause(2)
fb.fill(0)
msg(fb, 'Unblank', c, 2)
sleep(0.5)
fb.blank(0)
pause(1)
def test_border(fb, c):
print " Border"
fb.fill(0)
fb.draw.rect(c, Rect(0, 0, fb.xres-1, fb.yres-1), 1)
fb.draw.rect(c, Rect(2, 2, 4, 4), 0)
fb.draw.rect(c, Rect(2, fb.yres-6, 4, 4), 0)
fb.draw.rect(c, Rect(fb.xres-6, 2, 4, 4), 0)
fb.draw.rect(c, Rect(fb.xres-6, fb.yres-6, 4, 4), 0)
pause(2)
def test_raster(fb, c):
print " Raster"
fb.fill(0)
for y in range(0, fb.yres, 2):
for x in range(0, fb.xres, 2):
fb.putpixel(x, y, c)
fb.putpixel(x+1, y+1, c)
pause(2)
def test_rgb(fb):
if (fb.bits_per_pixel == 1):
return
print " RGB"
fb.fill(0)
width = (fb.xres-1)/3
fb.draw.rect(fb.rgb(255,0,0), Rect(0, 0, width, fb.yres), 0)
fb.putstr(5, fb.yres/2, 'RED', 0, 1)
fb.draw.rect(fb.rgb(0,255,0), Rect(width, 0, width, fb.yres), 0)
fb.putstr(5+width, fb.yres/2, 'GREEN', 0, 1)
fb.draw.rect(fb.rgb(0,0,255), Rect(2*width, 0, width, fb.yres), 0)
fb.putstr(5+2*width, fb.yres/2, 'BLUE', 0, 1)
pause(2)
def test_colors(fb):
if (fb.bits_per_pixel != 16):
return
print " Colors"
fb.fill(0)
fb.fbp.seek(0)
step = fb.xres * fb.yres
for i in range(0, step):
h = i/float(step)
rgb = colorsys.hsv_to_rgb(h, 1, 1)
red = int(rgb[0] * ((1 << fb.red.length) - 1))
green = int(rgb[1] * ((1 << fb.green.length) - 1))
blue = int(rgb[2] * ((1 << fb.blue.length) - 1))
color = fb.rgb(red, green, blue)
fb.fbp.write(struct.pack("H", color))
pause(2)
def usage():
print __doc__
def main(argv):
device = '/dev/fb0'
try:
opts, args = getopt.getopt(argv, "hd:", ["help", "device="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-d", "--device"):
device = arg
fb = Framebuffer(device)
print fb
red = fb.rgb(255,0,0)
yellow = fb.rgb(255,255,0)
green = fb.rgb(0,255,0)
show_name(fb, red)
print "Tests:"
test_border(fb, yellow)
test_rgb(fb)
test_colors(fb)
test_raster(fb, red)
test_blank(fb, red)
show_done(fb, red)
fb.fill(0)
fb.close()
if __name__ == '__main__':
main(sys.argv[1:])