From 58b3339e79063b1da62ab4d86e7d10e3156b76ec Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Sat, 12 Mar 2016 22:22:21 +0000 Subject: [PATCH] ScrollPhat basic binary clock example --- examples/scrollphat-binary-clock.rb | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 examples/scrollphat-binary-clock.rb diff --git a/examples/scrollphat-binary-clock.rb b/examples/scrollphat-binary-clock.rb new file mode 100755 index 0000000..ad00a5f --- /dev/null +++ b/examples/scrollphat-binary-clock.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +require 'wiringpi' +STDOUT.write """ScrollPhat Binary Clock + +ScrollPhat will show hour, minute and second in binary across 3 lines. + +Press Ctrl+C to exit. +""" +class Clock < WiringPi::ScrollPhat + + @@running = false + + def tick + time = Time.new + x = 0 + y = 0 + [time.hour, time.min, time.sec].each do |p| + p.to_s(2).rjust(8,'0').split('').each do |d| + self.point(x, y, d.to_i) + x+=1 + end + y+=2 + x=0 + end + self.update + end + + def go! + @@running = true + trap("SIGINT") { self.stop! } + while @@running + self.tick + sleep 0.1 + end + end + + def stop! + STDOUT.write "\nGoodbye!\n" + @@running = false + end + +end + +clock = Clock.new +clock.go!