Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn HD44780 LCD Backlight Off on Shutdown #2443

Open
pieman3000 opened this issue Oct 21, 2024 · 1 comment
Open

Turn HD44780 LCD Backlight Off on Shutdown #2443

pieman3000 opened this issue Oct 21, 2024 · 1 comment
Labels
enhancement help wanted legacy_v2 Issues, discussions and PRs related to Version 2.x Tips Tricks Hacks

Comments

@pieman3000
Copy link

pieman3000 commented Oct 21, 2024

Version

2.7.0 - 550a258

Branch

master

OS

Raspbian GNU/Linux 11 (bullseye) Lite 32bit

Pi model

Zero 2W

Hardware

HD44780 connected via i2c backpack

What happened?

Wanted to have the LCD backlight turn off on shutdown. This probably isn't the cleanest code, but I don't understand what is safe to remove from the definitions at the start. All I did was remove all of the code that updated the screen, and just left the command to turn off the backlight.

I created a new .py file that simply turns off the backlight, and added an ExecStop command to run this to the service file.

Filename: i2c_lcd_backlight_off.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import i2c_lcd_driver
from time import *
import time
import subprocess
import numpy
# import datetime
from mpd import MPDClient
# constants
mylcd = i2c_lcd_driver.lcd()
info_at_lines_play = [" "] * 4
info_at_lines_pause = [" "] * 4
info_at_lines_stop = [" "] * 4
info_at_lines_mpd_not_running = [" "] * 4
################# CHANGE YOUR SETTINGS HERE!!! ###########################################
## Display settings                                                                     ##
n_cols = 20                 # EDIT!!!  <-- number of cols your display has              ##
n_rows = 4                  # EDIT!!!  <-- number of rows your display has              ##
val_delay = 0.4             # EDIT!!!  <-- speed of the scolling text                   ##
start_stop_sc_delay = 4                                                                 ##
use_state_icons = "yes"   # choose "yes" if you want to use this                        ##
blinking_icons = "yes"  # its an add-on for "use_state_icons", so "use_state_icons" must be "yes" ##
backlight_off_while_waiting = "yes"  # not active in state "play" and "pause"           ##
backlight_off_delay = 10  # Delay in seconds                                            ##
#### change the following strings to give your box a personal style                     ##
mpd_not_running_string = "MPD not running"                                              ##
music_stopped_string = "Music stopped!"                                                 ##
music_paused_string = "paused!"                                                         ##
##                                                                                      ##
################## CHANGE YOUR INFOS, WHICH WILL BE SHOWN ON THE DISPLAY #################
##                                                                                      ##
##  You can choose between the following infos:                                         ##
##  'date_and_time', 'artist', 'nothing', 'track_title', 'track_artist_title'           ##
##  'track_time', 'pause_string', 'stop_string', 'mpd_not_running_string'               ##
##  'track_time_and_number'                                                             ##
##                                                                                      ##
##  if you have less than 4 lines, you can leave the unused lines by default            ##
##                                                                                      ##
##  Choose infos while state is "play"                                                  ##
##                                                                                      ##
info_at_lines_play[0] = 'date_and_time'  # <-- Choose your favorite                    ##
info_at_lines_play[1] = 'artist'  # <-- Choose your favorite                           ##
info_at_lines_play[2] = 'title'  # <-- Choose your favorite                            ##
info_at_lines_play[3] = 'track_time_and_number'  # <-- Choose your favorite            ##
##                                                                                      ##
##  Choose infos while state is "pause"                                                 ##
##                                                                                      ##
info_at_lines_pause[0] = 'date_and_time'  # <-- Choose your favorite                    ##
info_at_lines_pause[1] = 'artist'  # <-- Choose your favorite                           ##
info_at_lines_pause[2] = 'title'  # <-- Choose your favorite                            ##
info_at_lines_pause[3] = 'pause_string'  # <-- Choose your favorite                     ##
##                                                                                      ##
##  Choose infos while state is "stop"                                                  ##
##                                                                                      ##
info_at_lines_stop[0] = 'date_and_time'  # <-- Choose your favorite                     ##
info_at_lines_stop[1] = 'nothing'  # <-- Choose your favorite                           ##
info_at_lines_stop[2] = 'nothing'  # <-- Choose your favorite                           ##
info_at_lines_stop[3] = 'stop_string'  # <-- Choose your favorite                       ##
##                                                                                      ##
##  Choose infos while state is "not_running"                                           ##
##                                                                                      ##
info_at_lines_mpd_not_running[0] = 'date_and_time'  # <-- Choose your favorite          ##
info_at_lines_mpd_not_running[1] = 'nothing'  # <-- Choose your favorite                ##
info_at_lines_mpd_not_running[2] = 'nothing'  # <-- Choose your favorite                ##
info_at_lines_mpd_not_running[3] = 'mpd_not_running_string'  # <-- Choose your favorite ##
##                                                                                      ##
##########################################################################################

## DO NOT EDIT!!!
clearline = " " * n_cols
string_track_title = " "
string_track_artist_title = " "
string_date_time = " "
track_number = " "
title = " "
playlist_length = " "
last_title = " "
i_counter = 0
state = " "
last_state = "not_running"
track_time = " "
current_time = time.time()
last_time = time.time()
if n_cols > 16:  # select date_string dependent on how many columns the display has (usually either 16 or 20 rows)
    date_string = "%d.%m.%Y %H:%M"
else:
    date_string = "%d.%m.%y %H:%M"  # save two character spaces  for displays showing only 16 characters per row

