Hello,

I’ve come across an unexpected issue that may be hard to diagnose due to required hardware, but here goes.

I have a Raspberry Pi connected to an LCD display that I’m testing turning the screen on and off (not worrying about displaying text, I’ve previously written a program that uses a DHT22 sensor to display the temperature & humidity and external weather conditions using the Pirate Weather API).

While trying to write a simple program just to turn the display on or off, I run into an issue.

Here’s the code:

import board
import datetime
# I2C driver from:
# https://gist.github.com/vay3t/8b0577acfdb27a78101ed16dd78ecba1
import I2C_LCD_driver
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("state", help="'on' to turn on the screen, 'off' to turn off",type=str)
args = parser.parse_args()

mylcd = I2C_LCD_driver.lcd()

match args.state:
    case "on":
        power = 1
    case "off":
        power = 0
    case _:
        print("Please enter 'on' or 'off'")
        power = None

if power != None:
    print(power) # this is just to test
    mylcd.backlight(power)

What’s happening that I don’t understand is if power == None, the if statement will not trigger but the display will turn on.

The only way I’ve been able to keep the display off is if I add an else statement:

else:
    pass

This is using Python 3.10. My understanding is the else should not be needed at all. Any suggestions as to why the display might be turning on, or a better suggestion on how to handle the match statement?

–EDIT–

So it turns out initializing the display is turning it on at the same time. For a community that had no activity for ~2 years before this post, I’m pleasantly surprised with the amount of responses I’ve gotten - you all are awesome!

  • CameronDev@programming.dev
    link
    fedilink
    English
    arrow-up
    4
    ·
    4 days ago

    Not sure why, but as for a code change suggestion:

    if args.state == "on":
        mylcd.backlight(1)
    else:
        mylcd.backlight(0)
    

    Does the print(power) output correctly? Also, is it possible that the initialisation of the mylcd is turning on the display, so if you dont explicitly turn it off again, the default is on?

    • bravemonkeyOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      4 days ago

      Yeh, that initialization was doing it. Not sure there’s a way to initialize it without powering it on, but at least I know!

      A lot of the code I posted came from both trying to get a working test showing me if it got to the place I wanted as well as wanting a third option without multiple elif statements. I’m really just learning Python so lots of ways it could be better, I’m sure.