Ver código fonte

add image reading, http availability checker.

Thomas Buck 1 ano atrás
pai
commit
ef7e56821f
25 arquivos alterados com 267 adições e 59 exclusões
  1. 2
    0
      README.md
  2. 42
    0
      camp_small.py
  3. 8
    13
      draw.py
  4. 84
    0
      image.py
  5. BIN
      images/attention.gif
  6. BIN
      images/bunny.gif
  7. BIN
      images/cactus.gif
  8. BIN
      images/cd.gif
  9. BIN
      images/chocobo.gif
  10. BIN
      images/earth.gif
  11. BIN
      images/eye.gif
  12. BIN
      images/grin.gif
  13. BIN
      images/hamster.gif
  14. BIN
      images/mario.gif
  15. BIN
      images/neko.gif
  16. BIN
      images/pcb.gif
  17. BIN
      images/smiley.gif
  18. BIN
      images/star.gif
  19. BIN
      images/warp.gif
  20. 31
    17
      life.py
  21. 7
    11
      manager.py
  22. 69
    0
      net.py
  23. 2
    8
      solid.py
  24. 2
    10
      splash.py
  25. 20
    0
      util.py

+ 2
- 0
README.md Ver arquivo

@@ -22,3 +22,5 @@ This project is licensed as beer-ware:
22 22
     ----------------------------------------------------------------------------
23 23
 