# lines that got to show
lines = [" " * n_cols] * n_rows
last_lines = [" " * n_cols] * n_rows

# User icons
user_icons = [
  [0b10000,  # Play
   0b11000,
   0b11100,
   0b11110,
   0b11100,
   0b11000,
   0b10000,
   0b00000],
  [0b00000,  # Pause
   0b11011,
   0b11011,
   0b11011,
   0b11011,
   0b11011,
   0b11011,
   0b00000],
  [0b00000,  # Stop
   0b11111,
   0b11111,
   0b11111,
   0b11111,
   0b11111,
   0b00000,
   0b00000],
  [0b00000,  # Offline
   0b00000,
   0b01010,
   0b00000,
   0b01110,
   0b10001,
   0b00000,
   0b00000]]


def print_changes(string_new, string_old, row):
    for pos in range(len(string_new)):
        if string_new[pos] != string_old[pos]:
            mylcd.lcd_display_string(string_new[pos], row, pos)


def fill_with_spaces(string1, length):
    if len(string1) <= length:
        return (string1 + " " * (length - len(string1)))
    else:
        return string1


def loop_string(string1, string2):
    my_long_string = string2
    title_max_length = n_cols - len(string1)  # max_len is dependent by len (track_number)
    position = numpy.clip((i_counter % (len(string2) - (title_max_length - 1) + 2 * start_stop_sc_delay)) - start_stop_sc_delay, 0, len(string2) - (title_max_length))
    scroll_text = my_long_string[position:(position + title_max_length)]
    return (string1 + scroll_text)


def print_nothing():
    return clearline


def print_pause_string():
    return (fill_with_spaces(music_paused_string, n_cols))


def print_stop_string():
    return (fill_with_spaces(music_stopped_string, n_cols))


def print_mpd_not_running_string():
    return (fill_with_spaces(mpd_not_running_string, n_cols))


def print_artist():
    if len(artist) <= n_cols:
        return fill_with_spaces(artist, n_cols)
    else:
        return loop_string("", artist)  # SC version


def print_track_title():
    # Write Track number & Title into the row of the display
    string_track_title = track_number + ":" + title
    if len(string_track_title) <= n_cols:
        return fill_with_spaces(string_track_title, n_cols)
    else:
        return loop_string(track_number + ":", title)  # SC version


def print_title():
    # Write Title into the row of the display
    if len(title) <= n_cols:
        return fill_with_spaces(title, n_cols)
    else:
        return loop_string("", title)  # SC version


def print_track_artist_title():
    string_track_artist_title = track_number + ":" + artist + " - " + title
    if len(string_track_artist_title) <= n_cols:
        return fill_with_spaces(string_track_artist_title, n_cols)
    else:
        return loop_string(track_number + ":", artist + " - " + title)  # SC version


def print_artist_title():
    string_artist_title = artist + " - " + title
    if len(string_artist_title) <= n_cols:
        return fill_with_spaces(string_artist_title, n_cols)
    else:
        return loop_string("", artist + " - " + title)  # SC version


def print_track_time():
    return fill_with_spaces(track_time, n_cols)


def print_track_time_and_number():
    song_of_playlist = track_number + "/" + playlist_length
    return (fill_with_spaces(track_time, n_cols))[:(n_cols - len(song_of_playlist))] + song_of_playlist


def print_date_time():
    return fill_with_spaces(time.strftime(date_string), n_cols)


def choose_line(info_text):
    switcher = {
            'pause_string': print_pause_string(),
            'stop_string': print_stop_string(),
            'mpd_not_running_string': print_mpd_not_running_string(),
            'track_title': print_track_title(),
            'track_artist_title': print_track_artist_title(),
            'artist_title': print_artist_title(),
            'artist': print_artist(),
            'title': print_title(),
            'date_and_time': print_date_time(),
            'nothing': print_nothing(),
            'track_time': print_track_time(),
            'track_time_and_number': print_track_time_and_number()
    }
    return switcher.get(info_text, fill_with_spaces("ERROR", n_cols))


def choose_icon(state):
    switcher = {
            'play': "\x00",
            'pause': "\x01",
            'stop': "\x02",
            'not_running': "\x03",
    }
    return switcher.get(state, " ")


def sec_to_min_and_sec(seconds):
    return (str('%d' % (int(seconds) / 60)) + ":" + str('%0.2d' % (int(seconds) % 60)))


######### BEGIN OF CODE ################################
##  init mpd-client
mylcd.backlight(0)

Filename: i2c-lcd.service

[Unit]
Description=LCD Matrix Display Service
After=network.target phoniebox-rfid-reader.service

[Service]
User=pi
Group=pi
Restart=always
WorkingDirectory=/home/pi/RPi-Jukebox-RFID
ExecStart=/home/pi/RPi-Jukebox-RFID/components/displays/HD44780-i2c/i2c_lcd.py
ExecStop=/home/pi/RPi-Jukebox-RFID/components/displays/HD44780-i2c/i2c_lcd_backlight_off.py

[Install]
WantedBy=multi-user.target

(Edited to fix code blocks)

Logs

No response

Configuration

No response

More info

No response

@s-martin
Copy link
Collaborator

Thanks for the improvement.

Anyone interested to provide an integrated solution is welcome to create a PR.

@s-martin s-martin changed the title 🐛 | Turn HD44780 LCD Backlight Off on Shutdown Turn HD44780 LCD Backlight Off on Shutdown Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted legacy_v2 Issues, discussions and PRs related to Version 2.x Tips Tricks Hacks
Projects
None yet
Development

No branches or pull requests

2 participants