Class: OLED

Inherits:
Object
  • Object
show all
Defined in:
lib/oled-control/oled.rb,
ext/oled-control/oled-control-gem.c

Constant Summary collapse

NORMAL =
0x06
FLIPPED =
0x05

Instance Method Summary collapse

Constructor Details

#initialize(i2c_bus = '/dev/i2c-1', i2c_address = 0x3c, flipped = false) ⇒ OLED

Returns a new instance of OLED.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/oled-control/oled.rb', line 7

def initialize(i2c_bus = '/dev/i2c-1', i2c_address = 0x3c, flipped = false)
  @flipped = flipped

  # the OLED display defines its own weird character set
  @character_conversion = {
      0xE5 => 0xAF, # å
      0xC5 => 0xAE, # Å
      0xF8 => 0xAC, # ø
      0xD8 => 0xAB, # Ø
      0xE6 => 0xBD, # æ
      0xC6 => 0xBC, # Æ
      0xD6 => 0x5C, # Ö
      0xF6 => 0x7C, # ö
      0xF1 => 0x7D, # ñ
      0xD1 => 0x5D, # Ñ
      0xE4 => 0x7B, # ä
      0xC4 => 0x5B, # Ä
      0xFC => 0x7E, # ü
      0xDC => 0x5E, # Ü
      0xE9 => 0xA5, # é
      0xC9 => 0xBF, # É
      0xED => 0xE8, # í
      0xCD => 0xE3, # Í
      0xDF => 0xBE, # ß
      0xE1 => 0xE7, # á
      0xC1 => 0xE2, # Á
      0xEA => 0xC7, # ê
      0xCA => 0xC6, # Ê
      0x40 => 0xA0, # @
      0xA4 => 0x24, # ¤
      0x24 => 0xA2, # $
      0xA3 => 0xA1, # £
      0xFA => 0xEA, # ú
      0xDA => 0xE5, # Ú
      # unsupported characters are mapped to closest match
      0xEF => 0x69, # ï -> i
      0xCF => 0x49, # Ï -> I
      0xEB => 0x65, # ë -> e
      0xCB => 0x45, # Ë -> E
      0xE3 => 0x61, # ã -> a
      0xC3 => 0x41, # Ã -> A
  }
  self.init(i2c_bus, i2c_address)
end

Instance Method Details

#clearObject



40
41
42
43
44
45
# File 'ext/oled-control/oled-control-gem.c', line 40

static VALUE clear(VALUE self) {
    if(!clear_display())
        rb_raise(rb_eRuntimeError, "failure clearing display");

    return Qtrue;
}

#clear_row(row) ⇒ Object



52
53
54
55
# File 'lib/oled-control/oled.rb', line 52

def clear_row(row)
  set_cursor(0, row)
  write(" "*20)
end

#command(cmd) ⇒ Object



57
58
59
# File 'lib/oled-control/oled.rb', line 57

def command(cmd)
  self.send_command(cmd)
end

#disableObject



55
56
57
58
59
60
61
# File 'ext/oled-control/oled-control-gem.c', line 55

static VALUE disable(VALUE self) {
    if(!display_disable()) {
        rb_raise(rb_eRuntimeError, "error disabling display");
    }

    return Qtrue;
}

#enableObject



47
48
49
50
51
52
53
# File 'ext/oled-control/oled-control-gem.c', line 47

static VALUE enable(VALUE self) {
    if(!display_enable()) {
        rb_raise(rb_eRuntimeError, "error enabling display");
    }

    return Qtrue;
}

#flipObject



65
66
67
68
69
70
71
72
# File 'lib/oled-control/oled.rb', line 65

def flip
  @flipped = !@flipped
  if @flipped
    command(NORMAL)
  else
    command(FLIPPED)
  end
end

#raw_command(cmd) ⇒ Object



61
62
63
# File 'lib/oled-control/oled.rb', line 61

def raw_command(cmd)
  self.send_raw_command(cmd)
end

#set_contrast(level) ⇒ Object



81
82
83
84
85
86
87
# File 'ext/oled-control/oled-control-gem.c', line 81

static VALUE set_contrast(VALUE self, VALUE level) {
    uint8_t val = NUM2UINT(level);
    if(!set_contrast_level(val)) {
        rb_raise(rb_eRuntimeError, "error setting contrast");
    }
    return Qtrue;
}

#set_cursor(col, row) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'ext/oled-control/oled-control-gem.c', line 9

static VALUE set_cursor(VALUE self, VALUE col, VALUE row) {
    uint8_t c = NUM2UINT(col);
    uint8_t r = NUM2UINT(row);

    if(!set_cursor_position(c, r))
        rb_raise(rb_eRuntimeError, "unable to set cursor position");

    return Qtrue;
}

#write(str) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/oled-control/oled.rb', line 74

def write(str)
  unless str.is_a?(String)
    str = str.to_s
  end
  iso_string = str.encode('iso-8859-1')
  encoded_string = []
  iso_string.each_byte{|b| encoded_string << (@character_conversion[b.ord].nil? ? b.ord : @character_conversion[b.ord]) }
  self.write_string(encoded_string.pack("c*"))
end