Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
381 lines (368 sloc) 11.8 KB
# A port of SeeedStudio's Library for the 96x96 OLED Display
# http://www.seeedstudio.com/depot/Grove-OLED-Display-096-p-824.html
# http://www.seeedstudio.com/wiki/Grove_-_OLED_Display_96*96
# https://github.com/Seeed-Studio/OLED_Display_96X96
import pyb
import time
from pyb import I2C
i2c = I2C(1, I2C.MASTER)
i2c.init(I2C.MASTER, baudrate=400000)
BasicFont = [
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00],
[0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00],
[0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00],
[0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00],
[0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00],
[0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00],
[0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00],
[0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00],
[0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00],
[0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00],
[0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00],
[0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00],
[0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00],
[0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00],
[0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00],
[0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00],
[0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00],
[0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00],
[0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00],
[0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00],
[0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00],
[0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00],
[0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00],
[0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00],
[0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00],
[0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00],
[0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00],
[0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00],
[0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00],
[0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00],
[0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00],
[0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00],
[0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00],
[0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00],
[0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00],
[0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00],
[0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00],
[0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00],
[0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00],
[0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00],
[0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00],
[0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00],
[0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00],
[0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00],
[0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00],
[0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00],
[0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00],
[0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00],
[0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00],
[0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00],
[0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00],
[0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00],
[0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00],
[0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00],
[0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00],
[0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00],
[0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00],
[0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00],
[0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00],
[0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00],
[0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00],
[0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00],
[0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00],
[0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00],
[0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00],
[0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00],
[0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00],
[0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00],
[0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00],
[0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00],
[0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00],
[0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00],
[0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00],
[0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00],
[0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00],
[0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00],
[0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00],
[0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00],
[0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00],
[0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00],
[0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00],
[0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00],
[0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00],
[0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00],
[0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00],
[0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00],
[0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00],
[0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00],
[0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00],
[0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00],
[0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00],
[0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00],
[0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00],
[0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00]
]
VERTICAL_MODE=01
HORIZONTAL_MODE=02
SeeedGrayOLED_Address=0x3c
SeeedGrayOLED_Command_Mode=0x80
SeeedGrayOLED_Data_Mode=0x40
SeeedGrayOLED_Display_Off_Cmd=0xAE
SeeedGrayOLED_Display_On_Cmd=0xAF
SeeedGrayOLED_Normal_Display_Cmd=0xA4
SeeedGrayOLED_Inverse_Display_Cmd=0xA7
SeeedGrayOLED_Activate_Scroll_Cmd=0x2F
SeeedGrayOLED_Dectivate_Scroll_Cmd=0x2E
SeeedGrayOLED_Set_ContrastLevel_Cmd=0x81
Scroll_Left=0x00
Scroll_Right=0x01
Scroll_2Frames=0x7
Scroll_3Frames=0x4
Scroll_4Frames=0x5
Scroll_5Frames=0x0
Scroll_25Frames=0x6
Scroll_64Frames=0x1
Scroll_128Frames=0x2
Scroll_256Frames=0x3
class SeeedGrayOLED():
grayH=0xF0
grayL=0x0F
def __init__(self):
self.sendCommand(0xFD)
# Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands
self.sendCommand(0x12);
self.sendCommand(0xAE)
# Set display off
self.sendCommand(0xA8)
# set multiplex ratio
self.sendCommand(0x5F)
# 96
self.sendCommand(0xA1)
# set display start line
self.sendCommand(0x00);
self.sendCommand(0xA2)
# set display offset
self.sendCommand(0x60);
self.sendCommand(0xA0)
# set remap
self.sendCommand(0x46);
self.sendCommand(0xAB)
# set vdd internal
self.sendCommand(0x01)
#
self.sendCommand(0x81)
# set contrasr
self.sendCommand(0x53)
# 100 nit
self.sendCommand(0xB1)
# Set Phase Length
self.sendCommand(0X51)
#
self.sendCommand(0xB3)
# Set Display Clock Divide Ratio/Oscillator Frequency
self.sendCommand(0x01);
self.sendCommand(0xB9)
#
self.sendCommand(0xBC)
# set pre_charge voltage/VCOMH
self.sendCommand(0x08)
# (0x08);
self.sendCommand(0xBE)
# set VCOMH
self.sendCommand(0X07)
# (0x07);
self.sendCommand(0xB6)
# Set second pre-charge period
self.sendCommand(0x01)
#
self.sendCommand(0xD5)
# enable second precharge and enternal vsl
self.sendCommand(0X62)
# (0x62);
self.sendCommand(0xA4)
# Set Normal Display Mode
self.sendCommand(0x2E)
# Deactivate Scroll
self.sendCommand(0xAF)
# Switch on display
time.sleep(.100)
self.sendCommand(0x75)
#Set Row Address
self.sendCommand(0x00)
#Start 0
self.sendCommand(0x5f)
#End 95
self.sendCommand(0x15)
#Set Column Address
self.sendCommand(0x08)
#Start from 8th Column of driver IC. This is 0th Column for OLED
self.sendCommand(0x37)
#End at (8 + 47)th column. Each Column has 2 pixels(segments)
#Init gray level for text. Default:Brightest White
def sendCommand(self, command):
data = bytearray(2)
data[0]=SeeedGrayOLED_Command_Mode
data[1]=command
i2c.send(data, addr=SeeedGrayOLED_Address)
def setContrastLevel(self, ContrastLevel):
self.sendCommand(SeeedGrayOLED_Set_ContrastLevel_Cmd)
self.sendCommand(ContrastLevel)
def setHorizontalMode(self):
self.sendCommand(0xA0)
# remap to
self.sendCommand(0x42)
# horizontal mode
#Row Address
self.sendCommand(0x75)
# Set Row Address
self.sendCommand(0x00)
# Start 0
self.sendCommand(0x5f)
# End 95
#Column Address
self.sendCommand(0x15)
# Set Column Address
self.sendCommand(0x08)
# Start from 8th Column of driver IC. This is 0th Column for OLED
self.sendCommand(0x37)
# End at (8 + 47)th column. Each Column has 2 pixels(or segments)
def setVerticalMode(self):
self.sendCommand(0xA0)
#remap to Vertical mode
self.sendCommand(0x46)
def setTextXY(self, Row, Column):
#Column Address
self.sendCommand(0x15)
#Set Column Address
self.sendCommand(0x08+(Column*4))
#Start Column: Start from 8
self.sendCommand(0x37)
#End Column
#Row Address
self.sendCommand(0x75)
#Set Row Address
self.sendCommand(0x00+(Row*8))
#Start Row
self.sendCommand(0x07+(Row*8))
#End Row
def clearDisplay(self):
for j in range(0, 48):
for i in range(0, 96):
#clear all columns
self.sendData([0x00])
def sendData(self, toSend):
ln=len(toSend)
data = bytearray(ln+1)
data[0]=SeeedGrayOLED_Data_Mode
for i in range(0, ln):
data[i+1]=toSend[i]
i2c.send(data, addr=SeeedGrayOLED_Address)
def setGrayLevel(self, grayLevel):
self.grayH = (grayLevel << 4) & 0xF0
self.grayL = grayLevel & 0x0F
def putChar(self, C):
if ord(C) < 32 or ord(C) > 127:
#Ignore non-printable ASCII characters. This can be modified for multilingual font.
C=' '
D=ord(C)
for i in [0, 2, 4, 6]:
for j in range(0, 8):
#Character is constructed two pixel at a time using vertical mode from the default 8x8 font
c=0x00
bit1=(BasicFont[D-32][i] >> j) & 0x01
bit2=(BasicFont[D-32][i+1] >> j) & 0x01
#Each bit is changed to a nibble
if(bit1>0):
c|=self.grayH
if(bit2>0):
c|=self.grayL
self.sendData([c])
def putString(self, s):
for i in s:
self.putChar(i)
def putNumber(self, long_num):
char_buffer=[0]*10
i = 0
f = 0
if long_num < 0:
f=1
self.putChar('-')
long_num = -long_num
elif long_num == 0:
f=1
self.putChar('0')
return f
while (long_num > 0):
char_buffer[i] = long_num % 10
i+=1
long_num /= 10
f=f+i
while(i>0):
self.putChar(48+char_buffer[i - 1])
i-=1
return f
def setNormalDisplay(self):
self.sendCommand(SeeedGrayOLED_Normal_Display_Cmd)
def setInverseDisplay(self):
self.sendCommand(SeeedGrayOLED_Inverse_Display_Cmd)
def activateScroll(self):
self.sendCommand(SeeedGrayOLED_Activate_Scroll_Cmd)
def deactivateScroll(self):
self.sendCommand(SeeedGrayOLED_Dectivate_Scroll_Cmd)
def drawBitmap(self, bitmaparray, bytes):
localAddressMode = addressingMode
if(localAddressMode != HORIZONTAL_MODE):
#Bitmap is drawn in horizontal mode
setHorizontalMode()
for i in range (0, bytes):
for j in [0, 2, 4, 6]:
c=0x00
bit1=bitmaparray[i] << j & 0x80
bit2=bitmaparray[i] << (j+1) & 0x80
#Each bit is changed to a nibble
if(bit1>0):
c|=self.grayH
if(bit2>0):
c|=self.grayL
self.sendData(c)
if(localAddressMode == VERTICAL_MODE):
#If Vertical Mode was used earlier, restore it.
setVerticalMode()
def setHorizontalScrollProperties(direction, startRow, endRow, startColumn, endColumn, scrollSpeed):
#Use the following defines for 'direction':
# Scroll_Left
# Scroll_Right
#Use the following defines for 'scrollSpeed':
# Scroll_2Frames
# Scroll_3Frames
# Scroll_4Frames
# Scroll_5Frames
# Scroll_25Frames
# Scroll_64Frames
# Scroll_128Frames
# Scroll_256Frames
if(Scroll_Right == direction):
self.sendCommand(0x27)
else:
self.sendCommand(0x26)
#Dummmy byte
self.sendCommand(0x00)
self.sendCommand(startRow)
self.sendCommand(scrollSpeed)
self.sendCommand(endRow)
self.sendCommand(startColumn+8)
self.sendCommand(endColumn+8)
#Dummmy byte
self.sendCommand(0x00)
OLED=SeeedGrayOLED()
#Initialize Object
OLED.clearDisplay()
OLED.setNormalDisplay()
OLED.setVerticalMode()
for i in range(0, 12):
OLED.setTextXY(i,0)
OLED.setGrayLevel(i)
OLED.putString("Hello World")
You can’t perform that action at this time.