Skip to content

Commit

Permalink
introduce adafruit1109 2x16 LCD with 5 keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored and deadprogram committed Apr 17, 2022
1 parent cac6338 commit 4565cc3
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 26 deletions.
71 changes: 49 additions & 22 deletions drivers/gpio/hd44780_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package gpio

import (
"errors"
"gobot.io/x/gobot"
"fmt"
"sync"
"time"

"gobot.io/x/gobot"
)

const (
Expand Down Expand Up @@ -71,12 +74,14 @@ type HD44780Driver struct {
busMode HD44780BusMode
pinRS *DirectPinDriver
pinEN *DirectPinDriver
pinRW *DirectPinDriver
pinDataBits []*DirectPinDriver
displayCtrl int
displayFunc int
displayMode int
connection gobot.Connection
gobot.Commander
mutex *sync.Mutex
}

// NewHD44780Driver return a new HD44780Driver
Expand All @@ -97,6 +102,7 @@ func NewHD44780Driver(a gobot.Connection, cols int, rows int, busMode HD44780Bus
pinEN: NewDirectPinDriver(a, pinEN),
connection: a,
Commander: gobot.NewCommander(),
mutex: &sync.Mutex{},
}

if h.busMode == HD44780_4BITMODE {
Expand Down Expand Up @@ -152,23 +158,27 @@ func (h *HD44780Driver) Start() (err error) {

time.Sleep(50 * time.Millisecond)

if err := h.activateWriteMode(); err != nil {
return err
}

if h.busMode == HD44780_4BITMODE {
if err := h.writeBits(0x03); err != nil {
if err := h.writeDataPins(0x03); err != nil {
return err
}
time.Sleep(5 * time.Millisecond)

if err := h.writeBits(0x03); err != nil {
if err := h.writeDataPins(0x03); err != nil {
return err
}
time.Sleep(100 * time.Microsecond)

if err := h.writeBits(0x03); err != nil {
if err := h.writeDataPins(0x03); err != nil {
return err
}
time.Sleep(100 * time.Microsecond)

if err := h.writeBits(0x02); err != nil {
if err := h.writeDataPins(0x02); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -217,6 +227,11 @@ func (h *HD44780Driver) Start() (err error) {
return h.Clear()
}

// SetRWPin initializes the optional RW pin
func (h *HD44780Driver) SetRWPin(pinRW string) {
h.pinRW = NewDirectPinDriver(h.connection, pinRW)
}

// Write output text to the display
func (h *HD44780Driver) Write(message string) (err error) {
col := 0
Expand Down Expand Up @@ -264,7 +279,7 @@ func (h *HD44780Driver) Home() (err error) {
// SetCursor move the cursor to the specified position
func (h *HD44780Driver) SetCursor(col int, row int) (err error) {
if col < 0 || row < 0 || col >= h.cols || row >= h.rows {
return errors.New("Invalid position value")
return fmt.Errorf("Invalid position value (%d, %d), range (%d, %d)", col, row, h.cols-1, h.rows-1)
}

return h.SendCommand(HD44780_SETDDRAMADDR | col + h.rowOffsets[row])
Expand Down Expand Up @@ -327,30 +342,41 @@ func (h *HD44780Driver) RightToLeft() (err error) {

// SendCommand send control command
func (h *HD44780Driver) SendCommand(data int) (err error) {
h.mutex.Lock()
defer h.mutex.Unlock()
if err := h.activateWriteMode(); err != nil {
return err
}
if err := h.pinRS.Off(); err != nil {
return err
}
if h.busMode == HD44780_4BITMODE {
if err := h.writeBits(data >> 4); err != nil {
if err := h.writeDataPins(data >> 4); err != nil {
return err
}
}

return h.writeBits(data)
return h.writeDataPins(data)
}

// WriteChar output a character to the display
func (h *HD44780Driver) WriteChar(data int) (err error) {
h.mutex.Lock()
defer h.mutex.Unlock()
if err := h.activateWriteMode(); err != nil {
return err
}

if err := h.pinRS.On(); err != nil {
return err
}
if h.busMode == HD44780_4BITMODE {
if err := h.writeBits(data >> 4); err != nil {
if err := h.writeDataPins(data >> 4); err != nil {
return err
}
}

return h.writeBits(data)
return h.writeDataPins(data)
}

// CreateChar create custom character
Expand All @@ -372,8 +398,14 @@ func (h *HD44780Driver) CreateChar(pos int, charMap [8]byte) (err error) {
return nil
}

// WriteBits output data to data-pins
func (h *HD44780Driver) writeBits(data int) (err error) {
func (h *HD44780Driver) activateWriteMode() (err error) {
if h.pinRW == nil {
return
}
return h.pinRW.Off()
}

func (h *HD44780Driver) writeDataPins(data int) (err error) {
for i, pin := range h.pinDataBits {
if ((data >> i) & 0x01) == 0x01 {
if err := pin.On(); err != nil {
Expand All @@ -385,17 +417,11 @@ func (h *HD44780Driver) writeBits(data int) (err error) {
}
}
}

return h.triggerPulse()
return h.fallingEdge()
}

// triggerPulse trigger enable pulse
func (h *HD44780Driver) triggerPulse() (err error) {
if err := h.pinEN.Off(); err != nil {
return err
}
time.Sleep(1 * time.Microsecond)

// fallingEdge creates falling edge to trigger data transmission
func (h *HD44780Driver) fallingEdge() (err error) {
if err := h.pinEN.On(); err != nil {
return err
}
Expand All @@ -404,7 +430,8 @@ func (h *HD44780Driver) triggerPulse() (err error) {
if err := h.pinEN.Off(); err != nil {
return err
}
time.Sleep(1 * time.Microsecond)
// fastest write operation at 190kHz mode takes 53 us
time.Sleep(60 * time.Microsecond)

return nil
}
8 changes: 4 additions & 4 deletions drivers/gpio/hd44780_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ func TestHD44780DriverSetCursor(t *testing.T) {
func TestHD44780DriverSetCursorInvalid(t *testing.T) {
d := initTestHD44780Driver()
d.Start()
gobottest.Assert(t, d.SetCursor(-1, 3), errors.New("Invalid position value"))
gobottest.Assert(t, d.SetCursor(2, 3), errors.New("Invalid position value"))
gobottest.Assert(t, d.SetCursor(0, -1), errors.New("Invalid position value"))
gobottest.Assert(t, d.SetCursor(0, 16), errors.New("Invalid position value"))
gobottest.Assert(t, d.SetCursor(-1, 3), errors.New("Invalid position value (-1, 3), range (1, 15)"))
gobottest.Assert(t, d.SetCursor(2, 3), errors.New("Invalid position value (2, 3), range (1, 15)"))
gobottest.Assert(t, d.SetCursor(0, -1), errors.New("Invalid position value (0, -1), range (1, 15)"))
gobottest.Assert(t, d.SetCursor(0, 16), errors.New("Invalid position value (0, 16), range (1, 15)"))
}

func TestHD44780DriverDisplayOn(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions drivers/i2c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go get -d -u gobot.io/x/gobot/...
## Hardware Support
Gobot has a extensible system for connecting to hardware devices. The following i2c devices are currently supported:

- Adafruit 2x16 RGB-LCD with 5 keys
- Adafruit Motor Hat
- ADS1015 Analog to Digital Converter
- ADS1115 Analog to Digital Converter
Expand Down
Loading

0 comments on commit 4565cc3

Please sign in to comment.