時計の文字盤に12個のLEDを付けて、雨の降る時刻を教えてくれるシステムです。
#!/usr/bin/ruby
require 'rexml/document'
require 'open-uri'
require 'pi_piper'
# 市町村コードで天気予報データを取得(21202:大垣市)
url = "http://api.tenki.jp/api/blog/parts/point_clock/forecast_map_point_21202.xml"
doc = REXML::Document.new(open(url))
# 現在時刻の取得 JST(+9h)
now = Time.now + 9*60*60
# シリアルパラレル変換にGPIO 23,24,25pinを使用
$pins = {
:ser => PiPiper::Pin.new(:pin => 25, :direction => :out), # serial data
:sck => PiPiper::Pin.new(:pin => 24, :direction => :out), # shift register clock
:rck => PiPiper::Pin.new(:pin => 23, :direction => :out) # latch clock
}
def shift(key)
$pins[key.to_sym].on
$pins[key.to_sym].off
end
def reset
$pins.values.each{|pin| pin.off}
end
def send_bits(data)
printf "0x%04x\n",data
0.upto(15) do |byte|
if ((1 << byte) & data) == 0
$pins[:ser].off
else
$pins[:ser].on
end
shift(:sck)
end
shift(:rck)
end
puts now
bits = 0
for i in 1..11 do
t = now + 60*60 * i
str = t.strftime("%Y-%m-%d %H:00:00")
id = doc.elements['//forecast[public_datetime="'+str+'"]/weather/id']
h = t.hour % 12
if id then
wid = id.text.to_i
print h, "\t", wid, "\n"
# 強雨(3)または弱雨(4)ならLED点灯
if (wid == 3 || wid == 4) then
bits |= 1 << (15-h)
end
end
end
puts ""
reset
send_bits(bits)