2424SOFTWARE.
2525"""
2626
27- __version__ = '1.1.0 '
27+ __version__ = '1.1.1 '
2828
2929from micropython import const
30- import time
3130import framebuf
3231
3332# commands
5655REG_CMD = const (0x80 )
5756REG_DATA = const (0x40 )
5857
59-
6058class SSD1327 :
61- def __init__ (self , width , height , external_vcc ):
59+ def __init__ (self , width = 128 , height = 128 ):
6260 self .width = width
6361 self .height = height
64- self .external_vcc = external_vcc
6562 self .buffer = bytearray (self .width * self .height // 2 )
6663 self .framebuf = framebuf .FrameBuffer (self .buffer , self .width , self .height , framebuf .GS4_HMSB )
64+
65+ self .col_addr = ((128 - self .width ) // 4 , 63 - ((128 - self .width ) // 4 ))
66+ # 96x96 (8, 55)
67+ # 128x128 (0, 63)
68+
69+ self .row_addr = (0 , self .height - 1 )
70+ # 96x96 (0, 95)
71+ # 128x128 (0, 127)
72+
73+ self .offset = 128 - self .height
74+ # 96x96 32
75+ # 128x128 0
76+
6777 self .poweron ()
6878 self .init_display ()
6979
@@ -73,7 +83,7 @@ def init_display(self):
7383 SET_DISP , # Display off
7484 # Resolution and layout
7585 SET_DISP_START_LINE , 0x00 ,
76- SET_DISP_OFFSET , 0x00 , # Set vertical offset by COM from 0~127
86+ SET_DISP_OFFSET , self . offset , # Set vertical offset by COM from 0~127
7787 # Set re-map
7888 # Enable column address re-map
7989 # Disable nibble re-map
@@ -94,20 +104,13 @@ def init_display(self):
94104 SET_GRAYSCALE_LINEAR , # Use linear greyscale lookup table
95105 SET_CONTRAST , 0x7f , # Medium brightness
96106 SET_DISP_MODE , # Normal, not inverted
97- # 96x96:
98- # SET_ROW_ADDR, 0 95,
99- # SET_COL_ADDR, 8, 55,
100- # 128x128:
101- # SET_ROW_ADDR, 0 127,
102- # SET_COL_ADDR, 0, 63,
103- SET_ROW_ADDR , 0x00 , self .height - 1 ,
104- SET_COL_ADDR , ((128 - self .width ) // 4 ), 63 - ((128 - self .width ) // 4 ),
105-
107+ SET_COL_ADDR , self .col_addr [0 ], self .col_addr [1 ],
108+ SET_ROW_ADDR , self .row_addr [0 ], self .row_addr [1 ],
106109 SET_SCROLL_DEACTIVATE ,
107110 SET_DISP | 0x01 ): # Display on
108111 self .write_cmd (cmd )
109112 self .fill (0 )
110- self .show ( )
113+ self .write_data ( self . buffer )
111114
112115 def poweroff (self ):
113116 self .write_cmd (SET_FN_SELECT_A )
@@ -123,16 +126,24 @@ def contrast(self, contrast):
123126 self .write_cmd (SET_CONTRAST )
124127 self .write_cmd (contrast ) # 0-255
125128
129+ def rotate (self , rotate ):
130+ self .poweroff ()
131+ self .write_cmd (SET_DISP_OFFSET )
132+ self .write_cmd (self .height if rotate else self .offset )
133+ self .write_cmd (SET_SEG_REMAP )
134+ self .write_cmd (0x42 if rotate else 0x51 )
135+ self .poweron ()
136+
126137 def invert (self , invert ):
127138 self .write_cmd (SET_DISP_MODE | (invert & 1 ) << 1 | (invert & 1 )) # 0xA4=Normal, 0xA7=Inverted
128139
129140 def show (self ):
130141 self .write_cmd (SET_COL_ADDR )
131- self .write_cmd (( 128 - self .width ) // 4 )
132- self .write_cmd (63 - (( 128 - self .width ) // 4 ) )
142+ self .write_cmd (self .col_addr [ 0 ] )
143+ self .write_cmd (self .col_addr [ 1 ] )
133144 self .write_cmd (SET_ROW_ADDR )
134- self .write_cmd (0x00 )
135- self .write_cmd (self .height - 1 )
145+ self .write_cmd (self . row_addr [ 0 ] )
146+ self .write_cmd (self .row_addr [ 1 ] )
136147 self .write_data (self .buffer )
137148
138149 def fill (self , col ):
@@ -141,44 +152,43 @@ def fill(self, col):
141152 def pixel (self , x , y , col ):
142153 self .framebuf .pixel (x , y , col )
143154
155+ def line (self , x1 , y1 , x2 , y2 , col ):
156+ self .framebuf .line (x1 , y1 , x2 , y2 , col )
157+
144158 def scroll (self , dx , dy ):
145159 self .framebuf .scroll (dx , dy )
146160 # software scroll
147161
148162 def text (self , string , x , y , col = 15 ):
149163 self .framebuf .text (string , x , y , col )
150164
165+ def write_cmd (self ):
166+ raise NotImplementedError
167+
168+ def write_data (self ):
169+ raise NotImplementedError
170+
151171
152172class SSD1327_I2C (SSD1327 ):
153- def __init__ (self , width , height , i2c , addr = 0x3c , external_vcc = False ):
173+ def __init__ (self , width , height , i2c , addr = 0x3c ):
154174 self .i2c = i2c
155175 self .addr = addr
156176 self .cmd_arr = bytearray ([REG_CMD , 0 ]) # Co=1, D/C#=0
157177 self .data_list = [bytes ((REG_DATA ,)), None ]
158- super ().__init__ (width , height , external_vcc )
178+ super ().__init__ (width , height )
159179
160180 def write_cmd (self , cmd ):
161181 self .cmd_arr [1 ] = cmd
162182 self .i2c .writeto (self .addr , self .cmd_arr )
163183
164- def write_data (self , buf ):
165- self .data_list [1 ] = buf
184+ def write_data (self , data_buf ):
185+ self .data_list [1 ] = data_buf
166186 self .i2c .writevto (self .addr , self .data_list )
167187
168188
169189class SEEED_OLED_96X96 (SSD1327_I2C ):
170190 def __init__ (self , i2c ):
171191 super ().__init__ (96 , 96 , i2c )
172- self .write_cmd (SET_DISP_OFFSET )
173- self .write_cmd (0x20 ) # Set vertical offset by COM from 0~127
174-
175- def rotate (self , rotate ):
176- self .poweroff ()
177- self .write_cmd (SET_DISP_OFFSET )
178- self .write_cmd (0x60 if rotate else 0x20 ) # 0x20=0 degrees, 0x60=180 degrees
179- self .write_cmd (SET_SEG_REMAP )
180- self .write_cmd (0x42 if rotate else 0x51 ) # 0x51=0 degrees, 0x42=180 degrees
181- self .poweron ()
182192
183193 def lookup (self , table ):
184194 # GS0 has no pre-charge and current drive
0 commit comments