Class: Denko::Display::HD44780

Inherits:
Object
  • Object
show all
Includes:
Behaviors::Lifecycle, Behaviors::MultiPin
Defined in:
lib/denko/display/hd44780.rb

Constant Summary collapse

LCD_CLEARDISPLAY =

Commands

0x01
LCD_RETURNHOME =
0x02
LCD_ENTRYMODESET =
0x04
LCD_DISPLAYCONTROL =
0x08
LCD_CURSORSHIFT =
0x10
LCD_FUNCTIONSET =
0x20
LCD_SETCGRAMADDR =
0x40
LCD_SETDDRAMADDR =
0x80
LCD_ENTRYRIGHT =

Flags for display entry mode

0x00
LCD_ENTRYLEFT =
0x02
LCD_ENTRYSHIFTINCREMENT =
0x01
LCD_ENTRYSHIFTDECREMENT =
0x00
LCD_DISPLAYON =

Flags for display on/off control

0x04
LCD_DISPLAYOFF =
0x00
LCD_CURSORON =
0x02
LCD_CURSOROFF =
0x00
LCD_BLINKON =
0x01
LCD_BLINKOFF =
0x00
LCD_DISPLAYMOVE =

Flags for display/cursor shift

0x08
LCD_CURSORMOVE =
0x00
LCD_MOVERIGHT =
0x04
LCD_MOVELEFT =
0x00
LCD_8BITMODE =

Flags for function set

0x10
LCD_4BITMODE =
0x00
LCD_2LINE =
0x08
LCD_1LINE =
0x00
LCD_5x10DOTS =
0x04
LCD_5x8DOTS =
0x00
CONTROL_TOGGLES =

Create a #key_on and #key_off method for each feature in this hash, using the constant in the value to send a control signal.

Eg. #display_on and #display_off.

{
  "display" => LCD_DISPLAYON,
  "cursor"  =>  LCD_CURSORON,
  "blink"   =>  LCD_BLINKON,
}

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Instance Attribute Summary collapse

Attributes included from Behaviors::MultiPin

#pin, #pins, #proxies

Attributes included from Behaviors::Component

#board, #params

Attributes included from Behaviors::State

#state

Instance Method Summary collapse

Methods included from Behaviors::Lifecycle

included

Methods included from Behaviors::MultiPin

#convert_pins, #proxy_pin, #proxy_states, #require_pin, #require_pins

Methods included from Behaviors::Component

#initialize, #micro_delay

Methods included from Behaviors::State

#update_state

Instance Attribute Details

#columnsObject

Default to 16x2 display if columns and rows given.



55
56
57
# File 'lib/denko/display/hd44780.rb', line 55

def columns
  @columns ||= params[:columns] || 16
end

#controlObject

Start with cursor off and no cursor blink.



78
79
80
# File 'lib/denko/display/hd44780.rb', line 78

def control
  @control ||= LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
end

#data_linesObject



50
51
52
# File 'lib/denko/display/hd44780.rb', line 50

def data_lines
  @data_lines ||= 4
end

#entry_modeObject



82
83
84
# File 'lib/denko/display/hd44780.rb', line 82

def entry_mode
  @entry_mode ||= LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
end

#functionObject

Fuction set byte to set up the LCD. These OR’ed defaults == 0x00.



64
65
66
# File 'lib/denko/display/hd44780.rb', line 64

def function
  @function ||= LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS
end

#row_offsetsObject

Offset memory address when moving cursor. Row 2 always starts at memory address 0x40. For 4 line LCDs:

Row 3 is immediately after row 1, +16 or 20 bytes, depending on columns.
Row 4 is immediately after row 2, +16 or 20 bytes, depending on columns.


73
74
75
# File 'lib/denko/display/hd44780.rb', line 73

def row_offsets
  @row_offsets ||= [0x00, 0x40, 0x00+columns, 0x40+columns]
end

#rowsObject



59
60
61
# File 'lib/denko/display/hd44780.rb', line 59

def rows
  @rows ||= params[:rows] || 2
end

Instance Method Details

#autoscroll_offObject



222
223
224
225
# File 'lib/denko/display/hd44780.rb', line 222

def autoscroll_off
  self.entry_mode &= ~LCD_ENTRYSHIFTINCREMENT;
  command(LCD_ENTRYMODESET | entry_mode);
end

#autoscroll_onObject



217
218
219
220
# File 'lib/denko/display/hd44780.rb', line 217

def autoscroll_on
  self.entry_mode |= LCD_ENTRYSHIFTINCREMENT;
  command(LCD_ENTRYMODESET | entry_mode);
end

#clearObject



158
159
160
161
# File 'lib/denko/display/hd44780.rb', line 158

def clear
  command(LCD_CLEARDISPLAY)
  micro_delay(2000)
end

#command(byte) ⇒ Object



234
235
236
# File 'lib/denko/display/hd44780.rb', line 234

def command(byte)
  send(byte, board.low)
end

#create_char(location, bitmap) ⇒ Object

Define custom characters as bitmaps.



228
229
230
231
232
# File 'lib/denko/display/hd44780.rb', line 228

def create_char(location, bitmap)
  location &= 0x7
  command(LCD_SETCGRAMADDR | (location << 3))
  bitmap.each { |byte| write byte }
