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

Basic implementation of various backlight drivers #689

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions backlight/PWM.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions backlight/backlight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package backlight

type Driver interface {
SetBrightness(uint8)
}
20 changes: 20 additions & 0 deletions backlight/onoff.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
33 changes: 33 additions & 0 deletions backlight/pinetime.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
Loading