Gestion du lcd à nomme lcd_api.py
import
time
class
LcdApi:
# Implements the API for talking with HD44780 compatible character LCDs.
# This class only knows what commands to send to the LCD, and not how to get
# them to the LCD.
#
# It is expected that a derived class will implement the hal_xxx functions.
#
# The following constant names were lifted from the avrlib lcd.h header file,
# with bit numbers changed to bit masks.
# HD44780 LCD controller command set
LCD_CLR
LCD_HOME
LCD_ENTRY_MODE
LCD_ENTRY_INC
LCD_ENTRY_SHIFT
LCD_ON_CTRL
LCD_ON_DISPLAY
LCD_ON_CURSOR
LCD_ON_BLINK
LCD_MOVE
LCD_MOVE_DISP
LCD_MOVE_RIGHT
LCD_FUNCTION
LCD_FUNCTION_8BIT
LCD_FUNCTION_2LINES
LCD_FUNCTION_10DOTS
LCD_FUNCTION_RESET
LCD_CGRAM
LCD_DDRAM
LCD_RS_CMD
LCD_RS_DATA
LCD_RW_WRITE
LCD_RW_READ
def __init__(self, num_lines, num_columns):
self.num_lines = num_lines
if self.num_lines > 4:
self.num_lines = 4
self.num_columns = num_columns
if self.num_columns > 40:
self.num_columns = 40
self.cursor_x = 0
self.cursor_y = 0
self.implied_newline = False
self.backlight = True
self.display_off()
self.backlight_on()
self.clear()
self.hal_write_command(self.LCD_ENTRY_MODE | self.LCD_ENTRY_INC)
self.hide_cursor()
self.display_on()
def clear(self):
# Clears the LCD display and moves the cursor to the top left corner
self.hal_write_command(self.LCD_CLR)
self.hal_write_command(self.LCD_HOME)
self.cursor_x = 0
self.cursor_y = 0
def show_cursor(self):
# Causes the cursor to be made visible
self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
=
0x01
# DB0: clear display
=
0x02
# DB1: return to home position
=
0x04
# DB2: set entry mode
=
0x02
# DB1: increment
=
0x01
# DB0: shift
=
0x08
# DB3: turn lcd/cursor on
=
0x04
# DB2: turn display on
=
0x02
# DB1: turn cursor on
=
0x01
# DB0: blinking cursor
=
0x10
# DB4: move cursor/display
=
0x08
# DB3: move display (0-> move cursor)
=
0x04
# DB2: move right (0-> left)
=
0x20
# DB5: function set
=
0x10
# DB4: set 8BIT mode (0->4BIT mode)
=
0x08
# DB3: two lines (0->one line)
=
0x04
# DB2: 5x10 font (0->5x7 font)
=
0x30
# See "Initializing by Instruction" section
=
0x40
# DB6: set CG RAM address
=
0x80
# DB7: set DD RAM address
=
0
= 1
= 0
= 1
self.LCD_ON_CURSOR)