display: detect display availability

(cherry picked from commit 922b47ca8fdd495b8b2835e9ab5b6430982e1735)
This commit is contained in:
Romain Bazile 2021-06-07 13:36:24 +02:00 committed by Romain Bazile
parent a23820e28d
commit 328d227b8f

View file

@ -16,20 +16,25 @@ import planktoscope.uuidName
class Display(object): class Display(object):
display_available = True
def __init__(self): def __init__(self):
# Raspberry Pi pin configuration: # Raspberry Pi pin configuration:
RST = None # on the PiOLED this pin isnt used RST = None # on the PiOLED this pin isnt used
try:
# 128x32 display with hardware I2C: # 128x32 display with hardware I2C:
self.__disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) self.__disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# Initialize library. # Initialize library.
self.__disp.begin() self.__disp.begin()
self.display_machine_name() self.display_machine_name()
logger.success("planktoscope.display is ready!") logger.success("planktoscope.display is ready!")
except Exception as e:
logger.error("Could not detect the display")
self.display_available = False
def display_machine_name(self): def display_machine_name(self):
if self.display_available:
self.__clear() self.__clear()
machineName = planktoscope.uuidName.machineName( machineName = planktoscope.uuidName.machineName(
machine=planktoscope.uuidName.getSerial() machine=planktoscope.uuidName.getSerial()
@ -37,6 +42,7 @@ class Display(object):
self.display_text(machineName.replace(" ", "\n")) self.display_text(machineName.replace(" ", "\n"))
def display_text(self, message): def display_text(self, message):
if self.display_available:
text = message.replace("\n", " ") text = message.replace("\n", " ")
logger.info(f"Displaying message {text}") logger.info(f"Displaying message {text}")
@ -93,12 +99,14 @@ class Display(object):
self.__disp.display() self.__disp.display()
def __clear(self): def __clear(self):
if self.display_available:
logger.trace("Clear the display") logger.trace("Clear the display")
# Clear display. # Clear display.
self.__disp.clear() self.__disp.clear()
self.__disp.display() self.__disp.display()
def stop(self): def stop(self):
if self.display_available:
logger.info("Display is out!") logger.info("Display is out!")
self.display_text("Cut the power\nin 5s") self.display_text("Cut the power\nin 5s")