end

#homeObject



163
164
165
166
# File 'lib/denko/display/hd44780.rb', line 163

def home
  command(LCD_RETURNHOME)
  micro_delay(2000)
end

#initialize_pins(options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/denko/display/hd44780.rb', line 88

def initialize_pins(options={})
  # All the required pins.
  [:rs, :enable, :d4, :d5, :d6, :d7].each do |symbol|
    proxy_pin(symbol, DigitalIO::Output)
  end

  # If any of d0-d3 was given, make them all non-optional.
  lower_bits_optional = (self.pins.keys & [:d0, :d1, :d2, :d3]).empty?
  [:d0, :d1, :d2, :d3].each do |symbol|
    proxy_pin(symbol, DigitalIO::Output, optional: lower_bits_optional)
  end

  # RW pin can be hardwired to GND, or given. Will be always pulled low.
  proxy_pin :rw, DigitalIO::Output, optional: true

  # Backlight can be hardwired, given here, or modeled as a separate component.
  # If HD44780 is on a digital register, and PWM is desired, use a separate component.
  proxy_pin :backlight, LED::Base, optional: true
end

#left_to_rightObject



199
200
201
202
# File 'lib/denko/display/hd44780.rb', line 199

def left_to_right
  self.entry_mode |= LCD_ENTRYLEFT
  command(LCD_ENTRYMODESET | entry_mode)
end

#pulse_enableObject



303
304
305
306
307
308
309
310
# File 'lib/denko/display/hd44780.rb', line 303

def pulse_enable
  enable.low
  micro_delay 1
  enable.high
  micro_delay 1
  enable.low
  micro_delay 100
end

#right_to_leftObject



204
205
206
207
# File 'lib/denko/display/hd44780.rb', line 204

def right_to_left
  self.entry_mode &= ~LCD_ENTRYLEFT
  command(LCD_ENTRYMODESET | entry_mode)
end

#scroll_leftObject



209
210
211
# File 'lib/denko/display/hd44780.rb', line 209

def scroll_left
  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT)
end

#scroll_rightObject



213
214
215
# File 'lib/denko/display/hd44780.rb', line 213

def scroll_right
  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT)
end

#send(byte, rs_level) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/denko/display/hd44780.rb', line 242

def send(byte, rs_level)
  # RS pin goes low to send commands, high to send data.
  rs.write(rs_level) unless rs.state == rs_level

  # Get the byte as a string of 0s and 1s, LSBFIRST.
  bits_from_byte = byte.to_s(2).rjust(8, "0").reverse

  # Write bits depending on connection.
  data_lines == 8 ? write8(bits_from_byte) : write4(bits_from_byte)
end

#text(str) ⇒ Object



175
176
177
# File 'lib/denko/display/hd44780.rb', line 175

def text(str)
  str.each_byte { |b| write b }
end

#text_cursor(col, row) ⇒ Object Also known as: move_to



168
169
170
171
172
# File 'lib/denko/display/hd44780.rb', line 168

def text_cursor(col, row)
  # Limit to the highest row, 0 indexed.
  row = (rows - 1) if row > (rows - 1)
  command(LCD_SETDDRAMADDR | (col + row_offsets[row]))
end

#write(byte) ⇒ Object



238
239
240
# File 'lib/denko/display/hd44780.rb', line 238

def write(byte)
  send(byte, board.high)
end

#write4(bits) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/denko/display/hd44780.rb', line 253

def write4(bits)
  if board.is_a_register?
    board.bit_set(d4.pin, bits[4].to_i)
    board.bit_set(d5.pin, bits[5].to_i)
    board.bit_set(d6.pin, bits[6].to_i)
    d7.write bits[7].to_i
    pulse_enable
    board.bit_set(d4.pin, bits[0].to_i)
    board.bit_set(d5.pin, bits[1].to_i)
    board.bit_set(d6.pin, bits[2].to_i)
    d7.write bits[3].to_i
    pulse_enable
  else
    d4.write bits[4].to_i
    d5.write bits[5].to_i
    d6.write bits[6].to_i
    d7.write bits[7].to_i
    pulse_enable
    d4.write bits[0].to_i
    d5.write bits[1].to_i
    d6.write bits[2].to_i
    d7.write bits[3].to_i
    pulse_enable
  end
end

#write8(bits) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/denko/display/hd44780.rb', line 279

def write8(bits)
  if board.is_a_register?
    board.bit_set(d0.pin, bits[0].to_i)
    board.bit_set(d1.pin, bits[1].to_i)
    board.bit_set(d2.pin, bits[2].to_i)
    board.bit_set(d3.pin, bits[3].to_i)
    board.bit_set(d4.pin, bits[4].to_i)
    board.bit_set(d5.pin, bits[5].to_i)
    board.bit_set(d6.pin, bits[6].to_i)
    d7.write bits[7].to_i
    pulse_enable
  else
    d0.write bits[0].to_i
    d1.write bits[1].to_i
    d2.write bits[2].to_i
    d3.write bits[3].to_i
    d4.write bits[4].to_i
    d5.write bits[5].to_i
    d6.write bits[6].to_i
    d7.write bits[7].to_i
    pulse_enable
  end
end