Browse Source

implement simple text drawing on pico

Thomas Buck 1 year ago
parent
commit
742eb2307b
11 changed files with 155 additions and 69 deletions
  1. 0
    63
      bdf.py
  2. 1
    1
      camp_small.py
  3. 3
    0
      copy.sh
  4. 1
    1
      manager.py
  5. 51
    1
      pico.py
  6. 1
    1
      qr.py
  7. 83
    0
      scroll.py
  8. 1
    1
      snake.py
  9. 1
    0
      splash.py
  10. 1
    1
      tetris.py
  11. 12
    0
      util.py

draw.py → bdf.py View File

16
 from bdfparser import Font
16
 from bdfparser import Font
17
 from PIL import Image
17
 from PIL import Image
18
 import os
18
 import os
19
-import time
20
 
19
 
21
 class DrawText:
20
 class DrawText:
22
     def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
21
     def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
123
             if xOff >= -16: # some wiggle room so chars dont disappear
122
             if xOff >= -16: # some wiggle room so chars dont disappear
124
                 self.drawGlyph(g, xOff, y + yOff, spacing)
123
                 self.drawGlyph(g, xOff, y + yOff, spacing)
125
         return w
124
         return w
126
-
127
-class ScrollText:
128
-    def __init__(self, g, t, f, i = 1, s = 75, fg = (255, 255, 255), bg = (0, 0, 0)):
129
-        self.gui = g
130
-        self.drawer = DrawText(self.gui, fg, bg)
131
-        self.iterations = i
132
-        self.speed = 1.0 / s
133
-
134
-        self.setText(t, f)
135
-        self.restart()
136
-
137
-    def setText(self, t, f):
138
-        self.text = t
139
-        self.font = f
140
-        self.width = self.drawer.text(self.text, self.font, 0, False)
141
-
142
-    def restart(self):
143
-        self.offset = -self.gui.width
144
-        self.last = time.time()
145
-        self.count = 0
146
-
147
-    def finished(self):
148
-        return (self.count >= self.iterations)
149
-
150
-    def draw(self):
151
-        if (time.time() - self.last) > self.speed:
152
-            off = (time.time() - self.last) / self.speed
153
-            self.offset += int(off)
154
-            self.last = time.time()
155
-            if self.offset >= self.width:
156
-                self.offset = -self.gui.width
157
-                self.count += 1
158
-
159
-        self.drawer.text(self.text, self.font, self.offset, True)
160
-
161
-if __name__ == "__main__":
162
-    import util
163
-    t = util.getTarget()
164
-
165
-    # show splash screen while initializing
166
-    from splash import SplashScreen
167
-    splash = SplashScreen(t)
168
-    t.loop_start()
169
-    splash.draw()
170
-    t.loop_end()
171
-
172
-    from manager import Manager
173
-    m = Manager(t)
174
-
175
-    scriptDir = os.path.dirname(os.path.realpath(__file__))
176
-    fontDir = os.path.join(scriptDir, "fonts")
177
-    for f in os.listdir(os.fsencode(fontDir)):
178
-        filename = os.fsdecode(f)
179
-        if not filename.endswith(".bdf"):
180
-            continue
181
-
182
-        fontName = filename[:-4]
183
-        s = fontName + " Abcdefgh " + fontName
184
-        m.add(ScrollText(t, s, fontName, 1, 75, (0, 255, 0), (0, 0, 25)))
185
-
186
-    m.restart()
187
-    t.loop(m.draw)

+ 1
- 1
camp_small.py View File

12
 
12
 
13
 if __name__ == "__main__":
13
 if __name__ == "__main__":
14
     from splash import SplashScreen
14
     from splash import SplashScreen
15
-    from draw import ScrollText
15
+    from scroll import ScrollText
16
     from solid import Solid
16
     from solid import Solid
17
     from life import GameOfLife
17
     from life import GameOfLife
18
     from net import CheckHTTP
18
     from net import CheckHTTP

+ 3
- 0
copy.sh View File

9
 cp net.py /pyboard
9
 cp net.py /pyboard
10
 cp solid.py /pyboard
10
 cp solid.py /pyboard
11
 cp splash.py /pyboard
11
 cp splash.py /pyboard
12
+cp life.py /pyboard
13
+cp qr.py /pyboard
12
 cp $1 /pyboard/main.py
14
 cp $1 /pyboard/main.py
13
 EOF
15
 EOF
14
 else
16
 else
22
 cp solid.py /pyboard
24
 cp solid.py /pyboard
23
 cp splash.py /pyboard
25
 cp splash.py /pyboard
24
 cp life.py /pyboard
26
 cp life.py /pyboard
27
+cp qr.py /pyboard
25
 EOF
28
 EOF
