Permalink
Browse files

Fixed time thread on LCD, added some special 'motd' for different dat…

…es in the year such as Christmas, Easter, bdays.
  • Loading branch information...
1 parent 6f095f8 commit c305ff7b9d56a60319261ec2cf5f870e3566eb5c @ndbroadbent committed Mar 31, 2011
Showing with 35 additions and 12 deletions.
  1. +35 −12 octopus_sinatra.rb
View
47 octopus_sinatra.rb
@@ -42,7 +42,11 @@ def relative(filename)
$lcdTimeThread = nil
def lcd_message(str, s_pos=21, e_pos=40, timeout=true)
- $lcdTimeThread.kill
+ # Kill 'clock' thread before writing message.
+ if $lcdTimeThread
+ $lcdTimeThread.kill
+ $lcdTimeThread = nil
+ end
$dsp420.write str, s_pos, e_pos
if timeout
sleep MsgDelay
@@ -51,16 +55,34 @@ def lcd_message(str, s_pos=21, e_pos=40, timeout=true)
end
def lcd_default # Default lcd display
- lcd_message "Flat 10C -", 1, 20, false
- lcd_message " Octopus / Internet ", 21, 40, false
- $lcdTimeThread = Thread.new do
+ lcd_message "Flat 10C -=- ", 1, 20, false
+
+ time = hk_time
+ motd = case [time.day, time.month]
+ when [25, 12] then " Merry Christmas! "
+ when easter(time.year) then " Jesus is Alive! "
+ when [5, 10] then " Happy B'day Masha! "
+ when [3, 6] then "Happy B'day Nathan! "
+ else " Octopus & Internet " # else, default message
+ end
+ lcd_message motd, 21, 40, false
+
+ $lcdTimeThread = Thread.new {
while true
- lcd_message hk_time_lcd_fmt(":"), 14, 20
+ $dsp420.write hk_time_lcd_fmt(":"), 14, 20, false
sleep 1
- lcd_message hk_time_lcd_fmt(" "), 14, 20
+ $dsp420.write hk_time_lcd_fmt(" "), 14, 20, false
sleep 1
end
- end
+ }
+end
+
+# Every good program should calculate the date of Easter at least once.
+def easter(year)
+ c=year/100;n=year-19*(year/19);k=(c-17)/25;i=c-c/4-(c-k)/3+19*n+15;i-=30*(i/30);
+ i-=(i/28)*(1 -(i/28)*(29/(i+1))*((21-n)/11));j=year+year/4+i+2-c+c/4;j-=7*(j/7);
+ l=i-j;month=3+(l+40)/44;day=l+28-31*(month/4);
+ [day, month]
end
def unlock_door_action
@@ -100,20 +122,21 @@ def user_select_options
$users.map {|name, params| "<option #{name == @user ? 'selected="true"' : ''}value=\"#{name}\">#{name}</option>" }.join
end
-# Evo T20 is synced to UTC. HK time is UTC +8
+# Evo T20 is synced to UTC. HK time is UTC - 3 hours
def hk_time
- Time.now + 8*60*60
+ Time.now - 3*60*60
end
def hk_time_fmt
hk_time.strftime("%Y-%m-%d %H:%M:%S")
end
-def hk_time_lcd_fmt(separator="")
+def hk_time_lcd_fmt(separator=":")
time = hk_time
hour, minute = time.hour, time.min
- hour, suffix = hour > 12 ? [hour - 12, "pm"] : [hour, "am"]
- "%02d#{separator}%02d%s" % [hour, minute, suffix]
+ hour, suffix = hour >= 12 ? [hour - 12, "pm"] : [hour, "am"]
+ hour = 12 if hour == 0
+ "%2d#{separator}%02d%s" % [hour, minute, suffix]
end
def xbmc_trigger(name)

0 comments on commit c305ff7

Please sign in to comment.