1 # -*- coding: utf-8 -*-
3 from machine
import Pin
,I2C
8 RGB1602_I2C
= I2C(0,sda
= RGB1602_SDA
,scl
= RGB1602_SCL
,freq
= 400000)
11 LCD_ADDRESS
= (0x7c>>1)
12 RGB_ADDRESS
= (0xc0>>1)
22 LCD_CLEARDISPLAY
= 0x01
24 LCD_ENTRYMODESET
= 0x04
25 LCD_DISPLAYCONTROL
= 0x08
26 LCD_CURSORSHIFT
= 0x10
27 LCD_FUNCTIONSET
= 0x20
28 LCD_SETCGRAMADDR
= 0x40
29 LCD_SETDDRAMADDR
= 0x80
31 #flags for display entry mode
34 LCD_ENTRYSHIFTINCREMENT
= 0x01
35 LCD_ENTRYSHIFTDECREMENT
= 0x00
37 #flags for display on/off control
45 #flags for display/cursor shift
46 LCD_DISPLAYMOVE
= 0x08
51 #flags for function set
60 def __init__(self
, col
, row
):
64 self
._showfunction
= LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS
;
65 self
.begin(self
._row
,self
._col
)
68 def command(self
,cmd
):
69 RGB1602_I2C
.writeto_mem(LCD_ADDRESS
, 0x80, chr(cmd
))
72 RGB1602_I2C
.writeto_mem(LCD_ADDRESS
, 0x40, chr(data
))
74 def setReg(self
,reg
,data
):
75 RGB1602_I2C
.writeto_mem(RGB_ADDRESS
, reg
, chr(data
))
78 def setRGB(self
,r
,g
,b
):
79 self
.setReg(REG_RED
,r
)
80 self
.setReg(REG_GREEN
,g
)
81 self
.setReg(REG_BLUE
,b
)
83 def setCursor(self
,col
,row
):
88 RGB1602_I2C
.writeto(LCD_ADDRESS
, bytearray([0x80,col
]))
91 self
.command(LCD_CLEARDISPLAY
)
93 def printout(self
,arg
):
94 if(isinstance(arg
,int)):
97 for x
in bytearray(arg
,'utf-8'):
102 self
._showcontrol |
= LCD_DISPLAYON
103 self
.command(LCD_DISPLAYCONTROL | self
._showcontrol
)
106 def begin(self
,cols
,lines
):
108 self
._showfunction |
= LCD_2LINE
110 self
._numlines
= lines
118 # Send function set command sequence
119 self
.command(LCD_FUNCTIONSET | self
._showfunction
)
120 #delayMicroseconds(4500); # wait more than 4.1ms
123 self
.command(LCD_FUNCTIONSET | self
._showfunction
);
124 #delayMicroseconds(150);
127 self
.command(LCD_FUNCTIONSET | self
._showfunction
)
128 # finally, set # lines, font size, etc.
129 self
.command(LCD_FUNCTIONSET | self
._showfunction
)
130 # turn the display on with no cursor or blinking default
131 self
._showcontrol
= LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
135 # Initialize to default text direction (for romance languages)
136 self
._showmode
= LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
138 self
.command(LCD_ENTRYMODESET | self
._showmode
);
140 self
.setReg(REG_MODE1
, 0)
141 # set LEDs controllable by both PWM and GRPPWM registers
142 self
.setReg(REG_OUTPUT
, 0xFF)
144 # 0010 0000 -> 0x20 (DMBLNK to 1, ie blinky mode)
145 self
.setReg(REG_MODE2
, 0x20)
148 def setColorWhite(self
):
149 self
.setRGB(255, 255, 255)