Advertisement
Greycheek

Analog Alarm Clock

Feb 23rd, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.44 KB | None | 0 0
  1. MAIN
  2.  
  3.  
  4. function setup()
  5.     if readLocalData("exist")==nil then  --first run
  6.         Storage:FactoryDefaults()
  7.     else
  8.         Storage:Load()
  9.     end        
  10.     
  11.     weekday={
  12.     [1]="Sunday",
  13.     [2]="Monday",
  14.     [3]="Tuesday",
  15.     [4]="Wednesday",
  16.     [5]="Thursday",
  17.     [6]="Friday",
  18.     [7]="Saturday"}
  19.     
  20.     month={
  21.     [1]="January",
  22.     [2]="February",
  23.     [3]="March",
  24.     [4]="April",
  25.     [5]="May",
  26.     [6]="June",
  27.     [7]="July",
  28.     [8]="August",
  29.     [9]="September",
  30.     [10]="October",
  31.     [11]="November",
  32.     [12]="December"}
  33.     
  34.     type={
  35.     [1]="HelveticaNeue-CondensedBlack",
  36.     [2]="GillSans-Light"}
  37.  
  38.     lineCapMode(ROUND)
  39.     smooth()
  40.     mode=FULLSCREEN
  41.     displayMode(mode)
  42.     textMode(CENTER)
  43.     spriteMode(CORNER)
  44.     fontSize(30)
  45.     X=((WIDTH-Size)/2)+(Size/2)
  46.     Y=((HEIGHT-Size)/2)+(Size/2)
  47.     
  48.     minutes=360/60
  49.     hours=360/12
  50.     seconds=360/60
  51.     lastsecond=0
  52.     
  53.     AlarmRing=0
  54.     Snoozing=0
  55.     SnoozeStop=-1
  56.     
  57.     touched=nil
  58.     SaveToggle=0
  59. end
  60.     
  61. function graphics()
  62.     background(Screen_Red,Screen_Green,Screen_Blue)  
  63.     font(type[Typeface])
  64. end
  65.  
  66. function ResetStrokeFill()
  67.     fill(Red,Green,Blue)       
  68.     tint(Red,Green,Blue,255)
  69.     stroke(Screen_Red,Screen_Green,Screen_Blue)
  70. end
  71.  
  72. function interface()
  73.     if  CurrentTouch.state==MOVING then
  74.         X,Y=CurrentTouch.x,CurrentTouch.y
  75.     end
  76.     if CurrentTouch.state==ENDED then touched=nil end
  77.     if CurrentTouch.state==BEGAN and touched==nil then
  78.         if CurrentTouch.tapCount==2 then
  79.             if mode==STANDARD then
  80.                 mode=FULLSCREEN
  81.             else
  82.                 mode=STANDARD
  83.             end
  84.             displayMode(mode)
  85.             touched=true
  86.         end
  87.     end      
  88. end
  89.  
  90. function SetAlarm(ATOD)
  91.     strokeWidth(math.max((Size*.06),7))
  92.     if Alarm_Switch==0 then
  93.         stroke(Red,Green,Blue)
  94.         fill(Screen_Red,Screen_Green,Screen_Blue)
  95.         tint(Red,Green,Blue,0)
  96.     end
  97.  
  98.     --Alarm Indicators
  99.     textMode(CORNER)
  100.     topline=(HEIGHT)-75
  101.     sprite(bell:spriteimage(),30,topline+5,26,26)
  102.     if Alarm_Minute<10 then
  103.         minstr=":0"..Alarm_Minute.." "
  104.     else
  105.         minstr=":"..Alarm_Minute.." "
  106.     end
  107.     text(Alarm_Hour..minstr..ATOD,60,topline)
  108.     textMode(CENTER)
  109.     translate(X,Y)
  110.     rotate(-((hours*Alarm_Hour)+((Alarm_Minute/60)*30)))
  111.     line(0,(Size*.45),0,0+(Size*.45))
  112.     resetMatrix() 
  113.     strokeWidth(0)
  114.     ResetStrokeFill()
  115. end
  116.  
  117. function RenderClock(thishour,thismin,thissec)
  118.     --Ticks
  119.     stroke(Screen_Red,Screen_Green,Screen_Blue)
  120.     lineCapMode(SQUARE)
  121.     tinytick=Size*.48
  122.     tick=Size*.46
  123.     tickmax=Size*.5
  124.     if Size>=400 then
  125.         strokeWidth(3)   
  126.         translate(X,Y)
  127.         for i=1,360 do
  128.             rotate(1)
  129.             line(0,tinytick,0,tickmax) --tiny tick      
  130.         end
  131.         resetMatrix()
  132.     end
  133.     if Size>=250 then
  134.         strokeWidth(3)
  135.         translate(X,Y)
  136.         for i=1,60 do
  137.             rotate(6)
  138.             line(0,tick,0,tickmax) --minutes tick      
  139.         end
  140.         resetMatrix()
  141.         strokeWidth(5)    
  142.         translate(X,Y)
  143.         for i=1,12 do
  144.             rotate(30)
  145.             line(0,tick,0,tickmax) --hours tick
  146.         end
  147.         resetMatrix()
  148.     end
  149.     lineCapMode(ROUND)
  150.         
  151.     --Second Hand
  152.     strokeWidth(4)
  153.     translate(X,Y)
  154.     rotate(-(seconds*thissec))
  155.     line(0,0,0,0+(Size*.5))    
  156.     if  (lastsecond~=thissec) and (Ticking==1) then Sound:ticktock() end
  157.     lastsecond=thissec
  158.     resetMatrix()
  159.     
  160.     --Minute Hand
  161.     strokeWidth(math.max((Size*.06),5))    
  162.     translate(X,Y)
  163.     rotate(-(minutes*thismin))
  164.     line(0,0,0,0+(Size*.38))
  165.     resetMatrix() 
  166.  
  167.     --Hour Hand                   
  168.     translate(X,Y)
  169.     rotate(-((hours*thishour)+((thismin/60)*30))) --Add "lean"
  170.     strokeWidth(math.max((Size*.1),5))    
  171.     stroke(Red,Green,Blue)
  172.     line(0,0,0,0+(Size*.25)) --outer ring
  173.     strokeWidth(math.max((Size*.08),5))    
  174.     stroke(Screen_Red,Screen_Green,Screen_Blue)
  175.     line(0,0,0,0+(Size*.25)) --hand
  176.     resetMatrix()
  177.     strokeWidth(0)
  178.     ellipse(X,Y,Size*.05) --center stud    
  179.     ResetStrokeFill()
  180. end
  181.  
  182. function TextDisplay(thishour,thismin,thissec,timeofday)
  183.     if Text==0 then fill(Screen_Red,Screen_Green,Screen_Blue) end
  184.     if thismin<10 then minstr=":0"..thismin else minstr=":"..thismin end
  185.     if thissec<10 then secstr=":0"..thissec else secstr=":"..thissec end
  186.     timenow=thishour..minstr..secstr.." "..timeofday.."  "
  187.     weekdaynow=weekday[now.wday]..", "
  188.     datenow=month[now.month].." "..now.day.." "..now.year.."  "
  189.     daynow="Day "..now.yday
  190.     text(timenow..weekdaynow..datenow..daynow,WIDTH/2,75)
  191.     ResetStrokeFill()   
  192. end    
  193.  
  194. function clock()
  195.     now=os.date("*t") --seems this gets passed to TextDisplay
  196.     thishour=now.hour
  197.     thismin=now.min
  198.     thissec=now.sec
  199.     if thishour<12 then
  200.         timeofday="AM"
  201.     else
  202.         timeofday="PM"
  203.         if thishour>12 then thishour=thishour-12 end
  204.     end
  205.     ellipse(X,Y,Size,Size)
  206.  
  207.     if Alarm_Time_of_Day==0 then ATOD="AM" else ATOD="PM" end    
  208.     SetAlarm(ATOD)    
  209.     RenderClock(thishour,thismin,thissec)
  210.     TextDisplay(thishour,thismin,thissec,timeofday)                     
  211.     return thishour,thismin,thissec,timeofday,ATOD
  212. end
  213.  
  214. function alarm(thishour,thismin,thissec,timeofday,ATOD)
  215.     if Alarm_Switch==1 then
  216.         if ATOD==timeofday then       
  217.             if Alarm_Hour==thishour then
  218.                 if Alarm_Minute==thismin then
  219.                     if AlarmRing==0 then AlarmRing=1 end
  220.                 end
  221.             end
  222.         end    
  223.     else
  224.         AlarmRing,Snoozing=0,0
  225.     end
  226.     Snooze:manage(thismin,thissec)
  227. end
  228.  
  229. -- This function gets called once every frame
  230. function draw()
  231.     graphics()
  232.     interface()
  233.     alarm(clock())
  234.     Storage:manage()
  235. end
  236.  
  237.  
  238. BELL TAB
  239.  
  240. bell = class()
  241.  
  242. function bell:spriteimage()
  243. local img = image(16, 16)
  244. img:set(1,3,255,255,255,255)
  245. img:set(2,3,255,255,255,255)
  246. img:set(2,4,255,255,255,255)
  247. img:set(3,3,255,255,255,255)
  248. img:set(3,4,255,255,255,255)
  249. img:set(3,5,255,255,255,255)
  250. img:set(4,3,255,255,255,255)
  251. img:set(4,4,255,255,255,255)
  252. img:set(4,5,255,255,255,255)
  253. img:set(4,6,255,255,255,255)
  254. img:set(4,7,255,255,255,255)
  255. img:set(4,8,255,255,255,255)
  256. img:set(5,3,255,255,255,255)
  257. img:set(5,4,255,255,255,255)
  258. img:set(5,5,255,255,255,255)
  259. img:set(5,6,255,255,255,255)
  260. img:set(5,7,255,255,255,255)
  261. img:set(5,8,255,255,255,255)
  262. img:set(5,9,255,255,255,255)
  263. img:set(5,10,255,255,255,255)
  264. img:set(5,11,255,255,255,255)
  265. img:set(5,12,255,255,255,255)
  266. img:set(5,13,255,255,255,255)
  267. img:set(5,14,255,255,255,255)
  268. img:set(6,3,255,255,255,255)
  269. img:set(6,4,255,255,255,255)
  270. img:set(6,5,255,255,255,255)
  271. img:set(6,6,255,255,255,255)
  272. img:set(6,7,255,255,255,255)
  273. img:set(6,8,255,255,255,255)
  274. img:set(6,9,255,255,255,255)
  275. img:set(6,10,255,255,255,255)
  276. img:set(6,11,255,255,255,255)
  277. img:set(6,12,255,255,255,255)
  278. img:set(6,13,255,255,255,255)
  279. img:set(6,14,255,255,255,255)
  280. img:set(6,15,255,255,255,255)
  281. img:set(7,1,255,255,255,255)
  282. img:set(7,2,255,255,255,255)
  283. img:set(7,3,255,255,255,255)
  284. img:set(7,4,255,255,255,255)
  285. img:set(7,5,255,255,255,255)
  286. img:set(7,6,255,255,255,255)
  287. img:set(7,7,255,255,255,255)
  288. img:set(7,8,255,255,255,255)
  289. img:set(7,9,255,255,255,255)
  290. img:set(7,10,255,255,255,255)
  291. img:set(7,11,255,255,255,255)
  292. img:set(7,12,255,255,255,255)
  293. img:set(7,13,255,255,255,255)
  294. img:set(7,14,255,255,255,255)
  295. img:set(7,15,255,255,255,255)
  296. img:set(8,1,255,255,255,255)
  297. img:set(8,2,255,255,255,255)
  298. img:set(8,3,255,255,255,255)
  299. img:set(8,4,255,255,255,255)
  300. img:set(8,5,255,255,255,255)
  301. img:set(8,6,255,255,255,255)
  302. img:set(8,7,255,255,255,255)
  303. img:set(8,8,255,255,255,255)
  304. img:set(8,9,255,255,255,255)
  305. img:set(8,10,255,255,255,255)
  306. img:set(8,11,255,255,255,255)
  307. img:set(8,12,255,255,255,255)
  308. img:set(8,13,255,255,255,255)
  309. img:set(8,14,255,255,255,255)
  310. img:set(8,15,255,255,255,255)
  311. img:set(9,1,255,255,255,255)
  312. img:set(9,2,255,255,255,255)
  313. img:set(9,3,255,255,255,255)
  314. img:set(9,4,255,255,255,255)
  315. img:set(9,5,255,255,255,255)
  316. img:set(9,6,255,255,255,255)
  317. img:set(9,7,255,255,255,255)
  318. img:set(9,8,255,255,255,255)
  319. img:set(9,9,255,255,255,255)
  320. img:set(9,10,255,255,255,255)
  321. img:set(9,11,255,255,255,255)
  322. img:set(9,12,255,255,255,255)
  323. img:set(9,13,255,255,255,255)
  324. img:set(9,14,255,255,255,255)
  325. img:set(9,15,255,255,255,255)
  326. img:set(10,3,255,255,255,255)
  327. img:set(10,4,255,255,255,255)
  328. img:set(10,5,255,255,255,255)
  329. img:set(10,6,255,255,255,255)
  330. img:set(10,7,255,255,255,255)
  331. img:set(10,8,255,255,255,255)
  332. img:set(10,9,255,255,255,255)
  333. img:set(10,10,255,255,255,255)
  334. img:set(10,11,255,255,255,255)
  335. img:set(10,12,255,255,255,255)
  336. img:set(10,13,255,255,255,255)
  337. img:set(10,14,255,255,255,255)
  338. img:set(10,15,255,255,255,255)
  339. img:set(11,3,255,255,255,255)
  340. img:set(11,4,255,255,255,255)
  341. img:set(11,5,255,255,255,255)
  342. img:set(11,6,255,255,255,255)
  343. img:set(11,7,255,255,255,255)
  344. img:set(11,8,255,255,255,255)
  345. img:set(11,9,255,255,255,255)
  346. img:set(11,10,255,255,255,255)
  347. img:set(11,11,255,255,255,255)
  348. img:set(11,12,255,255,255,255)
  349. img:set(11,13,255,255,255,255)
  350. img:set(11,14,255,255,255,255)
  351. img:set(12,3,255,255,255,255)
  352. img:set(12,4,255,255,255,255)
  353. img:set(12,5,255,255,255,255)
  354. img:set(12,6,255,255,255,255)
  355. img:set(12,7,255,255,255,255)
  356. img:set(12,8,255,255,255,255)
  357. img:set(13,3,255,255,255,255)
  358. img:set(13,4,255,255,255,255)
  359. img:set(13,5,255,255,255,255)
  360. img:set(14,3,255,255,255,255)
  361. img:set(14,4,255,255,255,255)
  362. img:set(15,3,255,255,255,255)
  363. return img
  364. end
  365.  
  366. SNOOZE TAB
  367.  
  368. Snooze = class()
  369.  
  370. function Snooze:manage(thismin,thissec)
  371.     --Start Snooze? Swipe to Start
  372.     if AlarmRing==1 then               
  373.         if CurrentTouch.state==MOVING and Snoozing==0 then
  374.             Snoozing=1
  375.             SnoozeStop=thismin+Snooze_Time+(thissec/60)
  376.             if SnoozeStop>59.99 then SnoozeStop=SnoozeStop-60 end
  377.         end 
  378.     end
  379.     --Is Snooze done?             
  380.     if SnoozeStop==thismin+(thissec/60) then
  381.         Snoozing=0
  382.         SnoozeStop=-1 --reset
  383.     end
  384.     if AlarmRing==1 and Snoozing==0 then Sound:alarm() end
  385. end
  386.  
  387. SOUND TAB
  388.  
  389. Sound = class()
  390.  
  391. function Sound:alarm()
  392.     sound(DATA, "ZgBAOwBAQEBAQEBAy5IJPvClpj4AAIA/SwBAf0BAQEBAQEBA")
  393.     sound(DATA, "ZgJAAAAAAAAAAAB5nc2zuwAAgD8AAAAAAAAAAAAAAD8AAEAA")  
  394. end
  395.  
  396. function Sound:ticktock()
  397.     if AlarmRing==1 and Snoozing==0 then
  398.     else
  399.         sound(DATA, "ZgFAf1cAf39/f39pAAAAAOU1lD4AAAAAAAB/f39/AEB/AD1/") 
  400.     end   
  401. end
  402.  
  403.  
  404. STORAGE TAB
  405.  
  406.  
  407. Storage = class()
  408.  
  409. function Storage:manage()
  410.     if SaveToggle~=Toggle_Save then
  411.         SaveToggle=Toggle_Save
  412.         self.Save()
  413.     end
  414.     if Reset_Factory_Defaults==1 then --not working right
  415. --        clearLocalData()
  416.         mode=FULLSCREEN
  417.         displayMode(mode)
  418.         clearParameters()
  419.         self.FactoryDefaults()
  420.         self.Save() --shouldn't be necessary
  421.         ResetStrokeFill()
  422.     end
  423. end
  424.  
  425. function Storage:FactoryDefaults()
  426.         iparameter("Toggle_Save",0,1,0)
  427.         iparameter("Reset_Factory_Defaults",0,1,0)
  428.         iparameter("Size",150,700,525)
  429.         iparameter("Ticking",0,1,1)
  430.         iparameter("Text",0,1,1)
  431.         iparameter("Typeface",1,2,1)
  432.         iparameter("Screen_Red",0,255,0)
  433.         iparameter("Screen_Green",0,255,0)
  434.         iparameter("Screen_Blue",0,257,0)
  435.         iparameter("Red",0,255,255)
  436.         iparameter("Green",0,255,180)
  437.         iparameter("Blue",0,255,0)
  438.         iparameter("Alarm_Switch",0,1,1)
  439.         iparameter("Alarm_Hour",1,12,5)
  440.         iparameter("Alarm_Minute",0,59,0)
  441.         iparameter("Alarm_Time_of_Day",0,1,0)
  442.         iparameter("Snooze_Time",0,20,10)
  443. end    
  444.  
  445. function Storage:Save()       
  446.         saveLocalData("exist",true)
  447.         saveLocalData("ipSize",Size)
  448.         saveLocalData("ipTicking",Ticking)
  449.         saveLocalData("ipText",Text)
  450.         saveLocalData("ipTypeface",Typeface)
  451.         saveLocalData("ipScreen_Red",Screen_Red)
  452.         saveLocalData("ipScreen_Green",Screen_Green)
  453.         saveLocalData("ipScreen_Blue",Screen_Blue)
  454.         saveLocalData("ipRed",Red)
  455.         saveLocalData("ipGreen",Green)
  456.         saveLocalData("ipBlue",Blue)
  457.         saveLocalData("ipAlarm_Switch",Alarm_Switch)
  458.         saveLocalData("ipAlarm_Hour",Alarm_Hour)
  459.         saveLocalData("ipAlarm_Minute",Alarm_Minute)
  460.         saveLocalData("ipATOD",ATOD)
  461.         saveLocalData("ipSnooze_Time",Snooze_Time)    
  462. end
  463.  
  464. function Storage:Load()
  465.         iparameter("Toggle_Save",0,1,0)
  466.         iparameter("Reset_Factory_Defaults",0,1,0)
  467.         iparameter("Size",150,700,readLocalData("ipSize"))
  468.         iparameter("Ticking",0,1,readLocalData("ipTicking"))
  469.         iparameter("Text",0,1,readLocalData("ipText"))
  470.         iparameter("Typeface",1,2,readLocalData("ipTypeface"))
  471.         iparameter("Screen_Red",0,255,readLocalData("ipScreen_Red"))
  472.         iparameter("Screen_Green",0,255,readLocalData("ipScreen_Green"))
  473.         iparameter("Screen_Blue",0,257,readLocalData("ipScreen_Blue"))
  474.         iparameter("Red",0,255,readLocalData("ipRed"))
  475.         iparameter("Green",0,255,readLocalData("ipGreen"))
  476.         iparameter("Blue",0,255,readLocalData("ipBlue"))
  477.         iparameter("Alarm_Switch",0,1,readLocalData("ipAlarm_Switch"))
  478.         iparameter("Alarm_Hour",1,12,readLocalData("ipAlarm_Hour"))
  479.         iparameter("Alarm_Minute",0,59,readLocalData("ipAlarm_Minute"))
  480.         iparameter("Alarm_Time_of_Day",0,1,readLocalData("ipATOD"))
  481.         iparameter("Snooze_Time",0,20,readLocalData("ipSnooze_Time"))   
  482. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement