Repository navigation
Expand file tree
/
Copy pathdisplay_generic_lcd_20x4_i2c.py
More file actions
502 lines (433 loc) · 19 KB
/
Copy pathdisplay_generic_lcd_20x4_i2c.py
File metadata and controls
502 lines (433 loc) · 19 KB
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
# coding=utf-8
#
# lcd_generic_20x4_i2c.py - Function to output to LCD
#
# Copyright (C) 2015-2020 Kyle T. Gabriel <mycodo@kylegabriel.com>
#
# This file is part of Mycodo
#
# Mycodo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycodo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycodo. If not, see <http://www.gnu.org/licenses/>.
#
# Contact at kylegabriel.com
#
import datetime
import json
import math
import time
import traceback
from flask_babel import lazy_gettext
from mycodo.config import MYCODO_VERSION
from mycodo.config_translations import TRANSLATIONS
from mycodo.databases.models import CustomController, FunctionChannel
from mycodo.functions.base_function import AbstractFunction
from mycodo.mycodo_flask.utils.utils_general import (
custom_channel_options_return_json, delete_entry_with_id)
from mycodo.utils.constraints_pass import (
constraints_pass_positive_or_zero_value, constraints_pass_positive_value)
from mycodo.utils.database import db_retrieve_table_daemon
from mycodo.utils.functions import parse_function_information
from mycodo.utils.lcd import format_measurement_line
from mycodo.utils.system_pi import cmd_output
# Set to how many lines the LCD has
lcd_lines = 4
lcd_x_characters = 20
def execute_at_creation(error, new_func, dict_functions=None):
try:
dict_controllers = parse_function_information()
for channel in range(lcd_lines):
new_channel = FunctionChannel()
new_channel.name = "Set 0 Line {}".format(channel)
new_channel.function_id = new_func.unique_id
new_channel.channel = channel
error, custom_options = custom_channel_options_return_json(
error, dict_controllers, None,
new_func.unique_id, channel,
device=new_func.device, use_defaults=True)
custom_options_dict = json.loads(custom_options)
custom_options_dict["name"] = new_channel.name
new_channel.custom_options = json.dumps(custom_options_dict)
new_channel.save()
except Exception:
error.append("execute_at_modification() Error: {}".format(traceback.print_exc()))
return error, new_func
def execute_at_modification(
messages,
mod_function,
request_form,
custom_options_dict_presave,
custom_options_channels_dict_presave,
custom_options_dict_postsave,
custom_options_channels_dict_postsave):
"""
This function allows you to view and modify the output and channel settings when the user clicks
save on the user interface. Both the output and channel settings are passed to this function, as
dictionaries. Additionally, both the pre-saved and post-saved options are available, as it's
sometimes useful to know what settings changed and from what values. You can modify the post-saved
options and these will be stored in the database.
:param mod_function: The post-saved output database entry, minus the custom_options settings
:param request_form: The requests.form object the user submitted
:param custom_options_dict_presave: dict of pre-saved custom output options
:param custom_options_channels_dict_presave: dict of pre-saved custom output channel options
:param custom_options_dict_postsave: dict of post-saved custom output options
:param custom_options_channels_dict_postsave: dict of post-saved custom output channel options
:return:
"""
page_refresh = False
try:
dict_controllers = parse_function_information()
channels = FunctionChannel.query.filter(
FunctionChannel.function_id == mod_function.unique_id)
# Ensure name doesn't get overwritten
selector_set = 0
selector_line = 0
for channel in range(channels.count()):
custom_options_channels_dict_postsave[channel]["name"] = "Set {} Line {}".format(
selector_set, selector_line)
selector_line += 1
if selector_line == lcd_lines:
selector_set += 1
selector_line = 0
end_channel = custom_options_dict_postsave['number_line_sets'] * lcd_lines
# Increase number of channels
if (custom_options_dict_postsave['number_line_sets'] >
custom_options_dict_presave['number_line_sets']):
page_refresh = True
start_channel = channels.count()
for index in range(start_channel, end_channel):
new_channel = FunctionChannel()
new_channel.name = "Set {} Line {}".format(
math.trunc(index / lcd_lines),
index - (math.trunc(index / lcd_lines) * lcd_lines))
new_channel.function_id = mod_function.unique_id
new_channel.channel = index
messages["error"], custom_options = custom_channel_options_return_json(
messages["error"],
dict_controllers,
request_form,
mod_function.unique_id,
index,
device=mod_function.device,
use_defaults=True)
custom_options_dict = json.loads(custom_options)
custom_options_dict["name"] = new_channel.name
new_channel.custom_options = json.dumps(custom_options_dict)
new_channel.save()
# Decrease number of channels
elif (custom_options_dict_postsave['number_line_sets'] <
custom_options_dict_presave['number_line_sets']):
page_refresh = True
for index, each_channel in enumerate(channels.all()):
if index >= end_channel:
delete_entry_with_id(FunctionChannel, each_channel.unique_id)
except Exception:
messages["error"].append("execute_at_modification() Error: {}".format(traceback.print_exc()))
return (messages,
mod_function,
custom_options_dict_postsave,
custom_options_channels_dict_postsave,
page_refresh)
FUNCTION_INFORMATION = {
'function_name_unique': 'display_generic_lcd_20x4_i2c',
'function_name': 'Display: Generic LCD 20x4 (I2C)',
'function_library': 'smbus2',
'execute_at_creation': execute_at_creation,
'execute_at_modification': execute_at_modification,
'message': 'This Function outputs to a generic 20x4 LCD display via I2C. Since this display can show 4 lines at a time, channels are added in sets of 4 when Number of Line Sets is modified. Every Period, the LCD will refresh and display the next set of lines. Therefore, the first 4 lines that are displayed are channels 0, 1, 2, and 3, then 4, 5, 6, and 7, and so on. After all channels have been displayed, it will cycle back to the beginning.',
'dependencies_module': [
('pip-pypi', 'smbus2', 'smbus2==0.4.1')
],
'options_disabled': [
'measurements_select',
'measurements_configure'
],
'function_actions': [
'backlight_on',
'backlight_off'
],
'custom_commands': [
{
'id': 'backlight_on',
'type': 'button',
'wait_for_return': False,
'name': 'Backlight On',
'phrase': "Turn backlight on"
},
{
'id': 'backlight_off',
'type': 'button',
'wait_for_return': False,
'name': 'Backlight Off',
'phrase': "Turn backlight off"
}
],
'custom_options': [
{
'id': 'period',
'type': 'float',
'default_value': 10,
'required': True,
'constraints_pass': constraints_pass_positive_value,
'name': "{} ({})".format(lazy_gettext('Period'), lazy_gettext('Seconds')),
'phrase': lazy_gettext('The duration between measurements or actions')
},
{
'id': 'i2c_address',
'type': 'text',
'default_value': '0x20',
'required': True,
'name': TRANSLATIONS['i2c_location']['title'],
'phrase': ''
},
{
'id': 'i2c_bus',
'type': 'integer',
'default_value': 1,
'required': True,
'name': TRANSLATIONS['i2c_bus']['title'],
'phrase': ''
},
{
'id': 'number_line_sets',
'type': 'integer',
'default_value': 1,
'required': True,
'constraints_pass': constraints_pass_positive_value,
'name': 'Number of Line Sets',
'phrase': 'How many sets of lines to cycle on the LCD'
}
],
'custom_channel_options': [
{
'id': 'line_display_type',
'type': 'select',
'default_value': '',
'required': True,
'options_select': [
('measurement_value', 'Last Measurement Value'),
('measurement_ts', 'Last Measurement Timestamp'),
('blank_line', 'Blank Line'),
('ip_address', 'IP Address'),
('current_time', 'Current Time'),
('text', 'Text')
],
'name': 'Line Display Type',
'phrase': 'What to display on the line'
},
{
'id': 'select_measurement',
'type': 'select_measurement',
'default_value': '',
'options_select': [
'Input',
'Function',
'Output',
'PID'
],
'name': 'Measurement',
'phrase': 'Measurement to display on the line'
},
{
'id': 'measure_max_age',
'type': 'integer',
'default_value': 360,
'required': True,
'constraints_pass': constraints_pass_positive_value,
'name': "{} ({})".format(lazy_gettext('Max Age'), lazy_gettext('Seconds')),
'phrase': lazy_gettext('The maximum age of the measurement to use')
},
{
'id': 'measurement_label',
'type': 'text',
'default_value': '',
'required': False,
'name': 'Measurement Label',
'phrase': 'Set to overwrite the default measurement label'
},
{
'id': 'measure_decimal',
'type': 'integer',
'default_value': 1,
'required': True,
'constraints_pass': constraints_pass_positive_or_zero_value,
'name': 'Measurement Decimal',
'phrase': 'The number of digits after the decimal'
},
{
'id': 'text',
'type': 'text',
'default_value': 'Text',
'required': True,
'name': TRANSLATIONS['text']['title'],
'phrase': "Text to display"
},
{
'id': 'display_unit',
'type': 'bool',
'default_value': True,
'required': True,
'name': 'Display Unit',
'phrase': "Display the measurement unit (if available)"
}
]
}
class CustomModule(AbstractFunction):
"""
Class to operate custom controller
"""
def __init__(self, function, testing=False):
super().__init__(function, testing=testing, name=__name__)
self.options_channels = {}
self.lcd = None
self.timer_loop = time.time()
self.line_sets = []
self.current_line_set = 0
self.line_y_dimensions = [0, 8, 16, 24]
self.pad = -2
self.lcd_is_on = None
self.lines_being_written = False
# Initialize custom options
self.period = None
self.i2c_address = None
self.i2c_bus = None
self.number_line_sets = None
# Set custom options
custom_function = db_retrieve_table_daemon(
CustomController, unique_id=self.unique_id)
self.setup_custom_options(
FUNCTION_INFORMATION['custom_options'], custom_function)
if not testing:
self.try_initialize()
def initialize(self):
from mycodo.devices.lcd_generic import LCD_Generic
try:
function_channels = db_retrieve_table_daemon(
FunctionChannel).filter(FunctionChannel.function_id == self.unique_id).all()
self.options_channels = self.setup_custom_channel_options_json(
FUNCTION_INFORMATION['custom_channel_options'], function_channels)
for each_set in range(self.number_line_sets):
self.line_sets.append([])
for each_line in range(lcd_lines):
self.line_sets[each_set].append(each_line)
self.logger.debug("Line sets: {}".format(self.line_sets))
lcd_settings_dict = {
"unique_id": self.unique_id,
"i2c_address": self.i2c_address,
"i2c_bus": self.i2c_bus,
"x_characters": lcd_x_characters,
"y_lines": lcd_lines
}
self.lcd = LCD_Generic(lcd_settings_dict=lcd_settings_dict)
self.lcd.lcd_init()
self.lcd_is_on = True
self.logger.debug("LCD Function started")
except:
self.logger.exception("Starting LCD Function")
def loop(self):
if self.timer_loop > time.time():
return
while self.timer_loop < time.time():
self.timer_loop += self.period
if not self.lcd:
self.logger.error("LCD not set up")
return
if not self.lcd_is_on:
return # Don't draw anything on an LCD that has the backlight off
# Generate lines to display
self.lines_being_written = True
lines_display = {}
for line in range(lcd_lines):
lines_display[line] = ""
for current_line in self.line_sets[self.current_line_set]:
current_channel = (self.current_line_set * lcd_lines) + current_line
self.logger.debug("Channel: {}, Set: {} Line: {}, ".format(
current_channel, self.current_line_set, current_line))
try:
# Get measurement value and timestamp
if self.options_channels['line_display_type'][current_channel] in [
'measurement_value', 'measurement_ts']:
lines_display[current_line] = "NONE"
measure_ts = None
measure_value = None
last_measurement = self.get_last_measurement(
self.options_channels['select_measurement'][current_channel]['device_id'],
self.options_channels['select_measurement'][current_channel]['measurement_id'],
max_age=self.options_channels['measure_max_age'][current_channel])
if last_measurement:
measure_ts = last_measurement[0]
measure_value = last_measurement[1]
if self.options_channels['line_display_type'][current_channel] == 'measurement_value':
if measure_value:
if self.options_channels['measure_decimal'][current_channel] == 0:
val_rounded = int(measure_value)
else:
val_rounded = round(
measure_value,
self.options_channels['measure_decimal'][current_channel])
lines_display[current_line] = format_measurement_line(
self.options_channels['select_measurement'][current_channel]['device_id'],
self.options_channels['select_measurement'][current_channel]['measurement_id'],
val_rounded,
lcd_x_characters,
display_unit=self.options_channels['display_unit'][current_channel],
label=self.options_channels['measurement_label'][current_channel])
elif self.options_channels['line_display_type'][current_channel] == 'measurement_ts':
if measure_ts:
# Convert UTC timestamp to local timezone
lines_display[current_line] = str(datetime.datetime.fromtimestamp(measure_ts))
elif self.options_channels['line_display_type'][current_channel] == 'current_time':
lines_display[current_line] = time.strftime('%Y-%m-%d %H:%M:%S')
elif self.options_channels['line_display_type'][current_channel] == 'text':
lines_display[current_line] = self.options_channels['text'][current_channel]
elif self.options_channels['line_display_type'][current_channel] == 'blank_line':
lines_display[current_line] = ""
elif self.options_channels['line_display_type'][current_channel] == 'ip_address':
str_ip_cmd = "ip addr | " \
"grep 'state UP' -A2 | " \
"tail -n1 | " \
"awk '{print $2}' | " \
"cut -f1 -d'/'"
ip_out, _, _ = cmd_output(str_ip_cmd)
lines_display[current_line] = ip_out.rstrip().decode("utf-8")
except Exception as err:
self.logger.error(
"Error generating channel {} line: {}".format(
current_channel, err))
lines_display[current_line] = "ERROR"
if self.current_line_set == len(self.line_sets) - 1:
self.current_line_set = 0
else:
self.current_line_set += 1
self.logger.debug("Displaying: {}".format(lines_display))
# Display lines
self.lcd.lcd_write_lines(
lines_display[0], lines_display[1], lines_display[2], lines_display[3])
self.lines_being_written = False
def stop_function(self):
self.lcd.lcd_init()
self.lcd_is_on = True
self.lcd.lcd_write_lines(
"Mycodo {}".format(MYCODO_VERSION), "LCD Deactivated", "", "")
#
# Actions
#
def backlight_on(self, args_dict=None):
"""Turn the backlight on."""
self.lcd_is_on = True
self.lcd.lcd_backlight(1)
def backlight_off(self, args_dict=None):
"""Turn the backlight off."""
self.lcd_is_on = False
while self.lines_being_written:
time.sleep(0.1) # Wait for lines to be written before turning backlight off
self.lcd.lcd_backlight(0)