88 python3 oled_test.py --address 0x3d --hold 8
99"""
1010import argparse
11+ import os
12+ import signal
1113import sys
1214import time
1315
1416from luma .core .interface .serial import i2c
1517from luma .core .render import canvas
1618from luma .oled .device import sh1106 , ssd1306
1719
20+ # The boot-time status display (little-internet-oled.service on the image)
21+ # owns the panel; it pauses while this claim file exists and resumes when it's
22+ # removed. The claim directory is that service's RuntimeDirectory (root:i2c,
23+ # 0775), so no sudo is needed. On systems without the service, claiming just
24+ # fails quietly — there's nothing to pause.
25+ CLAIM_FILE = "/run/little-internet/oled.claim"
26+
27+
28+ def claim_panel ():
29+ """Pause the boot status display; True if the claim file was written."""
30+ try :
31+ with open (CLAIM_FILE , "w" ) as fh :
32+ fh .write (f"{ os .getpid ()} \n " )
33+ except OSError :
34+ return False
35+ # Let an in-flight status frame land before we paint over it.
36+ time .sleep (0.5 )
37+ return True
38+
39+
40+ def release_panel (claimed ):
41+ if claimed :
42+ try :
43+ os .remove (CLAIM_FILE )
44+ except OSError :
45+ pass
46+
1847
1948def main ():
2049 p = argparse .ArgumentParser (description = "Smoke test an I2C SSD1306 OLED." )
@@ -38,14 +67,21 @@ def main():
3867 "and that I2C is enabled." )
3968 sys .exit (1 )
4069
41- with canvas (device ) as draw :
42- draw .rectangle (device .bounding_box , outline = "white" )
43- draw .text ((6 , 8 ), "OLED OK" , fill = "white" )
44- draw .text ((6 , 26 ), f"I2C { args .port } @ { hex (args .address )} " , fill = "white" )
45- draw .text ((6 , 44 ), f"{ device .width } x{ device .height } " , fill = "white" )
46- print (f"Drew test pattern on I2C { args .port } @ { hex (args .address )} . "
47- f"Holding { args .hold } s..." )
48- time .sleep (args .hold )
70+ # Python dies on SIGTERM without running `finally`, which would strand the
71+ # claim file and leave the status display paused; exit cleanly instead.
72+ signal .signal (signal .SIGTERM , lambda * _ : sys .exit (0 ))
73+ claimed = claim_panel ()
74+ try :
75+ with canvas (device ) as draw :
76+ draw .rectangle (device .bounding_box , outline = "white" )
77+ draw .text ((6 , 8 ), "OLED OK" , fill = "white" )
78+ draw .text ((6 , 26 ), f"I2C { args .port } @ { hex (args .address )} " , fill = "white" )
79+ draw .text ((6 , 44 ), f"{ device .width } x{ device .height } " , fill = "white" )
80+ print (f"Drew test pattern on I2C { args .port } @ { hex (args .address )} . "
81+ f"Holding { args .hold } s..." )
82+ time .sleep (args .hold )
83+ finally :
84+ release_panel (claimed )
4985 print ("Done." )
5086
5187
0 commit comments