164 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			164 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| #!/usr/bin/env python
 | |
| # -*- coding: utf-8 -*-
 | |
| 
 | |
| import httplib
 | |
| import urllib
 | |
| import json
 | |
| from vbctrl import *
 | |
| import random
 | |
| 
 | |
| real_hw = True
 | |
| 
 | |
| def adjust_blanks(s):
 | |
|     blank = '░'
 | |
|     return s.replace('░','`0')
 | |
| 
 | |
| def adjust_empty_spaces(s):
 | |
|     empty_space = ' '
 | |
|     return s.replace(' ', '`0')
 | |
| 
 | |
| def get_board_content():
 | |
|     url1 = 'https://app.vestaboard.com/api/v1/board/'
 | |
|     url2 = '/describe'
 | |
|     full_url = url1 + board_id + url2
 | |
|     page = urllib.urlopen(full_url)
 | |
|     #conn = httplib.HTTPSConnection('app.vestaboard.com')
 | |
|     #conn.request('GET', '/api/v1/board/%s/describe' % board_id)
 | |
|     #response = conn.getresponse()
 | |
|     contents = page.read()
 | |
|     #print response.status, response.reason
 | |
|     print page.getcode()
 | |
|     #retval = adjust_blanks(response.read())
 | |
|     contents2 = adjust_blanks(contents)
 | |
|     retval = adjust_empty_spaces(contents2)
 | |
|     #conn.close()
 | |
|     page.close()
 | |
|     return retval
 | |
| 
 | |
| def is_new_content(new, old):
 | |
|     retval = False
 | |
|     for v in zip(new,old):
 | |
|         if v[0] != v[1]:
 | |
|             retval = True
 | |
|             break
 | |
|     return retval
 | |
| 
 | |
| def random_colors():
 | |
|     proto = [ 
 | |
| 	'`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d',
 | |
| 	'`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d',
 | |
| 	'`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d',
 | |
| 	'`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d',
 | |
| 	'`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d',
 | |
| 	'`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d`%d']
 | |
|     lines = []    
 | |
|     for p in proto:
 | |
| 	s = p % (random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5),
 | |
|             random.randint(1, 5))
 | |
|         lines.append(s)
 | |
|     setup_digits(lines, left_to_right_swap=False)
 | |
|     
 | |
| # VBR (23x7)
 | |
| #board_id = '700df57c-8b0a-4166-8758-b9de6e900cb8'
 | |
| # VB (22x6)
 | |
| #board_id = 'demo-a3c6ed9d-0499-4368-a3e9-b132cfc94305'
 | |
| # Testaboard
 | |
| #board_id = 'demo-6a439bfd-fe37-4d82-af76-9cbf4fbd6367'
 | |
| board_id = ' '
 | |
| board_name = 'Testaboard'
 | |
| 
 | |
| # ANDREA
 | |
| timeouts = 15
 | |
| if real_hw:
 | |
|     print 'before pause'
 | |
|     time.sleep(timeouts)
 | |
|     print 'after pause'
 | |
|     print 'before init_uart'
 | |
|     board_init_uart()
 | |
|     print 'after init_uart'
 | |
|     print 'before blanks'
 | |
|     # ANDREA
 | |
|     blanks()
 | |
|     print 'after blanks'
 | |
|     print 'before 2nd pause'
 | |
|     time.sleep(timeouts)
 | |
|     print 'after 2nd pause'
 | |
| 
 | |
| last_lines = ['', '', '', '', '', '']
 | |
| 
 | |
| data = None
 | |
| while True:
 | |
| 
 | |
|     while board_id == ' ':
 | |
|         print 'Fetching board id...'
 | |
|         print 'Board id inexistant! Please set board id, then reboot.'
 | |
|         time.sleep(5)
 | |
| 
 | |
|     time.sleep(3)
 | |
| 
 | |
|     print('**********')
 | |
|     print board_name
 | |
|     print('**********')
 | |
|     r = get_board_content()
 | |
|     data = json.loads(r)
 | |
|     #print('DATA: %s') % data
 | |
|     ulines = data['message']['lines']
 | |
|     #print('ulines: %s') % ulines
 | |
|     lines = [l.encode('ascii', 'ignore') for l in ulines]
 | |
|     print lines
 | |
|     row_num = len(lines)
 | |
|     print('Number of rows: %d') % row_num
 | |
|     #for i in range(6):
 | |
|     	#print('%s || %s' %(lines[i], last_lines[i]))
 | |
| 
 | |
|     if row_num > 6:
 | |
|         print('* * * * * ')
 | |
|         print('ERROR: Too many rows; cannot send your message to the board. Sorry!')
 | |
|         print('* * * * * ')
 | |
| 
 | |
|     if row_num < 7:
 | |
|         row_len_flag = 0
 | |
|         for i in range(row_num):
 | |
|             row_string = lines[i]
 | |
|             #print('CURRENT ROW STRING: %s') % row_string
 | |
|             row_len = len(row_string) - row_string.count('`')
 | |
|             #print('CURRENT ROW LENGTH: %s') % row_len
 | |
|             if row_len > 22:
 | |
|                 row_len_flag = 1
 | |
|                 print('* * * * * ')
 | |
|                 print('ERROR: Too many characters in row %d; cannot send your message to the board. Sorry!') % i
 | |
|                 print('* * * * * ')
 | |
|         if row_len_flag == 0:
 | |
|             print('*** Jolly good! The message is OK to send to the board. ***')
 | |
|             if lines and is_new_content(lines, last_lines):
 | |
|             #if lines != last_lines:
 | |
|                 print 'before setup digits'
 | |
|                 # ANDREA
 | |
|                 setup_digits(lines, left_to_right_swap=False, real_hw=real_hw)
 | |
|                 print 'after setup digits'
 | |
|                 last_lines = lines
 | |
|                 print 'Waiting for bits to change...'
 | |
|                 time.sleep(7)
 | |
|                 print 'Fetching message...'
 | |
| 
 |