1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/drivers/gpio/hd44780_driver_test.go

239 lines
5.8 KiB
Go
Raw Normal View History

2020-03-15 13:27:08 +09:00
package gpio
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
2020-03-15 13:27:08 +09:00
)
var _ gobot.Driver = (*HD44780Driver)(nil)
// --------- HELPERS
func initTestHD44780Driver() *HD44780Driver {
d, _ := initTestHD44780Driver4BitModeWithStubbedAdaptor()
if err := d.Start(); err != nil {
panic(err)
}
return d
2020-03-15 13:27:08 +09:00
}
func initTestHD44780Driver4BitModeWithStubbedAdaptor() (*HD44780Driver, *gpioTestAdaptor) {
adaptor := newGpioTestAdaptor()
dataPins := HD44780DataPin{
D4: "22",
D5: "18",
D6: "16",
D7: "12",
}
return NewHD44780Driver(adaptor, 2, 16, HD44780_4BITMODE, "13", "15", dataPins), adaptor
}
func initTestHD44780Driver8BitModeWithStubbedAdaptor() (*HD44780Driver, *gpioTestAdaptor) {
adaptor := newGpioTestAdaptor()
dataPins := HD44780DataPin{
D0: "31",
D1: "33",
D2: "35",
D3: "37",
D4: "22",
D5: "18",
D6: "16",
D7: "12",
}
return NewHD44780Driver(adaptor, 2, 16, HD44780_8BITMODE, "13", "15", dataPins), adaptor
}
// --------- TESTS
func TestHD44780Driver(t *testing.T) {
d, _ := initTestHD44780Driver4BitModeWithStubbedAdaptor()
var a interface{} = d
2020-03-15 13:27:08 +09:00
_, ok := a.(*HD44780Driver)
if !ok {
t.Errorf("NewHD44780Driver() should have returned a *HD44780Driver")
}
}
func TestHD44780DriverHalt(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Halt())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverDefaultName(t *testing.T) {
d, _ := initTestHD44780Driver4BitModeWithStubbedAdaptor()
assert.True(t, strings.HasPrefix(d.Name(), "HD44780Driver"))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverSetName(t *testing.T) {
d, _ := initTestHD44780Driver4BitModeWithStubbedAdaptor()
2020-03-15 13:27:08 +09:00
d.SetName("my driver")
assert.Equal(t, "my driver", d.Name())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverStart(t *testing.T) {
d, _ := initTestHD44780Driver4BitModeWithStubbedAdaptor()
require.NoError(t, d.Start())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverStartError(t *testing.T) {
a := newGpioTestAdaptor()
var pins HD44780DataPin
var d *HD44780Driver
pins = HD44780DataPin{
D4: "22",
D5: "18",
D6: "16",
D7: "",
}
d = NewHD44780Driver(a, 2, 16, HD44780_4BITMODE, "13", "15", pins)
require.ErrorContains(t, d.Start(), "Initialization error")
2020-03-15 13:27:08 +09:00
pins = HD44780DataPin{
D0: "31",
D1: "33",
D2: "35",
D3: "37",
D4: "22",
D5: "18",
D6: "16",
D7: "",
}
d = NewHD44780Driver(a, 2, 16, HD44780_8BITMODE, "13", "15", pins)
require.ErrorContains(t, d.Start(), "Initialization error")
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverWrite(t *testing.T) {
var d *HD44780Driver
d, _ = initTestHD44780Driver4BitModeWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Write("hello gobot"))
2020-03-15 13:27:08 +09:00
d, _ = initTestHD44780Driver8BitModeWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Write("hello gobot"))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverWriteError(t *testing.T) {
var d *HD44780Driver
var a *gpioTestAdaptor
d, a = initTestHD44780Driver4BitModeWithStubbedAdaptor()
a.digitalWriteFunc = func(string, byte) error {
2020-03-15 13:27:08 +09:00
return errors.New("write error")
}
_ = d.Start()
require.ErrorContains(t, d.Write("hello gobot"), "write error")
2020-03-15 13:27:08 +09:00
d, a = initTestHD44780Driver8BitModeWithStubbedAdaptor()
a.digitalWriteFunc = func(string, byte) error {
2020-03-15 13:27:08 +09:00
return errors.New("write error")
}
_ = d.Start()
require.ErrorContains(t, d.Write("hello gobot"), "write error")
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverClear(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Clear())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverHome(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Home())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverSetCursor(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.SetCursor(0, 3))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverSetCursorInvalid(t *testing.T) {
d := initTestHD44780Driver()
require.ErrorContains(t, d.SetCursor(-1, 3), "Invalid position value (-1, 3), range (1, 15)")
require.ErrorContains(t, d.SetCursor(2, 3), "Invalid position value (2, 3), range (1, 15)")
require.ErrorContains(t, d.SetCursor(0, -1), "Invalid position value (0, -1), range (1, 15)")
require.ErrorContains(t, d.SetCursor(0, 16), "Invalid position value (0, 16), range (1, 15)")
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverDisplayOn(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Display(true))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverDisplayOff(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Display(false))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverCursorOn(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Cursor(true))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverCursorOff(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Cursor(false))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverBlinkOn(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Blink(true))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverBlinkOff(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Blink(false))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverScrollLeft(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.ScrollLeft())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverScrollRight(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.ScrollRight())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverLeftToRight(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.LeftToRight())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverRightToLeft(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.RightToLeft())
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverSendCommand(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.SendCommand(0x33))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverWriteChar(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.WriteChar(0x41))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverCreateChar(t *testing.T) {
d := initTestHD44780Driver()
charMap := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
require.NoError(t, d.CreateChar(0, charMap))
2020-03-15 13:27:08 +09:00
}
func TestHD44780DriverCreateCharError(t *testing.T) {
d := initTestHD44780Driver()
charMap := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
require.ErrorContains(t, d.CreateChar(8, charMap), "can't set a custom character at a position greater than 7")
2020-03-15 13:27:08 +09:00
}