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
51
52
53
54
55
56
57
58
59
60
61
# 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, # Ú
      0x5B => 0xFA, # [
      0x5D => 0xFC, # ]
      0x7B => 0xFD, # {
      0x7D => 0xFF, # }
      0xE8 => 0xA4, # è
      0xC8 => 0xC4, # È
      # 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
  }
  orientation = if @flipped
                  FLIPPED
                else
                  NORMAL
                end
  self.init(i2c_bus, i2c_address, orientation)
end

Instance Method Details

#clearObject



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

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

    return Qtrue;
}

#clear_row(row) ⇒ Object



63
64
65
66
# File 'lib/oled-control/oled.rb', line 63

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

#command(cmd) ⇒ Object



68
69
70
# File 'lib/oled-control/oled.rb', line 68

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

#create_character(position, chardata) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/oled-control/oled-control-gem.c', line 65

static VALUE create_character(VALUE self, VALUE position, VALUE chardata) {
    uint8_t data[8];
    Check_Type(chardata, T_ARRAY);
    Check_Type(position, T_FIXNUM);

    uint8_t pos = NUM2UINT(position);
    int len = RARRAY_LEN(chardata);
    if((len > 8) || (len < 8)) {
        rb_raise(rb_eRuntimeError, "error: array must be exactly of size 8");
        return Qnil;
    } else {
        for(int i=0; i<8; i++)
            data[i] = rb_ary_entry(chardata, i);
    }
    if(!create_custom_character(pos, data)) {
        rb_raise(rb_eRuntimeError, "error creating custom character");
        return Qnil;
    }
    return Qtrue;
}

#disableObject



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

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

    return Qtrue;
}

#enableObject



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

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

    return Qtrue;
}

#flipObject



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

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

#raw_command(cmd) ⇒ Object



72
73
74
# File 'lib/oled-control/oled.rb', line 72

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

#raw_write(str) ⇒ Object



85
86
87
# File 'lib/oled-control/oled.rb', line 85

def raw_write(str)
  self.write_string(str, str.size);
end

#set_contrast(level) ⇒ Object



104
105
106
107
108
109
110
# File 'ext/oled-control/oled-control-gem.c', line 104

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



89
90
91
92
93
94
95
96
97
98
# File 'lib/oled-control/oled.rb', line 89

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]) }
  result = encoded_string.pack("c*")
  self.write_string(result, result.size)
end