Share Pastebin
Guest
Public paste!

fclock.yab Sourc Code

By: a guest | Jun 14th, 2010 | Syntax: Basic4GL | Size: 5.18 KB | Hits: 58 | Expires: Never
Copy text to clipboard
  1. #!/boot/home/config/bin/yab
  2.  
  3. doc A little Alarm clock
  4. doc
  5. doc
  6. doc Leszek Lesner, 24.02.2010, BSD
  7.  
  8. // set DEBUG = 1 to print out all messages on the console
  9. DEBUG = 1
  10. dim cfg$(2)
  11. dim alarm$(2)
  12. dim music$(2)
  13. dim alarmnumbers$(2)
  14. dim alarmhour$(1)
  15. dim alarmmin$(1)
  16.  
  17. //--------------Where am I-----------------------
  18. if (peek("isbound")) then                    
  19.     WorkDir$ = peek$("directory")
  20.     else
  21.     WorkDir$ = system$("pwd")              
  22.     WorkDir$ = left$(ThisDir$, len(ThisDir$)-1)
  23. fi
  24. //-----------------------------------------------
  25.  
  26. sub loadcfg()
  27.         // Create default setting if none exists
  28.         system("if [ ! -f ~/config/settings/fclock ]; then echo 'alarm=-1:-1'> ~/config/settings/fclock ; echo 'music=-1'>> ~/config/settings/fclock; fi")
  29.  
  30.         // Block to get alarm time
  31.         cfg$ = system$("cat ~/config/settings/fclock | grep alarm")
  32.         if DEBUG = 1 then print cfg$ endif
  33.         num=split(cfg$,alarm$(),"=")
  34.         if DEBUG = 1 then print alarm$(2) endif
  35.         num=split(alarm$(2),alarmnumbers$(),":")
  36.         if DEBUG = 1 then
  37.                 print "Alarmhour: "+ alarmnumbers$(1)
  38.                 print "Alarmminute: " + trim$(alarmnumbers$(2))
  39.         endif
  40.         alarmhour$(1)=alarmnumbers$(1)
  41.         alarmmin$(1)=alarmnumbers$(2)
  42.  
  43.         // Block to get alarm music
  44.         cfgmusic$ = system$("cat ~/config/settings/fclock | grep music")
  45.         if DEBUG = 1 then print cfgmusic$ endif
  46.         num=split(cfgmusic$,music$(),"=")
  47.         if DEBUG = 1 then print music$(2) endif
  48.         music$(2) = trim$(music$(2))
  49. end sub
  50.  
  51. loadcfg()
  52. OpenWindow()
  53.  
  54. // Main Message Loop
  55. dim msg$(1)
  56.  
  57. CloseWindow()
  58.  
  59. end
  60.  
  61.  
  62. // Setup the main window here
  63. sub OpenWindow()
  64.         dim cut$(1)
  65.         dim cut2$(1)
  66.         dim msg$(1)
  67.         dim flush$(1)
  68.         dim sig$(1)
  69.         // window open 100,100 to 600,500, "MainWindow", "Main Window"
  70.         w = peek("desktopwidth")
  71.         h = peek("desktopheight")
  72.         WindowCentred(w, h, "MainWindow", "Main Window")
  73.         DRAW SET "BGColor", 0,0,0, "MainWindow"
  74.         option set "MainWindow", "Focus", true
  75.         while(not leavingLoop)
  76.                 //if(k$<>"" and k$<>old_k$) then
  77.                 //      print "You pressed "+k$
  78.                 //      old_k$ = k$
  79.                 //endif
  80.                 k$ = keyboard message$("MainWindow")
  81.                 if k$ = "a" then
  82.                         alarmwindow()
  83.                 elseif k$ = "q" then
  84.                         CloseWindow()
  85.                 endif
  86.                                
  87.  
  88.                 nCommands = token(message$, msg$(), "|")
  89.        
  90.                 for everyCommand = 1 to nCommands
  91.                         if(DEBUG and msg$(everyCommand)<>"") print msg$(everyCommand)
  92.  
  93.                 switch(msg$(everyCommand))
  94.                         case "_QuitRequested":
  95.                         case "MainWindow:_QuitRequested":
  96.                                 leavingLoop = true
  97.                                 break
  98.                         case "AlarmWindow:_QuitRequested":
  99.                                 window close "AlarmWindow"     
  100.                                 break
  101.                         case "Alarmbtn":
  102.                                 alarmwindow()
  103.                                 break
  104.                         default:
  105.                                 break
  106.                 end switch
  107.         next everyCommand
  108.         d$=date$
  109.         num=token(d$,cut2$(),"-")
  110.         month$=cut2$(2)
  111.         day$=cut2$(3)
  112.         year$=cut2$(4)
  113.         t$=time$
  114.         num=token(t$,cut$(),"-")
  115.                 h$=cut$(1)
  116.                 m$=cut$(2)
  117.                 s$=cut$(3)
  118.                 DRAW flush "MainWindow"
  119.                 IF not trim$(alarmhour$(1)) = "-1" OR not trim$(alarmmin$(1)) = "-1" THEN
  120.                         //clockimg = DRAW IMAGE w/2-120,h/2+45, WorkDir$+"img/clock.png","MainWindow"
  121.                         BUTTON IMAGE w/2-120,h/2+45,"Alarmbtn", WorkDir$+"img/alarmclock-pressed.png", WorkDir$+"img/alarmclock-notpressed.png", "", "MainWindow"
  122.                         DRAW set "lowcolor", 0,0,0, "MainWindow"
  123.                         DRAW set "highcolor" , 255,0,0, "MainWindow"
  124.                         DRAW set "DejaVu Sans Mono, Bold, 40", "MainWindow"
  125.                         DRAW text w/2-75,h/2+80, trim$(alarmhour$(1)) + ":" + trim$(alarmmin$(1)), "MainWindow"
  126.                 ENDIF // image
  127.                 DRAW set "lowcolor", 0,0,0, "MainWindow"
  128.                 DRAW set "highcolor" , 0,145,20, "MainWindow"
  129.                 DRAW set "DejaVu Sans Mono, Bold, 120", "MainWindow"
  130.                 //DRAW set "Digit, Regular, 120", "MainWindow"  // why is the lcd font not working ?
  131.                 //DRAW text w/2-188,h/2, h$+":"+m$, "MainWindow"
  132.                 DRAW text w/2-178,h/2, h$, "MainWindow"
  133.                 DRAW text w/2+10,h/2, m$, "MainWindow"
  134.                 DRAW set "DejaVu Sans Mono, Bold, 60", "MainWindow"
  135.                 DRAW text w/2-188,h/2+140, day$+"."+month$+"."+year$, "MainWindow"
  136.                 DRAW set "DejaVu Sans Mono, Bold, 20", "MainWindow"
  137.                 IF h$ = trim$(alarmhour$(1)) and m$ = trim$(alarmmin$(1)) and sig$ = "n" THEN
  138.                         sig$ = "y"
  139.                         print "ALARM"
  140.                         system("MediaPlayer "+ music$(2)+" &")
  141.                 ENDIF             // alarm             
  142.                 sleep 1
  143.                 DRAW set "DejaVu Sans Mono, Bold, 120", "MainWindow"
  144.                 DRAW set "lowcolor", 0,0,0, "MainWindow"
  145.             DRAW set "highcolor" , 0,145,20, "MainWindow"
  146.                 DRAW text w/2-50,h/2, ":", "MainWindow"
  147.                 DRAW set "DejaVu Sans Mono, Bold, 20", "MainWindow"
  148.                 // sig$ = "n"  // don't set it here
  149.                 IF s$ = "58" OR s$ = "59" THEN
  150.                         sig$ = "n"
  151.                 ENDIF // seconds set sig for alarm to work
  152.                 sleep 1
  153.                
  154.         wend
  155.        
  156.         if DEBUG = 1 then print w, h endif
  157.         return
  158. end sub
  159.  
  160. // Close down the main window
  161. sub CloseWindow()
  162.         window close "MainWindow"
  163.         return
  164. end sub
  165.  
  166. // Setup the window centred
  167. sub WindowCentred(x, y, id$, title$)
  168.         // get height and width of the desktop
  169.         w = peek("desktopwidth")
  170.         h = peek("desktopheight")
  171.         x1 = (w/2)-(x/2)
  172.         y1 = (h/2)-(y/2)
  173.         x2 = (w/2)+(x/2)
  174.         y2 = (h/2)+(y/2)
  175.         // finaly open(show) the window
  176.         window open x1,y1 to x2,y2, id$, title$
  177. end sub
  178.  
  179. sub alarmwindow()
  180.  
  181.         //WindowCentred(300,200,"AlarmWindow", "Set Alarm")
  182.         //DRAW Text 10,20, "Set Alarm time and alarm music here", "AlarmWindow"
  183.         bar$ = system$(WorkDir$ + "setalarm.yab")
  184.         loadcfg()
  185. end sub