60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from vestactrl import setup_digits, board_init_uart
|
|
import textwrap
|
|
import urllib2
|
|
import json
|
|
import time
|
|
|
|
def send_sign(text):
|
|
"""
|
|
Wraps and sends text to the Vestaboard.
|
|
"""
|
|
COLS = 22
|
|
ROWS = 6
|
|
|
|
# Wrap text to fit the board's width.
|
|
lines = textwrap.wrap(text, width=COLS)
|
|
|
|
# Truncate if the message is too long for the board's height.
|
|
if len(lines) > ROWS:
|
|
lines = lines[:ROWS]
|
|
|
|
# The board controller expects spaces to be replaced with `0 for a blank character.
|
|
board_lines = [line.replace(' ', '`0') for line in lines]
|
|
|
|
# setup_digits handles displaying the lines on the board.
|
|
# It will also vertically center the block of text.
|
|
setup_digits(board_lines, left_to_right_swap=False, real_hw=True)
|
|
|
|
|
|
def poll_and_display():
|
|
"""
|
|
Polls the Protospace API and displays the sign message.
|
|
"""
|
|
last_message = ""
|
|
url = 'https://api.my.protospace.ca/stats/'
|
|
|
|
while True:
|
|
try:
|
|
response = urllib2.urlopen(url)
|
|
data = json.load(response)
|
|
message = data.get('sign')
|
|
|
|
if message and message != last_message:
|
|
print "Updating sign with new message: " + message
|
|
send_sign(message.encode('utf-8'))
|
|
last_message = message
|
|
|
|
except (urllib2.URLError, ValueError, KeyError) as e:
|
|
print "Error: {}".format(e)
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# Initialize communication with the board.
|
|
board_init_uart()
|
|
poll_and_display()
|