charlcd 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0cbc4292fd38b43c45cfb88b22055fc74d64de5a
4
+ data.tar.gz: f13e769873d974d025b28ec509a53a49a1218910
5
+ SHA512:
6
+ metadata.gz: 9441545dfbc039f090819b2223ed93863e709104877c2d0e5cbca9ba460719fb1f0ec38223f7307bb677d21840a0053359cb6c4f8f3e6a6bce63d450f2e11f44
7
+ data.tar.gz: a1a0fc7cc7dda2db692981094045a9c7ee723c97cac5d5f29f53306b7cf9e9a70626104321068f6b1242ecbad552185d59029e3908e5daea98f43e8c9470113c
@@ -0,0 +1,31 @@
1
+ CharLcd
2
+ =======
3
+
4
+ Lcd display library compatible with the hd44780 controller on the raspberrypi.
5
+
6
+ wiring instructions:
7
+ http://learn.adafruit.com/drive-a-16x2-lcd-directly-with-a-raspberry-pi/overview
8
+
9
+ install:
10
+ ```
11
+ sudo gem install charlcd
12
+ ```
13
+
14
+ usage:
15
+ ``` ruby
16
+ require 'charlcd'
17
+
18
+ char_lcd = CharLcd.new
19
+ char_lcd.begin(16, 2)
20
+ char_lcd.message("First Line\nSecond Line")
21
+ ```
22
+
23
+ License
24
+ =======
25
+ (The MIT License)
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
28
+
29
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
30
+
31
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ require 'charLcd'
2
+
3
+ SLEEP_TIME = 0.005
4
+ COLUMNS = 16
5
+ LINES = 2
6
+
7
+ lcd = CharLcd.new
8
+ lcd.begin(COLUMNS, LINES)
9
+
10
+ lcd.blink
11
+
12
+ prng = Random.new
13
+
14
+ def rand_matrix(prng)
15
+ prng.rand(2).zero? ? prng.rand(33..127) : prng.rand(161..253)
16
+ end
17
+
18
+ (0..100).each do
19
+ c = prng.rand(COLUMNS)
20
+ for l in 0...LINES
21
+
22
+ count = prng.rand(5)
23
+ i = 0
24
+ while i < count
25
+ lcd.set_cursor(c, l)
26
+ lcd.write_4_bits(rand_matrix(prng), true)
27
+ lcd.set_cursor(c, l)
28
+ sleep(SLEEP_TIME)
29
+ i += 1
30
+ end
31
+
32
+ if prng.rand(10).zero?
33
+ count = prng.rand(5)
34
+ i = 0
35
+
36
+ c1 = prng.rand(COLUMNS)
37
+ l1 = prng.rand(LINES)
38
+
39
+ while i < count do
40
+ lcd.set_cursor(c1, l1)
41
+ lcd.write_4_bits(rand_matrix(prng), true)
42
+ lcd.set_cursor(c1, l1)
43
+ sleep(SLEEP_TIME)
44
+ i += 1
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ lcd.no_blink
52
+ lcd.clear
@@ -0,0 +1,194 @@
1
+ require 'pi_piper'
2
+
3
+ # code based on:
4
+ # https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_CharLCD/Adafruit_CharLCD.py
5
+
6
+ class CharLcd
7
+ # commands
8
+ LCD_CLEARDISPLAY = 0x01
9
+ LCD_RETURNHOME = 0x02
10
+ LCD_ENTRYMODESET = 0x04
11
+ LCD_DISPLAYCONTROL = 0x08
12
+ LCD_CURSORSHIFT = 0x10
13
+ LCD_FUNCTIONSET = 0x20
14
+ LCD_SETCGRAMADDR = 0x40
15
+ LCD_SETDDRAMADDR = 0x80
16
+
17
+ # flags for display entry mode
18
+ LCD_ENTRYRIGHT = 0x00
19
+ LCD_ENTRYLEFT = 0x02
20
+ LCD_ENTRYSHIFTINCREMENT = 0x01
21
+ LCD_ENTRYSHIFTDECREMENT = 0x00
22
+
23
+ # flags for display on/off control
24
+ LCD_DISPLAYON = 0x04
25
+ LCD_DISPLAYOFF = 0x00
26
+ LCD_CURSORON = 0x02
27
+ LCD_CURSOROFF = 0x00
28
+ LCD_BLINKON = 0x01
29
+ LCD_BLINKOFF = 0x00
30
+
31
+ # flags for display/cursor shift
32
+ LCD_DISPLAYMOVE = 0x08
33
+ LCD_CURSORMOVE = 0x00
34
+ LCD_MOVERIGHT = 0x04
35
+ LCD_MOVELEFT = 0x00
36
+
37
+ # flags for function set
38
+ LCD_8BITMODE = 0x10
39
+ LCD_4BITMODE = 0x00
40
+ LCD_2LINE = 0x08
41
+ LCD_1LINE = 0x00
42
+ LCD_5x10DOTS = 0x04
43
+ LCD_5x8DOTS = 0x00
44
+
45
+ def initialize(pin_rs = 25, pin_e = 24, pins_db = [23, 17, 27, 22])
46
+ @pin_rs = PiPiper::Pin.new(:pin => pin_rs, :direction => :out)
47
+ @pin_e = PiPiper::Pin.new(:pin => pin_e, :direction => :out)
48
+ @pins_db = []
49
+
50
+ pins_db.each { |pin_db| @pins_db.push(PiPiper::Pin.new(:pin => pin_db, :direction => :out)) }
51
+
52
+ write_4_bits(0x33) # initialization
53
+ write_4_bits(0x32) # initialization
54
+ write_4_bits(0x28) # 2 line 5x7 matrix
55
+ write_4_bits(0x0C) # turn cursor off 0x0E to enable cursor
56
+ write_4_bits(0x06) # shift cursor right
57
+
58
+ @display_control = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
59
+
60
+ @display_function = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS
61
+ @display_function |= LCD_2LINE
62
+
63
+ # Initialize to default text direction (for romance languages)
64
+ @display_mode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
65
+ write_4_bits(LCD_ENTRYMODESET | @display_mode) # set the entry mode
66
+
67
+ clear
68
+ end
69
+
70
+ def begin(cols, lines)
71
+ if lines > 1
72
+ @number_lines = lines
73
+ @display_function |= LCD_2LINE
74
+ end
75
+ end
76
+
77
+ def home
78
+ write_4_bits(LCD_RETURNHOME)
79
+ delay_microseconds(3000)
80
+ end
81
+
82
+ def clear
83
+ write_4_bits(LCD_CLEARDISPLAY) # command to clear display
84
+ delay_microseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
85
+ end
86
+
87
+ def set_cursor(col, row)
88
+ row_offsets = [0x00, 0x40, 0x14, 0x54]
89
+
90
+ row = @number_lines - 1 if (row > @number_lines)
91
+
92
+ write_4_bits(LCD_SETDDRAMADDR | col + row_offsets[row])
93
+ end
94
+
95
+ def no_display
96
+ @display_control &= ~LCD_DISPLAYON
97
+ write_4_bits(LCD_DISPLAYCONTROL | @display_control)
98
+ end
99
+
100
+ def display
101
+ @display_control |= LCD_DISPLAYON
102
+ write_4_bits(LCD_DISPLAYCONTROL | @display_control)
103
+ end
104
+
105
+ def no_cursor
106
+ @display_control &= ~LCD_CURSORON
107
+ write_4_bits(LCD_DISPLAYCONTROL | @display_control)
108
+ end
109
+
110
+ def cursor
111
+ @display_control |= LCD_CURSORON
112
+ write_4_bits(LCD_DISPLAYCONTROL | @display_control)
113
+ end
114
+
115
+ def blink
116
+ @display_control |= LCD_BLINKON
117
+ write_4_bits(LCD_DISPLAYCONTROL | @display_control)
118
+ end
119
+
120
+ def no_blink
121
+ @display_control &= ~LCD_BLINKON
122
+ write_4_bits(LCD_DISPLAYCONTROL | @display_control)
123
+ end
124
+
125
+ def scroll_display_left
126
+ write_4_bits(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT)
127
+ end
128
+
129
+ def scroll_display_right
130
+ write_4_bits(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT)
131
+ end
132
+
133
+ def left_to_right
134
+ @display_mode |= LCD_ENTRYLEFT
135
+ write_4_bits(LCD_ENTRYMODESET | @display_mode)
136
+ end
137
+
138
+ def right_to_left
139
+ @display_mode &= ~LCD_ENTRYLEFT
140
+ write_4_bits(LCD_ENTRYMODESET | @display_mode)
141
+ end
142
+
143
+ def autoscroll
144
+ @display_mode |= LCD_ENTRYSHIFTINCREMENT
145
+ write_4_bits(LCD_ENTRYMODESET | @display_mode)
146
+ end
147
+
148
+ def no_autoscroll
149
+ @display_mode &= ~LCD_ENTRYSHIFTINCREMENT
150
+ write_4_bits(LCD_ENTRYMODESET | @display_mode)
151
+ end
152
+
153
+ def message(text)
154
+ text.each_char do |char|
155
+ if char.eql?("\n")
156
+ write_4_bits(0xC0)
157
+ else
158
+ write_4_bits(char.ord, true)
159
+ end
160
+ end
161
+ end
162
+
163
+ def write_4_bits(bits, char_mode = false)
164
+ delay_microseconds(1000)
165
+
166
+ bits = bits.to_s(2).rjust(8, "0")
167
+
168
+ @pin_rs.update_value(char_mode)
169
+
170
+ @pins_db.each { |pin_db| pin_db.off }
171
+ (0..3).each { |i| @pins_db.reverse[i].on if bits[i].eql?("1") }
172
+
173
+ pulse_enable
174
+
175
+ @pins_db.each { |pin_db| pin_db.off }
176
+ (4..7).each { |i| @pins_db.reverse[i - 4].on if bits[i].eql?("1") }
177
+
178
+ pulse_enable
179
+ end
180
+
181
+ def pulse_enable
182
+ @pin_e.off
183
+ delay_microseconds(1)
184
+ @pin_e.on
185
+ delay_microseconds(1)
186
+ @pin_e.off
187
+ end
188
+
189
+ def delay_microseconds(microseconds)
190
+ seconds = microseconds/1_000_000.0
191
+ sleep(seconds)
192
+ end
193
+
194
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: charlcd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Fussuma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A lcd display library compatible with the hd44780 controller on the raspberrypi
14
+ email: fernandofussuma@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - examples/charlcd_matrix_example.rb
21
+ - lib/charlcd.rb
22
+ homepage: https://github.com/fernandofussuma/CharLcd-ruby-raspberry-pi
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Lcd display raspberrypi library!
46
+ test_files: []