Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/SSD1306.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'i2c'
require 'rmagick'
include Magick

require 'SSD1306/version'
require 'SSD1306/font'
Expand Down
87 changes: 58 additions & 29 deletions lib/SSD1306/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,44 +70,67 @@ def initialize(opts = {})
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
# Common Command
self.command SSD1306_DISPLAYOFF
self.command SSD1306_SETDISPLAYCLOCKDIV
self.command 0x80
self.command SSD1306_SETMULTIPLEX
#self.command 0x3F
self.command @height-1
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

# each size command
case @height
when 64 #128x64
self.command 0x12
self.command SSD1306_SETCONTRAST
if @vccstate == SSD1306_EXTERNALVCC
self.command 0x9F
else
self.command 0xCf
end
self.command SSD1306_SETPRECHARGE

when 32 #128x32
self.command 0x02
self.command SSD1306_SETCONTRAST
self.command 0x8f

when 16 #96x16 ?
self.command 0x02
self.command SSD1306_SETCONTRAST
if @vccstate == SSD1306_EXTERNALVCC
self.command 0x22
self.command 0x10
else
self.command 0xF1
self.command 0xAF
end
self.command SSD1306_SETVCOMDETECT
self.command 0x40
self.command SSD1306_DISPLAYALLON_RESUME
self.command SSD1306_NORMALDISPLAY

else
end

# Common Command
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

self.command SSD1306_DISPLAYON
self.clear!
Expand Down Expand Up @@ -141,7 +164,9 @@ def display!
# Write buffer data
# TODO: This works for I2C only
control = 0x40
@interface.write @address, control, @buffer.pack('c*')
# p [ "DEBUG", @buffer]
@buf = @buffer.compact
@interface.write @address, control, @buf.pack('c*')
end

def image(image)
Expand All @@ -155,6 +180,7 @@ def image(image)
bits = bits << 1
bits |= pix[(page*8*@width) + x + ((7-bit)*@width)] == 0 ? 0 : 1
end
# p [page,x,bits] unless bits
@buffer[index] = bits
index += 1
end
Expand Down Expand Up @@ -185,6 +211,7 @@ def println(string)
self.print_char 10 # 10 is ASCII for \n
string
end
alias :puts :println

def font_size
return @cursor.size
Expand Down Expand Up @@ -237,9 +264,11 @@ def print_char(b)
end
bytes = bytes.map {|b| b.to_i(2)}
bytes.reverse!
# p bytes
#
for page in 0...@cursor.size
for x_interval in 0...@cursor.size
@buffer[@cursor.buffer_index(page) + i*@cursor.size + x_interval] = bytes[page]
@buffer[@cursor.buffer_index(page) + i*@cursor.size + x_interval] = bytes[page]
end
end
end
Expand Down