26
 fi
29
 fi

+ 1
- 1
manager.py View File

49
 
49
 
50
 if __name__ == "__main__":
50
 if __name__ == "__main__":
51
     from splash import SplashScreen
51
     from splash import SplashScreen
52
-    from draw import ScrollText
52
+    from scroll import ScrollText
53
     from solid import Solid
53
     from solid import Solid
54
     from life import GameOfLife
54
     from life import GameOfLife
55
 
55
 

+ 51
- 1
pico.py View File

28
 
28
 
29
         self.matrix = interstate75.Interstate75(display = interstate75.DISPLAY_INTERSTATE75_32X32)
29
         self.matrix = interstate75.Interstate75(display = interstate75.DISPLAY_INTERSTATE75_32X32)
30
 
30
 
31
+        self.black = self.matrix.display.create_pen(0, 0, 0)
32
+        self.white = self.matrix.display.create_pen(255, 255, 255)
33
+
31
         self.loop_start() # initialize with blank image for ScrollText constructor
34
         self.loop_start() # initialize with blank image for ScrollText constructor
32
 
35
 
33
     def loop_start(self):
36
     def loop_start(self):
37
+        self.matrix.display.set_pen(self.black)
34
         self.matrix.display.clear()
38
         self.matrix.display.clear()
39
+        self.matrix.display.set_pen(self.white)
40
+
35
         return False # no input, never quit on our own
41
         return False # no input, never quit on our own
36
 
42
 
37
     def loop_end(self):
43
     def loop_end(self):
52
 
58
 
53
         pen = self.matrix.display.create_pen(color[0], color[1], color[2])
59
         pen = self.matrix.display.create_pen(color[0], color[1], color[2])
54
         self.matrix.display.set_pen(pen)
60
         self.matrix.display.set_pen(pen)
61
+
55
         self.matrix.display.pixel(int(x), int(y))
62
         self.matrix.display.pixel(int(x), int(y))
56
 
63
 
64
+class PicoText:
65
+    def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
66
+        self.gui = g
67
+        self.fg = fg
68
+        self.bg = bg
69
+        self.color = c
70
+
71
+    def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0, compat = True):
72
+        pen = self.gui.matrix.display.create_pen(self.fg[0], self.fg[1], self.fg[2])
73
+        self.gui.matrix.display.set_pen(pen)
74
+
75
+        self.gui.matrix.display.set_font(f)
76
+
77
+        if not compat:
78
+            x = 0
79
+            y = yOff
80
+        else:
81
+            # TODO
82
+            x = 0
83
+            y = int(self.gui.height / 2 - 4 + yOff)
84
+
85
+        self.gui.matrix.display.text(s, x, y, scale=1)
86
+
57
 if __name__ == "__main__":
87
 if __name__ == "__main__":
88
+    import time
89
+
58
     t = PicoMatrix(32, 32)
90
     t = PicoMatrix(32, 32)
59
-    t.loop(lambda: t.set_pixel(15, 15, (255, 255, 255)))
91
+    s = PicoText(t)
92
+
93
+    start = time.time()
94
+    i = 0
95
+    def helper():
96
+        global s, start, i
97
+
98
+        if (time.time() - start) > 2.0:
99
+            start = time.time()
100
+            i = (i + 1) % 2
101
+
102
+        if i == 0:
103
+            s.text("Abgj6", "bitmap6", 0, True, 0, False)
104
+            s.text("Abdgj8", "bitmap8", 0, True, 6 + 2, False)
105
+            s.text("Ag14", "bitmap14_outline", 0, True, 6 + 2 + 8 + 1, False)
106
+        else:
107
+            s.text("Drinks:", "bitmap8", 0)
108
+
109
+    t.loop(helper)

+ 1
- 1
qr.py View File

13
 import time
13
 import time
14
 import qrcode
14
 import qrcode
15
 import util
15
 import util
16
-from draw import DrawText
17
 
16
 
18
 class QRScreen:
17
 class QRScreen:
19
     def __init__(self, g, d, t = 10.0, h = None, f = None, c1 = (0, 0, 0), c2 = (255, 255, 255)):
18
     def __init__(self, g, d, t = 10.0, h = None, f = None, c1 = (0, 0, 0), c2 = (255, 255, 255)):
47
             self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
46
             self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
48
 
47
 
49
         if self.heading != None:
48
         if self.heading != None:
49
+            DrawText = util.getTextDrawer()
50
             self.text = DrawText(self.gui, self.c1, self.c2)
50
             self.text = DrawText(self.gui, self.c1, self.c2)
51
             self.yOff = self.gui.height - self.image.height
51
             self.yOff = self.gui.height - self.image.height
52
         else:
52
         else:

+ 83
- 0
scroll.py View File

