From 46dab20a2de94b7319b1bcfc6becf4ad8e2a3192 Mon Sep 17 00:00:00 2001 From: Ron Evans Date: Wed, 15 Aug 2018 12:25:07 +0200 Subject: [PATCH] i2c: add commands to JHD1313MDriver Signed-off-by: Ron Evans --- drivers/i2c/jhd1313m1_driver.go | 34 ++++++++++++++++++++++++++-- drivers/i2c/jhd1313m1_driver_test.go | 23 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/jhd1313m1_driver.go b/drivers/i2c/jhd1313m1_driver.go index 4aa44b36..1e55056b 100644 --- a/drivers/i2c/jhd1313m1_driver.go +++ b/drivers/i2c/jhd1313m1_driver.go @@ -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 } diff --git a/drivers/i2c/jhd1313m1_driver_test.go b/drivers/i2c/jhd1313m1_driver_test.go index c779e15e..424b5a7c 100644 --- a/drivers/i2c/jhd1313m1_driver_test.go +++ b/drivers/i2c/jhd1313m1_driver_test.go @@ -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) +}