Change backlight implementation

pull/91/head
George Sexton 1 year ago
parent 9eb1f7e740
commit 98dbd48829

@ -43,7 +43,7 @@ func NewAdafruitI2CBackpack(bus i2c.Bus, address uint16, rows, cols int) (*HD447
reset, _ := gr.ByOffset(4).(gpio.PinOut) reset, _ := gr.ByOffset(4).(gpio.PinOut)
enable, _ := gr.ByOffset(5).(gpio.PinOut) enable, _ := gr.ByOffset(5).(gpio.PinOut)
bl := gr.ByOffset(6).(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 // 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) gr, _ := chip.Group(d7, d6, d5, d4)
rs := &chip.Pins[rsPin] rs := &chip.Pins[rsPin]
e := &chip.Pins[enablePin] 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)
} }

@ -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{}

@ -44,8 +44,8 @@ func Example() {
pins := ls.Pins() pins := ls.Pins()
reset := pins[4].(gpio.PinOut) reset := pins[4].(gpio.PinOut)
enable := pins[5].(gpio.PinOut) enable := pins[5].(gpio.PinOut)
bl := pins[6].(gpio.PinOut) bl := hd44780.NewBacklight(pins[6].(gpio.PinOut))
lcd, err := hd44780.NewHD44780(ls, &reset, &enable, &bl, 2, 16) lcd, err := hd44780.NewHD44780(ls, &reset, &enable, bl, 2, 16)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

@ -38,17 +38,17 @@ const (
// //
// Implements periph.io/conn/x/display/TextDisplay and display.DisplayBacklight // Implements periph.io/conn/x/display/TextDisplay and display.DisplayBacklight
type HD44780 struct { type HD44780 struct {
dataPins gpio.Group dataPins gpio.Group
resetPin gpio.PinOut resetPin gpio.PinOut
enablePin gpio.PinOut enablePin gpio.PinOut
backlightPin gpio.PinOut bl interface{}
mode ifMode mode ifMode
rows int rows int
cols int cols int
on bool on bool
cursor bool cursor bool
blink bool blink bool
lastWrite int64 lastWrite int64
} }
const ( const (
@ -80,7 +80,8 @@ func getRowConstant(row, maxcols int) byte {
// connected using all 8 pins. // connected using all 8 pins.
func NewHD44780( func NewHD44780(
dataPinGroup gpio.Group, dataPinGroup gpio.Group,
resetPin, enablePin, backlightPin *gpio.PinOut, resetPin, enablePin *gpio.PinOut,
backlight interface{},
rows, cols int) (*HD44780, error) { rows, cols int) (*HD44780, error) {
mode := mode4Bit mode := mode4Bit
@ -89,14 +90,14 @@ func NewHD44780(
} }
display := &HD44780{ display := &HD44780{
dataPins: dataPinGroup, dataPins: dataPinGroup,
resetPin: *resetPin, resetPin: *resetPin,
enablePin: *enablePin, enablePin: *enablePin,
backlightPin: *backlightPin, bl: backlight,
mode: mode, mode: mode,
rows: rows, rows: rows,
cols: cols, cols: cols,
on: true, on: true,
} }
return display, display.init() return display, display.init()
} }
@ -277,18 +278,29 @@ func (lcd *HD44780) Halt() error {
return lcd.dataPins.Halt() return lcd.dataPins.Halt()
} }
// Turn the display's backlight on or off. You must supply a backlight control // Set the backlight intensity.
// pin when creating the display to use this.
func (lcd *HD44780) Backlight(intensity display.Intensity) error { func (lcd *HD44780) Backlight(intensity display.Intensity) error {
on := (intensity > 0) switch bl := lcd.bl.(type) {
err := lcd.Display(on) case display.DisplayBacklight:
if err != nil { return bl.Backlight(intensity)
return err 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 // delayWrite looks at the time of the last LCD write and if

@ -46,5 +46,5 @@ func NewPCF857xBackpack(bus i2c.Bus, address uint16, rows, cols int) (*HD44780,
reset := grPins[4].(gpio.PinOut) reset := grPins[4].(gpio.PinOut)
enable := grPins[5].(gpio.PinOut) enable := grPins[5].(gpio.PinOut)
bl := grPins[6].(gpio.PinOut) bl := grPins[6].(gpio.PinOut)
return NewHD44780(gr, &reset, &enable, &bl, rows, cols) return NewHD44780(gr, &reset, &enable, NewBacklight(bl), rows, cols)
} }

Loading…
Cancel
Save