A Raspberry Pi pipeline viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

raspipe.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3. import math
  4. import os
  5. import pygame
  6. import random
  7. import re
  8. import sys
  9. class RasPipe:
  10. size = width, height = 320, 240
  11. # size = width, height = 480, 320
  12. font_size = 48
  13. delay = 70
  14. input_buffer_size = 100
  15. input_lines = []
  16. display_lines = 7
  17. def __init__(self, infile):
  18. self.infile = infile
  19. pygame.init()
  20. exit
  21. self.screen = pygame.display.set_mode(self.size)
  22. self.font = pygame.font.Font(None, self.font_size)
  23. self.bgcolor = pygame.Color(0, 0, 0)
  24. self.fgcolor = pygame.Color(255, 255, 255)
  25. # A little bit of sound.
  26. pygame.mixer.init()
  27. self.click = pygame.mixer.Sound('./tick.wav')
  28. def run(self):
  29. tick = 0
  30. self.click.play()
  31. line = self.infile.readline()
  32. while line:
  33. for event in pygame.event.get():
  34. if event.type == pygame.QUIT:
  35. sys.exit()
  36. tick = tick + 1
  37. self.scale_display()
  38. self.screen.fill(self.bgcolor)
  39. # Get last display_lines of input...
  40. to_render = self.input_lines[-self.display_lines:]
  41. # ...and scroll them up the display:
  42. y = 0
  43. for render_line in to_render:
  44. y += self.font_size
  45. text_surface = self.font.render(render_line.rstrip(), True, self.fgcolor)
  46. # TODO: allow centering text
  47. # rect = text_surface.get_rect(center=((self.width / 2), y))
  48. rect = text_surface.get_rect(left=2, top=y)
  49. self.screen.blit(text_surface, rect)
  50. # A progress bar of sorts
  51. progress = (self.width / 100) * (len(self.input_lines) / 10)
  52. self.screen.fill(self.fgcolor, [0, 0, progress, self.font_size])
  53. if tick % self.display_lines == 0:
  54. self.click.play()
  55. # self.bgcolor.r = random.randrange(0, 255)
  56. # self.bgcolor.g = random.randrange(0, 255)
  57. # self.bgcolor.b = random.randrange(0, 255)
  58. # self.bgcolor.a = random.randrange(0, 255)
  59. pygame.display.flip()
  60. pygame.time.wait(self.delay);
  61. self.input_lines.append(line)
  62. line = self.infile.readline()
  63. def scale_display(self):
  64. """Set the current font size and delay based on amount of input"""
  65. original_font_size = self.font_size
  66. # How big should our font be, and how fast should text scroll?
  67. if len(self.input_lines) > 150:
  68. self.font_size = 18
  69. self.delay = 5
  70. elif len(self.input_lines) > 60:
  71. self.font_size = 20
  72. self.delay = 10
  73. elif len(self.input_lines) > 30:
  74. self.font_size = 24
  75. self.delay = 20
  76. if self.font_size != original_font_size:
  77. self.font = pygame.font.Font(None, self.font_size)
  78. # How many lines of text to display?
  79. self.display_lines = int(self.size[1] / self.font_size) - 1
  80. if __name__ == '__main__':
  81. rp = RasPipe(sys.stdin)
  82. rp.run()