Button click logging
#1
Posted 17 January 2012 - 10:53 PM
#2
Posted 18 January 2012 - 05:03 AM
Recording an event and log it on a SD is trivial, unless you don't need high precision on timing.
Here is an example, but *I DIDN'T TRIED IT*...
Be sure to add these "using" clauses:
using System.IO; using System.Text;
Then try to play around this base:
public class Program { private const TimeSpan Interval = TimeSpan.FromTicks(TimeSpan.TicksPerMinute * 5); private static int _counter; private static Timer _timer; public static void Main() { //create and start the timer _timer = new Timer( TimerTick, null, Interval, Interval); //provide your own logic to increment the counter //example: using the onboard button var sw = new InterruptPort( Pins.ONBOARD_SW1, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow); sw.OnInterrupt += new NativeEventHandler(sw_OnInterrupt); //block the program flow, otherwise the application will end Thread.Sleep(Timeout.Infinite); } static void sw_OnInterrupt(uint data1, uint data2, DateTime time) { _counter++; } private static void TimerTick(object state) { WriteData(_counter); _counter = 0; } private static void WriteData(int data) { var fileName = @"\\SD\\root\MyTextFile.txt"; //open the specified file for append, and as a stream using (FileStream stream = File.Open(fileName, FileMode.Append)) { //convert the given data to string, then to a byte array string text = data.ToString(); byte[] buffer = Encoding.UTF8.GetBytes(text); //write the buffer onto the file stream stream.Write(buffer, 0, buffer.Length); } } }
It's far to be perfect, but it should be a good point where start from.
Cheers
#3
Posted 18 January 2012 - 07:48 PM
#4
Posted 19 January 2012 - 05:22 AM
public Timer ( TimerCallback callback, Object state, TimeSpan dueTime, TimeSpan period)
The first parameter is the TimerCallback delegate. A delegate is much like a definition of a method signature, or -if you like better- the definition of the target of a function-pointer (that's improper, but my fellow understands better, since is a C/C++ programmer).
Bear in mind that the C# is a strongly-typed programming language, so you can't do anything that won't be strictly under control.
So, the TimerCallback represents the signature of the method which handles the events fired by the timer itself. Note that the name of the method hasn't any relevance, but the types involved do.
In my snippet I didn't explicitly used the TimerCallback delegate as often is used. The typical form is as follows:
_timer = new Timer( new TimerCallback(TimerTick), null, Interval, Interval;The "state" parameter is a commodity argument to pass in the event handler. It's worthwhile to point out that the handler runs is a different thread other than the others running. So, the very best practice would be to pass data from/to the timer handler through the "state" parameter.
Finally there are two intervals: dueTime and period. The first one is actually the period which has to elapse from the timer activation to the *first* event firing. Instead, the "period" is related to the interval that should observed for all the further events.
Again, it's important to remember that the timer is a mechanism running in a dedicated thread, at low-level. So, the threading system doesn't know about the upper C# management, and the user should take in account that.
So, there are just two important things to avoid to be forgotten:
- the event handler is running in a different, reserved thread other than the rest of the application, and
- once you don't need the timer instance anymore, you should Dispose it.
_timer.Change(Timeout.Infinite, Timeout.Infinite); //this stops the timer _timer.Dispose();
This ensures the correct deletion of the timer. Note that there's no way to guarantee that an extra-spurious event can't be fired. This might happen especially when the timer intervals are short. Thus, you should apply some other prevention mechanism, such a flag or else.
Hope it helps.
Cheers
#5
Posted 19 January 2012 - 01:22 PM
#6
Posted 19 January 2012 - 07:57 PM
#7
Posted 19 January 2012 - 10:59 PM
You may get some ideas by searching the forum for "bounce" and "debounce", this problem has been already discussed several times and there are working code samples...The only problem im having now is that when i press the button it seems to increment the counter by more than 1.
#8
Posted 20 January 2012 - 04:14 AM
The class goal is as a helper for a pushbutton input. It acts as debouncer, whereas auto-repeater if needed.
Hope that could help.
Cheers
#9
Posted 23 January 2012 - 04:26 PM
#10
Posted 23 January 2012 - 04:30 PM
Click on the big download-button @ http://netmftoolbox....ol/BrowseLatestWhere exactly do I find the class? Your link only shows the sample code...
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#11
Posted 23 January 2012 - 04:57 PM
Total BOCS Traveled Distance: 9708 miles | States Visited: 5
Track the Box
#12
Posted 23 January 2012 - 05:02 PM
#13
Posted 23 January 2012 - 05:25 PM
#14
Posted 23 January 2012 - 07:37 PM
How would I go about starting a stopwatch class when the button is clicked, and stopping when released?
private static InterruptPort button; public static void Main() { button = new InterruptPort(Pins.ONBOARD_SW1, true, ResistorModes.PullUp, InterruptModes.InterruptEdgeBoth); button.OnInterrupt += button_OnInterrupt; Thread.Sleep(Timeout.Infinite); } static void button_OnInterrupt(uint data1, uint data2, DateTime time) { // data2 is the pin state if(data2 == 1) { // Button pressed stopwatch.Start(); // or StartNew() } else { // Button released stopwatch.Stop(); } }
You can also use the time parameter passed to the interrupt handler, it contains the time at which the event occured.
#15
Posted 24 January 2012 - 07:55 AM
#16
Posted 24 January 2012 - 04:33 PM
#17
Posted 24 January 2012 - 04:39 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users