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
SET_CONTRAST=0x81
def binarize(px, thres=127):
- return px > thres
+ return px <= thres
class ssd1306:
def __init__(self):
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()
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")
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])