Class: I2CDevice::HD44780

Inherits:
I2CDevice show all
Defined in:
lib/i2c/device/hd44780.rb

Overview

I2C interface with HD44780 compatible commands

Direct Known Subclasses

ACM1602NI, AQM0802A

Constant Summary collapse

MAP =
Hash[
	[
		"。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゙゚".split(//).map {|c|
			c.force_encoding(Encoding::BINARY)
		},
		(0b10100001..0b11011111).map {|c|
			c.chr
		}
	].transpose
]

Constants inherited from I2CDevice

VERSION

Instance Attribute Summary

Attributes inherited from I2CDevice

#address

Instance Method Summary collapse

Methods inherited from I2CDevice

#i2cget, #i2cset

Constructor Details

#initialize(args = {}) ⇒ HD44780

Returns a new instance of HD44780.


18
19
20
21
22
# File 'lib/i2c/device/hd44780.rb', line 18

def initialize(args={})
	super
	@lines = []
	initialize_lcd
end

Instance Method Details

#clear_displayObject Also known as: clear


85
86
87
88
89
# File 'lib/i2c/device/hd44780.rb', line 85

def clear_display
	@lines.clear
	i2cset(0, 0b00000001)
	sleep 2.16e-3
end

#cursor_or_display_shift(s_c, r_l) ⇒ Object

s_c
Integer

Cursor or display

0

Cursor shift

1

Display shift

r_l
Integer

Direction

0

Left

1

Right


129
130
131
132
# File 'lib/i2c/device/hd44780.rb', line 129

def cursor_or_display_shift(s_c, r_l)
	i2cset(0, 0b00010000 | (s_c<<3) | (r_l<<2))
	sleep 60e-6
end

#define_character(n, array) ⇒ Object

n
Integer

Character code.

array
Array

Character data.

Usage:

lcd.define_character(0, [
    0,1,1,1,0,
    1,0,0,0,1,
    1,1,0,1,1,
    1,0,1,0,1,
    1,1,0,1,1,
    1,0,0,0,1,
    1,0,0,0,1,
    0,1,1,1,0,
])

72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/i2c/device/hd44780.rb', line 72

def define_character(n, array)
	raise "n < 8" unless n < 8
	raise "array size must be 40 (5x8)" unless array.size == 40

	array = array.each_slice(5).map {|i|
		i.inject {|r,i| (r << 1) + i }
	}
	set_cgram_address(8 * n)
	sleep 60e-6
	i2cset(*array.map {|i| [0x80, i] }.flatten)
	sleep 60e-6
end

#display_on_off_control(d, c, b) ⇒ Object

d
Integer

Set entire display on/off

0

Off

1

On

c
Integer

Cursor on/off

0

Off

1

On

b
Integer

Blink cursor

0

Off

1

On


118
119
120
121
# File 'lib/i2c/device/hd44780.rb', line 118

def display_on_off_control(d, c, b)
	i2cset(0, 0b00001000 | (d<<2) | (c<<1) | (b))
	sleep 60e-6
end

#entry_mode_set(i_d, s) ⇒ Object

i_d
Integer

Increment or decrement

0

Decrement

1

Increment

s
Integer

Shift entire display

0

Right

1

Left


104
105
106
107
# File 'lib/i2c/device/hd44780.rb', line 104

def entry_mode_set(i_d, s)
	i2cset(0, 0b00000100 | (i_d<<1) | (s))
	sleep 60e-6
end

#function_set(dl, n, f) ⇒ Object

dl
Integer

Data length

0

4bit

1

8bit

n
Integer

Number of display lines

0

1-line

1

2-line

f
Integer

Character font

0

Normal

1

Double font


143
144
145
146
# File 'lib/i2c/device/hd44780.rb', line 143

def function_set(dl, n, f)
	i2cset(0, 0b00100000 | (dl<<4) | (n<<3) | (f<<2))
	sleep 60e-6
end

#initialize_lcdObject

Initialize LCD controller sequence Display is cleared.


26
27
28
29
30
31
32
33
34
35
# File 'lib/i2c/device/hd44780.rb', line 26

def initialize_lcd
	function_set(1, 1, 0)
	sleep 4.1e-3
	function_set(1, 1, 0)
	sleep 100e-6
	function_set(1, 1, 0)
	function_set(1, 1, 0)
	display_on_off_control(1, 0, 0)
	clear
end

#put_line(line, str, force = false) ⇒ Object

line
Integer

Line number

str
String

Display string

force
true | false

Write data forcely.

Note: This method keep previous put_line strings and does not write without change. You must specify force to override this behaviour.


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/i2c/device/hd44780.rb', line 43

def put_line(line, str, force=false)
	str.force_encoding(Encoding::BINARY)
	str.gsub!(/#{MAP.keys.join('|')}/, MAP)

	str = "%- 16s" % str

	if force || str != @lines[line]
		# set ddram address
		set_ddram_address(0x40 * line)
		sleep 60e-6
		i2cset(*str.unpack("C*").map {|i| [0x80, i] }.flatten)
		sleep 60e-6
	end
	@lines[line] = str
end

#read_busy_flag_and_addressObject

Returns
Hash

Result

:busy
true | false

Busy flag

:address_counter
Integer

Current address count. 7-bit


165
166
167
168
169
170
171
# File 'lib/i2c/device/hd44780.rb', line 165

def read_busy_flag_and_address
	read = i2cget(0b01000000)
	{
		:busy => (read & 0b10000000) != 0,
		:address_counter => read & 0b01111111
	}
end

#return_homeObject


93
94
95
96
# File 'lib/i2c/device/hd44780.rb', line 93

def return_home
	i2cset(0, 0b00000010)
	sleep 1.52e-3
end

#set_cgram_address(address) ⇒ Object

address
Integer

CGRAM address 6-bit


149
150
151
152
153
# File 'lib/i2c/device/hd44780.rb', line 149

def set_cgram_address(address)
	address = address & 0b00111111
	i2cset(0, 0b01000000 | address)
	sleep 60e-6
end

#set_ddram_address(address) ⇒ Object

address
Integer

DDRAM address 7-bit


156
157
158
159
160
# File 'lib/i2c/device/hd44780.rb', line 156

def set_ddram_address(address)
	address = address & 0b01111111
	i2cset(0, 0b10000000 | address)
	sleep 60e-6
end