Skip to content

Instantly share code, notes, and snippets.

@teddokano
Last active August 4, 2023 01:37
Show Gist options
  • Save teddokano/504beee2201a237234db25519833dd3d to your computer and use it in GitHub Desktop.
Save teddokano/504beee2201a237234db25519833dd3d to your computer and use it in GitHub Desktop.
A character LCD: "AE_AQM0802" MicroPython operation sample
from machine import Pin, I2C
import utime
import ure
class AE_AQM0802:
DEFAULT_ADDR = (0x7C >> 1)
def __init__( self, i2c ):
self.i2c = i2c
self.address = self.DEFAULT_ADDR
self.cursor = { "x": 0, "y": 0 }
init_commands = [ 0x38, 0x39, 0x14, 0x7A, 0x54, 0x6C, 0x38, 0x0C, 0x01, 0x06 ]
self.width = 8
self.lines = 2
self.blank = "".join( [ " " for _ in range( self.width ) ] )
for v in init_commands:
self.command( v )
self.clear()
def send( self, data_flag, value ):
self.i2c.writeto( self.address, bytearray( [ 0x40 if data_flag else 0x00, value ] ) )
def command( self, command ):
self.send( False, command )
utime.sleep_ms( 10 )
def data( self, data ):
self.send( True, data )
utime.sleep_ms( 1 )
def clear( self ):
self.command( 0x01 )
def putc( self, char ):
self.data( char )
def puts( self, str, line_num = 0 ):
self.command( 0x80 + 0x40 * line_num )
for c in str:
self.putc( ord( c ) )
def print( self, string ):
if isinstance( string, str ):
splt = []
for i in range( 0, self.width * self.lines, self.width ):
splt += [ string[ i : i + self.width ] ]
self.print( splt )
else:
for i, s in enumerate( string ):
if s is not None:
self.puts( s + self.blank, i )
def main():
i2c = I2C( 0, freq = (400 * 1000) )
lcd = AE_AQM0802( i2c )
print( os.uname().machine + " is working!" )
print( "available I2C target(s): ", end = "" )
print( [ hex( i ) for i in i2c.scan() ] )
lcd.print( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" )
utime.sleep( 1 )
lcd.print( "192.168.100.222" )
utime.sleep( 1 )
lcd.print( "10.0.1.2" )
utime.sleep( 1 )
while True:
lcd.print( [ "ABCDEFGH", "12345678" ] )
utime.sleep( 1 )
lcd.print( [ "abcdefgh", None ] )
utime.sleep( 1 )
lcd.clear()
utime.sleep( 1 )
lcd.print( [ "ABCD", "1234" ] )
utime.sleep( 1 )
lcd.print( [ "abc", "" ] )
utime.sleep( 1 )
lcd.print( [ "", "ABC" ] )
utime.sleep( 1 )
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment