From 73cc408de717a9d0c28c26df883976963e64b2f5 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Sun, 3 Mar 2019 17:12:31 -0800 Subject: [PATCH] OLED 128x64 module from DIYMall --- python/samples/logo.png | Bin 0 -> 342 bytes python/samples/oled.py | 107 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 python/samples/logo.png create mode 100644 python/samples/oled.py diff --git a/python/samples/logo.png b/python/samples/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..db66e2d017fff790a7454af50354d28c43aa6ab6 GIT binary patch literal 342 zcmeAS@N?(olHy`uVBq!ia0vp^4M6O`$P6T3NW8THQfvV}A+A9B|Ns9>Z_d99WN{XF zL>4nJa0`PlBg3pY5H=O_D5X83{0$dCDOhCg(OQ{BTAg}b8}PkN*J7rQWHy3 zQxwWGOEMJPJ$(bh8~Mb6iuQTBIEGl9etW@??~nlxYd|~S-3Lsfdzq5imc+Fxr+k?f z#uKA`=SZObgB~7}9b24A{Tf*6Pft#Dmwzx{zH1?qmfnJGtZ%#+qE|m}%&9&qgPZ_tvY;F;D+XoZzX{R)e}xC9zSWisYk5CrKiHpc@Kl3<(9AmCq6kV!q(C2De hw?g1nBKQ4S;_a4O^UZiRW&nM{;OXk;vd$@?2>_D2dnNz? literal 0 HcmV?d00001 diff --git a/python/samples/oled.py b/python/samples/oled.py new file mode 100644 index 0000000..fdb9478 --- /dev/null +++ b/python/samples/oled.py @@ -0,0 +1,107 @@ +""" +Example for 128X64OLED Module +Available from multiple vendors, e.g. DIYMall + +This example loads the I2CDriver logo onto the display, +and flashes it four times. + +""" + +import sys +import time + +from PIL import Image, ImageChops + +from i2cdriver import I2CDriver + +SETCONTRAST = 0x81 +DISPLAYALLON_RESUME = 0xA4 +DISPLAYALLON = 0xA5 +NORMALDISPLAY = 0xA6 +INVERTDISPLAY = 0xA7 +DISPLAYOFF = 0xAE +DISPLAYON = 0xAF +SETDISPLAYOFFSET = 0xD3 +SETCOMPINS = 0xDA +SETVCOMDETECT = 0xDB +SETDISPLAYCLOCKDIV = 0xD5 +SETPRECHARGE = 0xD9 +SETMULTIPLEX = 0xA8 +SETLOWCOLUMN = 0x00 +SETHIGHCOLUMN = 0x10 +SETSTARTLINE = 0x40 +MEMORYMODE = 0x20 +COLUMNADDR = 0x21 +PAGEADDR = 0x22 +COMSCANINC = 0xC0 +COMSCANDEC = 0xC8 +SEGREMAP = 0xA0 +CHARGEPUMP = 0x8D + +class OLED: + def __init__(self, i2, a = 0x3c): + self.i2 = i2 + self.a = a + + self.command(DISPLAYOFF) + self.command(SETDISPLAYCLOCKDIV, 0x80) # the suggested ratio 0x80 + + self.command(SETMULTIPLEX, 0x3f) + + self.command(SETDISPLAYOFFSET, 0) + self.command(SETSTARTLINE | 0x0) + self.command(CHARGEPUMP, 0x14) + self.command(MEMORYMODE, 0) + self.command(SEGREMAP | 0x1) + self.command(COMSCANDEC) + + self.command(SETCOMPINS, 0x12) + self.command(SETCONTRAST, 0xcf) + + self.command(SETVCOMDETECT, 0x40) + self.command(DISPLAYALLON_RESUME) + self.command(NORMALDISPLAY) + + self.im = Image.new("1", (128,64), 1) + self.cls() + + def command(self, *c): + assert(self.i2.start(self.a, 0)) + assert(self.i2.write((0,) + c)) + self.i2.stop() + + def image(self, im): + for p in range(8): + pr = self.im.crop((0,8*p,128,8*p+8)).transpose(Image.ROTATE_270) + bb = im.crop((0,8*p,128,8*p+8)).transpose(Image.ROTATE_270) + diff = ImageChops.difference(pr, bb) + di = diff.getbbox() + if di is not None: + (x0, y0, x1, y1) = di + self.command(COLUMNADDR) + self.command(y0) + self.command(y1 - 1) + self.command(PAGEADDR) + self.command(p) + self.command(p + 1) + self.i2.start(self.a, 0) + self.i2.write([0x40]) + self.i2.write(bb.tobytes()[y0:y1]) + self.i2.stop() + self.im = im + self.command(DISPLAYON) + + def cls(self): + self.image(Image.new("1", (128,64), 0)) + +if __name__ == '__main__': + i2 = I2CDriver(sys.argv[1]) + d = OLED(i2) + + d.image(Image.open("logo.png").convert("1")) + + for i in range(4): + d.command(INVERTDISPLAY) + time.sleep(.5) + d.command(NORMALDISPLAY) + time.sleep(.5)