]>
vault307.fbx.one Git - RPI-PICO-I2C-LCD.git/blob - lcd_api.py
5 # Implements the API for talking with HD44780 compatible character LCDs.
6 # This class only knows what commands to send to the LCD, and not how to get
9 # It is expected that a derived class will implement the hal_xxx functions.
11 # The following constant names were lifted from the avrlib lcd.h header file,
12 # with bit numbers changed to bit masks.
14 # HD44780 LCD controller command set
15 LCD_CLR
= 0x01 # DB0: clear display
16 LCD_HOME
= 0x02 # DB1: return to home position
18 LCD_ENTRY_MODE
= 0x04 # DB2: set entry mode
19 LCD_ENTRY_INC
= 0x02 # DB1: increment
20 LCD_ENTRY_SHIFT
= 0x01 # DB0: shift
22 LCD_ON_CTRL
= 0x08 # DB3: turn lcd/cursor on
23 LCD_ON_DISPLAY
= 0x04 # DB2: turn display on
24 LCD_ON_CURSOR
= 0x02 # DB1: turn cursor on
25 LCD_ON_BLINK
= 0x01 # DB0: blinking cursor
27 LCD_MOVE
= 0x10 # DB4: move cursor/display
28 LCD_MOVE_DISP
= 0x08 # DB3: move display (0-> move cursor)
29 LCD_MOVE_RIGHT
= 0x04 # DB2: move right (0-> left)
31 LCD_FUNCTION
= 0x20 # DB5: function set
32 LCD_FUNCTION_8BIT
= 0x10 # DB4: set 8BIT mode (0->4BIT mode)
33 LCD_FUNCTION_2LINES
= 0x08 # DB3: two lines (0->one line)
34 LCD_FUNCTION_10DOTS
= 0x04 # DB2: 5x10 font (0->5x7 font)
35 LCD_FUNCTION_RESET
= 0x30 # See "Initializing by Instruction" section
37 LCD_CGRAM
= 0x40 # DB6: set CG RAM address
38 LCD_DDRAM
= 0x80 # DB7: set DD RAM address
46 def __init__(self
, num_lines
, num_columns
):
47 self
.num_lines
= num_lines
48 if self
.num_lines
> 4:
50 self
.num_columns
= num_columns
51 if self
.num_columns
> 40:
55 self
.implied_newline
= False
60 self
.hal_write_command(self
.LCD_ENTRY_MODE | self
.LCD_ENTRY_INC
)
65 # Clears the LCD display and moves the cursor to the top left corner
66 self
.hal_write_command(self
.LCD_CLR
)
67 self
.hal_write_command(self
.LCD_HOME
)
71 def show_cursor(self
):
72 # Causes the cursor to be made visible
73 self
.hal_write_command(self
.LCD_ON_CTRL | self
.LCD_ON_DISPLAY |
76 def hide_cursor(self
):
77 # Causes the cursor to be hidden
78 self
.hal_write_command(self
.LCD_ON_CTRL | self
.LCD_ON_DISPLAY
)
80 def blink_cursor_on(self
):
81 # Turns on the cursor, and makes it blink
82 self
.hal_write_command(self
.LCD_ON_CTRL | self
.LCD_ON_DISPLAY |
83 self
.LCD_ON_CURSOR | self
.LCD_ON_BLINK
)
85 def blink_cursor_off(self
):
86 # Turns on the cursor, and makes it no blink (i.e. be solid)
87 self
.hal_write_command(self
.LCD_ON_CTRL | self
.LCD_ON_DISPLAY |
91 # Turns on (i.e. unblanks) the LCD
92 self
.hal_write_command(self
.LCD_ON_CTRL | self
.LCD_ON_DISPLAY
)
94 def display_off(self
):
95 # Turns off (i.e. blanks) the LCD
96 self
.hal_write_command(self
.LCD_ON_CTRL
)
98 def backlight_on(self
):
99 # Turns the backlight on.
101 # This isn't really an LCD command, but some modules have backlight
102 # controls, so this allows the hal to pass through the command.
103 self
.backlight
= True
104 self
.hal_backlight_on()
106 def backlight_off(self
):
107 # Turns the backlight off.
109 # This isn't really an LCD command, but some modules have backlight
110 # controls, so this allows the hal to pass through the command.
111 self
.backlight
= False
112 self
.hal_backlight_off()
114 def move_to(self
, cursor_x
, cursor_y
):
115 # Moves the cursor position to the indicated position. The cursor
116 # position is zero based (i.e. cursor_x == 0 indicates first column).
117 self
.cursor_x
= cursor_x
118 self
.cursor_y
= cursor_y
119 addr
= cursor_x
& 0x3f
121 addr
+= 0x40 # Lines 1 & 3 add 0x40
122 if cursor_y
& 2: # Lines 2 & 3 add number of columns
123 addr
+= self
.num_columns
124 self
.hal_write_command(self
.LCD_DDRAM | addr
)
126 def putchar(self
, char
):
127 # Writes the indicated character to the LCD at the current cursor
128 # position, and advances the cursor by one position.
130 if self
.implied_newline
:
131 # self.implied_newline means we advanced due to a wraparound,
132 # so if we get a newline right after that we ignore it.
135 self
.cursor_x
= self
.num_columns
137 self
.hal_write_data(ord(char
))
139 if self
.cursor_x
>= self
.num_columns
:
142 self
.implied_newline
= (char
!= '\n')
143 if self
.cursor_y
>= self
.num_lines
:
145 self
.move_to(self
.cursor_x
, self
.cursor_y
)
147 def putstr(self
, string
):
148 # Write the indicated string to the LCD at the current cursor
149 # position and advances the cursor position appropriately.
153 def custom_char(self
, location
, charmap
):
154 # Write a character to one of the 8 CGRAM locations, available
155 # as chr(0) through chr(7).
157 self
.hal_write_command(self
.LCD_CGRAM |
(location
<< 3))
158 self
.hal_sleep_us(40)
160 self
.hal_write_data(charmap
[i
])
161 self
.hal_sleep_us(40)
162 self
.move_to(self
.cursor_x
, self
.cursor_y
)
164 def hal_backlight_on(self
):
165 # Allows the hal layer to turn the backlight on.
166 # If desired, a derived HAL class will implement this function.
169 def hal_backlight_off(self
):
170 # Allows the hal layer to turn the backlight off.
171 # If desired, a derived HAL class will implement this function.
174 def hal_write_command(self
, cmd
):
175 # Write a command to the LCD.
176 # It is expected that a derived HAL class will implement this function.
177 raise NotImplementedError
179 def hal_write_data(self
, data
):
180 # Write data to the LCD.
181 # It is expected that a derived HAL class will implement this function.
182 raise NotImplementedError
184 def hal_sleep_us(self
, usecs
):
185 # Sleep for some time (given in microseconds)