Browse Source

a simple raspipe.py and raspipe_pitft.sh

Scroll lines of stdin across the display, scaling them according to
input length.

Pretty silly, but a good start for the tutorial.
Brennen Bearnes 4 years ago
parent
commit
5b80d89715
2 changed files with 59 additions and 31 deletions
  1. 53
    31
      raspipe.py
  2. 6
    0
      raspipe_pitft.sh

+ 53
- 31
raspipe.py View File

@@ -3,46 +3,68 @@
3 3
 
4 4
 import sys
5 5
 import pygame
6
-import re
7 6
 
8
-pygame.init()
7
+class RasPipe:
8
+    size = width, height = 320, 240
9
+    font_size = 30
10
+    black = (0, 0, 0)
11
+    white = (255,255,255)
12
+    input_buffer_size = 100
13
+    input_lines = []
9 14
 
10
-size = width, height = 320, 240
15
+    def __init__(self, infile):
16
+        self.infile = infile
17
+        pygame.init()
18
+        self.screen = pygame.display.set_mode(self.size)
19
+        self.font = pygame.font.Font(None, self.font_size) 
11 20
 
12
-black = (0, 0, 0)
13
-white = (255,255,255)
14
-font_big = pygame.font.Font(None, 30) 
21
+    def run(self):
22
+        line = self.infile.readline()
23
+        while line:
24
+            for event in pygame.event.get():
25
+              if event.type == pygame.QUIT:
26
+                sys.exit()
15 27
 
16
-screen = pygame.display.set_mode(size)
28
+            self.scale_display()
17 29
 
18
-line = sys.stdin.readline()
19
-while line:
20
-    stars = re.sub('\S', '★', line)
30
+            self.screen.fill(self.black)
21 31
 
22
-    for event in pygame.event.get():
23
-      if event.type == pygame.QUIT:
24
-        sys.exit()
32
+            # Get last 7 lines of input...
33
+            to_render = self.input_lines[-self.display_lines:]
25 34
 
26
-    screen.fill(black)
35
+            # ...and scroll them up the display.
36
+            y = 0
37
+            for render_line in to_render:
38
+                y += self.font_size
39
+                text_surface = self.font.render(render_line.rstrip(), True, self.white)
40
+                rect = text_surface.get_rect(center=(160, y))
41
+                self.screen.blit(text_surface, rect)
27 42
 
28
-    text_surface = font_big.render(line.rstrip(), True, white)
29
-    rect = text_surface.get_rect(center=(160, 30))
30
-    screen.blit(text_surface, rect)
43
+            pygame.display.flip()
31 44
 
32
-#   a = 100
33
-#   if pygame.key.get_focused():
34
-#     press = pygame.key.get_pressed()
35
-#     for i in xrange(0,len(press)):
36
-#       if press[i] == 1:
37
-#         name = pygame.key.name(i)
38
-#         text = font_big.render(name, True, white)
39
-#         screen.blit(text, (100, a))
40
-#         a=a+100
41
-#         if name == "q":
42
-#           sys.exit()
45
+            pygame.time.wait(100);
43 46
 
44
-    pygame.display.flip()
47
+            self.input_lines.append(line)
48
+            line = self.infile.readline()
45 49
 
46
-    pygame.time.wait(500);
50
+    def scale_display(self):
51
+       original_font_size = self.font_size
47 52
 
48
-    line = sys.stdin.readline()
53
+       # How big should our font be?
54
+       if len(self.input_lines) > 20:
55
+           self.font_size = 20
56
+
57
+       if len(self.input_lines) > 50:
58
+           self.font_size = 15
59
+
60
+       if len(self.input_lines) > 100:
61
+           self.font_size = 10
62
+
63
+       if self.font_size != original_font_size:
64
+           self.font = pygame.font.Font(None, self.font_size) 
65
+
66
+       # How many lines of text to display?
67
+       self.display_lines = (self.size[1] / self.font_size) - 1
68
+
69
+rp = RasPipe(sys.stdin)
70
+rp.run()

+ 6
- 0
raspipe_pitft.sh View File

@@ -0,0 +1,6 @@
1
+#!/usr/bin/env bash
2
+
3
+# A wrapper for displaying raspipe.py output on /dev/fb1
4
+
5
+export SDL_FBDEV=/dev/fb1
6
+cat /dev/stdin | ./raspipe.py

Loading…
Cancel
Save