24 24
 The included font from [farsil/ibmfonts](https://github.com/farsil/ibmfonts) is licensed as `CC-BY-SA-4.0`.
25
+
26
+The included GIFs are from [GifCities](https://gifcities.org/?q=32)

+ 42
- 0
camp_small.py Ver arquivo

@@ -0,0 +1,42 @@
1
+#!/usr/bin/env python3
2
+
3
+# ----------------------------------------------------------------------------
4
+# "THE BEER-WARE LICENSE" (Revision 42):
5
+# <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
6
+# you can do whatever you want with this stuff. If we meet some day, and you
7
+# think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8
+# ----------------------------------------------------------------------------
9
+
10
+if __name__ == "__main__":
11
+    from splash import SplashScreen
12
+    from draw import ScrollText
13
+    from solid import Solid
14
+    from life import GameOfLife
15
+    from net import CheckHTTP
16
+    from image import ImageScreen
17
+    from manager import Manager
18
+
19
+    import util
20
+    t = util.getTarget()
21
+
22
+    splash = SplashScreen(t)
23
+    t.loop_start()
24
+    splash.draw()
25
+    t.loop_end()
26
+
27
+    success = Manager(t)
28
+    success.add(ScrollText(t, "TODO"))
29
+    success.add(Solid(t, 1.0))
30
+
31
+    fail = Manager(t)
32
+    fail.add(ImageScreen(t, "attention.gif", 0.2, 2, 20.0, (0, 0, 0)))
33
+    fail.add(ScrollText(t, "The UbaBot Cocktail machine is currently closed. Please come back later for more drinks!", 2))
34
+    fail.add(Solid(t, 2.0))
35
+    fail.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
36
+    fail.add(Solid(t, 2.0))
37
+
38
+    d = CheckHTTP("http://ubabot.frubar.net")
39
+    d.success(success)
40
+    d.fail(fail)
41
+
42
+    t.debug_loop(d.draw)

+ 8
- 13
draw.py Ver arquivo

@@ -84,9 +84,9 @@ class DrawText:
84 84
             if xOff >= -10: # some wiggle room so chars dont disappear
85 85
                 self.drawGlyph(g, xOff, y)
86 86
         return w
87
-
87
+import sys
88 88
 class ScrollText:
89
-    def __init__(self, g, t, i = 1, s = 50):
89
+    def __init__(self, g, t, i = 1, s = 75):
90 90
         self.gui = g
91 91
         self.drawer = DrawText(self.gui)
92 92
         self.text = t
@@ -106,7 +106,8 @@ class ScrollText:
106 106
 
107 107
     def draw(self):
108 108
         if (time.time() - self.last) > self.speed:
109
-            self.offset += (time.time() - self.last) / self.speed
109
+            off = (time.time() - self.last) / self.speed
110
+            self.offset += int(off)
110 111
             self.last = time.time()
111 112
             if self.offset >= self.width:
112 113
                 self.offset = -self.gui.width
@@ -115,14 +116,8 @@ class ScrollText:
115 116
         self.drawer.text(self.text, self.offset, True)
116 117
 
117 118
 if __name__ == "__main__":
118
-    import platform
119
-    t = None
120
-    if platform.machine() == "armv7l":
121
-        from pi import PiMatrix
122
-        t = PiMatrix()
123
-    else:
124
-        from test import TestGUI
125
-        t = TestGUI()
126
-
127
-    d = ScrollText(t, "Hello, World!")
119
+    import util
120
+    t = util.getTarget()
121
+
122
+    d = ScrollText(t, "This is a long scrolling text. Is it too fast or maybe too slow?")
128 123
     t.debug_loop(d.draw)

+ 84
- 0
image.py Ver arquivo

@@ -0,0 +1,84 @@
1
+#!/usr/bin/env python3
2
+
3
+# ----------------------------------------------------------------------------
4
+# "THE BEER-WARE LICENSE" (Revision 42):
5
+# <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
6
+# you can do whatever you want with this stuff. If we meet some day, and you
7
+# think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8
+# ----------------------------------------------------------------------------
9
+
10
+from PIL import Image
11
+import time
12
+import os
13
+
14
+class ImageScreen:
15
+    def __init__(self, g, p, t = 0.2, i = 1, to = 20.0, bg = None):
16
+        self.gui = g
17
+        self.time = t
18
+        self.iterations = i
19
+        self.timeout = to
20
+        self.background = bg
21
+
22
+        scriptDir = os.path.dirname(os.path.realpath(__file__))
23
+        self.path = os.path.join(scriptDir, "images", p)
24
+        self.image = Image.open(self.path)
25
+        print(p, self.image.width, self.image.height, self.image.is_animated, self.image.n_frames)
26
+
27
+        self.restart()
28
+
29
+    def restart(self):
30
+        self.start = time.time()
31
+        self.frame = time.time()
32
+        self.count = 0
33
+        self.done = 0
34
+        self.image.seek(0)
35
+
36
+    def finished(self):
37
+        if self.done >= self.iterations:
38
+            return True
39
+        return (time.time() - self.start) >= self.timeout
40
+
41
+    def draw(self):
42
+        if self.image.is_animated:
43
+            if (time.time() - self.frame) >= self.time:
44
+                self.frame = time.time()
45
+                self.count = (self.count + 1) % self.image.n_frames
46
+                if self.count == 0:
47
+                    self.done += 1
48
+
49
+                self.image.seek(self.count)
50
+
51
+        p = self.image.getpalette()
52
+        for x in range(0, self.image.width):
53
+            for y in range(0, self.image.height):
54
+                v = self.image.getpixel((x, y))
55
+                if isinstance(v, int):
56
+                    c = None
57
+                    if self.background != None:
58
+                        if "transparency" in self.image.info:
59
+                            if v == self.image.info["transparency"]:
60
+                                c = self.background
61
+                        else:
62
+                            if v == self.image.info["background"]:
63
+                                c = self.background
64
+                    if c == None:
65
+                        c = (p[v * 3 + 0], p[v * 3 + 1], p[v * 3 + 2])
66
+                    self.gui.set_pixel(x, y, c)
67
+                else:
68
+                    self.gui.set_pixel(x, y, v)
69
+
70
+if __name__ == "__main__":
71
+    import util
72
+    t = util.getTarget()
73
+
74
+    from manager import Manager
75
+    m = Manager(t)
76
+
77
+    scriptDir = os.path.dirname(os.path.realpath(__file__))
78
+    imageDir = os.path.join(scriptDir, "images")
79
+    for f in os.listdir(os.fsencode(imageDir)):
80
+        filename = os.fsdecode(f)
81
+        m.add(ImageScreen(t, os.path.join(imageDir, filename)))
82
+
83
+    m.restart()
84
+    t.debug_loop(m.draw)

BIN
images/attention.gif Ver arquivo


BIN
images/bunny.gif Ver arquivo


BIN
images/cactus.gif Ver arquivo


BIN
images/cd.gif Ver arquivo


BIN
images/chocobo.gif Ver arquivo


BIN
images/earth.gif Ver arquivo


BIN
images/eye.gif Ver arquivo


BIN
images/grin.gif Ver arquivo


BIN
images/hamster.gif Ver arquivo


BIN
images/mario.gif Ver arquivo


BIN
images/neko.gif Ver arquivo


BIN
images/pcb.gif Ver arquivo


BIN
images/smiley.gif Ver arquivo


BIN
images/star.gif Ver arquivo


BIN
images/warp.gif Ver arquivo


+ 31
- 17
life.py Ver arquivo

@@ -11,21 +11,25 @@ import time
11 11
 import random
12 12
 
13 13
 class GameOfLife:
14
-    def __init__(self, g, f = 20, c1 = (255, 255, 255), c2 = (0, 0, 0), t = 20.0, rc = None):
14
+    def __init__(self, g, f = 20, c1 = (255, 255, 255), c2 = (0, 0, 0), t = 20.0, rc = None, bt = 45.0):
15 15
         self.gui = g
16 16
         self.interval = 1.0 / f
17 17
         self.setColors(c1, c2)
18 18
         self.timeout = t
19 19
         self.randomizeColors = rc
20
+        self.backupTimeout = bt
20 21
         random.seed()
21 22
         self.restart()
22 23
 
24
+        self.editDistFinish = 20
25
+
23 26
     def restart(self):
24 27
         self.data = self.init()
25 28
         self.start = time.time()
26 29
         self.last = time.time()
27 30
         self.lastColor = time.time()
28 31
         self.done = False
32
+        self.lastDiff = 100
29 33
 
30 34
         if self.randomizeColors != None:
31 35
             self.randomize()
@@ -53,8 +57,18 @@ class GameOfLife:
53 57
         return data
54 58
 
55 59
     def finished(self):
56
-        if self.done or ((time.time() - self.start) > self.timeout):
60
+        if self.done:
57 61
             return True
62
+
63
+        if self.timeout != None:
64
+            if (time.time() - self.start) > self.timeout:
65
+                return True
66
+        else:
67
+            if self.lastDiff < self.editDistFinish:
68
+                return True
69
+            if (time.time() - self.start) > self.backupTimeout:
70
+                return True
71
+
58 72
         return False
59 73
 
60 74
     def alive(self, data, x, y):
@@ -93,13 +107,19 @@ class GameOfLife:
93 107
                     self.data[x][y] = False
94 108
 
95 109
         # compare new and old states
96
-        same = True
110
+        diff = 0
97 111
         for x in range(0, self.gui.width):
98 112
             for y in range(0, self.gui.height):
99 113
                 if self.data[x][y] != old[x][y]:
100
-                    same = False
101
-                    break
102
-        self.done = same
114
+                    diff += 1
115
+                    if self.timeout == None:
116
+                        if diff >= self.editDistFinish:
117
+                            break
118
+                    else:
119
+                        if diff >= 1:
120
+                            break
121
+        self.done = (diff == 0)
122
+        self.lastDiff = diff
103 123
 
104 124
     def draw(self):
105 125
         if (time.time() - self.last) > self.interval:
@@ -109,7 +129,7 @@ class GameOfLife:
109 129
         if (self.randomizeColors != None) and (self.randomizeColors != True):
110 130
             if (time.time() - self.lastColor) > self.randomizeColors:
111 131
                 self.lastColor = time.time()
112
-                g.randomize()
132
+                self.randomize()
113 133
 
114 134
         for x in range(0, self.gui.width):
115 135
             for y in range(0, self.gui.height):
@@ -119,16 +139,10 @@ class GameOfLife:
119 139
                     self.gui.set_pixel(x, y, self.colorBG)
120 140
 
121 141
 if __name__ == "__main__":
122
-    import platform
123
-    t = None
124
-    if platform.machine() == "armv7l":
125
-        from pi import PiMatrix
126
-        t = PiMatrix()
127
-    else:
128
-        from test import TestGUI
129
-        t = TestGUI()
130
-
131
-    g = GameOfLife(t, 20, (255, 255, 255), (0, 0, 0), 20.0, 2.0)
142
+    import util
143
+    t = util.getTarget()
144
+
145
+    g = GameOfLife(t, 20, (255, 255, 255), (0, 0, 0), None, 2.0)
132 146
 
133 147
     def helper():
134 148
         if g.finished():

+ 7
- 11
manager.py Ver arquivo

@@ -53,19 +53,15 @@ if __name__ == "__main__":
53 53
     from solid import Solid
54 54
     from life import GameOfLife
55 55
 
56
-    import platform
57
-    t = None
58
-    if platform.machine() == "armv7l":
59
-        from pi import PiMatrix
60
-        t = PiMatrix()
61
-    else:
62
-        from test import TestGUI
63
-        t = TestGUI()
56
+    import util
57
+    t = util.getTarget()
64 58
 
65
-    m = Manager(t)
59
+    splash = SplashScreen(t)
60
+    t.loop_start()
61
+    splash.draw()
62
+    t.loop_end()
66 63
 
67
-    m.add(SplashScreen(t), 2)
68
-    m.add(Solid(t, 1.0))
64
+    m = Manager(t)
69 65
 
70 66
     m.add(ScrollText(t, "This appears once"))
71 67
     m.add(Solid(t, 1.0))

+ 69
- 0
net.py Ver arquivo

@@ -0,0 +1,69 @@
1
+#!/usr/bin/env python3
2
+
3
+# ----------------------------------------------------------------------------
4
+# "THE BEER-WARE LICENSE" (Revision 42):
5
+# <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
6
+# you can do whatever you want with this stuff. If we meet some day, and you
7
+# think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8
+# ----------------------------------------------------------------------------
9
+
10
+import time
11
+import requests
12
+
13
+class CheckHTTP:
14
+    def __init__(self, u, r = 600.0):
15
+        self.url = u
16
+        self.refresh = r
17
+        self.successScreen = None
18
+        self.failScreen = None
19
+        self.restart()
20
+
21
+    def success(self, s):
22
+        self.successScreen = s
23
+
24
+    def fail(self, f):
25
+        self.failScreen = f
26
+
27
+    def restart(self):
28
+        self.start = time.time()
29
+        self.response = None
30
+        self.request()
31
+
32
+        if self.successScreen != None:
33
+            self.successScreen.restart()
34
+        if self.failScreen != None:
35
+            self.failScreen.restart()
36
+
37
+    def request(self):
38
+        if (self.response == None) or ((time.time() - self.start) >= self.refresh):
39
+            self.start = time.time()
40
+            try:
41
+                r = requests.get(self.url)
42
+                self.response = r.ok
43
+            except:
44
+                self.response = False
45
+
46
+    def finished(self):
47
+        self.request()
48
+        if self.response:
49
+            return self.successScreen.finished()
50
+        else:
51
+            return self.failScreen.finished()
52
+
53
+    def draw(self):
54
+        self.request()
55
+        if self.response:
56
+            return self.successScreen.draw()
57
+        else:
58
+            return self.failScreen.draw()
59
+
60
+if __name__ == "__main__":
61
+    from draw import ScrollText
62
+    import util
63
+    t = util.getTarget()
64
+
65
+    d = CheckHTTP("http://xythobuz.de")
66
+    d.success(ScrollText(t, "Success"))
67
+    d.fail(ScrollText(t, "Failure"))
68
+
69
+    t.debug_loop(d.draw)

+ 2
- 8
solid.py Ver arquivo

@@ -28,14 +28,8 @@ class Solid:
28 28
                 self.gui.set_pixel(x, y, self.color)
29 29
 
30 30
 if __name__ == "__main__":
31
-    import platform
32
-    t = None
33
-    if platform.machine() == "armv7l":
34
-        from pi import PiMatrix
35
-        t = PiMatrix()
36
-    else:
37
-        from test import TestGUI
38
-        t = TestGUI()
31
+    import util
32
+    t = util.getTarget()
39 33
 
40 34
     d = Solid(t, 1.0, (0, 255, 0))
41 35
     t.debug_loop(d.draw)

+ 2
- 10
splash.py Ver arquivo

@@ -32,15 +32,7 @@ class SplashScreen:
32 32
         pass
33 33
 
34 34
 if __name__ == "__main__":
35
-    import platform
36
-    t = None
37
-    if platform.machine() == "armv7l":
38
-        from pi import PiMatrix
39
-        t = PiMatrix()
40
-    else:
41
-        from test import TestGUI
42
-        t = TestGUI()
43
-
35
+    import util
36
+    t = util.getTarget()
44 37
     s = SplashScreen(t)
45
-    s.draw()
46 38
     t.debug_loop(s.draw)

+ 20
- 0
util.py Ver arquivo

@@ -0,0 +1,20 @@
1
+#!/usr/bin/env python3
2
+
3
+# ----------------------------------------------------------------------------
4
+# "THE BEER-WARE LICENSE" (Revision 42):
5
+# <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
6
+# you can do whatever you want with this stuff. If we meet some day, and you
7
+# think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8
+# ----------------------------------------------------------------------------
9
+
10
+import platform
11
+
12
+def getTarget():
13
+    t = None
14
+    if platform.machine() == "armv7l":
15
+        from pi import PiMatrix
16
+        t = PiMatrix()
17
+    else:
18
+        from test import TestGUI
19
+        t = TestGUI()
20
+    return t

Carregando…
Cancelar
Salvar