forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P036_FrameOLED.py
674 lines (637 loc) · 23.2 KB
/
_P036_FrameOLED.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#!/usr/bin/env python3
#############################################################################
##################### Framed OLED plugin for RPIEasy ########################
#############################################################################
#
# Available commands:
# OLEDFRAMEDCMD,<value> - value can be: on, off, low, med, high
# OLEDFRAMEDCMD,scroll - scroll to next page
#
# Loosely based on marvellous ESPEasy Plugin 036.
#
# Copyright (C) 2018-2021 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
# Made with the support of Budman1758
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import time
import misc
import gpios
import commands
import math
import Settings
import linux_network as Network
import linux_os as OS
from datetime import datetime
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from PIL import ImageFont, ImageDraw, Image
class Plugin(plugin.PluginProto):
PLUGIN_ID = 36
PLUGIN_NAME = "Display - Framed OLED"
PLUGIN_VALUENAME1 = "OLED"
P36_Nlines = 12
P36_CONTRAST_LOW = 64
P36_CONTRAST_MED = 0xCF
P36_CONTRAST_HIGH = 0xFF
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_I2C
self.vtype = rpieGlobals.SENSOR_TYPE_NONE
self.ports = 0
self.valuecount = 0
self.senddataoption = False
self.timeroption = True
self.timeroptional = True
self.formulaoption = False
self.device = None
self.width = None
self.height = None
self.lines = []
self.ufont = None
self.hfont = None
self.ypos = []
self.dispimage = None
self.textbuffer = []
self.lastlineindex = 0
self.pages = 0
self.actualpage = 0
self.conty1 = 0
self.conty2 = 0
self.headline = 0
self.lastwifistrength = -1
self.writeinprogress = 0
self.initval = -1
self.btnval = -1
self.btntime = 0
self.displaystate = -1
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
if self.enabled:
if self.taskdevicepin[0]>=0:
try:
gpios.HWPorts.remove_event_detect(int(self.taskdevicepin[0]))
except:
pass
try:
self.btntime = 0
self.btnval = -1
self.initval = int(gpios.HWPorts.input(int(self.taskdevicepin[0])))
gpios.HWPorts.add_event_detect(int(self.taskdevicepin[0]),gpios.BOTH,self.p036_handler)
except:
pass
try:
i2cl = self.i2c
except:
i2cl = -1
try:
i2cport = gpios.HWPorts.geti2clist()
if i2cl==-1:
i2cl = int(i2cport[0])
except:
i2cport = []
if len(i2cport)>0 and i2cl>-1:
if self.interval>2:
nextr = self.interval-2
else:
nextr = self.interval
self.initialized = False
serialdev = None
self.taskdevicepluginconfig[1] = int(float(self.taskdevicepluginconfig[1]))
if self.taskdevicepluginconfig[1] != 0: # i2c address
serialdev = i2c(port=i2cl, address=self.taskdevicepluginconfig[1])
else:
return self.initialized
self.device = None
try:
if "x" in str(self.taskdevicepluginconfig[3]):
resstr = str(self.taskdevicepluginconfig[3]).split('x')
self.width = int(resstr[0])
self.height = int(resstr[1])
else:
self.width = None
self.height = None
except:
self.width = None
self.height = None
if str(self.taskdevicepluginconfig[0]) != "0" and str(self.taskdevicepluginconfig[0]).strip() != "": # display type
try:
if str(self.taskdevicepluginconfig[0])=="ssd1306":
from luma.oled.device import ssd1306
if self.height is None:
self.device = ssd1306(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = ssd1306(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
elif str(self.taskdevicepluginconfig[0])=="sh1106":
from luma.oled.device import sh1106
if self.height is None:
self.device = sh1106(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = sh1106(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
elif str(self.taskdevicepluginconfig[0])=="ssd1309":
from luma.oled.device import ssd1309
if self.height is None:
self.device = ssd1309(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = ssd1309(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
elif str(self.taskdevicepluginconfig[0])=="ssd1331":
from luma.oled.device import ssd1331
if self.height is None:
self.device = ssd1331(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = ssd1331(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
elif str(self.taskdevicepluginconfig[0])=="ssd1351":
from luma.oled.device import ssd1351
if self.height is None:
self.device = ssd1351(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = ssd1351(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
elif str(self.taskdevicepluginconfig[0])=="ssd1322":
from luma.oled.device import ssd1322
if self.height is None:
self.device = ssd1322(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = ssd1322(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
elif str(self.taskdevicepluginconfig[0])=="ssd1325":
from luma.oled.device import ssd1325
if self.height is None:
self.device = ssd1325(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = ssd1325(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
elif str(self.taskdevicepluginconfig[0])=="ssd1327":
from luma.oled.device import ssd1327
if self.height is None:
self.device = ssd1327(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])))
else:
self.device = ssd1327(serialdev, rotate=int(float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height)
self.initialized = True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"OLED can not be initialized! "+str(e))
self.enabled = False
self.device = None
return False
if self.device is not None:
try:
lc = int(self.taskdevicepluginconfig[4])
self.width = self.device.width
self.height = self.device.height
except:
lc = 1
if lc < 1:
lc = 1
elif lc>4:
lc = 4
try:
defh = 10
if self.height!=64: # correct y coords
defh = int(defh * (self.height/64))
self.hfont=ImageFont.truetype('img/UbuntuMono-R.ttf', defh)
lineheight=11
if lc==1:
lineheight = 24
self.ypos = [20,0,0,0]
elif lc==2:
lineheight = 16
self.ypos = [15,34,0,0]
elif lc==3:
lineheight = 12
self.ypos = [13,25,37,0]
elif lc==4:
lineheight = 10
self.ypos = [12,22,32,42]
if self.height!=64: # correct y coords
for p in range(len(self.ypos)):
self.ypos[p] = int(self.ypos[p] * (self.height/64))
lineheight = int(lineheight * (self.height/64))
self.ufont=ImageFont.truetype('img/UbuntuMono-R.ttf', lineheight) # use size
except Exception as e:
print(e)
try:
self.device.show()
self.displaystate = 1
except:
self.displaystate = 0
if self.interval>2:
nextr = self.interval-2
else:
nextr = 0
self._lastdataservetime = rpieTime.millis()-(nextr*1000)
try:
self.dispimage = Image.new('1', (self.device.width,self.device.height), "black")
self.conty1 = 12
if self.height!=64: # correct y coords
self.conty1 = int(self.conty1 * (self.height/64))
self.conty2 = self.device.height-self.conty1
self.textbuffer = []
self.actualpage = 0
self.lastlineindex = self.P36_Nlines
for l in reversed(range(self.P36_Nlines)):
if (str(self.lines[l]).strip()!="") and (str(self.lines[l]).strip()!="0"):
self.lastlineindex = l
break
try:
self.pages = math.ceil((self.lastlineindex+1) / int(self.taskdevicepluginconfig[4]))
except:
self.pages = 0
except Exception as e:
self.initialized = False
try:
cont = int(self.taskdevicepluginconfig[6])
except:
cont = 0
if cont>0:
self.device.contrast(cont)
draw = ImageDraw.Draw(self.dispimage)
maxcols = int(self.taskdevicepluginconfig[7]) # auto decrease font size if needed
if maxcols < 1:
maxcols = 1
tstr = "X"*maxcols
try:
sw = draw.textsize(tstr,self.ufont)[0]
except:
sw = self.device.width
while (sw>self.device.width):
lineheight-=1
self.ufont=ImageFont.truetype('img/UbuntuMono-R.ttf', lineheight)
sw = draw.textsize(tstr,self.ufont)[0]
self.writeinprogress = 0
else:
self.initialized = False
else:
self.plugin_exit()
def webform_load(self): # create html page for settings
choice1 = str(self.taskdevicepluginconfig[0]) # store display type
import luma.oled.device
options = luma.oled.device.__all__
webserver.addHtml("<tr><td>Display type:<td>")
webserver.addSelector_Head("p036_type",True)
for d in range(len(options)):
webserver.addSelector_Item(options[d],options[d],(choice1==options[d]),False)
webserver.addSelector_Foot()
choice2 = int(float(self.taskdevicepluginconfig[1])) # store i2c address
options = ["0x3c","0x3d"]
optionvalues = [0x3c,0x3d]
webserver.addFormSelector("Address","p036_adr",len(options),options,optionvalues,None,choice2)
webserver.addFormNote("Enable <a href='pinout'>I2C bus</a> first, than <a href='i2cscanner'>search for the used address</a>!")
choice3 = int(float(self.taskdevicepluginconfig[2])) # store rotation state
options = ["Normal","Rotate by 180"]
optionvalues = [0,2]
webserver.addFormSelector("Mode","p036_rotate",len(optionvalues),options,optionvalues,None,choice3)
options = ["Default","128x64","128x128","128x32","96x96","96x64","64x48","64x32"]
choice4 = self.taskdevicepluginconfig[3] # store resolution
webserver.addHtml("<tr><td>Resolution:<td>")
webserver.addSelector_Head("p036_res",False)
for d in range(len(options)):
webserver.addSelector_Item(options[d],options[d],(choice4==options[d]),False)
webserver.addSelector_Foot()
choice5 = int(float(self.taskdevicepluginconfig[4])) # store line count
webserver.addHtml("<tr><td>Lines per Frame:<td>")
webserver.addSelector_Head("p036_linecount",False)
for l in range(1,5):
webserver.addSelector_Item(str(l),l,(l==choice5),False)
webserver.addSelector_Foot()
choice6 = int(float(self.taskdevicepluginconfig[5])) # transition speed
options = ["Very Slow","Slow","Fast","Very Fast","Instant"]
optionvalues = [1,2,4,8,32]
webserver.addFormSelector("Scroll","p036_scroll",len(optionvalues),options,optionvalues,None,choice6)
for l in range(self.P36_Nlines):
try:
linestr = self.lines[l]
except:
linestr = ""
webserver.addFormTextBox("Line"+str(l+1),"p036_template"+str(l),linestr,128)
choice7 = int(float(self.taskdevicepluginconfig[6])) # contrast
options = ["Low","Medium","High"]
optionvalues = [self.P36_CONTRAST_LOW, self.P36_CONTRAST_MED, self.P36_CONTRAST_HIGH]
webserver.addFormSelector("Contrast","p036_contrast",len(optionvalues),options,optionvalues,None,choice7)
webserver.addFormNumericBox("Try to display # characters per row","p036_charperl",self.taskdevicepluginconfig[7],1,32)
webserver.addFormNote("Leave it '1' if you do not care")
webserver.addFormPinSelect("Display button", "p036_button", self.taskdevicepin[0])
return True
def plugin_exit(self):
self.initialized = False
try:
self.displaystate = 0
if self.device is not None:
self.device.clear()
self.device.hide()
except:
pass
try:
gpios.HWPorts.remove_event_detect(self.taskdevicepin[0])
except:
pass
def webform_save(self,params): # process settings post reply
par = webserver.arg("p036_type",params)
if par == "":
par = 0
self.taskdevicepluginconfig[0] = str(par)
par = webserver.arg("p036_adr",params)
if par == "":
par = 0
self.taskdevicepluginconfig[1] = int(par)
par = webserver.arg("p036_rotate",params)
if par == "":
par = 0
self.taskdevicepluginconfig[2] = int(par)
par = webserver.arg("p036_res",params)
self.taskdevicepluginconfig[3] = str(par)
par = webserver.arg("p036_linecount",params)
if par == "":
par = 1
self.taskdevicepluginconfig[4] = int(par)
for l in range(self.P36_Nlines):
linestr = webserver.arg("p036_template"+str(l),params).strip()
# if linestr!="" and linestr!="0":
try:
self.lines[l]=linestr
except:
self.lines.append(linestr)
par = webserver.arg("p036_scroll",params)
if par == "":
par = 1
self.taskdevicepluginconfig[5] = int(par)
par = webserver.arg("p036_contrast",params)
if par == "":
par = self.P36_CONTRAST_MED
self.taskdevicepluginconfig[6] = int(par)
par = webserver.arg("p036_charperl",params)
if par == "":
par = 1
self.taskdevicepluginconfig[7] = int(par)
par = webserver.arg("p036_button",params)
if par == "":
par = -1
self.taskdevicepin[0] = int(par)
self.plugin_init()
return True
def showfirstpage(self):
draw = ImageDraw.Draw(self.dispimage)
draw.rectangle( ((0,self.conty1),(self.width,self.conty2)) ,fill="black")
for l in range(int(self.taskdevicepluginconfig[4])):
tpos = int((self.device.width-(draw.textsize(self.textbuffer[0][l],self.ufont)[0]))/2)-2
if tpos<0:
tpos = 0
draw.text( (tpos,self.ypos[l]), self.textbuffer[0][l], fill="white", font=self.ufont)
self.device.display(self.dispimage)
self.actualpage = 1
def scrollnextpage(self):
draw = ImageDraw.Draw(self.dispimage)
ax = 0
step=int(self.taskdevicepluginconfig[5])
for offset in range(0,self.width,step):
draw.rectangle( ((0,self.conty1),(self.width,self.conty2)) ,fill="black")
for l in range(int(self.taskdevicepluginconfig[4])):
tpos = int((self.device.width-(draw.textsize(self.textbuffer[0][l],self.ufont)[0]))/2)-2
if tpos<0:
tpos = 0
draw.text( (tpos+ax, self.ypos[l]), self.textbuffer[0][l], fill="white", font=self.ufont)
for l in range(int(self.taskdevicepluginconfig[4])):
tpos = int((self.device.width-(draw.textsize(self.textbuffer[1][l],self.ufont)[0]))/2)-2
if tpos<0:
tpos = 0
draw.text( (tpos+ax+self.device.width, self.ypos[l]), self.textbuffer[1][l], fill="white", font=self.ufont)
self.device.display(self.dispimage)
ax -= step
if self.initialized==False:
break
draw.rectangle( ((0,self.conty1),(self.width,self.conty2)) ,fill="black")
for l in range(int(self.taskdevicepluginconfig[4])): # last position
tpos = int((self.device.width-(draw.textsize(self.textbuffer[1][l],self.ufont)[0]))/2)-2
if tpos<0:
tpos = 0
draw.text( (tpos, self.ypos[l]), self.textbuffer[1][l], fill="white", font=self.ufont)
self.display_footer()
def display_time(self):
draw = ImageDraw.Draw(self.dispimage)
cx = 28
if self.width!=128: # correct x coords
cx = int(cx * (self.width/128))
cy = 10
if self.height!=64: # correct y coords
cy = int(cy * (self.height/64))
draw.rectangle( ((0,0),(cx,cy)) ,fill="black")
draw.text( (0,0), datetime.now().strftime('%H:%M'), fill="white", font=self.hfont)
self.device.display(self.dispimage)
def display_wifibars(self):
connected = True
try:
rssi = int(OS.get_rssi())
except:
rssi = -100
connected = False
if rssi<-99:
connected = False
nbars_filled = (rssi+100)/8
x = self.device.width - 23 # 105
y = 0
size_x = 15
if self.width!=128: # correct x coords
size_x = int(size_x * (self.width/128))
x = int(x * (self.width/128))
size_y = 10
if self.height!=64: # correct y coords
size_y = int(size_y * (self.height/64))
nbars = 5
width = int(size_x / nbars)
size_x = width * nbars -1
if self.lastwifistrength != nbars_filled:
self.lastwifistrength = nbars_filled
draw = ImageDraw.Draw(self.dispimage)
draw.rectangle( ((x,y),(x+size_x,y+size_y)) ,fill="black")
if connected:
for ibar in range(0,nbars):
height = size_y * (ibar+1) / nbars
xpos = x + ibar * width
ypos = y + size_y - height
if (ibar<=nbars_filled):
draw.rectangle( ((xpos,ypos),(xpos+width-1,ypos+height)),fill="white")
else:
draw.rectangle( ((xpos,ypos),(xpos+width-1,ypos+1)),fill="white")
draw.rectangle( ((xpos,y+size_y-1),(xpos+width-1,y+size_y)),fill="white")
return True
def display_footer(self):
ft = ""
for p in range(self.pages):
if p==self.actualpage:
ft += str(u"\u2022") # "•"
else:
ft += str(u"\u00B7") # "·"
draw = ImageDraw.Draw(self.dispimage)
tpos = int((self.device.width-(draw.textsize(ft,self.hfont)[0]))/2)-2
if tpos<0:
tpos = 0
cx1 = 20
cx2 = 108
if self.width!=128: # correct x coords
cx1 = int(cx1 * (self.width/128))
cx2 = int(cx2 * (self.width/128))
draw.rectangle( ((cx1,self.conty2),(cx2,self.device.height)) ,fill="black")
draw.text( (tpos, self.conty2), ft, fill="white", font=self.hfont)
self.device.display(self.dispimage)
def display_name(self):
draw = ImageDraw.Draw(self.dispimage)
tstr = ""
if self.headline == 0:
tstr = str(Settings.Settings["Name"])
self.headline = 1
else:
try:
wdev = Settings.NetMan.getfirstwirelessdev()
except:
wdev = False
if wdev:
tstr = str(Network.get_ssid(wdev))
self.headline = 0
cx1 = 29
cx2 = 106
if self.width!=128: # correct x coords
cx1 = int(cx1 * (self.width/128))
cx2 = int(cx2 * (self.width/128))
cy = 12
if self.height!=64: # correct y coords
cy = int(cy * (self.height/64))
draw.rectangle( ((cx1,0),(cx2,cy)) ,fill="black")
tpos = int((self.device.width-(draw.textsize(tstr,self.hfont)[0]))/2)-2
if tpos<0:
tpos = 0
draw.text( (tpos, 0), tstr, fill="white", font=self.hfont)
def plugin_read(self): # deal with data processing at specified time interval
if self.initialized and self.enabled and self.device and self.writeinprogress==0:
try:
if self.dispimage:
self.writeinprogress = 1
self.display_time()
self.display_name()
self.display_wifibars()
if self.pages==1:
spos = 0
else:
spos = (self.actualpage*int(self.taskdevicepluginconfig[4]))
if len(self.textbuffer)>1: # previous value in pos 0, second in 1 normal loop
for l in range(int(self.taskdevicepluginconfig[4])):
self.textbuffer[1][l] = self.oledparse(str(self.lines[spos+l]))
elif len(self.textbuffer)==1: # previous value in pos 0, no second
tbuf = []
for l in range(int(self.taskdevicepluginconfig[4])):
tbuf.append(self.oledparse(str(self.lines[spos+l])))
self.textbuffer.append(tbuf)
else: # no values in buffer
self.display_footer()
tbuf = []
for l in range(int(self.taskdevicepluginconfig[4])):
tbuf.append(self.oledparse(str(self.lines[spos+l])))
self.textbuffer.append(tbuf)
self.showfirstpage()
self._lastdataservetime = rpieTime.millis()
self.writeinprogress = 0
return True
self.scrollnextpage()
if self.pages>1:
if self.actualpage<(self.pages-1):
self.actualpage += 1
else:
self.actualpage = 0
if len(self.textbuffer)>1: # skew things after scroll
for l in range(int(self.taskdevicepluginconfig[4])):
self.textbuffer[0][l] = self.textbuffer[1][l]
self.writeinprogress = 0
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"OLED write error! "+str(e))
self.writeinprogress = 0
self._lastdataservetime = rpieTime.millis()
# print(self.textbuffer,self.actualpage,self.pages)
return True
def p036_handler(self,channel=0):
if self.taskdevicepin[0]>=0:
try:
btnval = int(gpios.HWPorts.input(int(self.taskdevicepin[0])))
if btnval != self.btnval:
self.btnval = btnval
if btnval != self.initval: #btn pressed
self.btntime = time.time()
else: #returned to default state, measure time
presstime = (time.time()-self.btntime)
if presstime >= 1.5: #longpress
if self.displaystate==1:
self.displaystate = 0
self.device.hide()
else:
self.displaystate = 1
self.device.show()
else: #shortpress
self._lastdataservetime = rpieTime.millis()
self.plugin_read()
except:
pass
def plugin_write(self,cmd):
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0] == "oledframedcmd":
try:
cmd = cmdarr[1].strip()
except:
cmd = ""
try:
if self.device is not None:
if cmd == "on":
self.device.show()
self.displaystate = 1
res = True
elif cmd == "off":
self.device.hide()
self.displaystate = 0
res = True
elif cmd == "low":
self.device.contrast(self.P36_CONTRAST_LOW)
res = True
elif cmd == "med":
self.device.contrast(self.P36_CONTRAST_MED)
res = True
elif cmd == "high":
self.device.contrast(self.P36_CONTRAST_HIGH)
res = True
elif cmd == "scroll":
self._lastdataservetime = rpieTime.millis()
self.plugin_read()
res = True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"OLED command error! "+str(e))
res = False
return res
def oledparse(self,ostr):
cl, st = commands.parseruleline(ostr)
if st=="CMD":
resstr=str(cl)
else:
resstr=str(ostr)
if "{" in resstr or "&" in resstr:
resstr = resstr.replace("{D}","˚").replace("°","˚")
resstr = resstr.replace("{<<}","«").replace("«","«")
resstr = resstr.replace("{>>} ","»").replace("»","»")
resstr = resstr.replace("{u} ","µ").replace("µ ","µ")
resstr = resstr.replace("{E}","€").replace("€","€")
resstr = resstr.replace("{Y}","¥").replace("¥","¥")
resstr = resstr.replace("{P}","£").replace("£","£")
resstr = resstr.replace("{c}","¢").replace("¢","¢")
resstr = resstr.replace("{^1}","¹").replace("¹","¹")
resstr = resstr.replace("{^2}","²").replace("²","²")
resstr = resstr.replace("{^3}","³").replace("³","³")
resstr = resstr.replace("{1_4}","¼").replace("¼","¼")
resstr = resstr.replace("{1_2}","½").replace("&frac24;","½")
resstr = resstr.replace("{3_4}","¾").replace("¾","¾")
resstr = resstr.replace("{+-}","±").replace("±","±")
resstr = resstr.replace("{x}","×").replace("×","×")
resstr = resstr.replace("{..}","÷").replace("÷","÷")
return resstr