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

hd44780: configure and write fixes #647

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
43 changes: 33 additions & 10 deletions hd44780/gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ package hd44780

import (
"errors"
"time"

"machine"
)

type WriteByteType int

const (
FullByte WriteByteType = 3
HighNibble WriteByteType = 2
LowNibble WriteByteType = 1
)

type GPIO struct {
dataPins []machine.Pin
en machine.Pin
rw machine.Pin
rs machine.Pin

write func(data byte)
write func(data byte, wt WriteByteType)
read func() byte
}

Expand Down Expand Up @@ -48,6 +57,10 @@ func newGPIO(dataPins []machine.Pin, en, rs, rw machine.Pin, mode byte) Device {
}
}

func (g *GPIO) WriteHighNibble(data byte) {
g.write(data, HighNibble)
}

// SetCommandMode sets command/instruction mode
func (g *GPIO) SetCommandMode(set bool) {
if set {
Expand All @@ -68,26 +81,36 @@ func (g *GPIO) Write(data []byte) (n int, err error) {
g.rw.Low()
}
for _, d := range data {
g.write(d)
g.write(d, FullByte)
n++
}
return n, nil
}

func (g *GPIO) write8BitMode(data byte) {
g.en.High()
func (g *GPIO) write8BitMode(data byte, _ WriteByteType) {
g.setPins(data)
g.en.Low()
g.pulseEnable()
}

func (g *GPIO) write4BitMode(data byte) {
g.en.High()
g.setPins(data >> 4)
g.en.Low()
func (g *GPIO) write4BitMode(data byte, wt WriteByteType) {
if wt&HighNibble != 0 {
g.setPins(data >> 4)
g.pulseEnable()
}

if wt&LowNibble != 0 {
g.setPins(data)
g.pulseEnable()
}
}

func (g *GPIO) pulseEnable() {
g.en.Low()
time.Sleep(time.Microsecond)
g.en.High()
g.setPins(data)
time.Sleep(time.Microsecond)
g.en.Low()
time.Sleep(100 * time.Microsecond)
}

// Read reads len(data) bytes from display RAM to data starting from RAM address counter position
Expand Down
9 changes: 4 additions & 5 deletions hd44780/hd44780.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (

type Buser interface {
io.ReadWriter
WriteHighNibble(data byte)
SetCommandMode(set bool)
WriteOnly() bool
}
Expand Down Expand Up @@ -115,19 +116,17 @@ func (d *Device) Configure(cfg Config) error {
time.Sleep(15 * time.Millisecond)

d.bus.SetCommandMode(true)
d.bus.Write([]byte{DATA_LENGTH_8BIT})
d.bus.WriteHighNibble(DATA_LENGTH_8BIT)
time.Sleep(5 * time.Millisecond)

for i := 0; i < 2; i++ {
d.bus.Write([]byte{DATA_LENGTH_8BIT})
d.bus.WriteHighNibble(DATA_LENGTH_8BIT)
time.Sleep(150 * time.Microsecond)

}

if d.datalength == DATA_LENGTH_4BIT {
d.bus.Write([]byte{DATA_LENGTH_4BIT})
d.bus.WriteHighNibble(DATA_LENGTH_4BIT)
}

// Busy flag is now accessible
d.SendCommand(memoryMap | cfg.Font | d.datalength)
d.SendCommand(DISPLAY_OFF)
Expand Down
Loading