SSD1306 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f6bdd49eb64a9f801a61eb64b7135a148ea3f03
4
- data.tar.gz: 962e75ce784a53d16171ae9b9b83ab1d6f970d6c
3
+ metadata.gz: 9b72c1225182fe4b68f84c697098533da9ec29e8
4
+ data.tar.gz: 792815951029ec30523482333453a4dbaa7a66d4
5
5
  SHA512:
6
- metadata.gz: f5d166a8a28ba37a08e73a11a56dbf1c60954601aff8125cade56c268e17d8fdd28521691e3f7c0c4a2f7edb5e11e4dff5981cbda794534fcd6802cb087f38b3
7
- data.tar.gz: 261705f1e7e3ffe51360734d60ecca7893f297c1dfe7d4f70fc954d7bd5e5b23cac4ce6d17cc100c56765a88720a6838576d69df9f6906e3da362b421e6d9b5d
6
+ metadata.gz: b5f1b3ae71467695c79177221262d6bdad627d73d9ebbe69b877bd729a073897de7e6e9631a46d5269606574e207c4b603d62f967a52b12cc180e136a2b42e4b
7
+ data.tar.gz: c47d2cdfaad24d25a7c083c45074a2e6542cf42f409c307b58315606d66f7eb0880bf2d87eb6162343d3da3c24584b423f5cb75a07531676f89355b714d60cb9
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # SSD1306
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/SSD1306`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ This is a library for the SSD1306 OLED Display written in Ruby. It was inspired by Adafruit's libraries written in Python and C. Much of the original logic was simply ported from their Python library, but in addition this Ruby library provides additional functionality such as `print` and `println` helpers. The library also features simple image parsing.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,55 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ Using the library is simple. To start with, require the library and initialize the display:
24
+
25
+ ```ruby
26
+ require 'SSD1306'
27
+ disp = SSD1306::Display.new
28
+ ```
29
+
30
+ The default options are suitable for the Raspberry Pi and a 128x64 display. These can be overridden by specifying values like so:
31
+
32
+ ```ruby
33
+ disp = SSD1306::Display.new(protocol: :i2c, path: '/dev/i2c-1', address: 0x3C, width: 128, height: 64)
34
+ ```
35
+
36
+ Writing text on the display is simple:
37
+
38
+ ```ruby
39
+ disp.println "This is my IP Address:"
40
+ disp.println "" # The same as disp.print "\n"
41
+ disp.font_size 2
42
+ disp.println ip_address
43
+ disp.display!
44
+ ```
45
+
46
+ You can also display monochrome images:
47
+
48
+ ```ruby
49
+ include Magick # RMagick is a dependency
50
+ image = Image.read("path/to/my/image.png").first # Image.read returns an array
51
+
52
+ disp.image(image) # Pass in an RMagick image object
53
+ disp.display!
54
+ ```
55
+
56
+ They display can also be easily cleared:
57
+
58
+ ```ruby
59
+ disp.clear
60
+ disp.display!
61
+
62
+ # Or more simply:
63
+ disp.clear!
64
+ ```
65
+
66
+ Check out the source code for additional information. It's not very hard to read.
67
+
68
+ ## To-do
69
+
70
+ * Currently only 1, 2, and 4 work as font sizes. In the meantime, to use other fonts or font sizes, I recommend using RMagick's `annotate` feature and passing in an image.
71
+ * Implement SPI. Only I2C is currently supported. (For v1.0)
26
72
 
27
73
  ## Development
28
74
 
@@ -40,4 +86,4 @@ This library is inspired by the Adafruit SSD1306 Python library, available here:
40
86
 
41
87
  ## License
42
88
 
43
- Copyright (c) 2016 Xavier Bick under the MIT License. See LICENSE.txt for details.
89
+ Copyright (c) 2016 Xavier Bick under the MIT License. See the LICENSE file for details.
@@ -8,17 +8,31 @@ module SSD1306
8
8
  @y_pos = 0
9
9
  end
10
10
 
11
+ def reset
12
+ reset_pos
13
+ reset_size
14
+ end
15
+
16
+ def reset_pos
17
+ @x_pos = 0
18
+ @y_pos = 0
19
+ end
20
+
21
+ def reset_size
22
+ @size = 1
23
+ end
24
+
11
25
  def increment
12
- if @x_pos >= (126 - 5*@size)
26
+ if @x_pos >= (127 - 6*@size)
13
27
  newline
14
28
  else
15
- @x_pos += 5*size
29
+ @x_pos += (6*@size)
16
30
  end
17
31
  end
18
32
 
19
33
  def newline
20
34
  @x_pos = 0
21
- @y_pos += 8*size
35
+ @y_pos += 8*@size
22
36
  end
23
37
 
24
38
  def set_pos(x, y)
@@ -26,9 +40,9 @@ module SSD1306
26
40
  @y_pos = y
27
41
  end
28
42
 
29
- def buffer_index
43
+ def buffer_index(page_offset = 0)
30
44
  page = @y_pos / 8
31
- index = (page*128) + @x_pos
45
+ index = (page*128 + page_offset*128) + @x_pos
32
46
  end
33
47
 
34
48
  def position_in_buffer
@@ -163,6 +163,7 @@ module SSD1306
163
163
 
164
164
  def clear
165
165
  @buffer = [0]*(@width*@pages)
166
+ @cursor.reset
166
167
  end
167
168
 
168
169
  def clear!
@@ -174,6 +175,7 @@ module SSD1306
174
175
  string.each_byte do |c|
175
176
  self.print_char c
176
177
  end
178
+ string
177
179
  end
178
180
 
179
181
  def println(string)
@@ -181,11 +183,11 @@ module SSD1306
181
183
  self.print_char c
182
184
  end
183
185
  self.print_char 10 # 10 is ASCII for \n
186
+ string
184
187
  end
185
188
 
186
- #TODO Do font_size
187
189
  def font_size(new_size)
188
- raise "font_size not yet implemented"
190
+ @cursor.size = new_size
189
191
  end
190
192
 
191
193
  #TODO Implement Contrast functionality
@@ -208,7 +210,23 @@ module SSD1306
208
210
  @cursor.newline
209
211
  elsif b > 31
210
212
  for i in 0...5
211
- @buffer[@cursor.buffer_index + i] = FONT[(b*5) + i]
213
+ if @cursor.size == 1
214
+ @buffer[@cursor.buffer_index + i] = FONT[(b*5) + i]
215
+ else
216
+ byte = FONT[(b*5) + i].to_s(2).rjust(8, '0')
217
+ a = byte.chars.each_slice(8/@cursor.size).map(&:join)
218
+ bytes = []
219
+ a.each do |e|
220
+ bytes << e.chars.map {|c| c*(8/e.length)}.join
221
+ end
222
+ bytes = bytes.map {|b| b.to_i(2)}
223
+ bytes.reverse!
224
+ for page in 0...@cursor.size
225
+ for x_interval in 0...@cursor.size
226
+ @buffer[@cursor.buffer_index(page) + i*@cursor.size + x_interval] = bytes[page]
227
+ end
228
+ end
229
+ end
212
230
  end
213
231
  @cursor.increment
214
232
  end
@@ -1,3 +1,3 @@
1
1
  module SSD1306
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: SSD1306
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Bick