Class: I2C::Drivers::SS1602::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/i2c/drivers/ss1602/display.rb

Overview

Driver class for the SainSmart 1602 I2C LCD Display.

see github.com/andec/i2c

Parts copied from github.com/paulbarber/raspi-gpio/blob/master/lcd_display.py

Defined Under Namespace

Classes: BusDevice, Cursor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bus_or_bus_name, device_address) ⇒ Display

Returns a new instance of Display.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/i2c/drivers/ss1602/display.rb', line 16

def initialize(bus_or_bus_name, device_address)
  if bus_or_bus_name.respond_to?(:write)
    @device = BusDevice.new(bus_or_bus_name, device_address)
  else
    @device = BusDevice.new(I2C.create(bus_or_bus_name), device_address)
  end

  @rows = 2
  @cols = 16

  init_sequence

  @cursor = Cursor.new(self)
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



14
15
16
# File 'lib/i2c/drivers/ss1602/display.rb', line 14

def cols
  @cols
end

#cursorObject (readonly)

Returns the value of attribute cursor.



14
15
16
# File 'lib/i2c/drivers/ss1602/display.rb', line 14

def cursor
  @cursor
end

#rowsObject (readonly)

Returns the value of attribute rows.



14
15
16
# File 'lib/i2c/drivers/ss1602/display.rb', line 14

def rows
  @rows
end

Instance Method Details

#clearObject



31
32
33
34
# File 'lib/i2c/drivers/ss1602/display.rb', line 31

def clear
  write(COMMAND_CLEARDISPLAY)
  write(COMMAND_RETURNHOME)
end

#offObject



58
59
60
# File 'lib/i2c/drivers/ss1602/display.rb', line 58

def off
  write(COMMAND_DISPLAYCONTROL | FLAG_DISPLAYOFF)
end

#onObject



54
55
56
# File 'lib/i2c/drivers/ss1602/display.rb', line 54

def on
  write(COMMAND_DISPLAYCONTROL | FLAG_DISPLAYON)
end

#text(string, row, pad = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/i2c/drivers/ss1602/display.rb', line 36

def text(string, row, pad = false)
  case row
  when 0
    write(0x80)
  when 1
    write(0xC0)
  else
    raise "Only rows #{0..(@rows - 1)} are supported"
  end

  # Right-pad with spaces so that the line only shows string
  string = sprintf('%-1$*2$s', string, @cols) if pad

  string.each_char do |c|
    write(c.ord, BIT_RS)
  end
end

#write(cmd, mode = 0) ⇒ Object

Send a low-level command to the display



65
66
67
68
# File 'lib/i2c/drivers/ss1602/display.rb', line 65

def write(cmd, mode = 0)
  write_four_bits(mode | (cmd & 0xF0))
  write_four_bits(mode | ((cmd << 4) & 0xF0))
end