From 9bc838523f0e922a4572174ce32fff3d59401c7a Mon Sep 17 00:00:00 2001 From: Yorick Smilda Date: Wed, 26 Jun 2024 21:03:39 +0200 Subject: [PATCH] Basic implementation of various backlight drivers --- backlight/PWM.go | 31 +++++++++++++++++++++++++++++++ backlight/backlight.go | 5 +++++ backlight/onoff.go | 20 ++++++++++++++++++++ backlight/pinetime.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 backlight/PWM.go create mode 100644 backlight/backlight.go create mode 100644 backlight/onoff.go create mode 100644 backlight/pinetime.go diff --git a/backlight/PWM.go b/backlight/PWM.go new file mode 100644 index 000000000..9fa9b1a65 --- /dev/null +++ b/backlight/PWM.go @@ -0,0 +1,31 @@ +package backlight + +import "machine" + +type PWM interface { + Configure(config machine.PWMConfig) error + Channel(pin machine.Pin) (channel uint8, err error) + Top() uint32 + Set(channel uint8, value uint32) +} + +type PWMDriver struct { + pwm PWM + ch uint8 +} + +func NewPWMDriver(pwm PWM, pin machine.Pin) (PWMDriver, error) { + err := pwm.Configure(machine.PWMConfig{}) + if err != nil { + return PWMDriver{}, err + } + ch, err := pwm.Channel(pin) + if err != nil { + return PWMDriver{}, err + } + return PWMDriver{pwm, ch}, nil +} + +func (b PWMDriver) SetBrightness(brightness uint8) { + b.pwm.Set(b.ch, b.pwm.Top()*uint32(brightness)/255) +} diff --git a/backlight/backlight.go b/backlight/backlight.go new file mode 100644 index 000000000..a89e32c97 --- /dev/null +++ b/backlight/backlight.go @@ -0,0 +1,5 @@ +package backlight + +type Driver interface { + SetBrightness(uint8) +} diff --git a/backlight/onoff.go b/backlight/onoff.go new file mode 100644 index 000000000..ac28f9873 --- /dev/null +++ b/backlight/onoff.go @@ -0,0 +1,20 @@ +package backlight + +import "machine" + +type OnOffDriver struct { + blPin machine.Pin +} + +func NewOnOffDriver(pin machine.Pin) OnOffDriver { + pin.Configure(machine.PinConfig{Mode: machine.PinOutput}) + return OnOffDriver{blPin: pin} +} + +func (b OnOffDriver) SetBrightness(brightness uint8) { + if brightness < 128 { + b.blPin.Low() + } else { + b.blPin.High() + } +} diff --git a/backlight/pinetime.go b/backlight/pinetime.go new file mode 100644 index 000000000..c893900de --- /dev/null +++ b/backlight/pinetime.go @@ -0,0 +1,33 @@ +package backlight + +import "machine" + +type PineTimeDriver struct { + blLowPin machine.Pin + blMidPin machine.Pin + blHighPin machine.Pin +} + +func NewPineTimeDriver(lowPin, midPin, highPin machine.Pin) PineTimeDriver { + lowPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) + midPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) + highPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) + return PineTimeDriver{blLowPin: lowPin, blMidPin: midPin, blHighPin: highPin} +} + +// Not sure if you can combine the three pins to get intermediate brightness levels.s +func (b PineTimeDriver) SetBrightness(brightness uint8) { + if brightness < 85 { + b.blLowPin.Low() + b.blMidPin.High() + b.blHighPin.High() + } else if brightness < 170 { + b.blLowPin.High() + b.blMidPin.Low() + b.blHighPin.High() + } else { + b.blLowPin.High() + b.blMidPin.High() + b.blHighPin.Low() + } +}