83 - Timers in an alarm clock
Alright well I went to go work on my game today, and turns out my version of Unity was horrible outdated. I was using Unity 4.x where Unity 2017 has come out. Why they called the version 2017, I don't know, or I guess really care. Anyways, it took a while for that to install and then I had to mess around with some settings to get visual studio hook up to it. Because I'm not using MonoDevelop, I have no idea if it's good or bad, but I'm just so use to visual studio. Oh I guess this would be a good time to mention it. Both Unity and Visual Studio have free versions, as long as you make less then X per year. Well I don't want to just leave you with that, so let's go over some of the code I've already worked on for this small open source project. First let me explain what it is. A desktop alarm clock app, that's pretty much it. Here is where the code is located if you want to download it and mess around. I wanted a little alarm clock that sat in my system tray and I could set up to pop up and yell at me when it was time to take my medication, or anything else I guess. Now I wanted to use WPF because I like it more then win-forms but I found out it would of been a pain to get it working in the system tray. Luckily I found this library that did exactly what I needed! Okay let's cover the timing loop that is used for trigging the alarm. public AlarmTimer(MainWindow window) { TimeForAlarms = new List(); var timer = new Timer(); timer.Elapsed += timerTriggered; timer.Interval = 1000; timer.Start(); Window = window; } Alright so when you make a new AlarmTimer class we pretty much are just setting up a Timer. Timer is a class used for running events every X milliseconds on a different thread. So let's go over how I set up the timer. First we are adding an event to it, in C# you can add events to stuff with += and in order for it to work, you need to set up the params in the correct way, it's always going to be object sender and some EventArgs e. Then we set the interval to 1000, and because it's in milliseconds this triggers every second. Now I wouldn't trust this to run exactly on every second, but for what we need it's good enough. private void timerTriggered(object sender, ElapsedEventArgs e) { foreach (var item in TimeForAlarms) { if (item.AlarmTriggered() && !item.getTriggered()) { //do stuff } } } Alright so this is the method that is called every second. Clearly it isn't finish, so let's look at what is there so far. First we are looping over every alarm saved, we are saving them in a List by the way. Then for every alarm we check if the alarm should get triggered, and if the alarm has been triggered. The reason we check if the alarm is already triggered is we don't want an alarm to pop up, close it half a minute later, then the alarm to pop back up a second later. Alright now for the Alarm class it self public abstract class TimeForAlarm { private bool Triggered = false; public bool getTriggered() { return Triggered; } public void AlarmWentOff() { Triggered = true; var timer = new Timer(); timer.Interval = 60000; timer.Elapsed += ResetTrigger; timer.Start(); } private void ResetTrigger(object sender, ElapsedEventArgs e) { Triggered = false; ((Timer)sender).Stop(); } public abstract bool AlarmTriggered(); } So we are using inheritance here, we have three different sub classes that handle different types of alarms. So when we use a sub class that means it has all the things we define in this parent class. A bool for storing if it's been triggered, and those 4 methods. Now the first three methods we write code for because they are the same no matter what time of alarm it is. For the last method we know what kind of signature we need, but the code will change between the different classes. Alright let's go over the two main methods here, AlarmWentOff and ResetTrigger. The whole purpose of these methods is to make sure the alarm isn't triggered again if it's turned off within the triggering minute. You can see we are setting up a timer again with an event method, but this time we set the interval to 60000, this is roughly a minute. As before this wont be exactly a minute, but it will at least be a minute at minimum, so good enough. Then in ResetTrigger, which will be called a minute later, sets Triggered to false and stops the timer. This lets the Alarm to be triggered again. Now the whole thing with putting Timer in () is called boxing/unboxing. You can see in the method that sender is just an object, but in this case we know what kind of class it is, a Timer. So with (Timer) we can force it to believe it's that type of object. You have to be careful with this kind of stuff because if sender wasn't a Timer, it would throw errors. Okay, now let's quickly go over just one of the three sub classes public class DailyAlarm : TimeForAlarm { private DateTime Alarm; public void SetTime(int hour, int minuite) { Alarm = new DateTime(1, 1, 1, hour, minuite, 0); } public override bool AlarmTriggered() { var now = DateTime.Now; if (Alarm.Hour == now.Hour && Alarm.Minute == now.Minute) return true; return false; } This class is used for setting up an alarm that's ran every day. So in SetTime, we only need the hour and minute, because the day/date doesn't matter. After taking in hour and minute we just save that info as a DateTime object. This object is used for saving the exact time and day, down to the millisecond. Next we have AlarmTriggered, really all this does is check the important values in Alarm compared to now. Nothing too complex, and it's more or less the same for the other two classes. If you are curious, again you can check the code over here Alright, I guess that's it for today. Sorry there was nothing about my video game, but my time is limited, and my computer is slow, so installing Unity took a while. Well tomorrow I'll be working on this project again, so look forward to it. Also I thought about it, and the next few video game days, I'm just going to work on Unity tutorials. I haven't touched Unity in a while, and it's a new version. I figured it wouldn't hurt to touch up on some basis. Anyways, have a good day nerds!
4/9/2018 10:56:10 PM

Add Comment Auther