]> vault307.fbx.one Git - random.git/blob - RGB1602.py
different projects to learn new things
[random.git] / RGB1602.py
1 # -*- coding: utf-8 -*-
2 import time
3 from machine import Pin,I2C
4
5 RGB1602_SDA = Pin(4)
6 RGB1602_SCL = Pin(5)
7
8 RGB1602_I2C = I2C(0,sda = RGB1602_SDA,scl = RGB1602_SCL ,freq = 400000)
9
10 #Device I2C Arress
11 LCD_ADDRESS = (0x7c>>1)
12 RGB_ADDRESS = (0xc0>>1)
13
14 #color define
15
16 REG_RED = 0x04
17 REG_GREEN = 0x03
18 REG_BLUE = 0x02
19 REG_MODE1 = 0x00
20 REG_MODE2 = 0x01
21 REG_OUTPUT = 0x08
22 LCD_CLEARDISPLAY = 0x01
23 LCD_RETURNHOME = 0x02
24 LCD_ENTRYMODESET = 0x04
25 LCD_DISPLAYCONTROL = 0x08
26 LCD_CURSORSHIFT = 0x10
27 LCD_FUNCTIONSET = 0x20
28 LCD_SETCGRAMADDR = 0x40
29 LCD_SETDDRAMADDR = 0x80
30
31 #flags for display entry mode
32 LCD_ENTRYRIGHT = 0x00
33 LCD_ENTRYLEFT = 0x02
34 LCD_ENTRYSHIFTINCREMENT = 0x01
35 LCD_ENTRYSHIFTDECREMENT = 0x00
36
37 #flags for display on/off control
38 LCD_DISPLAYON = 0x04
39 LCD_DISPLAYOFF = 0x00
40 LCD_CURSORON = 0x02
41 LCD_CURSOROFF = 0x00
42 LCD_BLINKON = 0x01
43 LCD_BLINKOFF = 0x00
44
45 #flags for display/cursor shift
46 LCD_DISPLAYMOVE = 0x08
47 LCD_CURSORMOVE = 0x00
48 LCD_MOVERIGHT = 0x04
49 LCD_MOVELEFT = 0x00
50
51 #flags for function set
52 LCD_8BITMODE = 0x10
53 LCD_4BITMODE = 0x00
54 LCD_2LINE = 0x08
55 LCD_1LINE = 0x00
56 LCD_5x8DOTS = 0x00
57
58
59 class RGB1602:
60 def __init__(self, col, row):
61 self._row = row
62 self._col = col
63
64 self._showfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
65 self.begin(self._row,self._col)
66
67
68 def command(self,cmd):
69 RGB1602_I2C.writeto_mem(LCD_ADDRESS, 0x80, chr(cmd))
70
71 def write(self,data):
72 RGB1602_I2C.writeto_mem(LCD_ADDRESS, 0x40, chr(data))
73
74 def setReg(self,reg,data):
75 RGB1602_I2C.writeto_mem(RGB_ADDRESS, reg, chr(data))
76
77
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)
82
83 def setCursor(self,col,row):
84 if(row == 0):
85 col|=0x80
86 else:
87 col|=0xc0;
88 RGB1602_I2C.writeto(LCD_ADDRESS, bytearray([0x80,col]))
89
90 def clear(self):
91 self.command(LCD_CLEARDISPLAY)
92 time.sleep(0.002)
93 def printout(self,arg):
94 if(isinstance(arg,int)):
95 arg=str(arg)
96
97 for x in bytearray(arg,'utf-8'):
98 self.write(x)
99
100
101 def display(self):
102 self._showcontrol |= LCD_DISPLAYON
103 self.command(LCD_DISPLAYCONTROL | self._showcontrol)
104
105
106 def begin(self,cols,lines):
107 if (lines > 1):
108 self._showfunction |= LCD_2LINE
109
110 self._numlines = lines
111 self._currline = 0
112
113
114
115 time.sleep(0.05)
116
117
118 # Send function set command sequence
119 self.command(LCD_FUNCTIONSET | self._showfunction)
120 #delayMicroseconds(4500); # wait more than 4.1ms
121 time.sleep(0.005)
122 # second try
123 self.command(LCD_FUNCTIONSET | self._showfunction);
124 #delayMicroseconds(150);
125 time.sleep(0.005)
126 # third go
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
132 self.display()
133 # clear it off
134 self.clear()
135 # Initialize to default text direction (for romance languages)
136 self._showmode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
137 # set the entry mode
138 self.command(LCD_ENTRYMODESET | self._showmode);
139 # backlight init
140 self.setReg(REG_MODE1, 0)
141 # set LEDs controllable by both PWM and GRPPWM registers
142 self.setReg(REG_OUTPUT, 0xFF)
143 # set MODE2 values
144 # 0010 0000 -> 0x20 (DMBLNK to 1, ie blinky mode)
145 self.setReg(REG_MODE2, 0x20)
146 self.setColorWhite()
147
148 def setColorWhite(self):
149 self.setRGB(255, 255, 255)