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 +4 -4
- data/README.md +51 -5
- data/lib/SSD1306/cursor.rb +19 -5
- data/lib/SSD1306/display.rb +21 -3
- data/lib/SSD1306/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b72c1225182fe4b68f84c697098533da9ec29e8
|
4
|
+
data.tar.gz: 792815951029ec30523482333453a4dbaa7a66d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5f1b3ae71467695c79177221262d6bdad627d73d9ebbe69b877bd729a073897de7e6e9631a46d5269606574e207c4b603d62f967a52b12cc180e136a2b42e4b
|
7
|
+
data.tar.gz: c47d2cdfaad24d25a7c083c45074a2e6542cf42f409c307b58315606d66f7eb0880bf2d87eb6162343d3da3c24584b423f5cb75a07531676f89355b714d60cb9
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# SSD1306
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
89
|
+
Copyright (c) 2016 Xavier Bick under the MIT License. See the LICENSE file for details.
|
data/lib/SSD1306/cursor.rb
CHANGED
@@ -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 >= (
|
26
|
+
if @x_pos >= (127 - 6*@size)
|
13
27
|
newline
|
14
28
|
else
|
15
|
-
@x_pos +=
|
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
|
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
|
data/lib/SSD1306/display.rb
CHANGED
@@ -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
|
-
|
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
|
-
@
|
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
|
data/lib/SSD1306/version.rb
CHANGED