1
+#!/usr/bin/env python3
2
+
3
+# Uses the Python BDF format bitmap font parser:
4
+# https://github.com/tomchen/bdfparser
5
+#
6
+# And the pillow Python Imaging Library:
7
+# https://github.com/python-pillow/Pillow
8
+#
9
+# ----------------------------------------------------------------------------
10
+# "THE BEER-WARE LICENSE" (Revision 42):
11
+# <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
12
+# you can do whatever you want with this stuff. If we meet some day, and you
13
+# think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
14
+# ----------------------------------------------------------------------------
15
+
16
+from PIL import Image
17
+import os
18
+import time
19
+import util
20
+
21
+class ScrollText:
22
+    def __init__(self, g, t, f, i = 1, s = 75, fg = (255, 255, 255), bg = (0, 0, 0)):
23
+        DrawText = util.getTextDrawer()
24
+
25
+        self.gui = g
26
+        self.drawer = DrawText(self.gui, fg, bg)
27
+        self.iterations = i
28
+        self.speed = 1.0 / s
29
+
30
+        self.setText(t, f)
31
+        self.restart()
32
+
33
+    def setText(self, t, f):
34
+        self.text = t
35
+        self.font = f
36
+        self.width = self.drawer.text(self.text, self.font, 0, False)
37
+
38
+    def restart(self):
39
+        self.offset = -self.gui.width
40
+        self.last = time.time()
41
+        self.count = 0
42
+
43
+    def finished(self):
44
+        return (self.count >= self.iterations)
45
+
46
+    def draw(self):
47
+        if (time.time() - self.last) > self.speed:
48
+            off = (time.time() - self.last) / self.speed
49
+            self.offset += int(off)
50
+            self.last = time.time()
51
+            if self.offset >= self.width:
52
+                self.offset = -self.gui.width
53
+                self.count += 1
54
+
55
+        self.drawer.text(self.text, self.font, self.offset, True)
56
+
57
+if __name__ == "__main__":
58
+    import util
59
+    t = util.getTarget()
60
+
61
+    # show splash screen while initializing
62
+    from splash import SplashScreen
63
+    splash = SplashScreen(t)
64
+    t.loop_start()
65
+    splash.draw()
66
+    t.loop_end()
67
+
68
+    from manager import Manager
69
+    m = Manager(t)
70
+
71
+    scriptDir = os.path.dirname(os.path.realpath(__file__))
72
+    fontDir = os.path.join(scriptDir, "fonts")
73
+    for f in os.listdir(os.fsencode(fontDir)):
74
+        filename = os.fsdecode(f)
75
+        if not filename.endswith(".bdf"):
76
+            continue
77
+
78
+        fontName = filename[:-4]
79
+        s = fontName + " Abcdefgh " + fontName
80
+        m.add(ScrollText(t, s, fontName, 1, 75, (0, 255, 0), (0, 0, 25)))
81
+
82
+    m.restart()
83
+    t.loop(m.draw)

+ 1
- 1
snake.py View File

7
 # think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
7
 # think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8
 # ----------------------------------------------------------------------------
8
 # ----------------------------------------------------------------------------
9
 
9
 
10
-from draw import ScrollText
10
+from scroll import ScrollText
11
 import time
11
 import time
12
 import random
12
 import random
13
 
13
 

+ 1
- 0
splash.py View File

14
         self.height = self.gui.panelH
14
         self.height = self.gui.panelH
15
 
15
 
16
     def draw(self):
16
     def draw(self):
17
+        # repeat the image on each available panel
17
         for x in range(0, int(self.gui.width / self.width)):
18
         for x in range(0, int(self.gui.width / self.width)):
18
             for y in range(0, int(self.gui.height / self.height)):
19
             for y in range(0, int(self.gui.height / self.height)):
19
                 self.drawOnce(x * self.width, y * self.height)
20
                 self.drawOnce(x * self.width, y * self.height)

+ 1
- 1
tetris.py View File

7
 # think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
7
 # think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8
 # ----------------------------------------------------------------------------
8
 # ----------------------------------------------------------------------------
9
 
9
 
10
-from draw import ScrollText
10
+from scroll import ScrollText
11
 import time
11
 import time
12
 import random
12
 import random
13
 
13
 

+ 12
- 0
util.py View File

140
         return requests.get
140
         return requests.get
141
 
141
 
142
     return None
142
     return None
143
+
144
+def getTextDrawer():
145
+    try:
146
+        # Try BDF parser library
147
+        from bdf import DrawText
148
+        return DrawText
149
+    except:
150
+        # fall back to the Pico Interstate75 implementation
151
+        from pico import PicoText
152
+        return PicoText
153
+
154
+    return None

Loading…
Cancel
Save