pioneer600

Pioneer600 sample for headless raspberry pi servers
git clone git://git.ckyln.com/pioneer600.git
Log | Files | Refs | README

SSD1306.py (5636B)


      1 import spidev 
      2 import RPi.GPIO as GPIO
      3 import time
      4 
      5 # Constants
      6 SSD1306_SETCONTRAST = 0x81
      7 SSD1306_DISPLAYALLON_RESUME = 0xA4
      8 SSD1306_DISPLAYALLON = 0xA5
      9 SSD1306_NORMALDISPLAY = 0xA6
     10 SSD1306_INVERTDISPLAY = 0xA7
     11 SSD1306_DISPLAYOFF = 0xAE
     12 SSD1306_DISPLAYON = 0xAF
     13 SSD1306_SETDISPLAYOFFSET = 0xD3
     14 SSD1306_SETCOMPINS = 0xDA
     15 SSD1306_SETVCOMDETECT = 0xDB
     16 SSD1306_SETDISPLAYCLOCKDIV = 0xD5
     17 SSD1306_SETPRECHARGE = 0xD9
     18 SSD1306_SETMULTIPLEX = 0xA8
     19 SSD1306_SETLOWCOLUMN = 0x00
     20 SSD1306_SETHIGHCOLUMN = 0x10
     21 SSD1306_SETSTARTLINE = 0x40
     22 SSD1306_MEMORYMODE = 0x20
     23 SSD1306_COLUMNADDR = 0x21
     24 SSD1306_PAGEADDR = 0x22
     25 SSD1306_COMSCANINC = 0xC0
     26 SSD1306_COMSCANDEC = 0xC8
     27 SSD1306_SEGREMAP = 0xA0
     28 SSD1306_CHARGEPUMP = 0x8D
     29 SSD1306_EXTERNALVCC = 0x1
     30 SSD1306_SWITCHCAPVCC = 0x2
     31 
     32 # Scrolling constants
     33 SSD1306_ACTIVATE_SCROLL = 0x2F
     34 SSD1306_DEACTIVATE_SCROLL = 0x2E
     35 SSD1306_SET_VERTICAL_SCROLL_AREA = 0xA3
     36 SSD1306_RIGHT_HORIZONTAL_SCROLL = 0x26
     37 SSD1306_LEFT_HORIZONTAL_SCROLL = 0x27
     38 SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29
     39 SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A
     40 
     41 class SSD1306(object):
     42 	"""class for SSD1306  128*64 0.96inch OLED displays."""
     43 	
     44 	def __init__(self,rst,dc,spi):
     45 		self.width = 128
     46 		self.height = 64
     47 		self._pages = 8
     48 		self._buffer = [0]*(self.width*self._pages)
     49 		#Initialize DC RST pin
     50 		self._dc = dc
     51 		self._rst = rst
     52 		GPIO.setmode(GPIO.BCM)
     53 		GPIO.setwarnings(False)
     54 		GPIO.setup(self._dc,GPIO.OUT)
     55 		GPIO.setup(self._rst,GPIO.OUT)
     56 		#Initialize SPI
     57 		self._spi = spi
     58 	def command(self,cmd):
     59 		"""Send command byte to display"""
     60 		GPIO.output(self._dc,GPIO.LOW)
     61 		self._spi.writebytes([cmd])
     62 	def data(self,val):
     63 		"""Send byte of data to display"""
     64 		GPIO.output(self._dc,GPIO.HIGHT)
     65 		self._spi.writebytes([val])
     66 	def begin(self,vccstate=SSD1306_SWITCHCAPVCC):
     67 		"""Initialize dispaly"""
     68 		self._vccstate = vccstate
     69 		self.reset()
     70 		self.command(SSD1306_DISPLAYOFF)                    # 0xAE
     71 		self.command(SSD1306_SETDISPLAYCLOCKDIV)            # 0xD5
     72 		self.command(0x80)                     # the suggested ra    tio 0x80
     73 		
     74 		self.command(SSD1306_SETMULTIPLEX)                  # 0xA8
     75 		self.command(0x3F)
     76 		self.command(SSD1306_SETDISPLAYOFFSET)              # 0xD3
     77 		self.command(0x0)                                   # no offset
     78 		self.command(SSD1306_SETSTARTLINE | 0x0)            # line #0
     79 		self.command(SSD1306_CHARGEPUMP)                    # 0x8D
     80 		if self._vccstate == SSD1306_EXTERNALVCC:
     81 			self.command(0x10)
     82 		else:
     83 			self.command(0x14)
     84 		self.command(SSD1306_MEMORYMODE)                    # 0x20
     85 		self.command(0x00)                            # 0x0 act like ks0108        
     86 		self.command(SSD1306_SEGREMAP | 0x1)
     87 		self.command(SSD1306_COMSCANDEC)
     88 		self.command(SSD1306_SETCOMPINS)                    # 0xDA
     89 		self.command(0x12)
     90 		self.command(SSD1306_SETCONTRAST)                   # 0x81
     91 		if self._vccstate == SSD1306_EXTERNALVCC:
     92 			self.command(0x9F)
     93 		else:
     94 			self.command(0xCF)
     95 		self.command(SSD1306_SETPRECHARGE)                  # 0xd9
     96 		if self._vccstate == SSD1306_EXTERNALVCC:
     97 			self.command(0x22)
     98 		else:
     99 			self.command(0xF1)
    100 		self.command(SSD1306_SETVCOMDETECT)                 # 0xDB
    101 		self.command(0x40)
    102 		self.command(SSD1306_DISPLAYALLON_RESUME)           # 0xA4
    103 		self.command(SSD1306_NORMALDISPLAY)                 # 0xA6
    104 		self.command(SSD1306_DISPLAYON)
    105 	def reset(self):
    106 		"""Reset the display"""
    107 		GPIO.output(self._rst,GPIO.HIGH)
    108 		time.sleep(0.001)
    109 		GPIO.output(self._rst,GPIO.LOW)
    110 		time.sleep(0.010)
    111 		GPIO.output(self._rst,GPIO.HIGH)
    112 	def display(self):
    113 		"""Write display buffer to physical display"""
    114 		self.command(SSD1306_COLUMNADDR)
    115 		self.command(0)                  #Cloumn start address
    116 		self.command(self.width-1)     #Cloumn end address
    117 		self.command(SSD1306_PAGEADDR)
    118 		self.command(0)					 #Page start address
    119 		self.command(self._pages-1)		 #Page end address
    120 		#Write buffer data
    121 		GPIO.output(self._dc,GPIO.HIGH)
    122 		self._spi.writebytes(self._buffer)
    123 	def image(self, image):
    124 		"""Set buffer to value of Python Imaging Library image."""
    125 		if image.mode != '1':
    126 			raise ValueError('Image must be in mode 1.')
    127 		imwidth, imheight = image.size
    128 		if imwidth != self.width or imheight != self.height:
    129 			raise ValueError('Image must be same dimensions as display \
    130 				({0}x{1}).' .format(self.width, self.height))
    131 
    132 		pix = image.load()
    133 		# Iterate through the memory pages
    134 		index = 0
    135 		for page in range(self._pages):
    136 			# Iterate through all x axis columns.
    137 			for x in range(self.width):
    138 			# Set the bits for the column of pixels at the current position.
    139 				bits = 0
    140 				# Don't use range here as it's a bit slow
    141 				for bit in [0, 1, 2, 3, 4, 5, 6, 7]:
    142 					bits = bits << 1
    143 					bits |= 0 if pix[(x, page*8+7-bit)] == 0 else 1
    144 				# Update buffer byte and increment to next byte.
    145 				self._buffer[index] = bits
    146 				index += 1
    147 	def clear(self):
    148 		"""Clear contents of image buffer"""
    149 		self._buffer = [0]*(self.width*self._pages)
    150 	def set_contrast(self, contrast):
    151 		"""Sets the contrast of the display.
    152 		Contrast should be a value between 0 and 255."""
    153 		if contrast < 0 or contrast > 255:
    154 			raise ValueError('Contrast must be a value from 0 to 255).')
    155 		self.command(SSD1306_SETCONTRAST)
    156 		self.command(contrast)
    157 
    158 	def dim(self, dim):
    159 		"""Adjusts contrast to dim the display if dim is True, 
    160 		otherwise sets the contrast to normal brightness if dim is False."""
    161         # Assume dim display.
    162         contrast = 0
    163         # Adjust contrast based on VCC if not dimming.
    164         if not dim:
    165             if self._vccstate == SSD1306_EXTERNALVCC:
    166                 contrast = 0x9F
    167             else:
    168                 contrast = 0xCF
    169 
    170 
    171 
    172