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,91 +16,99 @@ 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:
self.__disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# 128x32 display with hardware I2C: # Initialize library.
self.__disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) self.__disp.begin()
self.display_machine_name()
# Initialize library. logger.success("planktoscope.display is ready!")
self.__disp.begin() except Exception as e:
logger.error("Could not detect the display")
self.display_machine_name() self.display_available = False
logger.success("planktoscope.display is ready!")
def display_machine_name(self): def display_machine_name(self):
self.__clear() if self.display_available:
machineName = planktoscope.uuidName.machineName( self.__clear()
machine=planktoscope.uuidName.getSerial() machineName = planktoscope.uuidName.machineName(
) machine=planktoscope.uuidName.getSerial()
self.display_text(machineName.replace(" ", "\n")) )
self.display_text(machineName.replace(" ", "\n"))
def display_text(self, message): def display_text(self, message):
text = message.replace("\n", " ") if self.display_available:
logger.info(f"Displaying message {text}") text = message.replace("\n", " ")
logger.info(f"Displaying message {text}")
# Clear display. # Clear display.
self.__clear() self.__clear()
width = self.__disp.width width = self.__disp.width
height = self.__disp.height height = self.__disp.height
# Create blank image for drawing. # Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color. # Make sure to create image with mode '1' for 1-bit color.
image = PIL.Image.new("1", (width, height)) image = PIL.Image.new("1", (width, height))
# Get drawing object to draw on image. # Get drawing object to draw on image.
draw = PIL.ImageDraw.Draw(image) draw = PIL.ImageDraw.Draw(image)
# Draw a black filled box to clear the image. # Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0) draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Draw some shapes. # Draw some shapes.
# First define some constants to allow easy resizing of shapes. # First define some constants to allow easy resizing of shapes.
padding = -2 padding = -2
top = padding top = padding
bottom = height - padding bottom = height - padding
# Move left to right keeping track of the current x position for drawing shapes. # Move left to right keeping track of the current x position for drawing shapes.
x = 0 x = 0
# Load default font. # Load default font.
font = PIL.ImageFont.truetype( font = PIL.ImageFont.truetype(
font="truetype/dejavu/DejaVuSansMono.ttf", size=15 font="truetype/dejavu/DejaVuSansMono.ttf", size=15
) )
# Draw a black filled box to clear the image. # Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0) draw.rectangle((0, 0, width, height), outline=0, fill=0)
text_size = font.getsize_multiline(message) text_size = font.getsize_multiline(message)
x = width / 2 - text_size[0] / 2 x = width / 2 - text_size[0] / 2
draw.text((x, 0), message, font=font, fill=255, align="center") draw.text((x, 0), message, font=font, fill=255, align="center")
# draw.text((0, top + 15), "READY", font=font, fill=255) # draw.text((0, top + 15), "READY", font=font, fill=255)
# now = datetime.datetime.isoformat(datetime.datetime.now())[:-16] # now = datetime.datetime.isoformat(datetime.datetime.now())[:-16]
# draw.text( # draw.text(
# (68, 0), # (68, 0),
# now, # now,
# font=PIL.ImageFont.truetype(font="truetype/dejavu/DejaVuSansMono.ttf", size=10), # font=PIL.ImageFont.truetype(font="truetype/dejavu/DejaVuSansMono.ttf", size=10),
# fill=255, # fill=255,
# ) # )
# draw.text((x, top + 16), str(Disk), font=font, fill=255) # draw.text((x, top + 16), str(Disk), font=font, fill=255)
# draw.text((x, top + 24), "wlan0:" + str(IP), font=font, fill=255) # draw.text((x, top + 24), "wlan0:" + str(IP), font=font, fill=255)
# Display image. # Display image.
self.__disp.image(image) self.__disp.image(image)
self.__disp.display() self.__disp.display()
def __clear(self): def __clear(self):
logger.trace("Clear the display") if self.display_available:
# Clear display. logger.trace("Clear the display")
self.__disp.clear() # Clear display.
self.__disp.display() self.__disp.clear()
self.__disp.display()
def stop(self): def stop(self):
logger.info("Display is out!") if self.display_available:
self.display_text("Cut the power\nin 5s") logger.info("Display is out!")
self.display_text("Cut the power\nin 5s")
if __name__ == "__main__": if __name__ == "__main__":