Class: Tinkerforge::BrickletSegmentDisplay4x7V2
- Inherits:
-
Object
- Object
- Tinkerforge::BrickletSegmentDisplay4x7V2
- Defined in:
- lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb
Instance Method Summary collapse
-
#all_on ⇒ Object
Turns all 35 segments on.
-
#bar_graph(value1, value2 = value1) ⇒ Object
Displays an 8-segment bar graph for 1 or 2 values in the range 0..1.
-
#clear ⇒ Object
(also: #blackout)
Clears the display.
-
#clock(utc = false) ⇒ Object
Continuously displays the current time, in 24-hour format.
-
#glyphs ⇒ Object
Returns the definition of glyphs for Unicode chracters.
-
#print(text_or_object = '') ⇒ Object
Displays a string.
-
#segments ⇒ Object
Returns the state of all 35 segments.
-
#segments=(*segments) ⇒ Object
Sets the state of all 35 segments.
-
#segments_string ⇒ Object
Returns the state of all 35 segments as a string of 1s and 0s.
-
#segments_string=(segments) ⇒ Object
Sets the state of all 35 segments using a string of 1s and 0s.
-
#state ⇒ Object
Returns the device's state.
-
#stop ⇒ Object
Stops automatic updating of the display.
-
#thread ⇒ Object
Returns the current thread automatically updating the display.
Instance Method Details
#all_on ⇒ Object
Turns all 35 segments on.
39 40 41 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 39 def all_on self.segments = [true]*35 end |
#bar_graph(value1, value2 = value1) ⇒ Object
Displays an 8-segment bar graph for 1 or 2 values in the range 0..1.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 147 def (value1, value2=value1) value1, value2 = [value1, value2].map { |v| (v.to_f.clamp(0,1) * 8).round } frame = [false] * 35 [ 5, 1, 13, 9, 21, 17, 29, 25 ].first(value1).each do |n| frame[n] = true end [ 4, 2, 12, 10, 20, 18, 28, 26 ].first(value2).each do |n| frame[n] = true end self.segments = frame [value1, value2] end |
#clear ⇒ Object Also known as: blackout
Clears the display.
6 7 8 9 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 6 def clear set_segments [false]*8, [false]*8, [false]*8, [false]*8, [false]*2, false true end |
#clock(utc = false) ⇒ Object
Continuously displays the current time, in 24-hour format.
Starts a new thread automatically updating the display. Use the stop method to end.
By default uses local time, or optionally UTC.
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 182 def clock(utc=false) stop @thread = Thread.new do while true t = utc ? Time.now.getutc : Time.now print t.strftime("%H#{t.sec.even? ? ':' : ''}%M") sleep 0.5 end end end |
#glyphs ⇒ Object
Returns the definition of glyphs for Unicode chracters.
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 74 def glyphs @@glyphs ||= { '0' => '1111110', '1' => '0110000', '2' => '1101101', '3' => '1111001', '4' => '0110011', '5' => '1011011', '6' => '1011111', '7' => '1110000', '8' => '1111111', '9' => '1111011', 'A' => '1110111', 'b' => '0011111', 'B' => '1111111', 'c' => '0001101', 'C' => '1001110', 'd' => '0111101', 'E' => '1001111', 'F' => '1000111', 'G' => '1011110', 'H' => '0110111', 'h' => '0010111', 'I' => '0110000', 'i' => '0010000', 'J' => '0111000', 'L' => '0001110', 'l' => '0001100', 'N' => '1110110', 'n' => '0010101', 'O' => '1111110', 'o' => '0011101', 'P' => '1100111', 'q' => '1110011', 'r' => '0000101', 'S' => '1011011', 't' => '0001111', 'U' => '0111110', 'u' => '0011100', 'V' => '0111110', 'v' => '0011100', 'Y' => '0111011', 'y' => '0111011', 'ö' => '1011101', 'ü' => '1011100', ' ' => '0000000', '_' => '0001000', '-' => '0000001', '¯' => '1000000', '=' => '0001001', '≡' => '1001001', '(' => '1001110', ')' => '1111000', '[' => '1001110', ']' => '1111000', '°' => '1100011', } end |
#print(text_or_object = '') ⇒ Object
Displays a string.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 49 def print(text_or_object='') if text_or_object.respond_to?( :_print_4, true) print ( text_or_object.send(:_print_4) rescue '' ) else out = '' colon = false text_or_object.to_s.chars.each do |c| if glyphs.key? c out << glyphs[c] << '0' elsif c == '.' and out[-1] == '0' out[-1] = '1' elsif c == ':' and out.size == 16 colon = true else raise "Can not display character '#{c}'" end end self.segments_string = out[0,32].ljust(32,'0') + ( colon ? '110' : '000' ) nil end end |
#segments ⇒ Object
Returns the state of all 35 segments.
14 15 16 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 14 def segments get_segments.flatten end |
#segments=(*segments) ⇒ Object
Sets the state of all 35 segments.
19 20 21 22 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 19 def segments=(*segments) segs = segments.flatten set_segments segs[0,8], segs[8,8], segs[16,8], segs[24,8], segs[32,2], segs[34] end |
#segments_string ⇒ Object
Returns the state of all 35 segments as a string of 1s and 0s.
25 26 27 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 25 def segments_string segments.map { |s| s ? '1' : '0' }.join end |
#segments_string=(segments) ⇒ Object
Sets the state of all 35 segments using a string of 1s and 0s.
For readability, the string may include spaces and underscores, which will be ignored.
34 35 36 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 34 def segments_string=(segments) self.segments = segments.gsub(/[ _]/, '').ljust(35, '0').chars.map { |s| s == '1' } end |
#state ⇒ Object
Returns the device's state.
44 45 46 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 44 def state super.merge( 'brightness' => get_brightness ) end |
#stop ⇒ Object
Stops automatic updating of the display.
170 171 172 173 174 175 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 170 def stop if thread thread.exit @thread = nil end end |
#thread ⇒ Object
Returns the current thread automatically updating the display.
165 166 167 |
# File 'lib/tinderfridge/devices/bricklet_segment_display_4x7_v2/bricklet_segment_display_4x7_v2.rb', line 165 def thread @thread end |