]> defiant.homedns.org Git - a4wd3.git/commitdiff
oled_ssd1306: rotate by 180°, turn off on shutdown
authorErik Andresen <erik@vontaene.de>
Sun, 11 Oct 2026 17:12:08 +0000 (19:12 +0200)
committerErik Andresen <erik@vontaene.de>
Sun, 11 Oct 2026 17:12:08 +0000 (19:12 +0200)
scripts/oled_ssd1306.py

index f0bf6f197f8b64268c11d521d2b0a526492f6dab..4dd2f736037d9a010c1d61db58233d00d1e855fb 100755 (executable)
@@ -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])