-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssd1305.py
443 lines (368 loc) · 9.41 KB
/
ssd1305.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import os, smbus, sched, time, datetime, random, io, threading, socket
import OPi.GPIO as GPIO
SSD1305 = 0x3C
I2CNUM = 0
i2cdev = 0
UDP_IP = "192.168.0.140"
UDP_PORT = 4000
buffer = bytearray(128*34)
framebuffer = bytearray(128*32>>3)
numbers_big = bytearray(480*32)
drawdots = False
exp = 5
cycles = exp
first_trans = False
tph_file = "/tmp/tph"
co2_file = "/tmp/co2level"
heat_file = "/tmp/heating"
cdate = 0
heating = False
t = 0
p = 0
h = 0
co2 = 0
id = 0
def init_ssd1305():
global SSD1305, I2CNUM, i2cdev
global buffer, framebuffer, numbers_big
buffer = bytearray(128*34)
framebuffer = bytearray(128*32>>3)
numbers_big = bytearray(480*32)
file = open("numbers-1.raw","rb")
numbers_big = file.read(480*32)
file.close
i2cdev = smbus.SMBus(I2CNUM)
#On
i2cdev.write_byte_data(SSD1305, 0x80, 0xAF)
#MUX / number of lines
i2cdev.write_byte_data(SSD1305, 0x80, 0xA8)
i2cdev.write_byte_data(SSD1305, 0x80, 31)
#Horisontal addressing mode
i2cdev.write_byte_data(SSD1305, 0x80, 0x20)
i2cdev.write_byte_data(SSD1305, 0x80, 0x0)
#Reverse/set orientation
i2cdev.write_byte_data(SSD1305, 0x80, 0xC8)
i2cdev.write_byte_data(SSD1305, 0x80, 0xA1)
#Columns
i2cdev.write_byte_data(SSD1305, 0x80, 0x21)
i2cdev.write_byte_data(SSD1305, 0x80, 4)
i2cdev.write_byte_data(SSD1305, 0x80, 131)
#Pages
i2cdev.write_byte_data(SSD1305, 0x80, 0x22)
i2cdev.write_byte_data(SSD1305, 0x80, 0)
i2cdev.write_byte_data(SSD1305, 0x80, 3)
#clean
for x in range(128*32>>3):
i2cdev.write_byte_data(SSD1305, 0x40, 0 )
#Columns
i2cdev.write_byte_data(SSD1305, 0x80, 0x21)
i2cdev.write_byte_data(SSD1305, 0x80, 4)
i2cdev.write_byte_data(SSD1305, 0x80, 131)
#Pages
i2cdev.write_byte_data(SSD1305, 0x80, 0x22)
i2cdev.write_byte_data(SSD1305, 0x80, 0)
i2cdev.write_byte_data(SSD1305, 0x80, 3)
i2cdev.write_byte_data(SSD1305, 0x80, 0x81)
i2cdev.write_byte_data(SSD1305, 0x80, 0x0)
def putdot(x,y,w):
global buffer, framebuffer, numbers_big
bufoffset=y*128+x
for y in range(w):
for x in range(w):
buffer[bufoffset]=0xff
bufoffset+=1
bufoffset-=w
bufoffset+=128
def putchar(val,bufoffset,charwidth):
global buffer, framebuffer, numbers_big
charoffset = val*32
for x in range(32):
for y in range(charwidth):
buffer[bufoffset]=numbers_big[charoffset]
charoffset+=1
bufoffset+=1
charoffset-=charwidth
bufoffset-=charwidth
charoffset+=480
bufoffset+=128
def render():
global buffer, framebuffer
#Move pointer to start
#Columns
i2cdev.write_byte_data(SSD1305, 0x80, 0x21)
i2cdev.write_byte_data(SSD1305, 0x80, 4)
i2cdev.write_byte_data(SSD1305, 0x80, 131)
#Pages
i2cdev.write_byte_data(SSD1305, 0x80, 0x22)
i2cdev.write_byte_data(SSD1305, 0x80, 0)
i2cdev.write_byte_data(SSD1305, 0x80, 3)
#Put buffer to framebuffer
foffset = 0
lineconst = 7<<7
for y in range(4):
rowoffset = y<<10
for x in range(128):
xoff = x+rowoffset+lineconst
pixel = (buffer[xoff]>>7)
for z in range(7):
xoff-=128
pixel=pixel<<1
pixel = pixel | (buffer[xoff]>>7)
framebuffer[foffset] = pixel & 0xff
foffset += 1
for x in range(16):
i2cdev.write_i2c_block_data(SSD1305, 0x40, list(framebuffer[(x<<5):(x<<5)+32]))
def render_slow():
global buffer, framebuffer
#Move pointer to start
#Columns
i2cdev.write_byte_data(SSD1305, 0x80, 0x21)
i2cdev.write_byte_data(SSD1305, 0x80, 4)
i2cdev.write_byte_data(SSD1305, 0x80, 131)
#Pages
i2cdev.write_byte_data(SSD1305, 0x80, 0x22)
i2cdev.write_byte_data(SSD1305, 0x80, 0)
i2cdev.write_byte_data(SSD1305, 0x80, 3)
#Put buffer to framebuffer
foffset = 0
lineconst = 7<<7
for y in range(4):
rowoffset = y<<10
for x in range(128):
xoff = x+rowoffset+lineconst
pixel = (buffer[xoff]>>7)
for z in range(7):
xoff-=128
pixel=pixel<<1
pixel = pixel | (buffer[xoff]>>7)
framebuffer[foffset] = pixel & 0xff
foffset += 1
for x in range(16):
i2cdev.write_i2c_block_data(SSD1305, 0x40, list(framebuffer[(x<<5):(x<<5)+32]))
time.sleep(0.01)
#Read all data once per minute
def vals_func():
global tph_file, co2_file, heatfile
global cdate,t,p,h,co2,heating
s.enter(60, 1, vals_func, ())
#Get TPH
try:
tphfile=open(tph_file,"r")
data=tphfile.read()
tphfile.close()
cdate,t,p,h=map(float,data.split(","))
cdate=datetime.datetime.fromtimestamp(cdate)
except:
t=20
p=985 #Earth avg from wiki
h=50
cdate=datetime.datetime.now()
try:
tphfile=open(co2_file,"r")
co2=tphfile.read()
tphfile.close()
except:
co2=800
heating = os.path.isfile(heat_file)
#print(cdate,t,p,h,co2,heating)
def co2_func():
global SSD1305, I2CNUM, i2cdev
global buffer, framebuffer, first_trans,exp
global drawdots, cycles, t, p, h, co2, heating
del buffer
buffer = bytearray(128*32)
first_trans = (cycles == exp)
cycles-=1
if (cycles > 0):
s.enter(1, 1, co2_func, ())
else:
s.enter(1, 1, humi_func, ())
cycles = exp
temp = int(co2)
val = (temp // 1000)
bufoffset = 0
if (val > 0):
putchar(val,bufoffset,28)
val = (temp // 100) % 10
bufoffset = 28+2
putchar(val,bufoffset,28)
val = (temp // 10) % 10
bufoffset = 28+2+28+2
putchar(val,bufoffset,28)
val = temp % 10
bufoffset = 28+2+28+2+28+2
putchar(val,bufoffset,28)
bufoffset=119
putchar(13,bufoffset,9)
if first_trans:
render_slow()
first_trans = False
else:
render()
def humi_func():
global SSD1305, I2CNUM, i2cdev
global buffer, framebuffer, first_trans,exp
global drawdots, cycles, t, p, h, co2, heating
del buffer
buffer = bytearray(128*32)
first_trans = (cycles == exp)
cycles-=1
if (cycles > 0):
s.enter(1, 1, humi_func, ())
else:
s.enter(1, 1, pres_func, ())
cycles = exp
temp = int(h)
val = (temp // 100)
bufoffset = 0
if (val > 0):
putchar(val,bufoffset,28)
val = (temp // 10) % 10
bufoffset = 31
putchar(val,bufoffset,28)
val = temp % 10
bufoffset = 63
putchar(val,bufoffset,28)
bufoffset=127-32
putchar(14,bufoffset,32)
if first_trans:
render_slow()
first_trans = False
else:
render()
def pres_func():
global SSD1305, I2CNUM, i2cdev
global buffer, framebuffer, first_trans,exp
global drawdots, cycles, t, p, h, co2, heating
del buffer
buffer = bytearray(128*32)
first_trans = (cycles == exp)
cycles-=1
if (cycles > 0):
s.enter(1, 1, pres_func, ())
else:
s.enter(1, 1, time_func, ())
cycles = exp
temp = int(p)
val = (temp // 1000)
bufoffset = 0
if (val > 0):
putchar(val,bufoffset,28)
val = (temp // 100) % 10
bufoffset = 28+2
putchar(val,bufoffset,28)
val = (temp // 10) % 10
bufoffset = 28+2+28+2
putchar(val,bufoffset,28)
val = temp % 10
bufoffset = 28+2+28+2+28+2
putchar(val,bufoffset,28)
bufoffset=119
putchar(12,bufoffset,9)
if first_trans:
render_slow()
first_trans = False
else:
render()
def temp_func():
global SSD1305, I2CNUM, i2cdev
global buffer, framebuffer, first_trans,exp
global drawdots, cycles, t, p, h, co2, heating
del buffer
buffer = bytearray(128*32)
first_trans = (cycles == exp)
cycles-=1
if (cycles > 0):
s.enter(1, 1, temp_func, ())
else:
s.enter(1, 1, co2_func, ())
cycles = exp
temp = int(round(t*10))
val = temp // 100
bufoffset = 0
if (val > 0):
putchar(val,bufoffset,28)
val = (temp % 100) // 10
bufoffset = 30
putchar(val,bufoffset,28)
val = (temp % 10)
bufoffset = 70
putchar(val,bufoffset,28)
putdot(61,27,4)
if (heating):
bufoffset = 98
putchar(11,bufoffset,28)
else:
bufoffset = 70+34
putchar(10,bufoffset,20)
if first_trans:
render_slow()
first_trans = False
else:
render()
def time_func():
global SSD1305, I2CNUM, i2cdev
global buffer, framebuffer, first_trans, exp
global drawdots, cycles
first_trans = (cycles == exp)
cycles-=1
if (cycles > 0):
s.enter(1, 1, time_func, ())
else:
s.enter(1, 1, temp_func, ())
cycles = exp
#FPS start_time=time.time()
del buffer
buffer = bytearray(128*32)
now = datetime.datetime.now()
val = now.hour // 10
bufoffset = 0
if (val > 0):
putchar(val,bufoffset,28)
val = now.hour % 10
bufoffset = 28+1
putchar(val,bufoffset,28)
val = now.minute // 10
bufoffset = 64+8-1
putchar(val,bufoffset,28)
val = now.minute % 10
bufoffset = 64+8+28
putchar(val,bufoffset,28)
drawdots = not drawdots
if drawdots:
putdot(61,12,4)
putdot(61,20,4)
if first_trans:
render_slow()
first_trans = False
else:
render()
#FPS print("FPS: ", 1.0 / (time.time() - start_time))
def udprec():
global UDP_IP, UDP_PORT
global cdate,t,p,h,co2,heating,id
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(256)
if data[0:8] == b'climdata':
clim = data.decode().split(",")
cdate = datetime.datetime.fromtimestamp(float(clim[1]))
id = int(clim[2])
t = float(clim[3])
p = float(clim[4])
h = float(clim[5])
co2 = int(clim[6])
heating = bool(int(clim[7]))
print (id,t,p,h,co2,clim[7],heating)
sock.close()
UDP_REC = threading.Thread(target=udprec)
UDP_REC.start()
init_ssd1305()
s = sched.scheduler(time.time, time.sleep)
s.enter(1, 1, time_func, ())
#s.enter(1, 1, vals_func, ())
s.run()
while True:
time.sleep(1)
i2cdev.write_byte_data(SSD1305, 0x80, 0xAE)