Difference between revisions of "SSD1306"
Jump to navigation
Jump to search
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| + | = Samples = | ||
| + | [[File:DSC00046.JPG|400px]] | ||
| + | [[File:DSC00048.JPG|400px]] | ||
| + | [[File:DSC00040.JPG|400px]] | ||
| + | |||
= Wiring = | = Wiring = | ||
| + | see also {{Link|target=i2c bus}} | ||
* https://learn.adafruit.com/monochrome-oled-breakouts/python-wiring | * https://learn.adafruit.com/monochrome-oled-breakouts/python-wiring | ||
<pre> | <pre> | ||
| Line 31: | Line 37: | ||
== show.py == | == show.py == | ||
<source lang='python'> | <source lang='python'> | ||
| + | # Basic example of clearing and drawing pixels on a SSD1306 OLED display. | ||
| + | # This example and library is meant to work with Adafruit CircuitPython API. | ||
| + | # Author: Tony DiCola | ||
| + | # Author: Wolfgang Fahl | ||
| + | # License: Public Domain | ||
| + | |||
# Import all board pins. | # Import all board pins. | ||
from board import SCL, SDA | from board import SCL, SDA | ||
| Line 37: | Line 49: | ||
# Import the SSD1306 module. | # Import the SSD1306 module. | ||
import adafruit_ssd1306 | import adafruit_ssd1306 | ||
| + | # time helpers | ||
import time | import time | ||
| + | from time import strftime | ||
| + | |||
| + | class TestOLED(object): | ||
| + | """ test the OLED SSD1306 display""" | ||
| + | |||
| + | def __init__(self,width,height): | ||
| + | """ construct me with the given width and height""" | ||
| + | # Create the I2C interface. | ||
| + | self.i2c = busio.I2C(SCL, SDA) | ||
| + | |||
| + | # Create the SSD1306 OLED class. | ||
| + | # The first two parameters are the pixel width and pixel height. Change these | ||
| + | # to the right size for your display! | ||
| + | self.oled= adafruit_ssd1306.SSD1306_I2C(width, height, self.i2c) | ||
| + | # Alternatively you can change the I2C address of the device with an addr parameter: | ||
| + | #display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x31) | ||
| + | |||
| + | def white(self): | ||
| + | """ show all white pixels""" | ||
| + | # Clear the display. Always call show after changing pixels to make the display | ||
| + | # update visible! | ||
| + | self.oled.fill(1) | ||
| + | self.oled.show() | ||
| + | |||
| + | def black(self): | ||
| + | """ show all black pixels""" | ||
| + | self.oled.fill(0) | ||
| + | self.oled.show() | ||
| + | |||
| + | def pattern(self): | ||
| + | """ show some pixel patterns""" | ||
| + | for p in range(32,2,-2): | ||
| + | self.oled.fill(0) | ||
| + | for y in range(self.oled.height): | ||
| + | for x in range(self.oled.width): | ||
| + | if x%(2*p)==0 and y%p==0: | ||
| + | self.oled.pixel(x,y,1) | ||
| + | self.oled.show() | ||
| + | |||
| + | def shapes(self): | ||
| + | """ show some shapes""" | ||
| + | w=self.oled.width | ||
| + | h=self.oled.height | ||
| + | for d in range(0,h//2,2): | ||
| + | self.oled.fill(0) | ||
| + | self.oled.rect(d, d, w-2*d,h-2*d, True) | ||
| + | self.oled.line(d, d, w-d,h-d, True) | ||
| + | self.oled.line(d, h-d, w-d, d, True) | ||
| + | self.oled.fill_rect((w-d-1)//2, (h-d-1)//2, d+1, d+1, True) | ||
| + | self.oled.show() | ||
| − | + | def text(self,lines,size=1): | |
| − | + | """ show the given lines of text at the given size""" | |
| + | self.oled.fill(0) | ||
| + | yp=0 | ||
| + | for line in lines: | ||
| + | self.oled.text(line,0,yp,True,size=size) | ||
| + | yp=yp+10*size | ||
| + | self.oled.show() | ||
| − | + | def clock(self): | |
| − | # | + | # please note that the order of lines is in reverse at size 2 |
| − | + | while True: | |
| − | + | curDate=strftime("%Y-%m-%d") | |
| − | + | curTime=strftime("%H:%M:%S") | |
| − | + | test.text({curTime,curDate},size=2) | |
| − | + | time.sleep(1) | |
| − | |||
| − | + | test=TestOLED(128,64) | |
| − | + | time.sleep(1) | |
| − | + | test.black() | |
| − | |||
time.sleep(1) | time.sleep(1) | ||
| − | + | test.shapes() | |
| − | + | time.sleep(1) | |
| − | + | test.pattern() | |
| − | + | time.sleep(1) | |
| − | + | test.text({"Good morning","Dear Maria!","How are you?"}) | |
| − | + | time.sleep(3) | |
| − | + | test.clock() | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
</source> | </source> | ||
<source lang='bash' highlight='1'> | <source lang='bash' highlight='1'> | ||
python3 show.py | python3 show.py | ||
</source> | </source> | ||
| − | [[File: | + | [[File:DSC00040.JPG]] |
| + | |||
= Datasheet = | = Datasheet = | ||
<pdf>https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf</pdf> | <pdf>https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf</pdf> | ||
<headertabs/> | <headertabs/> | ||
[[Category:Raspberry]] | [[Category:Raspberry]] | ||