class Time7Segment
Hex values used:
https://en.wikipedia.org/wiki/Seven-segment_display#Displaying_letters
Public Class Methods
new(gpio=%w(22 27 17 24 11 4 23 8 20 10 18 25), refresh: 0.005)
click to toggle source
d1 d2 d3 d4 a b c d e f g dp
# File lib/time7segment.rb, line 31 def initialize(gpio=%w(22 27 17 24 11 4 23 8 20 10 18 25), refresh: 0.005) pins = SimpleRaspberryPi.new(gpio).pins @digits, @segments, @refresh = pins.take(4), pins.slice(4..-1), refresh a = [0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B] @numerals = a.map do |x| x.to_s(2).rjust(7,'0').chars.map {|x| x == '1' ? :off : :on } end end
Public Instance Methods
start()
click to toggle source
# File lib/time7segment.rb, line 44 def start loop do Time.now.strftime("%H%M").chars.each.with_index do |x,i| n = x.to_i display(n); @segments[7].method( i == 1 ? :off : :on).call @digits[i].on refresh = @refresh # reduce the sleep time for digit 1 since it takes less time to # update the segments. This gives a more balanced brightness display, # especially for digits 0 and 8 refresh -= refresh / 2 if n == 1 sleep refresh @digits[i].off end end end
Private Instance Methods
display(n)
click to toggle source
Displays the segments for an input number between 0-9
# File lib/time7segment.rb, line 75 def display(n) @numerals[n].each.with_index {|x,i| @segments[i].method(x).call} end