hd44780

package
v0.0.0-...-11ea3f4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2018 License: BSD-3-Clause Imports: 1 Imported by: 0

README

HD44780 over I2C

This provides control of an HD44780-compatible device that is connected via an I2C expander.

Usage

Import the packages:

// import the require modules
import(
	"github.com/mrmorphic/hwio"
	"github.com/mrmorphic/hwio/devices/hd44780"
)

Initialise by fetching an i2c module from the driver. You can get instances of devices attached to the bus.

// Get the i2c module from the driver. i2c is the canonical name. On the BeagleBone, it can also
// be referred to as i2c2.
m, e := hwio.GetModule("i2c")

// Assert that it is an I2C module
i2c := m.(hwio.I2CModule)

Get the HD44780 device, so you make requests of it:

// Get a display device on this i2c bus. You need to provide it a device profile. Two profiles
// are currently implemented, PROFILE_MJKDZ and PROFILE_PCF8574, corresponding to two commonly found
// port extenders used to interface to LCD displays.
display := hd44780.NewHD44780(i2c, 0x20, hd44780.PROFILE_MJKDZ)

// Initialise the display with the size of display you have.
display.Init(20, 4)

// The display may not show anything if the backlight is turned off
display.SetBacklight(true)

// If you want to see the cursor, you can turn it on
display.Cursor()

To display things, you can:

// clear the display
display.Clear()

// Set cursor back to (0,0)
display.Home()

// Set cursor to a specific column and row (both zero based)
display.SetCursor(0, 1)  // second line

// output a single character
display.Data('A')

// Use any function that expects a Writer to output to the display. This is because the HD44780 type
// implements Writer interface.
fmt.Fprintf(display, "Hi, %s", name)

Note that characters that are output to the device are not necessarily displayed consequetively. In particular wrapping may not work as you expect. This is because of how the display unit maps it's display buffer to positions on the screen. This is described in the datasheet for the HD44780 unit.

An alternative way to create the device is to use NewHD44780Extended instead of NewHD44780. This is useful if you have an i2c extender that does not confirm to the builtin profiles:

display := NewHD44780Extended(i2c, 0x27,
	0, // en
	1, // rw
	2, // rs
	4, // d4
	5, // d5
	6, // d6
	7, // d7polarity int) *HD44780 {
	3, // backlight,
	hd44780.POSITIVE) // polarity

The pin values are the bit positions for that pin, with 7 being MSB and0 being LSB. The underlying assumption is that the port extender is 8 bit. This package will not work for 16 bit extenders, for example.

Notes

This has been tested on an mjkdz i2c expander and a 20x4 character display, and works correctly. Other LCD i2c expanders may map En, Rw and Rs pins differently, however. If you have issues with a different I2C device, let me know, or submit a pull request with the configuration that works for you.

Documentation

Index

Constants

View Source
const (
	// commands
	LCD_CLEARDISPLAY   byte = 0x01
	LCD_RETURNHOME     byte = 0x02
	LCD_ENTRYMODESET   byte = 0x04
	LCD_DISPLAYCONTROL byte = 0x08
	LCD_CURSORSHIFT    byte = 0x10
	LCD_FUNCTIONSET    byte = 0x20
	LCD_SETCGRAMADDR   byte = 0x40
	LCD_SETDDRAMADDR   byte = 0x80

	// flags for display entry mode
	LCD_ENTRYRIGHT          byte = 0x00
	LCD_ENTRYLEFT           byte = 0x02
	LCD_ENTRYSHIFTINCREMENT byte = 0x01
	LCD_ENTRYSHIFTDECREMENT byte = 0x00

	// flags for display on/off control
	LCD_DISPLAYON  byte = 0x04
	LCD_DISPLAYOFF byte = 0x00
	LCD_CURSORON   byte = 0x02
	LCD_CURSOROFF  byte = 0x00
	LCD_BLINKON    byte = 0x01
	LCD_BLINKOFF   byte = 0x00

	// flags for display/cursor shift
	LCD_DISPLAYMOVE byte = 0x08
	LCD_CURSORMOVE  byte = 0x00
	LCD_MOVERIGHT   byte = 0x04
	LCD_MOVELEFT    byte = 0x00

	// flags for function set
	LCD_8BITMODE byte = 0x10
	LCD_4BITMODE byte = 0x00
	LCD_2LINE    byte = 0x08
	LCD_1LINE    byte = 0x00
	LCD_5x10DOTS byte = 0x04
	LCD_5x8DOTS  byte = 0x00

	// constants for backlight polarity
	POSITIVE = 0
	NEGATIVE = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HD44780

type HD44780 struct {
	// contains filtered or unexported fields
}

func NewHD44780

func NewHD44780(module hwio.I2CModule, address int, profile I2CExpanderProfile) *HD44780

func NewHD44780Extended

func NewHD44780Extended(module hwio.I2CModule, address int, en int, rw int, rs int, d4 int, d5 int, d6 int, d7 int, bl int, polarity int) *HD44780

func (*HD44780) Autoscroll

func (display *HD44780) Autoscroll()

This will 'right justify' text from the cursor

func (display *HD44780) Blink()

func (*HD44780) Clear

func (display *HD44780) Clear()

func (*HD44780) Command

func (display *HD44780) Command(command byte)

func (*HD44780) Cursor

func (display *HD44780) Cursor()

func (*HD44780) Data

func (display *HD44780) Data(data byte)

func (*HD44780) Display

func (display *HD44780) Display()

func (*HD44780) Home

func (display *HD44780) Home()

func (*HD44780) Init

func (display *HD44780) Init(cols int, lines int)

func (*HD44780) LeftToRight

func (display *HD44780) LeftToRight()

This is for text that flows Left to Right

func (*HD44780) NoAutoscroll

func (display *HD44780) NoAutoscroll()

This will 'left justify' text from the cursor

func (display *HD44780) NoBlink()

Turn on and off the blinking cursor

func (*HD44780) NoCursor

func (display *HD44780) NoCursor()

Turns the underline cursor on/off

func (*HD44780) NoDisplay

func (display *HD44780) NoDisplay()

Turn the display on/off (quickly)

func (*HD44780) RightToLeft

func (display *HD44780) RightToLeft()

This is for text that flows Right to Left

func (*HD44780) ScrollDisplayLeft

func (display *HD44780) ScrollDisplayLeft()

These commands scroll the display without changing the RAM

func (*HD44780) ScrollDisplayRight

func (display *HD44780) ScrollDisplayRight()

func (*HD44780) SetBacklight

func (display *HD44780) SetBacklight(on bool)

func (*HD44780) SetCursor

func (display *HD44780) SetCursor(col int, row int)

func (*HD44780) Write

func (display *HD44780) Write(p []byte) (n int, err error)

type I2CExpanderProfile

type I2CExpanderProfile int
const (

	// mjkdz devices are commonly found in the wild
	PROFILE_MJKDZ I2CExpanderProfile = iota

	// devices based on PCF8574 are also around, but wired a little bit differently.
	PROFILE_PCF8574
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL