Skip to content

Instantly share code, notes, and snippets.

@mraxus
Last active March 22, 2017 22:22
Show Gist options
  • Save mraxus/8685669c00082b0e0896568cf69dae4f to your computer and use it in GitHub Desktop.
Save mraxus/8685669c00082b0e0896568cf69dae4f to your computer and use it in GitHub Desktop.
Py-script for reading a file and displaying the text with the Raspberry scroll pHAT
#!/usr/bin/env python
### http://docs.pimoroni.com/scrollphat
from collections import namedtuple
import json
import os
import sys
import time
import scrollphat
BRIGHTNESS = 10
FILENAME = './scroll.json'
FLASH_BRIGHTNESS = 150
MESSAGE = 'GO TEAM'
message = MESSAGE
class JsonFile(object):
@staticmethod
def _json_object_hook(d):
return namedtuple('X', d.keys())(*d.values())
def __init__(self, filename):
self.content = {}
self.timestamp = 0
self.filename = filename
self.read()
def _get_timestamp(self):
return os.stat(self.filename).st_mtime
def has_changed(self):
timestamp = self._get_timestamp()
return self.timestamp != timestamp
def read(self):
self.timestamp = self._get_timestamp()
try:
with open(FILENAME) as file:
self.content = json.loads(file.read(), object_hook=JsonFile._json_object_hook)
return True
except ex:
print ex
return False
def main():
global message
scrollphat.set_brightness(BRIGHTNESS)
file = JsonFile(FILENAME)
while True:
try:
if file.has_changed():
file.read()
flash_screen()
scroll_text(file.content.message, 0.1)
except KeyboardInterrupt:
scrollphat.clear()
sys.exit(-1)
def json2obj(data):
return json.loads(data, object_hook=_json_object_hook)
def load_json_file():
try:
with open(FILENAME) as data_file:
content = json2obj(data_file.read())
return content
except ex:
print ex
return None
def read_file(default_text):
try:
with open(FILENAME) as data_file:
content = json.load(data_file)
return content['message']
except:
return default_text
def flash_screen():
scrollphat.set_brightness(FLASH_BRIGHTNESS)
scrollphat.set_pixels(lambda x, y: True, True)
time.sleep(0.15)
scrollphat.set_pixels(lambda x, y: False, True)
time.sleep(0.15)
scrollphat.set_pixels(lambda x, y: True, True)
time.sleep(0.15)
scrollphat.set_pixels(lambda x, y: False, True)
scrollphat.set_brightness(BRIGHTNESS)
def scroll_text(text, sleep_sec):
scrollphat.clear_buffer()
scrollphat.write_string(text, 11)
len = scrollphat.buffer_len()
while len > 0:
len -= 1
scrollphat.scroll()
time.sleep(sleep_sec)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment