Class: 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
  }
]

Constants inherited from I2CDevice

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

#clearObject



75
76
77
78
# File 'lib/i2c/device/hd44780.rb', line 75

def clear
  @lines.clear
  clear_display
end

#clear_displayObject



80
81
82
83
# File 'lib/i2c/device/hd44780.rb', line 80

def clear_display
  i2cset(0, 0b00000001)
  sleep 2.16e-3
end

#cursor_or_display_shift(s_c, r_l) ⇒ Object



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

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

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, ])



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/i2c/device/hd44780.rb', line 62

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: set entire display on/off c: cursor on/off b: blink cursor



100
101
102
103
# File 'lib/i2c/device/hd44780.rb', line 100

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 : increment or decrement: 1: increment, 0: decrement s : shift entire display: 1: left, 0: right



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

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 data_length: 1: 8bit, 0: 4bit n number_of_display_lines: 1: 2-line, 0: 1-line f character_font: 1: double font, 0: normal



113
114
115
116
# File 'lib/i2c/device/hd44780.rb', line 113

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

#initialize_lcdObject



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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/i2c/device/hd44780.rb', line 35

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



130
131
132
133
134
135
136
# File 'lib/i2c/device/hd44780.rb', line 130

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

#return_homeObject



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

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

#set_cgram_address(address) ⇒ Object



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

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

#set_ddram_address(address) ⇒ Object



124
125
126
127
128
# File 'lib/i2c/device/hd44780.rb', line 124

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