-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
_P057_HT16K33_LED.py
218 lines (203 loc) · 6.08 KB
/
_P057_HT16K33_LED.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
#!/usr/bin/env python3
#############################################################################
##################### HT16K33 LED plugin for RPIEasy ########################
#############################################################################
#
# ESPEasy Plugin to control a 16x8 LED matrix with chip HT16K33
#
# List of commands:
# M,<param>,<param>,<param>, ... with decimal values
# MNUM,<param>,<param>,<param>, ... with decimal values for 7-segment displays
# MBR,<0-15> set display brightness, between 0 and 15
#
# List of M* params:
# <value>
# Writes a decimal values to actual segment starting with 0
# <seg>=<value>
# Writes a decimal values to given segment (0...7)
# "CLEAR"
# Set all LEDs to 0.
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import rpieGlobals
import rpieTime
import time
import misc
import gpios
import webserver
import lib.HT16K33.Adafruit_8x8 as HT16K33
import lib.HT16K33.Adafruit_7Segment as HT167S
class Plugin(plugin.PluginProto):
PLUGIN_ID = 57
PLUGIN_NAME = "Display - HT16K33 LED (EXPERIMENTAL)"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_I2C
self.vtype = rpieGlobals.SENSOR_TYPE_NONE
self.valuecount = 0
self.senddataoption = False
self.timeroption = False
self.inverselogicoption = False
self.recdataoption = False
self.ht16 = None
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0]=0
self.initialized = False
self.timer100ms = False
if self.enabled:
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:
try:
i2ca = int(self.taskdevicepluginconfig[0])
except:
i2ca = 0
if i2ca>0:
try:
if int(self.taskdevicepluginconfig[1])==1:
self.ht16 = HT167S.SevenSegment(address=int(i2ca),i2cbusnum=i2cl)
else:
self.ht16 = HT16K33.EightByEight(address=int(i2ca),i2cbusnum=i2l)
self.timer100ms = True
self.initialized = True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 device requesting failed: "+str(e))
self.ht16 = None
def webform_load(self):
choice1 = int(float(self.taskdevicepluginconfig[0])) # store i2c address
optionvalues = []
for i in range(0x70, 0x78):
optionvalues.append(i)
options = []
for i in range(len(optionvalues)):
options.append(str(hex(optionvalues[i])))
webserver.addFormSelector("Address","p057_adr",len(options),options,optionvalues,None,choice1)
webserver.addFormNote("Enable <a href='pinout'>I2C bus</a> first, than <a href='i2cscanner'>search for the used address</a>!")
choice2 = int(float(self.taskdevicepluginconfig[1]))
options = ["Direct 8-16 rows","7 segment"]
optionvalues = [0,1]
webserver.addFormSelector("Display type","p057_type",len(options),options,optionvalues,None,choice2)
return True
def webform_save(self,params):
cha = False
par = webserver.arg("p057_adr",params)
if par == "":
par = 0x70
if self.taskdevicepluginconfig[0] != int(par):
cha = True
self.taskdevicepluginconfig[0] = int(par)
par = webserver.arg("p057_type",params)
if par == "":
par = 0x70
if self.taskdevicepluginconfig[1] != int(par):
cha = True
self.taskdevicepluginconfig[1] = int(par)
if cha:
self.plugin_init()
return True
def plugin_write(self,cmd):
res = False
if self.ht16 is None:
return res
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0] == "mbr":
res = True
try:
val = int(cmdarr[1].strip())
except:
val = -1
if val>-1 and val<16:
try:
self.ht16.disp.setBrightness(val)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 error: "+str(e))
elif cmdarr[0] == "m":
for c in range(len(cmdarr)):
if "=" in cmdarr[c]: # seg=value
res = True
lv = cmdarr[c].split("=")
try:
c0 = int(lv[0].strip())
r0 = int(lv[1].strip())
except:
c0 = -1
r0 = -1
if c0>=0 and c0<16 and r0>=0:
try:
if int(self.taskdevicepluginconfig[1])==1:
self.ht16.writeDigitRaw(c0,r0)
else:
self.ht16.writeRowRaw(c0,r0)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 error: "+str(e))
elif cmdarr[c].strip().lower()=="clear":
res = True
try:
if int(self.taskdevicepluginconfig[1])==1:
self.ht16.disp.clear()
else:
self.ht16.clear()
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 error: "+str(e))
else:
try:
val = int(cmdarr[c].strip())
res = True
except:
val = -1
if val>-1 and c<16:
try:
if int(self.taskdevicepluginconfig[1])==1:
self.ht16.writeDigitRaw(c-1,val)
else:
self.ht16.writeRowRaw(c-1,val)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 error: "+str(e))
elif cmdarr[0] == "mnum":
if int(self.taskdevicepluginconfig[1])!=1:
return False
for c in range(len(cmdarr)):
if "=" in cmdarr[c]: # seg=value
res = True
lv = cmdarr[c].split("=")
try:
c0 = int(lv[0].strip())
r0 = int(lv[1].strip())
except:
c0 = -1
r0 = -1
if c0>=0 and c0<16 and r0>=0:
try:
self.ht16.writeDigit(c0,r0)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 error: "+str(e))
elif cmdarr[c].strip().lower()=="clear":
res = True
try:
self.ht16.disp.clear()
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 error: "+str(e))
else:
try:
val = int(cmdarr[c].strip())
res = True
except:
val = -1
if val>-1 and c<16:
try:
self.ht16.writeDigit(c-1,val)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"HT16K33 error: "+str(e))
return res