-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
104 lines (78 loc) · 3.67 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Created by Rory Glynn */
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using System.Threading;
namespace AgentWatchApplication1
{
public class Program
{
/*Constants*/
static int DISPLAY_HEIGHT = 128;
static int DISPLAY_WIDTH = 128;
static int DISPLAY_MARGIN = 2;
static Bitmap _display;
static InterruptPort _buttonUp;
static InterruptPort _buttonDown;
static int _countVal = 0;
static string _resetLabel = "Reset";
static string _incrementLabel = "Increment";
public static void Main()
{
// initialize display buffer
_display = new Bitmap(Bitmap.MaxWidth, Bitmap.MaxHeight);
_countVal = -1;
incrementCount(0, 0, DateTime.Now);
_buttonUp = new InterruptPort(HardwareProvider.HwProvider.GetButtonPins(Button.VK_UP), false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLow);
_buttonDown = new InterruptPort(HardwareProvider.HwProvider.GetButtonPins(Button.VK_DOWN), false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLow);
_buttonUp.OnInterrupt += incrementCount;
_buttonDown.OnInterrupt += resetCount;
// go to sleep; all further code should be timer-driven or event-driven
Thread.Sleep(Timeout.Infinite);
}
private static void resetCount(uint data1, uint data2, DateTime time)
{
_countVal = -1;
incrementCount(data1, data2, time);
}
private static void incrementCount(uint data1, uint data2, DateTime time)
{
if (_countVal < 9999999)
_countVal++;
_display.Clear();
Font fontNinaB = Resources.GetFont(Resources.FontResources.NinaB);
Font fontStencil = Resources.GetFont(Resources.FontResources.stencil72);
// Use a smaller version of the Stencil font for larger numbers
if (_countVal > 99999)
fontStencil = Resources.GetFont(Resources.FontResources.stencil24);
else if (_countVal > 999)
fontStencil = Resources.GetFont(Resources.FontResources.stencil32);
else if (_countVal > 99)
fontStencil = Resources.GetFont(Resources.FontResources.stencil48);
int charsAcross = DISPLAY_WIDTH / fontStencil.CharWidth('0');
_display.DrawText(_countVal.ToString("d" + charsAcross), fontStencil, Color.White,
(DISPLAY_WIDTH % fontStencil.CharWidth('0')) / 2,
(DISPLAY_HEIGHT - fontStencil.Height) / 2);
// Draw labels for Reset and Increment buttons.
_display.DrawText(_resetLabel, fontNinaB, Color.White, alignRight(_resetLabel, fontNinaB), alignBottom(fontNinaB));
_display.DrawText(_incrementLabel, fontNinaB, Color.White, alignRight(_incrementLabel, fontNinaB), DISPLAY_MARGIN);
_display.Flush();
}
private static int alignRight(string text, Font textFont)
{
int increaseLabelWidth = 0;
char[] increaseCharArray = _incrementLabel.ToCharArray(0, _incrementLabel.Length);
for (int letterNo = 0; letterNo < increaseCharArray.Length; letterNo++)
{
increaseLabelWidth += textFont.CharWidth(increaseCharArray[letterNo]);
}
return DISPLAY_WIDTH - increaseLabelWidth - DISPLAY_MARGIN;
}
private static int alignBottom(Font textFont)
{
return DISPLAY_HEIGHT - textFont.Height - DISPLAY_MARGIN;
}
}
}