From 3cf8e8b971d03f7f647174ac0804c900cf05cd9e Mon Sep 17 00:00:00 2001 From: Erik Andresen Date: Sun, 11 Oct 2026 19:12:08 +0200 Subject: [PATCH] =?utf8?q?oled=5Fssd1306:=20rotate=20by=20180=C2=B0,=20tur?= =?utf8?q?n=20off=20on=20shutdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- scripts/oled_ssd1306.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/scripts/oled_ssd1306.py b/scripts/oled_ssd1306.py index f0bf6f1..4dd2f73 100755 --- a/scripts/oled_ssd1306.py +++ b/scripts/oled_ssd1306.py @@ -4,13 +4,14 @@ import sys import struct import gi +import signal gi.require_version('Gst', '1.0') from gi.repository import GObject, Gst from time import sleep from pyshared.i2c import * SET_DISPLAY_ON=0xaf -SET_DISPLAY_OFF=0xa4 +SET_DISPLAY_OFF=0xae SET_MULTIPLEX_RATIO=0xa8 SET_DISPLAY_OFFSET=0xd3 SET_COM_PINS_HARDWARE_CONFIGURATION=0xda @@ -23,7 +24,7 @@ SET_PAGE_ADDRESS=0x22 SET_CONTRAST=0x81 def binarize(px, thres=127): - return px > thres + return px <= thres class ssd1306: def __init__(self): @@ -32,9 +33,11 @@ class ssd1306: self.width=128 self.height=64 self.initialize() + self.loop = None + signal.signal(signal.SIGINT, self.handle_sigint) def play(self, src): - self.pipeline = Gst.parse_launch('%s ! decodebin ! videoconvert ! videoscale add-borders=true ! videorate average-period=1000000000 max-rate=4 ! video/x-raw,format=GRAY8,width=128,height=64 ! appsink' % (src)) + self.pipeline = Gst.parse_launch('%s ! decodebin ! videoconvert ! videoscale add-borders=true ! videorate average-period=1000000000 max-rate=4 ! rotate angle=3.1416 ! video/x-raw,format=GRAY8,width=128,height=64 ! appsink' % (src)) appsink = self.pipeline.get_by_name('appsink0') bus = self.pipeline.get_bus() bus.add_signal_watch() @@ -42,8 +45,8 @@ class ssd1306: appsink.set_property('emit-signals', True) appsink.connect('new-sample', self.new_appsink_buffer) self.pipeline.set_state(Gst.State.PLAYING) - loop = GObject.MainLoop() - loop.run() + self.loop = GObject.MainLoop() + self.loop.run() def new_appsink_buffer(self, appsink): sample = appsink.emit("pull-sample") @@ -134,9 +137,16 @@ class ssd1306: i2c_write_reg(self.addr, 0b01000000, "".join(data)) def clear(self): - img = [0x00] * (128*64) + img = [0xff] * (128*64) self.ssd1306_data(img) + def handle_sigint(self, signum, frame): + print "Turning off" + if self.loop is not None: + self.loop.quit() + self.ssd1306_cmd(SET_DISPLAY_OFF) + sys.exit(0) + if __name__ == "__main__": pOled = ssd1306() pOled.play(sys.argv[1]) -- 2.39.5