-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflipdotsim.py
85 lines (66 loc) · 2.47 KB
/
flipdotsim.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
"""
A package that allows for simulating the display without the need of a physical
display. It relies on the pygame-package.
The simulator can be used in the following way.
Creating a display with specific dimensions.
>>> import flipdotsim
>>> fds = flipdotsim.FlipDotSim(width=28, height=13)
Set two pixels at the top left to be turned on.
>>> fds.px(0,0, True)
>>> fds.px(0,1, True)
Actually turn on or off all pixels.
>>> fds.show()
"""
import pygame
import displayprovider
YELLOWDOT_FILE = "ressources/y.jpg"
BLACKDOT_FILE = "ressources/b.jpg"
IMG_WIDTH_HEIGHT = 20 # pixel on each side
class FlipDotSim(displayprovider.DisplayBase):
'Simulator class that shows the display in a pygame GUI.'
def __init__(self, width=28, height=13, fps=30):
super().__init__(width, height)
pygame.init()
pygame.display.set_caption("FlipDot Simulator")
self.screen = pygame.display.set_mode(
(self.width*IMG_WIDTH_HEIGHT, self.height*IMG_WIDTH_HEIGHT))
self.y = pygame.image.load(YELLOWDOT_FILE).convert()
self.b = pygame.image.load(BLACKDOT_FILE).convert()
self.clock = pygame.time.Clock()
self.fps = fps
self.clear()
def set(self, x, y):
self.screen.blit(self.y, (x*IMG_WIDTH_HEIGHT, y*IMG_WIDTH_HEIGHT))
def reset(self, x, y):
self.screen.blit(self.b, (x*IMG_WIDTH_HEIGHT, y*IMG_WIDTH_HEIGHT))
def px(self, x, y, val):
"""Set a pixel to on or off at (X|Y). The dot will not be displayed
immediately."""
if val:
self.set(x, y)
else:
self.reset(x, y)
def show(self):
"""Show the current state of all pixels on the display."""
# empty the event queue to prevent it from being full
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
pygame.display.flip()
self.clock.tick(self.fps)
def clear(self, invert=False):
for x in range(self.width):
for y in range(self.height):
if invert:
self.set(x, y)
else:
self.reset(x, y)
if __name__ == '__main__':
import flipdotfont
import configuration
fds = FlipDotSim(
width=configuration.WIDTH,
height=configuration.HEIGHT,
fps=configuration.simulator["fps"])
fdw = flipdotfont.TextScroller(fds)
fdw.scrolltext('Test 12345!', flipdotfont.big_font(), 1)