i2c: add commands to JHD1313MDriver

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2018-08-15 12:25:07 +02:00
parent eb7c7c5689
commit 46dab20a2d
2 changed files with 55 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package i2c
import (
"fmt"
"strconv"
"time"
"gobot.io/x/gobot"
@ -74,6 +75,7 @@ type JHD1313M1Driver struct {
name string
connector Connector
Config
gobot.Commander
lcdAddress int
lcdConnection Connection
rgbAddress int
@ -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,
}
@ -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
}
@ -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
}

View File

@ -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)
}