-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmitLight.py
227 lines (177 loc) · 4.42 KB
/
transmitLight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import pygame
import binascii
import time
import serial
import schedule
import sys
import math
withSerial = False
gamma = 2.0
levelNum = 9
MIN = 0
MAX = levelNum - 2
REPEAT = levelNum - 1
previousNum = levelNum + 1
#El checksum sera de 6 digitos en octal (777777) permite 32kbytes
checksum = 0
# el periodo no puede ser menor que el refresh rate de la pantalla. (60hz - 16.6ms)
# Creo que lo mas seguro seria mantenernos abajo de 30hz (33.3ms) para estar seguros de que funcione en cualquier hardware.
periodo = .070
if withSerial:
SCK = serial.Serial('/dev/ttyACM0', 115200)
SCK.timeout = periodo
'''
Low level function to color screen to a determined value
This function takes care of the color repetition, so it should be the last to manage values
'''
def outDigit(digit):
# global variable to know wath was sended before and implement repetitions
global previousNum
#makes sure that digit is a char
digit = str(digit)
# if this value was the last sended use the REPEAT value
if digit == previousNum:
# print "REPEAT"
previousNum = levelNum + 1
paint(REPEAT)
# if the previous sended value is diferent from this, send it as is
else:
paint(int(digit))
previousNum = digit
'''
Sends a single character
'''
def sendChar(char):
global checksum
checksum = checksum + int(oct(int(binascii.hexlify(char), 16)),8)
# converts char to his ASCII octal representation
asciiChar = oct(int(binascii.hexlify(char), 16))[1:]
# if the value has less than 3 digits fill it with leading zeros
while len(asciiChar) < 3:
asciiChar = "0" + str(asciiChar)
# sends the digits
for digit in asciiChar:
outDigit(digit)
print char + " ==> " + asciiChar
'''
outputs a ramp of colors from 0 to valores
'''
def ramp(valores):
for i in range(valores):
paint(i)
'''
Sends a word char by char
'''
def sendWord(word):
for letter in word:
sendChar(letter)
def sendChecksum():
global checksum
toSend = oct(checksum)
while(len(toSend) < 6):
toSend = '0' + toSend
for digit in toSend:
outDigit(digit)
print "checksum: " + toSend
'''
Init sequence, after this we can start sending the data
'''
def INIT():
paint(MAX)
paint(REPEAT)
ramp(levelNum)
paint(MIN)
paint(REPEAT)
paint(MIN)
paint(REPEAT)
print("INIT")
time.sleep(periodo*5)
'''
Sends end of text char ETX
'''
def ETX():
outDigit(0)
outDigit(0)
outDigit(3)
print "ETX ==> 003"
'''
Sends end of transmission char EOT
'''
def EOT():
outDigit(0)
outDigit(0)
outDigit(4)
print "EOT ==> 004"
'''
Sends new line char LF
'''
def newLine():
outDigit(0)
outDigit(1)
outDigit(2)
print "LF ==> 012"
'''
Sends start of text char STX
'''
def STX():
outDigit(0)
outDigit(0)
outDigit(2)
print "STX ==> 002"
'''
Inverts gamma correction improving linearity of values
@param value requested value (0-levelNum)
@param levelNum maximum value of the used scale
'''
def getColor(value, levelNum):
previo = (value * (255.0/(levelNum - 1)))
final = 255.0 * math.pow((previo / 255.0), (1.0 / gamma))
return (final,final,final)
'''
Low level function that changes window color
'''
def paint(colorValue):
# fills window with requested value
screen.fill(getColor(colorValue, levelNum))
if withSerial:
returned = SCK.readline()
rr = str(returned).strip()
# print "returned: " + rr
#pygame stuff
pygame.display.flip()
# Sleeps for the requested period.
time.sleep(periodo)
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
print str(periodo*1000) + " ms -- " + str( ( (1/periodo) * math.sqrt(levelNum-1) ) / 8.0 ) + " bytes/seg"
while True:
# There should be a pause of more time than the watchdog timeout to give the kit time to restart after errors.
paint(MIN)
time.sleep(1.5)
checksum = 0
INIT()
STX()
sendWord("auth\n")
sendWord("mySSID\n")
sendWord("myPASS\n")
sendWord("myTOKEN\n")
ETX()
sendChecksum()
EOT()
'''
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == 27: sys.exit() # esc key exits
if event.key == 32: state = 2 # space starts calibration
if event.key == 275:
# faster
periodo = periodo - 0.005
print str(periodo*1000) + " ms -- " + str( ( (1/periodo) * math.sqrt(levelNum-1) ) / 8.0 ) + " bytes/seg"
if event.key == 276:
# slower
periodo = periodo + 0.005
print str(periodo*1000) + " ms -- " + str( ( (1/periodo) * math.sqrt(levelNum-1) ) / 8.0 ) + " bytes/seg"
# print event.key
if state == 3: sendChar(str(unichr(event.key)))
'''