From 98dbd48829dbe3973c0b3b391a502c9fbf13bf41 Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 8 Mar 2025 16:50:07 -0700 Subject: [PATCH] Change backlight implementation --- hd44780/af_i2c_backpack.go | 6 ++-- hd44780/backlight.go | 34 ++++++++++++++++++ hd44780/example_test.go | 4 +-- hd44780/hd44780.go | 70 ++++++++++++++++++++++--------------- hd44780/pcf857x_backpack.go | 2 +- 5 files changed, 81 insertions(+), 35 deletions(-) create mode 100644 hd44780/backlight.go diff --git a/hd44780/af_i2c_backpack.go b/hd44780/af_i2c_backpack.go index 126c446..0c6475c 100644 --- a/hd44780/af_i2c_backpack.go +++ b/hd44780/af_i2c_backpack.go @@ -43,7 +43,7 @@ func NewAdafruitI2CBackpack(bus i2c.Bus, address uint16, rows, cols int) (*HD447 reset, _ := gr.ByOffset(4).(gpio.PinOut) enable, _ := gr.ByOffset(5).(gpio.PinOut) bl := gr.ByOffset(6).(gpio.PinOut) - return NewHD44780(gr, &reset, &enable, &bl, rows, cols) + return NewHD44780(gr, &reset, &enable, NewBacklight(bl), rows, cols) } // This function returns a display configured to use the SPI side of the Adafruit @@ -54,7 +54,7 @@ func NewAdafruitSPIBackpack(conn spi.Conn, rows, cols int) (*HD44780, error) { gr, _ := chip.Group(d7, d6, d5, d4) rs := &chip.Pins[rsPin] e := &chip.Pins[enablePin] - bt := &chip.Pins[backlightPin] + bl := &chip.Pins[backlightPin] - return NewHD44780(gr, rs, e, bt, rows, cols) + return NewHD44780(gr, rs, e, NewBacklight(bl), rows, cols) } diff --git a/hd44780/backlight.go b/hd44780/backlight.go new file mode 100644 index 0000000..880842b --- /dev/null +++ b/hd44780/backlight.go @@ -0,0 +1,34 @@ +// Copyright 2025 The Periph Authors. All rights reserved. +// Use of this source code is governed under the Apache License, Version 2.0 +// that can be found in the LICENSE file. + +package hd44780 + +import ( + "periph.io/x/conn/v3/display" + "periph.io/x/conn/v3/gpio" +) + +// A monochrome backlight Implements display.Backlight It uses a single +// GPIO Pin to turn the backlight on or off. +type GPIOMonoBacklight struct { + blPin gpio.PinOut +} + +// Given a GPIO pin that turns the backlight on/off, construct a monobacklight +// to use with HD44780. +func NewBacklight(blPin gpio.PinOut) *GPIOMonoBacklight { + return &GPIOMonoBacklight{blPin: blPin} +} + +// Turn the display backlight on or off. +func (bl *GPIOMonoBacklight) Backlight(intensity display.Intensity) (err error) { + if intensity == 0 { + err = bl.blPin.Out(gpio.Low) + } else { + err = bl.blPin.Out(gpio.High) + } + return err +} + +var _ display.DisplayBacklight = &GPIOMonoBacklight{} diff --git a/hd44780/example_test.go b/hd44780/example_test.go index 20a10f1..91ee104 100644 --- a/hd44780/example_test.go +++ b/hd44780/example_test.go @@ -44,8 +44,8 @@ func Example() { pins := ls.Pins() reset := pins[4].(gpio.PinOut) enable := pins[5].(gpio.PinOut) - bl := pins[6].(gpio.PinOut) - lcd, err := hd44780.NewHD44780(ls, &reset, &enable, &bl, 2, 16) + bl := hd44780.NewBacklight(pins[6].(gpio.PinOut)) + lcd, err := hd44780.NewHD44780(ls, &reset, &enable, bl, 2, 16) if err != nil { log.Fatal(err) } diff --git a/hd44780/hd44780.go b/hd44780/hd44780.go index 54c9c97..fe62007 100644 --- a/hd44780/hd44780.go +++ b/hd44780/hd44780.go @@ -38,17 +38,17 @@ const ( // // Implements periph.io/conn/x/display/TextDisplay and display.DisplayBacklight type HD44780 struct { - dataPins gpio.Group - resetPin gpio.PinOut - enablePin gpio.PinOut - backlightPin gpio.PinOut - mode ifMode - rows int - cols int - on bool - cursor bool - blink bool - lastWrite int64 + dataPins gpio.Group + resetPin gpio.PinOut + enablePin gpio.PinOut + bl interface{} + mode ifMode + rows int + cols int + on bool + cursor bool + blink bool + lastWrite int64 } const ( @@ -80,7 +80,8 @@ func getRowConstant(row, maxcols int) byte { // connected using all 8 pins. func NewHD44780( dataPinGroup gpio.Group, - resetPin, enablePin, backlightPin *gpio.PinOut, + resetPin, enablePin *gpio.PinOut, + backlight interface{}, rows, cols int) (*HD44780, error) { mode := mode4Bit @@ -89,14 +90,14 @@ func NewHD44780( } display := &HD44780{ - dataPins: dataPinGroup, - resetPin: *resetPin, - enablePin: *enablePin, - backlightPin: *backlightPin, - mode: mode, - rows: rows, - cols: cols, - on: true, + dataPins: dataPinGroup, + resetPin: *resetPin, + enablePin: *enablePin, + bl: backlight, + mode: mode, + rows: rows, + cols: cols, + on: true, } return display, display.init() } @@ -277,18 +278,29 @@ func (lcd *HD44780) Halt() error { return lcd.dataPins.Halt() } -// Turn the display's backlight on or off. You must supply a backlight control -// pin when creating the display to use this. +// Set the backlight intensity. func (lcd *HD44780) Backlight(intensity display.Intensity) error { - on := (intensity > 0) - err := lcd.Display(on) - if err != nil { - return err + switch bl := lcd.bl.(type) { + case display.DisplayBacklight: + return bl.Backlight(intensity) + case display.DisplayRGBBacklight: + return bl.RGBBacklight(intensity, intensity, intensity) + default: + return display.ErrNotImplemented } - if lcd.backlightPin != nil { - err = lcd.backlightPin.Out(gpio.Level(on)) +} + +// For units that have an RGB Backlight, set the backlight color/intensity. +// The range of the values is 0-255. +func (lcd *HD44780) RGBBacklight(red, green, blue display.Intensity) error { + switch bl := lcd.bl.(type) { + case display.DisplayRGBBacklight: + return bl.RGBBacklight(red, green, blue) + case display.DisplayBacklight: + return bl.Backlight(red | green | blue) + default: + return display.ErrNotImplemented } - return err } // delayWrite looks at the time of the last LCD write and if diff --git a/hd44780/pcf857x_backpack.go b/hd44780/pcf857x_backpack.go index b1b9afa..939c922 100644 --- a/hd44780/pcf857x_backpack.go +++ b/hd44780/pcf857x_backpack.go @@ -46,5 +46,5 @@ func NewPCF857xBackpack(bus i2c.Bus, address uint16, rows, cols int) (*HD44780, reset := grPins[4].(gpio.PinOut) enable := grPins[5].(gpio.PinOut) bl := grPins[6].(gpio.PinOut) - return NewHD44780(gr, &reset, &enable, &bl, rows, cols) + return NewHD44780(gr, &reset, &enable, NewBacklight(bl), rows, cols) }