#!/usr/bin/env python # -*- coding: utf-8 -*- # --------------------------------------------------------------------- # pertelian.py: python module to control Pertelian X2040 USB LCD # --------------------------------------------------------------------- # Copyright (c) 2019 BOXTEC AG # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL # THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -------------------------------------------------------------------------- # """ __author__ = "boxtec (src@boxtec.ch)" __appname__ = "pertelian.py" __version__ = "1.00" __date__ = "2019-04-17" __license__ = "BSD" """ ####################### # IMPORTS ####################### import serial, time, struct ####################### # CONSTANTS ####################### LCD_WIDTH = 20 STRING_WIDTH = 20 SDELAY_DEFAULT = 0.01 #DEFAULT_INIT = [0x38, 0x06, 0x10, 0x0c, 0x01] DEFAULT_INIT = [0x38, 0x06, 0x10, 0x0c, 0x01] FILLER_CHAR = " " ####################### # Display class ####################### class Display(): # local methods def __init__(self, port='/dev/ttyUSB0', baudrate=115200, init_seq=DEFAULT_INIT): self._sp = serial.Serial(port, baudrate) self._display_init(init_seq) self.backlight(True) self.enable() def __del__(self): self._sp.close() def _display_init(self, init_seq): for cmd in init_seq: self._sendcmd(cmd) def _senddata(self, data, datatype="C", delay=SDELAY_DEFAULT): for c in data: self._sp.write(chr(c)) time.sleep(delay) def _sendcmd(self, cmd, delay=SDELAY_DEFAULT): cmd_data = (0xfe, cmd) self._senddata(cmd_data, delay) # public methods def enable(self): self._sendcmd(0x0c) def disable(self): self.clear() self.backlight(False) self._sendcmd(0x08) def backlight(self, on=True): if on: self._sendcmd(0x03) else: self._sendcmd(0x02) def clear(self): self._sendcmd(0x01) def lcdprintln(self, text): char_list = [] for c in text: char_list.append(ord(c)) if len(char_list) >= 20: break while len(char_list) < STRING_WIDTH: char_list.append(ord(FILLER_CHAR)) self._senddata(char_list) def lcdprint(self, text): char_list = [] for c in text: char_list.append(ord(c)) self._senddata(char_list) def lcdcursor_set(self, line, position): #Row1-4 baseaddress: rowbase = [0x0, 0x40, 0x14, 0x54] self._sendcmd((rowbase[line-1] + position) | 0x80) def lcdscroll(self, text="", scroll_delay=0.1, iterations=1): if len(text) > 0: lcdprintln(text) for c in range(0, iterations): for i in range(0, STRING_WIDTH): self._sendcmd(0x1c) time.sleep(scroll_delay)