1
+ # /*****************************************************************************
2
+ # * | File : OLED_1in5_rgb.py
3
+ # * | Author : Waveshare team
4
+ # * | Function : Driver for OLED_1in5_rgb
5
+ # * | Info :
6
+ # *----------------
7
+ # * | This version: V2.0
8
+ # * | Date : 2020-08-17
9
+ # * | Info :
10
+ # ******************************************************************************/
11
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ # of this software and associated documnetation files (the "Software"), to deal
13
+ # in the Software without restriction, including without limitation the rights
14
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ # copies of the Software, and to permit persons to whom the Software is
16
+ # furished to do so, subject to the following conditions:
17
+ #
18
+ # The above copyright notice and this permission notice shall be included in
19
+ # all copies or substantial portions of the Software.
20
+ #
21
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
+ # THE SOFTWARE.
28
+ #
29
+
30
+ from . import config
31
+ import time
32
+
33
+ Device_SPI = config .Device_SPI
34
+ Device_I2C = config .Device_I2C
35
+
36
+ OLED_WIDTH = 128 #OLED width
37
+ OLED_HEIGHT = 128 #OLED height
38
+
39
+ class OLED_1in5_rgb (config .RaspberryPi ):
40
+
41
+ """ Write register address and data """
42
+ def command (self , cmd ):
43
+ self .digital_write (self .DC_PIN ,False )
44
+ self .spi_writebyte ([cmd ])
45
+
46
+ """ Write data """
47
+ def data (self , data ):
48
+ self .digital_write (self .DC_PIN ,True )
49
+ self .spi_writebyte ([data ])
50
+
51
+ def Init (self ):
52
+ if (self .module_init () != 0 ):
53
+ return - 1
54
+
55
+ self .width = OLED_WIDTH
56
+ self .height = OLED_HEIGHT
57
+
58
+ """Initialize dispaly"""
59
+ self .reset ()
60
+
61
+ if (self .Device == Device_I2C ):
62
+ print ("Only Device_SPI, Please revise config.py !!!" )
63
+ exit ()
64
+
65
+ self .command (0xfd ) # command lock
66
+ self .data (0x12 )
67
+ self .command (0xfd ) # command lock
68
+ self .data (0xB1 )
69
+
70
+ self .command (0xae ) # display off
71
+ self .command (0xa4 ) # Normal Display mode
72
+
73
+ self .command (0x15 ) # set column address
74
+ self .data (0x00 ) # column address start 00
75
+ self .data (0x7f ) # column address end 127
76
+ self .command (0x75 ) # set row address
77
+ self .data (0x00 ) # row address start 00
78
+ self .data (0x7f ) # row address end 127
79
+
80
+ self .command (0xB3 )
81
+ self .data (0xF1 )
82
+
83
+ self .command (0xCA )
84
+ self .data (0x7F )
85
+
86
+ self .command (0xa0 ) # set re-map & data format
87
+ self .data (0x74 ) # Horizontal address increment
88
+
89
+ self .command (0xa1 ) # set display start line
90
+ self .data (0x00 ) # start 00 line
91
+
92
+ self .command (0xa2 ) # set display offset
93
+ self .data (0x00 )
94
+
95
+ self .command (0xAB )
96
+ self .command (0x01 )
97
+
98
+ self .command (0xB4 )
99
+ self .data (0xA0 )
100
+ self .data (0xB5 )
101
+ self .data (0x55 )
102
+
103
+ self .command (0xC1 )
104
+ self .data (0xC8 )
105
+ self .data (0x80 )
106
+ self .data (0xC0 )
107
+
108
+ self .command (0xC7 )
109
+ self .data (0x0F )
110
+
111
+ self .command (0xB1 )
112
+ self .data (0x32 )
113
+
114
+ self .command (0xB2 )
115
+ self .data (0xA4 )
116
+ self .data (0x00 )
117
+ self .data (0x00 )
118
+
119
+ self .command (0xBB )
120
+ self .data (0x17 )
121
+
122
+ self .command (0xB6 )
123
+ self .data (0x01 )
124
+
125
+ self .command (0xBE )
126
+ self .data (0x05 )
127
+
128
+ self .command (0xA6 )
129
+
130
+ time .sleep (0.1 )
131
+ self .command (0xAF );#--turn on oled panel
132
+
133
+
134
+ def reset (self ):
135
+ """Reset the display"""
136
+ self .digital_write (self .RST_PIN ,True )
137
+ time .sleep (0.1 )
138
+ self .digital_write (self .RST_PIN ,False )
139
+ time .sleep (0.1 )
140
+ self .digital_write (self .RST_PIN ,True )
141
+ time .sleep (0.1 )
142
+
143
+ def clear (self ):
144
+ _buffer = [0x00 ]* (self .width * self .height * 2 )
145
+ self .ShowImage (_buffer )
146
+
147
+ def getbuffer (self , image ):
148
+ buf = [0x00 ] * ((self .width * 2 ) * self .height )
149
+ imwidth , imheight = image .size
150
+ pixels = image .load ()
151
+ for y in range (imheight ):
152
+ for x in range (imwidth ):
153
+ # Set the bits for the column of pixels at the current position.
154
+ buf [x * 2 + y * imwidth * 2 ] = ((pixels [x ,y ][0 ] & 0xF8 ) | (pixels [x ,y ][1 ] >> 5 ))
155
+ buf [x * 2 + 1 + y * imwidth * 2 ] = (((pixels [x ,y ][1 ]<< 3 ) & 0xE0 ) | (pixels [x ,y ][2 ] >> 3 ))
156
+ return buf
157
+
158
+ def ShowImage (self , pBuf ):
159
+ self .command (0x15 ) # set column address
160
+ self .data (0x00 ) # column address start 00
161
+ self .data (0x7f ) # column address end 127
162
+ self .command (0x75 ) # set row address
163
+ self .data (0x00 ) # row address start 00
164
+ self .data (0x7f ) # row address end 127
165
+ self .command (0x5C );
166
+ for i in range (0 , self .height ):
167
+ for j in range (0 , self .width * 2 ):
168
+ self .data (pBuf [j + self .width * 2 * i ])
169
+ return
170
+
171
+
0 commit comments