Class: SSD1306::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/SSD1306/display.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Display


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/SSD1306/display.rb', line 42

def initialize(opts = {})
  default_options = {
    protocol: :i2c,
    path:     '/dev/i2c-1',
    address:  0x3C,
    width:    128,
    height:   64,
    reset:    24,
    vccstate: SSD1306_SWITCHCAPVCC
  }
  options = default_options.merge(opts)

  @protocol = options[:protocol]
  @path     = options[:path]
  @address  = options[:address]
  @width    = options[:width]
  @height   = options[:height]
  @vccstate = options[:vccstate]
  @pages    = @height / 8
  @buffer   = [0]*(@width*@pages)
  @cursor   = Cursor.new
  @reset    = options[:reset]
  if @protocol == :i2c
    @interface = I2C.create(@path)
  elsif @protocol == :spi
    raise "SPI Not Supported Currently"
  else
    raise "Unrecognized protocol"
  end

  # For 128 x 64 display
  if @height == 64
    self.command SSD1306_DISPLAYOFF
    self.command SSD1306_SETDISPLAYCLOCKDIV
    self.command 0x80
    self.command SSD1306_SETMULTIPLEX
    self.command 0x3F
    self.command SSD1306_SETDISPLAYOFFSET
    self.command 0x0
    self.command(SSD1306_SETSTARTLINE | 0x0)
    self.command SSD1306_CHARGEPUMP
    if @vccstate == SSD1306_EXTERNALVCC
      self.command 0x10
    else
      self.command 0x14
    end
    self.command SSD1306_MEMORYMODE
    self.command 0x00
    self.command(SSD1306_SEGREMAP | 0x1)
    self.command SSD1306_COMSCANDEC
    self.command SSD1306_SETCOMPINS
    self.command 0x12
    self.command SSD1306_SETCONTRAST
    if @vccstate == SSD1306_EXTERNALVCC
      self.command 0x9F
    else
      self.command 0xCf
    end
    self.command SSD1306_SETPRECHARGE
    if @vccstate == SSD1306_EXTERNALVCC
      self.command 0x22
    else
      self.command 0xF1
    end
    self.command SSD1306_SETVCOMDETECT
    self.command 0x40
    self.command SSD1306_DISPLAYALLON_RESUME
    self.command SSD1306_NORMALDISPLAY
  end

  self.command SSD1306_DISPLAYON
  self.clear!
end

Instance Attribute Details

#addressObject

Returns the value of attribute address


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def address
  @address
end

#bufferObject

Returns the value of attribute buffer


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def buffer
  @buffer
end

#cursorObject

Returns the value of attribute cursor


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def cursor
  @cursor
end

#heightObject

Returns the value of attribute height


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def height
  @height
end

#interfaceObject

Returns the value of attribute interface


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def interface
  @interface
end

#pathObject

Returns the value of attribute path


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def path
  @path
end

#protocolObject

Returns the value of attribute protocol


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def protocol
  @protocol
end

#vccstateObject

Returns the value of attribute vccstate


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def vccstate
  @vccstate
end

#widthObject

Returns the value of attribute width


40
41
42
# File 'lib/SSD1306/display.rb', line 40

def width
  @width
end

Instance Method Details

#clearObject


164
165
166
167
# File 'lib/SSD1306/display.rb', line 164

def clear
  @buffer = [0]*(@width*@pages)
  @cursor.reset
end

#clear!Object


169
170
171
172
# File 'lib/SSD1306/display.rb', line 169

def clear!
  self.clear
  self.display!
end

#command(c) ⇒ Object


124
125
126
127
# File 'lib/SSD1306/display.rb', line 124

def command(c)
  control = 0x00
  @interface.write @address, control, c
end

#data(d) ⇒ Object


129
130
131
132
# File 'lib/SSD1306/display.rb', line 129

def data(d)
  control = 0x40
  @interface.write @address, control, d
end

#debug_bufferObject


120
121
122
# File 'lib/SSD1306/display.rb', line 120

def debug_buffer
  @buffer = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8, 0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, 0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF, 0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F, 0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, 0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E, 0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06, 0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8, 0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C, 0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07, 0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
end

#dim(dim) ⇒ Object

TODO: Implement Dimming functionality


203
204
205
# File 'lib/SSD1306/display.rb', line 203

def dim(dim)
  raise "Dim not implemented yet"
end

#display!Object


134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/SSD1306/display.rb', line 134

def display!
  self.command SSD1306_COLUMNADDR
  self.command 0
  self.command(@width - 1)
  self.command SSD1306_PAGEADDR
  self.command 0
  self.command(@pages - 1)
  # Write buffer data
  # TODO: This works for I2C only
  control = 0x40
  @interface.write @address, control, @buffer.pack('c*')
end

#font_sizeObject


189
190
191
# File 'lib/SSD1306/display.rb', line 189

def font_size
  return @cursor.size
end

#font_size=(new_size) ⇒ Object


193
194
195
# File 'lib/SSD1306/display.rb', line 193

def font_size=(new_size)
  @cursor.size = new_size
end

#image(image) ⇒ Object


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/SSD1306/display.rb', line 147

def image(image)
  image.image_type = BilevelType
  pix = image.export_pixels(0, 0, @width, @height, 'I')
  index = 0
  for page in 0...@pages
    for x in 0...@width
      bits = 0
      for bit in [0, 1, 2, 3, 4, 5, 6, 7]
        bits = bits << 1
        bits |= pix[(page*8*@width) + x + ((7-bit)*@width)] == 0 ? 0 : 1
      end
      @buffer[index] = bits
      index += 1
    end
  end
end

174
175
176
177
178
179
# File 'lib/SSD1306/display.rb', line 174

def print(string)
  string.each_byte do |c|
    self.print_char c
  end
  string
end

#println(string) ⇒ Object


181
182
183
184
185
186
187
# File 'lib/SSD1306/display.rb', line 181

def println(string)
  string.each_byte do |c|
    self.print_char c
  end
  self.print_char 10 # 10 is ASCII for \n
  string
end

#resetObject


116
117
118
# File 'lib/SSD1306/display.rb', line 116

def reset
  #TODO Reset logic
end

#set_contrast(contrast) ⇒ Object

TODO: Implement Contrast functionality


198
199
200
# File 'lib/SSD1306/display.rb', line 198

def set_contrast(contrast)
  raise "Contrast not yet implemented"
end