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