diff --git a/platforms/i2c/i2c.go b/platforms/i2c/i2c.go index 8e680fe8..02697dbc 100644 --- a/platforms/i2c/i2c.go +++ b/platforms/i2c/i2c.go @@ -7,9 +7,10 @@ import ( ) var ( - ErrEncryptedBytes = errors.New("Encrypted bytes") - ErrNotEnoughBytes = errors.New("Not enough bytes read") - ErrNotReady = errors.New("Device is not ready") + ErrEncryptedBytes = errors.New("Encrypted bytes") + ErrNotEnoughBytes = errors.New("Not enough bytes read") + ErrNotReady = errors.New("Device is not ready") + ErrInvalidPosition = errors.New("Invalid position value") ) const ( diff --git a/platforms/i2c/jhd1313m1_driver.go b/platforms/i2c/jhd1313m1_driver.go index c31ec9c3..9eb2208a 100644 --- a/platforms/i2c/jhd1313m1_driver.go +++ b/platforms/i2c/jhd1313m1_driver.go @@ -34,6 +34,8 @@ const ( LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00 LCD_2LINE = 0x08 + + LCD_2NDLINEOFFSET = 0x40 ) var _ gobot.Driver = (*JHD1313M1Driver)(nil) @@ -145,6 +147,12 @@ func (h *JHD1313M1Driver) Home() (err error) { func (h *JHD1313M1Driver) Write(message string) (err error) { for _, val := range message { + if val == '\n' { + if err = h.SetPosition(16); err != nil { + return + } + continue + } if err = h.connection.I2cWrite(h.lcdAddress, []byte{0x40, byte(val)}); err != nil { break } @@ -152,6 +160,23 @@ func (h *JHD1313M1Driver) Write(message string) (err error) { return } +// SetPosition sets the cursor and the data display to pos. +// 0..15 are the positions in the first display line. +// 16..32 are the positions in the second display line. +func (h *JHD1313M1Driver) SetPosition(pos int) (err error) { + if pos < 0 || pos > 31 { + err = ErrInvalidPosition + return + } + offset := byte(pos) + if pos >= 16 { + offset -= 16 + offset |= LCD_2NDLINEOFFSET + } + err = h.command([]byte{LCD_SETDDRAMADDR | offset}) + return +} + func (h *JHD1313M1Driver) Halt() (errs []error) { return } func (h *JHD1313M1Driver) command(buf []byte) (err error) {