diff --git a/04B_08__.TTF b/04B_08__.TTF new file mode 100644 index 0000000..aadf20e Binary files /dev/null and b/04B_08__.TTF differ diff --git a/drive/SSD1305.py b/drive/SSD1305.py new file mode 100644 index 0000000..068381e --- /dev/null +++ b/drive/SSD1305.py @@ -0,0 +1,173 @@ +#coding=utf-8 +from . import config +import time + +Device_SPI = config.Device_SPI +Device_I2C = config.Device_I2C + +OLED_WIDTH = 128 #OLED width +OLED_HEIGHT = 32 #OLED height + +#******Rolling Direction****** +VERTICAL = True +HORIZONTAL = False + +# Scrolling constants +SSD1305_ACTIVATE_SCROLL = 0x2F +SSD1305_DEACTIVATE_SCROLL = 0x2E +SSD1305_SET_VERTICAL_SCROLL_AREA = 0xA3 +SSD1305_RIGHT_HORIZONTAL_SCROLL = 0x26 +SSD1305_LEFT_HORIZONTAL_SCROLL = 0x27 +SSD1305_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = 0x29 +SSD1305_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = 0x2A + +class SSD1305(object): + def __init__(self): + # Call base class constructor. + self.width = OLED_WIDTH + self.height = OLED_HEIGHT + self._pages = self.height // 8 + self._buffer = [0]*(self.width*self._pages) + #Initialize DC RST pin + self.RPI = config.RaspberryPi() + self._dc = self.RPI.GPIO_DC_PIN + self._rst = self.RPI.GPIO_RST_PIN + self.Device = self.RPI.Device + + def command(self, cmd): + if(self.Device == Device_SPI): + self.RPI.digital_write(self._dc,False) + self.RPI.spi_writebyte([cmd]) + else: + self.RPI.i2c_writebyte(0x00, cmd) + + + def Init(self): + if (self.RPI.module_init() != 0): + return -1 + """Initialize dispaly""" + self.reset() + + # 128x32 pixel specific initialization. + self.command(0xAE)#--turn off oled panel + self.command(0x04)#--Set Lower Column Start Address for Page Addressing Mode + self.command(0x10)#--Set Higher Column Start Address for Page Addressing Mode + self.command(0x40)#--Set Display Start Line + self.command(0x81)#--Set Contrast Control for BANK0 + self.command(0x80)#--Contrast control register is set + self.command(0xA1)#--Set Segment Re-map + self.command(0xA6)#--Set Normal/Inverse Display + self.command(0xA8)#--Set Multiplex Ratio + self.command(0x1F) + self.command(0xC8)#--Set COM Output Scan Direction + self.command(0xD3)#--Set Display Offset + self.command(0x00) + self.command(0xD5)#--Set Display Clock Divide Ratio/ Oscillator Frequency + self.command(0xF0) + self.command(0xD8)#--Set Area Color Mode ON/OFF & Low Power Display Mode + self.command(0x05) + self.command(0xD9)#--Set pre-charge period + self.command(0xC2) + self.command(0xDA)#--Set COM Pins Hardware Configuration + self.command(0x12) + self.command(0xDB)#--Set VCOMH Deselect Level + self.command(0x08)#--Set VCOM Deselect Level + self.command(0xAF)#--Normal Brightness Display ON + + def reset(self): + """Reset the display""" + self.RPI.digital_write(self._rst,True) + time.sleep(0.1) + self.RPI.digital_write(self._rst,False) + time.sleep(0.1) + self.RPI.digital_write(self._rst,True) + time.sleep(0.1) + + def getbuffer(self, image): + """Set buffer to value of Python Imaging Library image. The image should + be in 1 bit mode and a size equal to the display size. + """ + if image.mode != '1': + raise ValueError('Image must be in mode 1.') + imwidth, imheight = image.size + if imwidth != self.width or imheight != self.height: + raise ValueError('Image must be same dimensions as display ({0}x{1}).' \ + .format(self.width, self.height)) + # Grab all the pixels from the image, faster than getpixel. + pix = image.load() + # Iterate through the memory pages + index = 0 + for page in range(self._pages): + # Iterate through all x axis columns. + for x in range(self.width): + # Set the bits for the column of pixels at the current position. + bits = 0 + # Don't use range here as it's a bit slow + for bit in [0, 1, 2, 3, 4, 5, 6, 7]: + bits = bits << 1 + bits |= 0 if pix[(x, page*8+7-bit)] == 0 else 1 + # Update buffer byte and increment to next byte. + self._buffer[index] = bits + index += 1 + + + def ShowImage(self): + for page in range(0,self._pages): + # set page address # + self.command(0xB0 + page) + # set low column address # + self.command(0x04); + # set high column address # + self.command(0x10); + # write data # + # time.sleep(0.01) + if(self.Device == Device_SPI): + self.RPI.digital_write(self._dc,True) + for i in range(0,self.width): + if(self.Device == Device_SPI): + # self.RPI.digital_write(self._dc,True) + self.RPI.spi_writebyte([self._buffer[i+self.width*page]]); + else : + self.RPI.i2c_writebyte(0x40, self._buffer[i+self.width*page]) + + def clear(self): + """Clear contents of image buffer""" + _buffer = [0xff]*(self.width * self.height//8) + self.ShowImage() + + def SSD1305_Scrolling_Set(self): + self.command(SSD1305_DEACTIVATE_SCROLL) + if HORIZONTAL: + self.command(SSD1305_LEFT_HORIZONTAL_SCROLL) + self.command(0x01)#Set number of column scroll offset + self.command(0x00)#Define start page address + self.command(0x00)#Set time interval between each scroll step in terms of frame frequency + self.command(0x07) #Define end page address + elif VERTICAL: + self.command(SSD1305_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL) + self.command(0x00)#Set number of column scroll offset + self.command(0x00)#Define start page address + self.command(0x00)#Set time interval between each scroll step in terms of frame frequency + self.command(0x07)#Define end page address + self.command(0x01)#Vertical scrolling offset + else: + self.command(SSD1305_LEFT_HORIZONTAL_SCROLL) + self.command(0x00) #Set number of column scroll offset + self.command(0x00) #Define start page address + self.command(0x00) #Set time interval between each scroll step in terms of frame frequency + self.command(0x07) #Define end page address + + def SSD1305_Scrolling_Start(self): + self.command(SSD1305_ACTIVATE_SCROLL) + + + + + + + + + + + + diff --git a/drive/config.py b/drive/config.py new file mode 100644 index 0000000..ad2b499 --- /dev/null +++ b/drive/config.py @@ -0,0 +1,108 @@ +# /***************************************************************************** +# * | File : config.py +# * | Author : Waveshare team +# * | Function : Hardware underlying interface,for Raspberry pi +# * | Info : +# *---------------- +# * | This version: V1.0 +# * | Date : 2020-06-17 +# * | Info : +# ******************************************************************************/ +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documnetation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# + + +import time +from smbus import SMBus +import spidev +import ctypes +from gpiozero import * + +# Pin definition +RST_PIN = 25 +DC_PIN = 24 + +Device_SPI = 1 +Device_I2C = 0 + +class RaspberryPi: + def __init__(self,spi=spidev.SpiDev(0,0),spi_freq=40000000,rst = 27,dc = 25,bl = 18,bl_freq=1000,i2c=None): + self.INPUT = False + self.OUTPUT = True + + if(Device_SPI == 1): + self.Device = Device_SPI + self.spi = spi + else : + self.Device = Device_I2C + self.address = 0x3c + self.bus = SMBus(1) + + self.GPIO_RST_PIN = self.gpio_mode(RST_PIN,self.OUTPUT) + self.GPIO_DC_PIN = self.gpio_mode(DC_PIN,self.OUTPUT) + + + def delay_ms(self,delaytime): + time.sleep(delaytime / 1000.0) + + def gpio_mode(self,Pin,Mode): + if Mode: + return DigitalOutputDevice(Pin,active_high = True,initial_value =False) + else: + return DigitalInputDevice(Pin,pull_up=None,active_state=True) + + def gpio_pwm(self,Pin): + return PWMOutputDevice(Pin,frequency = 10000) + + def set_pwm_Duty_cycle(self,Pin,value): + Pin.value = value + + def digital_write(self, Pin, value): + if value: + Pin.on() + else: + Pin.off() + + def digital_read(self, Pin): + return Pin.value + + def spi_writebyte(self,data): + self.spi.writebytes([data[0]]) + + def i2c_writebyte(self,reg, value): + self.bus.write_byte_data(self.address, reg, value) + + def module_init(self): + self.digital_write(self.GPIO_RST_PIN,False) + if(self.Device == Device_SPI): + self.spi.max_speed_hz = 1000000 + self.spi.mode = 0b11 + # CS_PIN.off() + self.digital_write(self.GPIO_DC_PIN,False) + return 0 + + def module_exit(self): + if(self.Device == Device_SPI): + self.spi.close() + else : + self.bus.close() + self.digital_write(self.GPIO_RST_PIN,False) + self.digital_write(self.GPIO_DC_PIN,False) + +### END OF FILE ###