# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 """ This lambda control LCD panel on control box. Values are coming from Device shadow. """ import traceback from awsiot.greengrasscoreipc.clientv2 import ( GreengrassCoreIPCClientV2 ) import logging import sys import functools from threading import Timer, Event import os import gpiozero import time import os COMPONENT_NAME = os.environ.get('COMPONENT_NAME') args = sys.argv[1:] # Set up logging logging.basicConfig() log = logging.getLogger() log.setLevel(args[0]) def callback(func, sensor=None): try: @functools.wraps(func) def wrapper(value): return functools.partial(func, value)(sensor) return wrapper except Exception: print("#### Error : {}".format(traceback.format_exc())) def error_handler(e): log.error("##### IPC Error : {}".format(e)) return False class LcdManager: def __init__(self): try: self.component_name = COMPONENT_NAME # Creating a greengrass IPC client self.gg_client = GreengrassCoreIPCClientV2() # loading configuration self.frequency = int(self.load_config("frequency")) self.ipc_flow_meter = self.load_config("ipc_flow_meter") self.ipc_temperature = self.load_config("ipc_temperature") self.ipc_volume_level = self.load_config("ipc_volume_level") # Bloc screen for advertisement :) self.init_done = False # IPC storage self.listeners = {} # data filtering variables self.precedent_value = {} self.value_avg = {} # set up LCD GPIO self.LCD_CS = 5 self.LCD_RST = 6 self.LCD_A0 = 13 self.LCD_CLK = 16 self.LCD_SI = 26 self.GPIO_LCD_CS = gpiozero.OutputDevice(self.LCD_CS) self.GPIO_LCD_RST = gpiozero.OutputDevice(self.LCD_RST) self.GPIO_LCD_A0 = gpiozero.OutputDevice(self.LCD_A0) self.GPIO_LCD_CLK = gpiozero.OutputDevice(self.LCD_CLK) self.GPIO_LCD_SI = gpiozero.OutputDevice(self.LCD_SI) self.GPIO_LCD_CS.on() self.GPIO_LCD_RST.off() self.GPIO_LCD_RST.on() # Register to IPC sensor = "flow_meter_2" self.precedent_value[sensor] = 0 log.debug("#### Store {} listener".format(sensor)) self.listeners[sensor] = callback(self.value_update, sensor) self.gg_client.subscribe_to_topic( topic=self.ipc_flow_meter, on_stream_event=self.listeners[sensor], on_stream_error=error_handler ) sensor = "temperature" self.precedent_value[sensor] = 0 log.debug("#### Store {} listener".format(sensor)) self.listeners[sensor] = callback(self.value_update, sensor) self.gg_client.subscribe_to_topic( topic=self.ipc_temperature, on_stream_event=self.listeners[sensor], on_stream_error=error_handler ) sensor = "volume_level" self.precedent_value[sensor] = 0 log.debug("#### Store {} listener".format(sensor)) self.listeners[sensor] = callback(self.value_update, sensor) self.gg_client.subscribe_to_topic( topic=self.ipc_volume_level, on_stream_event=self.listeners[sensor], on_stream_error=error_handler ) self.sensors = ["volume_level", "temperature", "flow_meter_2"] self.volume = str(0) self.temp = str(0) self.flow = str(0) self.update_number = 0 self.ASCII168 = ( (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), # " ",0 (0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x30, 0x00, 0x00, 0x00), # "!",1 (0x00, 0x10, 0x0C, 0x06, 0x10, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), # """,2 (0x40, 0xC0, 0x78, 0x40, 0xC0, 0x78, 0x40, 0x00, 0x04, 0x3F, 0x04, 0x04, 0x3F, 0x04, 0x04, 0x00), # "#",3 (0x00, 0x70, 0x88, 0xFC, 0x08, 0x30, 0x00, 0x00, 0x00, 0x18, 0x20, 0xFF, 0x21, 0x1E, 0x00, 0x00), # "$",4 (0xF0, 0x08, 0xF0, 0x00, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x21, 0x1C, 0x03, 0x1E, 0x21, 0x1E, 0x00), # "%",5 (0x00, 0xF0, 0x08, 0x88, 0x70, 0x00, 0x00, 0x00, 0x1E, 0x21, 0x23, 0x24, 0x19, 0x27, 0x21, 0x10), # "&",6 (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xB0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00), # ",",7 (0x00, 0x00, 0x00, 0xE0, 0x18, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x07, 0x18, 0x20, 0x40, 0x00), # "(",8 (0x00, 0x02, 0x04, 0x18, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x18, 0x07, 0x00, 0x00, 0x00), # ")",9 (0x40, 0x40, 0x80, 0xF0, 0x80, 0x40, 0x40, 0x00, 0x02, 0x02, 0x01, 0x0F, 0x01, 0x02, 0x02, 0x00), # "*",10 (0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x1F, 0x01, 0x01, 0x01, 0x00), # "+",11 (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xB0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00), # ",",12 (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01), # "-",13 (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00), # ".",14 (0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x18, 0x04, 0x00, 0x60, 0x18, 0x06, 0x01, 0x00, 0x00, 0x00), # "/",15 (0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x10, 0x0F, 0x00), # "0",16 (0x00, 0x10, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00), # "1",17 (0x00, 0x70, 0x08, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x30, 0x28, 0x24, 0x22, 0x21, 0x30, 0x00), # "2",18 (0x00, 0x30, 0x08, 0x88, 0x88, 0x48, 0x30, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00), # "3",19 (0x00, 0x00, 0xC0, 0x20, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x07, 0x04, 0x24, 0x24, 0x3F, 0x24, 0x00), # "4",20 (0x00, 0xF8, 0x08, 0x88, 0x88, 0x08, 0x08, 0x00, 0x00, 0x19, 0x21, 0x20, 0x20, 0x11, 0x0E, 0x00), # "5",21 (0x00, 0xE0, 0x10, 0x88, 0x88, 0x18, 0x00, 0x00, 0x00, 0x0F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00), # "6",22 (0x00, 0x38, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00), # "7",23 (0x00, 0x70, 0x88, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x1C, 0x22, 0x21, 0x21, 0x22, 0x1C, 0x00), # "8",24 (0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x00, 0x31, 0x22, 0x22, 0x11, 0x0F, 0x00), # "9",25 (0x00, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00), # ":",26 (0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x00, 0x00, 0x00, 0x00), # ";",27 (0x00, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00), # "<",28 (0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00), # "=",29 (0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00), # ">",30 (0x00, 0x70, 0x48, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x30, 0x36, 0x01, 0x00, 0x00), # "?",31 (0xC0, 0x30, 0xC8, 0x28, 0xE8, 0x10, 0xE0, 0x00, 0x07, 0x18, 0x27, 0x24, 0x23, 0x14, 0x0B, 0x00), # "@",32 (0x00, 0x00, 0xC0, 0x38, 0xE0, 0x00, 0x00, 0x00, 0x20, 0x3C, 0x23, 0x02, 0x02, 0x27, 0x38, 0x20), # "A",33 (0x08, 0xF8, 0x88, 0x88, 0x88, 0x70, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00), # "B",34 (0xC0, 0x30, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 0x07, 0x18, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00), # "C",35 (0x08, 0xF8, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00), # "D",36 (0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x23, 0x20, 0x18, 0x00), # "E",37 (0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00), # "F",38 (0xC0, 0x30, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x07, 0x18, 0x20, 0x20, 0x22, 0x1E, 0x02, 0x00), # "G",39 (0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x21, 0x3F, 0x20), # "H",40 (0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00), # "I",41 (0x00, 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, 0x00), # "J",42 (0x08, 0xF8, 0x88, 0xC0, 0x28, 0x18, 0x08, 0x00, 0x20, 0x3F, 0x20, 0x01, 0x26, 0x38, 0x20, 0x00), # "K",43 (0x08, 0xF8, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x30, 0x00), # "L",44 (0x08, 0xF8, 0xF8, 0x00, 0xF8, 0xF8, 0x08, 0x00, 0x20, 0x3F, 0x00, 0x3F, 0x00, 0x3F, 0x20, 0x00), # "M",45 (0x08, 0xF8, 0x30, 0xC0, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x20, 0x00, 0x07, 0x18, 0x3F, 0x00), # "N",46 (0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00), # "O",47 (0x08, 0xF8, 0x08, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x01, 0x00, 0x00), # "P",48 (0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x18, 0x24, 0x24, 0x38, 0x50, 0x4F, 0x00), # "Q",49 (0x08, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x70, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x0C, 0x30, 0x20), # "R",50 (0x00, 0x70, 0x88, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x38, 0x20, 0x21, 0x21, 0x22, 0x1C, 0x00), # "S",51 (0x18, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x18, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00), # "T",52 (0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00), # "U",53 (0x08, 0x78, 0x88, 0x00, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x07, 0x38, 0x0E, 0x01, 0x00, 0x00), # "V",54 (0xF8, 0x08, 0x00, 0xF8, 0x00, 0x08, 0xF8, 0x00, 0x03, 0x3C, 0x07, 0x00, 0x07, 0x3C, 0x03, 0x00), # "W",55 (0x08, 0x18, 0x68, 0x80, 0x80, 0x68, 0x18, 0x08, 0x20, 0x30, 0x2C, 0x03, 0x03, 0x2C, 0x30, 0x20), # "X",56 (0x08, 0x38, 0xC8, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00), # "Y",57 (0x10, 0x08, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x20, 0x38, 0x26, 0x21, 0x20, 0x20, 0x18, 0x00), # "Z",58 (0x00, 0x00, 0x00, 0xFE, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x40, 0x40, 0x40, 0x00), # "[",59 (0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x18, 0x04, 0x00, 0x60, 0x18, 0x06, 0x01, 0x00, 0x00, 0x00), # "/",60 (0x00, 0x02, 0x02, 0x02, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7F, 0x00, 0x00, 0x00), # "]",61 (0x00, 0x00, 0x04, 0x02, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), # "^",62 (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01), # "-",63 (0x00, 0x0C, 0x30, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x38, 0xC0, 0x00), # "\",64 (0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x19, 0x24, 0x22, 0x22, 0x22, 0x3F, 0x20), # "a",65 (0x08, 0xF8, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00), # "b",66 (0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x20, 0x11, 0x00), # "c",67 (0x00, 0x00, 0x00, 0x80, 0x80, 0x88, 0xF8, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x10, 0x3F, 0x20), # "d",68 (0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x22, 0x22, 0x22, 0x22, 0x13, 0x00), # "e",69 (0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0x18, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00), # "f",70 (0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x6B, 0x94, 0x94, 0x94, 0x93, 0x60, 0x00), # "g",71 (0x08, 0xF8, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20), # "h",72 (0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00), # "i",73 (0x00, 0x00, 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00), # "j",74 (0x08, 0xF8, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x24, 0x02, 0x2D, 0x30, 0x20, 0x00), # "k",75 (0x00, 0x08, 0x08, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00), # "l",76 (0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x3F, 0x20, 0x00, 0x3F), # "m",77 (0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20), # "n",79 (0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00), # "o",80 (0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xA1, 0x20, 0x20, 0x11, 0x0E, 0x00), # "p",81 (0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0xA0, 0xFF, 0x80), # "q",82 (0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x20, 0x3F, 0x21, 0x20, 0x00, 0x01, 0x00), # "r",83 (0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x33, 0x24, 0x24, 0x24, 0x24, 0x19, 0x00), # "s",84 (0x00, 0x80, 0x80, 0xE0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x00, 0x00), # "t",85 (0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x10, 0x3F, 0x20), # "u",86 (0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x01, 0x0E, 0x30, 0x08, 0x06, 0x01, 0x00), # "v",87 (0x80, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, 0x0F, 0x30, 0x0C, 0x03, 0x0C, 0x30, 0x0F, 0x00), # "w",88 (0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x31, 0x2E, 0x0E, 0x31, 0x20, 0x00), # "x",89 (0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x81, 0x8E, 0x70, 0x18, 0x06, 0x01, 0x00), # "y",90 (0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x21, 0x30, 0x2C, 0x22, 0x21, 0x30, 0x00), # "z",91 (0x00, 0x00, 0x00, 0x00, 0x80, 0x7C, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x40, 0x40), # "{",92 (0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00), # "|",93 (0x00, 0x02, 0x02, 0x7C, 0x80, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x3F, 0x00, 0x00, 0x00, 0x00), # "}",94 (0x00, 0x06, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), # "~",95 ) # entering lazy loop self.lazy_loop() except Exception: print("#### Error : {}".format(traceback.format_exc())) def load_config(self, key): try: temp = self.gg_client.get_configuration( component_name=self.component_name, key_path=[key]).value[key] log.debug("#### Received from config for {} : {}".format(key, temp)) return temp except Exception: log.error("#### Reading configuration error : {}".format(traceback.format_exc())) def lcd_init(self): try: self.GPIO_LCD_CS.on() self.GPIO_LCD_RST.off() self.GPIO_LCD_RST.on() self.lcd_tranfer_data(0xe2, 0) # Internal reset self.lcd_tranfer_data(0xa2, 0) # Sets the LCD drive voltage bias ratio ## TODO: i set to a2 instead of a3 ##A2: 1/9 bias ##A3: 1/7 bias (ST7565V) self.lcd_tranfer_data(0xa0, 0) # Sets the display RAM address SEG output correspondence ##A0: normal ##A1: reverse self.lcd_tranfer_data(0xc8, 0) # Select COM output scan direction ##C0~C7: normal direction ##C8~CF: reverse direction self.lcd_tranfer_data(0xa4, 0) # Display all points ON/OFF ##A4: normal display ##A5: all points ON self.lcd_tranfer_data(0xa6, 0) # Sets the LCD display normal/inverted ##A6: normal ##A7: inverted self.lcd_tranfer_data(0x2F, 0) # select internal power supply operating mode ##28~2F: Operating mode self.lcd_tranfer_data(0x60, 0) # Display start line set ##40~7F Display start address self.lcd_tranfer_data(0x27, 0) # V5 voltage regulator internal resistor ratio set(contrast) ##20~27 small~large self.lcd_tranfer_data(0x81, 0) # Electronic volume mode set ##81: Set the V5 output voltage self.lcd_tranfer_data(0x30, 0) # Electronic volume register set ##00~3F: electronic volume register self.lcd_tranfer_data(0b10101111, 0) # Display ON/OFF ## 0b10101111: ON ## 0b10101110: OFF Timer(60, self.lcd_init).start() except Exception: print("#### Error : {}".format(traceback.format_exc())) def clearscreen(self): try: os.system('clear') except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_ascii168_string(self, x_pos, y_pos, msg): try: string_len = len(msg) for i in range(0, string_len): self.lcd_ascii168(x_pos + i * 8, y_pos, ord(msg[i]) - 32) except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_ascii168(self, x_pos, y_pos, char): try: self.lcd_set_page(y_pos, x_pos) for i in range(0, 8): self.lcd_tranfer_data(self.ASCII168[char][i], 1) self.lcd_set_page(y_pos + 1, x_pos) for i in range(8, 16): self.lcd_tranfer_data(self.ASCII168[char][i], 1) except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_clear(self): try: self.GPIO_LCD_CS.off() for i in range(0, 8): self.lcd_set_page(i, 0) for j in range(0, 128): self.lcd_tranfer_data(0x00, 1) self.GPIO_LCD_CS.on() except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_set_page(self, page, column): try: lsb = column & 0x0f msb = column & 0xf0 msb >>= 4 msb |= 0x10 page |= 0xb0 self.lcd_tranfer_data(page, 0) self.lcd_tranfer_data(msb, 0) self.lcd_tranfer_data(lsb, 0) except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_tranfer_data(self, value, a0): try: self.GPIO_LCD_CS.off() self.GPIO_LCD_CLK.on() if a0: self.GPIO_LCD_A0.on() else: self.GPIO_LCD_A0.off() self.lcd_byte(value) self.GPIO_LCD_CS.on() except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_byte(self, bits): try: tmp = bits for i in range(0, 8): self.GPIO_LCD_CLK.off() if tmp & 0x80: self.GPIO_LCD_SI.on() else: self.GPIO_LCD_SI.off() tmp = (tmp << 1) self.GPIO_LCD_CLK.on() except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_welcome_message(self): try: self.lcd_clear() self.lcd_ascii168_string(0, 0, " AWS") self.lcd_ascii168_string(0, 2, " EMEA") self.lcd_ascii168_string(0, 4, " Prototyping") self.lcd_ascii168_string(0, 6, " Labs") except Exception: print("#### Error : {}".format(traceback.format_exc())) def lcd_report_update(self): try: if self.init_done: self.lcd_clear() self.lcd_ascii168_string(0, 0, " Current Values") self.lcd_ascii168_string(0, 2, " Flow : " + str(self.flow)) self.lcd_ascii168_string(0, 4, " Temp : " + str(self.temp)) self.lcd_ascii168_string(0, 6, " Volume : " + str(self.volume)) except Exception: print("#### Error : {}".format(traceback.format_exc())) finally: Timer(1, self.lcd_report_update).start() def lazy_loop(self): Timer(5, self.lazy_loop).start() # receive an led array values def value_update(self, value, sensor=None): try: updated_value = value.binary_message.message.decode('utf-8') log.debug("#### value: {} for sensor: {}".format(updated_value, sensor)) if sensor not in self.sensors: log.error("Sensor {} not configured!".format(sensor)) return if sensor == "flow_meter_2": log.debug("#### flow : {}".format(updated_value)) self.flow = str(updated_value) elif sensor == "temperature": self.temp = str(updated_value) elif sensor == "volume_level": self.volume = str(updated_value) self.update_number = self.update_number + 1 return except Exception: print("#### Error : {}".format(traceback.format_exc())) lcd_manager = LcdManager() lcd_manager.lcd_init() lcd_manager.lcd_clear() lcd_manager.lcd_welcome_message() time.sleep(10) lcd_manager.init_done = True lcd_manager.lcd_report_update() Event().wait()