#!/boot/home/config/bin/yab
doc A little Alarm clock
doc
doc
doc Leszek Lesner, 24.02.2010, BSD
// set DEBUG = 1 to print out all messages on the console
DEBUG = 1
dim cfg$(2)
dim alarm$(2)
dim music$(2)
dim alarmnumbers$(2)
dim alarmhour$(1)
dim alarmmin$(1)
//--------------Where am I-----------------------
if (peek("isbound")) then
WorkDir$ = peek$("directory")
else
WorkDir$ = system$("pwd")
WorkDir$ = left$(ThisDir$, len(ThisDir$)-1)
fi
//-----------------------------------------------
sub loadcfg()
// Create default setting if none exists
system("if [ ! -f ~/config/settings/fclock ]; then echo 'alarm=-1:-1'> ~/config/settings/fclock ; echo 'music=-1'>> ~/config/settings/fclock; fi")
// Block to get alarm time
cfg$ = system$("cat ~/config/settings/fclock | grep alarm")
if DEBUG = 1 then print cfg$ endif
num=split(cfg$,alarm$(),"=")
if DEBUG = 1 then print alarm$(2) endif
num=split(alarm$(2),alarmnumbers$(),":")
if DEBUG = 1 then
print "Alarmhour: "+ alarmnumbers$(1)
print "Alarmminute: " + trim$(alarmnumbers$(2))
endif
alarmhour$(1)=alarmnumbers$(1)
alarmmin$(1)=alarmnumbers$(2)
// Block to get alarm music
cfgmusic$ = system$("cat ~/config/settings/fclock | grep music")
if DEBUG = 1 then print cfgmusic$ endif
num=split(cfgmusic$,music$(),"=")
if DEBUG = 1 then print music$(2) endif
music$(2) = trim$(music$(2))
end sub
loadcfg()
OpenWindow()
// Main Message Loop
dim msg$(1)
CloseWindow()
end
// Setup the main window here
sub OpenWindow()
dim cut$(1)
dim cut2$(1)
dim msg$(1)
dim flush$(1)
dim sig$(1)
// window open 100,100 to 600,500, "MainWindow", "Main Window"
w = peek("desktopwidth")
h = peek("desktopheight")
WindowCentred(w, h, "MainWindow", "Main Window")
DRAW SET "BGColor", 0,0,0, "MainWindow"
option set "MainWindow", "Focus", true
while(not leavingLoop)
//if(k$<>"" and k$<>old_k$) then
// print "You pressed "+k$
// old_k$ = k$
//endif
k$ = keyboard message$("MainWindow")
if k$ = "a" then
alarmwindow()
elseif k$ = "q" then
CloseWindow()
endif
nCommands = token(message$, msg$(), "|")
for everyCommand = 1 to nCommands
if(DEBUG and msg$(everyCommand)<>"") print msg$(everyCommand)
switch(msg$(everyCommand))
case "_QuitRequested":
case "MainWindow:_QuitRequested":
leavingLoop = true
break
case "AlarmWindow:_QuitRequested":
window close "AlarmWindow"
break
case "Alarmbtn":
alarmwindow()
break
default:
break
end switch
next everyCommand
d$=date$
num=token(d$,cut2$(),"-")
month$=cut2$(2)
day$=cut2$(3)
year$=cut2$(4)
t$=time$
num=token(t$,cut$(),"-")
h$=cut$(1)
m$=cut$(2)
s$=cut$(3)
DRAW flush "MainWindow"
IF not trim$(alarmhour$(1)) = "-1" OR not trim$(alarmmin$(1)) = "-1" THEN
//clockimg = DRAW IMAGE w/2-120,h/2+45, WorkDir$+"img/clock.png","MainWindow"
BUTTON IMAGE w/2-120,h/2+45,"Alarmbtn", WorkDir$+"img/alarmclock-pressed.png", WorkDir$+"img/alarmclock-notpressed.png", "", "MainWindow"
DRAW set "lowcolor", 0,0,0, "MainWindow"
DRAW set "highcolor" , 255,0,0, "MainWindow"
DRAW set "DejaVu Sans Mono, Bold, 40", "MainWindow"
DRAW text w/2-75,h/2+80, trim$(alarmhour$(1)) + ":" + trim$(alarmmin$(1)), "MainWindow"
ENDIF // image
DRAW set "lowcolor", 0,0,0, "MainWindow"
DRAW set "highcolor" , 0,145,20, "MainWindow"
DRAW set "DejaVu Sans Mono, Bold, 120", "MainWindow"
//DRAW set "Digit, Regular, 120", "MainWindow" // why is the lcd font not working ?
//DRAW text w/2-188,h/2, h$+":"+m$, "MainWindow"
DRAW text w/2-178,h/2, h$, "MainWindow"
DRAW text w/2+10,h/2, m$, "MainWindow"
DRAW set "DejaVu Sans Mono, Bold, 60", "MainWindow"
DRAW text w/2-188,h/2+140, day$+"."+month$+"."+year$, "MainWindow"
DRAW set "DejaVu Sans Mono, Bold, 20", "MainWindow"
IF h$ = trim$(alarmhour$(1)) and m$ = trim$(alarmmin$(1)) and sig$ = "n" THEN
sig$ = "y"
print "ALARM"
system("MediaPlayer "+ music$(2)+" &")
ENDIF // alarm
sleep 1
DRAW set "DejaVu Sans Mono, Bold, 120", "MainWindow"
DRAW set "lowcolor", 0,0,0, "MainWindow"
DRAW set "highcolor" , 0,145,20, "MainWindow"
DRAW text w/2-50,h/2, ":", "MainWindow"
DRAW set "DejaVu Sans Mono, Bold, 20", "MainWindow"
// sig$ = "n" // don't set it here
IF s$ = "58" OR s$ = "59" THEN
sig$ = "n"
ENDIF // seconds set sig for alarm to work
sleep 1
wend
if DEBUG = 1 then print w, h endif
return
end sub
// Close down the main window
sub CloseWindow()
window close "MainWindow"
return
end sub
// Setup the window centred
sub WindowCentred(x, y, id$, title$)
// get height and width of the desktop
w = peek("desktopwidth")
h = peek("desktopheight")
x1 = (w/2)-(x/2)
y1 = (h/2)-(y/2)
x2 = (w/2)+(x/2)
y2 = (h/2)+(y/2)
// finaly open(show) the window
window open x1,y1 to x2,y2, id$, title$
end sub
sub alarmwindow()
//WindowCentred(300,200,"AlarmWindow", "Set Alarm")
//DRAW Text 10,20, "Set Alarm time and alarm music here", "AlarmWindow"
bar$ = system$(WorkDir$ + "setalarm.yab")
loadcfg()
end sub