<s> from __future__ import division <EOL> import logging <EOL> import time <EOL> import Adafruit_GPIO as GPIO <EOL> import Adafruit_GPIO . SPI as SPI <EOL> SSD1306_I2C_ADDRESS = <NUM_LIT> <EOL> SSD1306_SETCONTRAST = <NUM_LIT> <EOL> SSD1306_DISPLAYALLON_RESUME = <NUM_LIT> <EOL> SSD1306_DISPLAYALLON = <NUM_LIT> <EOL> SSD1306_NORMALDISPLAY = <NUM_LIT> <EOL> SSD1306_INVERTDISPLAY = <NUM_LIT> <EOL> SSD1306_DISPLAYOFF = <NUM_LIT> <EOL> SSD1306_DISPLAYON = <NUM_LIT> <EOL> SSD1306_SETDISPLAYOFFSET = <NUM_LIT> <EOL> SSD1306_SETCOMPINS = <NUM_LIT> <EOL> SSD1306_SETVCOMDETECT = <NUM_LIT> <EOL> SSD1306_SETDISPLAYCLOCKDIV = <NUM_LIT> <EOL> SSD1306_SETPRECHARGE = <NUM_LIT> <EOL> SSD1306_SETMULTIPLEX = <NUM_LIT> <EOL> SSD1306_SETLOWCOLUMN = <NUM_LIT> <EOL> SSD1306_SETHIGHCOLUMN = <NUM_LIT> <EOL> SSD1306_SETSTARTLINE = <NUM_LIT> <EOL> SSD1306_MEMORYMODE = <NUM_LIT> <EOL> SSD1306_COLUMNADDR = <NUM_LIT> <EOL> SSD1306_PAGEADDR = <NUM_LIT> <EOL> SSD1306_COMSCANINC = <NUM_LIT> <EOL> SSD1306_COMSCANDEC = <NUM_LIT> <EOL> SSD1306_SEGREMAP = <NUM_LIT> <EOL> SSD1306_CHARGEPUMP = <NUM_LIT> <EOL> SSD1306_EXTERNALVCC = <NUM_LIT> <EOL> SSD1306_SWITCHCAPVCC = <NUM_LIT> <EOL> SSD1306_ACTIVATE_SCROLL = <NUM_LIT> <EOL> SSD1306_DEACTIVATE_SCROLL = <NUM_LIT> <EOL> SSD1306_SET_VERTICAL_SCROLL_AREA = <NUM_LIT> <EOL> SSD1306_RIGHT_HORIZONTAL_SCROLL = <NUM_LIT> <EOL> SSD1306_LEFT_HORIZONTAL_SCROLL = <NUM_LIT> <EOL> SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = <NUM_LIT> <EOL> SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = <NUM_LIT> <EOL> class SSD1306Base ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , width , height , rst , dc = None , sclk = None , din = None , cs = None , <EOL> gpio = None , spi = None , i2c_bus = None , i2c_address = SSD1306_I2C_ADDRESS , <EOL> i2c = None ) : <EOL> self . _log = logging . getLogger ( '<STR_LIT>' ) <EOL> self . _spi = None <EOL> self . _i2c = None <EOL> self . width = width <EOL> self . height = height <EOL> self . _pages = height // <NUM_LIT:8> <EOL> self . _buffer = [ <NUM_LIT:0> ] * ( width * self . _pages ) <EOL> self . _gpio = gpio <EOL> if self . _gpio is None : <EOL> self . _gpio = GPIO . get_platform_gpio ( ) <EOL> self . _rst = rst <EOL> self . _gpio . setup ( self . _rst , GPIO . OUT ) <EOL> if spi is not None : <EOL> self . _log . debug ( '<STR_LIT>' ) <EOL> self . _spi = spi <EOL> self . _spi . set_clock_hz ( <NUM_LIT> ) <EOL> elif sclk is not None and din is not None and cs is not None : <EOL> self . _log . debug ( '<STR_LIT>' ) <EOL> self . _spi = SPI . BitBang ( self . _gpio , sclk , din , None , cs ) <EOL> elif i2c is not None : <EOL> self . _log . debug ( '<STR_LIT>' ) <EOL> self . _i2c = i2c . get_i2c_device ( i2c_address ) <EOL> else : <EOL> self . _log . debug ( '<STR_LIT>' ) <EOL> import Adafruit_GPIO . I2C as I2C <EOL> if i2c_bus is None : <EOL> self . _i2c = I2C . get_i2c_device ( i2c_address ) <EOL> else : <EOL> self . _i2c = I2C . get_i2c_device ( i2c_address , busnum = i2c_bus ) <EOL> if self . _spi is not None : <EOL> if dc is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _dc = dc <EOL> self . _gpio . setup ( self . _dc , GPIO . OUT ) <EOL> def _initialize ( self ) : <EOL> raise NotImplementedError <EOL> def command ( self , c ) : <EOL> """<STR_LIT>""" <EOL> if self . _spi is not None : <EOL> self . _gpio . set_low ( self . _dc ) <EOL> self . _spi . write ( [ c ] ) <EOL> else : <EOL> control = <NUM_LIT> <EOL> self . _i2c . write8 ( control , c ) <EOL> def data ( self , c ) : <EOL> """<STR_LIT>""" <EOL> if self . _spi is not None : <EOL> self . _gpio . set_high ( self . _dc ) <EOL> self . _spi . write ( [ c ] ) <EOL> else : <EOL> control = <NUM_LIT> <EOL> self . _i2c . write8 ( control , c ) <EOL> def begin ( self , vccstate = SSD1306_SWITCHCAPVCC ) : <EOL> """<STR_LIT>""" <EOL> self . _vccstate = vccstate <EOL> self . reset ( ) <EOL> self . _initialize ( ) <EOL> self . command ( SSD1306_DISPLAYON ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _gpio . set_high ( self . _rst ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . _gpio . set_low ( self . _rst ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . _gpio . set_high ( self . _rst ) <EOL> def display ( self ) : <EOL> """<STR_LIT>""" <EOL> self . command ( SSD1306_COLUMNADDR ) <EOL> self . command ( <NUM_LIT:0> ) <EOL> self . command ( self . width - <NUM_LIT:1> ) <EOL> self . command ( SSD1306_PAGEADDR ) <EOL> self . command ( <NUM_LIT:0> ) <EOL> self . command ( self . _pages - <NUM_LIT:1> ) <EOL> if self . _spi is not None : <EOL> self . _gpio . set_high ( self . _dc ) <EOL> self . _spi . write ( self . _buffer ) <EOL> else : <EOL> for i in range ( <NUM_LIT:0> , len ( self . _buffer ) , <NUM_LIT:16> ) : <EOL> control = <NUM_LIT> <EOL> self . _i2c . writeList ( control , self . _buffer [ i : i + <NUM_LIT:16> ] ) <EOL> def image ( self , image ) : <EOL> """<STR_LIT>""" <EOL> if image . mode != '<STR_LIT:1>' : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> imwidth , imheight = image . size <EOL> if imwidth != self . width or imheight != self . height : <EOL> raise ValueError ( '<STR_LIT>' . format ( self . width , self . height ) ) <EOL> pix = image . load ( ) <EOL> index = <NUM_LIT:0> <EOL> for page in range ( self . _pages ) : <EOL> for x in range ( self . width ) : <EOL> bits = <NUM_LIT:0> <EOL> for bit in [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ] : <EOL> bits = bits << <NUM_LIT:1> <EOL> bits |= <NUM_LIT:0> if pix [ ( x , page * <NUM_LIT:8> + <NUM_LIT:7> - bit ) ] == <NUM_LIT:0> else <NUM_LIT:1> <EOL> self . _buffer [ index ] = bits <EOL> index += <NUM_LIT:1> <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _buffer = [ <NUM_LIT:0> ] * ( self . width * self . _pages ) <EOL> def set_contrast ( self , contrast ) : <EOL> """<STR_LIT>""" <EOL> if contrast < <NUM_LIT:0> or contrast > <NUM_LIT:255> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . command ( SSD1306_SETCONTRAST ) <EOL> self . command ( contrast ) <EOL> def dim ( self , dim ) : <EOL> """<STR_LIT>""" <EOL> contrast = <NUM_LIT:0> <EOL> if not dim : <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> contrast = <NUM_LIT> <EOL> else : <EOL> contrast = <NUM_LIT> <EOL> class SSD1306_128_64 ( SSD1306Base ) : <EOL> def __init__ ( self , rst , dc = None , sclk = None , din = None , cs = None , gpio = None , <EOL> spi = None , i2c_bus = None , i2c_address = SSD1306_I2C_ADDRESS , <EOL> i2c = None ) : <EOL> super ( SSD1306_128_64 , self ) . __init__ ( <NUM_LIT> , <NUM_LIT:64> , rst , dc , sclk , din , cs , <EOL> gpio , spi , i2c_bus , i2c_address , i2c ) <EOL> def _initialize ( self ) : <EOL> self . command ( SSD1306_DISPLAYOFF ) <EOL> self . command ( SSD1306_SETDISPLAYCLOCKDIV ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETMULTIPLEX ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETDISPLAYOFFSET ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETSTARTLINE | <NUM_LIT> ) <EOL> self . command ( SSD1306_CHARGEPUMP ) <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> self . command ( <NUM_LIT> ) <EOL> else : <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_MEMORYMODE ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SEGREMAP | <NUM_LIT> ) <EOL> self . command ( SSD1306_COMSCANDEC ) <EOL> self . command ( SSD1306_SETCOMPINS ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETCONTRAST ) <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> self . command ( <NUM_LIT> ) <EOL> else : <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETPRECHARGE ) <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> self . command ( <NUM_LIT> ) <EOL> else : <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETVCOMDETECT ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_DISPLAYALLON_RESUME ) <EOL> self . command ( SSD1306_NORMALDISPLAY ) <EOL> class SSD1306_128_32 ( SSD1306Base ) : <EOL> def __init__ ( self , rst , dc = None , sclk = None , din = None , cs = None , gpio = None , <EOL> spi = None , i2c_bus = None , i2c_address = SSD1306_I2C_ADDRESS , <EOL> i2c = None ) : <EOL> super ( SSD1306_128_32 , self ) . __init__ ( <NUM_LIT> , <NUM_LIT:32> , rst , dc , sclk , din , cs , <EOL> gpio , spi , i2c_bus , i2c_address , i2c ) <EOL> def _initialize ( self ) : <EOL> self . command ( SSD1306_DISPLAYOFF ) <EOL> self . command ( SSD1306_SETDISPLAYCLOCKDIV ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETMULTIPLEX ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETDISPLAYOFFSET ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETSTARTLINE | <NUM_LIT> ) <EOL> self . command ( SSD1306_CHARGEPUMP ) <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> self . command ( <NUM_LIT> ) <EOL> else : <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_MEMORYMODE ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SEGREMAP | <NUM_LIT> ) <EOL> self . command ( SSD1306_COMSCANDEC ) <EOL> self . command ( SSD1306_SETCOMPINS ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETCONTRAST ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETPRECHARGE ) <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> self . command ( <NUM_LIT> ) <EOL> else : <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETVCOMDETECT ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_DISPLAYALLON_RESUME ) <EOL> self . command ( SSD1306_NORMALDISPLAY ) <EOL> class SSD1306_96_16 ( SSD1306Base ) : <EOL> def __init__ ( self , rst , dc = None , sclk = None , din = None , cs = None , gpio = None , <EOL> spi = None , i2c_bus = None , i2c_address = SSD1306_I2C_ADDRESS , <EOL> i2c = None ) : <EOL> super ( SSD1306_96_16 , self ) . __init__ ( <NUM_LIT> , <NUM_LIT:16> , rst , dc , sclk , din , cs , <EOL> gpio , spi , i2c_bus , i2c_address , i2c ) <EOL> def _initialize ( self ) : <EOL> self . command ( SSD1306_DISPLAYOFF ) <EOL> self . command ( SSD1306_SETDISPLAYCLOCKDIV ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETMULTIPLEX ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETDISPLAYOFFSET ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETSTARTLINE | <NUM_LIT> ) <EOL> self . command ( SSD1306_CHARGEPUMP ) <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> self . command ( <NUM_LIT> ) <EOL> else : <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_MEMORYMODE ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SEGREMAP | <NUM_LIT> ) <EOL> self . command ( SSD1306_COMSCANDEC ) <EOL> self . command ( SSD1306_SETCOMPINS ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETCONTRAST ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETPRECHARGE ) <EOL> if self . _vccstate == SSD1306_EXTERNALVCC : <EOL> self . command ( <NUM_LIT> ) <EOL> else : <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_SETVCOMDETECT ) <EOL> self . command ( <NUM_LIT> ) <EOL> self . command ( SSD1306_DISPLAYALLON_RESUME ) <EOL> self . command ( SSD1306_NORMALDISPLAY ) </s>
