Skip to content

Commit

Permalink
i2c: add commands to JHD1313MDriver
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Aug 15, 2018
1 parent eb7c7c5 commit 46dab20
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
34 changes: 32 additions & 2 deletions drivers/i2c/jhd1313m1_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package i2c

import (
"fmt"
"strconv"
"time"

"gobot.io/x/gobot"
Expand Down Expand Up @@ -74,6 +75,7 @@ type JHD1313M1Driver struct {
name string
connector Connector
Config
gobot.Commander
lcdAddress int
lcdConnection Connection
rgbAddress int
Expand All @@ -92,6 +94,7 @@ func NewJHD1313M1Driver(a Connector, options ...func(Config)) *JHD1313M1Driver {
name: gobot.DefaultName("JHD1313M1"),
connector: a,
Config: NewConfig(),
Commander: gobot.NewCommander(),
lcdAddress: 0x3E,
rgbAddress: 0x62,
}
Expand All @@ -100,6 +103,31 @@ func NewJHD1313M1Driver(a Connector, options ...func(Config)) *JHD1313M1Driver {
option(j)
}

j.AddCommand("SetRGB", func(params map[string]interface{}) interface{} {
r, _ := strconv.Atoi(params["r"].(string))
g, _ := strconv.Atoi(params["g"].(string))
b, _ := strconv.Atoi(params["b"].(string))
return j.SetRGB(r, g, b)
})
j.AddCommand("Clear", func(params map[string]interface{}) interface{} {
return j.Clear()
})
j.AddCommand("Home", func(params map[string]interface{}) interface{} {
return j.Home()
})
j.AddCommand("Write", func(params map[string]interface{}) interface{} {
msg := params["msg"].(string)
return j.Write(msg)
})
j.AddCommand("SetPosition", func(params map[string]interface{}) interface{} {
pos, _ := strconv.Atoi(params["pos"].(string))
return j.SetPosition(pos)
})
j.AddCommand("Scroll", func(params map[string]interface{}) interface{} {
lr, _ := strconv.ParseBool(params["lr"].(string))
return j.Scroll(lr)
})

return j
}

Expand Down Expand Up @@ -242,8 +270,10 @@ func (h *JHD1313M1Driver) SetPosition(pos int) (err error) {
return
}

func (h *JHD1313M1Driver) Scroll(leftToRight bool) error {
if leftToRight {
// Scroll sets the scrolling direction for the display, either left to right, or
// right to left.
func (h *JHD1313M1Driver) Scroll(lr bool) error {
if lr {
_, err := h.lcdConnection.Write([]byte{LCD_CMD, LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT})
return err
}
Expand Down
23 changes: 23 additions & 0 deletions drivers/i2c/jhd1313m1_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,26 @@ func TestJHD1313MDriverSetCustomCharWriteError(t *testing.T) {
data := [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
gobottest.Assert(t, d.SetCustomChar(0, data), errors.New("write error"))
}

func TestJHD1313MDriverCommands(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
d.Start()

err := d.Command("SetRGB")(map[string]interface{}{"r": "1", "g": "1", "b": "1"})
gobottest.Assert(t, err, nil)

err = d.Command("Clear")(map[string]interface{}{})
gobottest.Assert(t, err, nil)

err = d.Command("Home")(map[string]interface{}{})
gobottest.Assert(t, err, nil)

err = d.Command("Write")(map[string]interface{}{"msg": "Hello"})
gobottest.Assert(t, err, nil)

err = d.Command("SetPosition")(map[string]interface{}{"pos": "1"})
gobottest.Assert(t, err, nil)

err = d.Command("Scroll")(map[string]interface{}{"lr": "true"})
gobottest.Assert(t, err, nil)
}

0 comments on commit 46dab20

Please sign in to comment.