diff --git a/LotteryNumberPicker/Button.cs b/LotteryNumberPicker/Button.cs new file mode 100644 index 0000000..08b67fc --- /dev/null +++ b/LotteryNumberPicker/Button.cs @@ -0,0 +1,57 @@ +using System; +using Microsoft.SPOT.Hardware; + +namespace LotteryNumberPicker2 +{ + public sealed class Button + { + private readonly InterruptPort _port; + + public event NoParamEventHandler Pressed; + public event NoParamEventHandler Released; + + public Button(Cpu.Pin pin) + { + _port = new InterruptPort(pin, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); + _port.OnInterrupt += port_OnInterrupt; + } + + public bool IsPressed + { + get { return !_port.Read(); } + } + + private void port_OnInterrupt(uint data1, uint data2, DateTime time) + { + _port.DisableInterrupt(); + + if (data2 > 0) + { + OnPressed(); + } + else + { + OnReleased(); + } + + _port.EnableInterrupt(); + _port.ClearInterrupt(); + } + + private void OnReleased() + { + if (Released != null) + { + Released(); + } + } + + private void OnPressed() + { + if (Pressed != null) + { + Pressed(); + } + } + } +} \ No newline at end of file diff --git a/LotteryNumberPicker/LotteryNumberPicker2.csproj b/LotteryNumberPicker/LotteryNumberPicker2.csproj new file mode 100644 index 0000000..5eb0043 --- /dev/null +++ b/LotteryNumberPicker/LotteryNumberPicker2.csproj @@ -0,0 +1,47 @@ + + + + LotteryNumberPicker2 + Exe + LotteryNumberPicker2 + {b69e3092-b931-443c-abe7-7e7b65f2a37f};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 9.0.21022 + 2.0 + {F42FB074-AF2E-4A19-8D07-3AFB9686E50A} + v4.1 + $(MSBuildExtensionsPath32)\Microsoft\.NET Micro Framework\ + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LotteryNumberPicker/LotteryNumberPicker2.sln b/LotteryNumberPicker/LotteryNumberPicker2.sln new file mode 100644 index 0000000..1e4d8dc --- /dev/null +++ b/LotteryNumberPicker/LotteryNumberPicker2.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LotteryNumberPicker2", "LotteryNumberPicker2.csproj", "{F42FB074-AF2E-4A19-8D07-3AFB9686E50A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F42FB074-AF2E-4A19-8D07-3AFB9686E50A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F42FB074-AF2E-4A19-8D07-3AFB9686E50A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F42FB074-AF2E-4A19-8D07-3AFB9686E50A}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {F42FB074-AF2E-4A19-8D07-3AFB9686E50A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F42FB074-AF2E-4A19-8D07-3AFB9686E50A}.Release|Any CPU.Build.0 = Release|Any CPU + {F42FB074-AF2E-4A19-8D07-3AFB9686E50A}.Release|Any CPU.Deploy.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/LotteryNumberPicker/NoParamEventHandler.cs b/LotteryNumberPicker/NoParamEventHandler.cs new file mode 100644 index 0000000..9ac6de9 --- /dev/null +++ b/LotteryNumberPicker/NoParamEventHandler.cs @@ -0,0 +1,4 @@ +namespace LotteryNumberPicker2 +{ + public delegate void NoParamEventHandler(); +} \ No newline at end of file diff --git a/LotteryNumberPicker/Program.cs b/LotteryNumberPicker/Program.cs new file mode 100644 index 0000000..7484b17 --- /dev/null +++ b/LotteryNumberPicker/Program.cs @@ -0,0 +1,65 @@ +using System; +using System.Threading; +using Microsoft.SPOT; +using SecretLabs.NETMF.Hardware.NetduinoPlus; + +namespace LotteryNumberPicker2 +{ + public class Program + { + private const int MaxLotteryNumber = 49; + private static readonly SevenSegmentLedController Controller = new SevenSegmentLedController(); + private static readonly Random Random = new Random(); + + public static void Main() + { + // A dedicated thread to render the multi-plexed LEDs + var renderThread = new Thread(RenderWorker) + { + Priority = ThreadPriority.Lowest + }; + renderThread.Start(); + + // Prove we can render 0 --> 99 + CountCycle(); + + // Set to 0 (Blanks the displays) + Controller.SetNumber(0); + + // Configure the user-interface button + var button = new Button(Pins.GPIO_PIN_D8); + button.Pressed += OnButtonPressed; + button.Released += OnButtonReleased; + + // Wait forever... + Thread.Sleep(Timeout.Infinite); + } + + private static void RenderWorker() + { + while (true) + Controller.Render(); + } + + private static void OnButtonPressed() + { + int selectedNumber = Random.Next(MaxLotteryNumber); + Debug.Print("OnButtonPressed: " + selectedNumber); + Controller.SetNumber(selectedNumber); + } + + private static void OnButtonReleased() + { + Debug.Print("OnButtonReleased"); + } + + private static void CountCycle() + { + for (int i = 0; i < 100; i++) + { + Controller.SetNumber(i); + Thread.Sleep(30); + } + } + } +} \ No newline at end of file diff --git a/LotteryNumberPicker/Properties/AssemblyInfo.cs b/LotteryNumberPicker/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6dce746 --- /dev/null +++ b/LotteryNumberPicker/Properties/AssemblyInfo.cs @@ -0,0 +1,25 @@ +using System.Reflection; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. + +[assembly: AssemblyTitle("LotteryNumberPicker2")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("LotteryNumberPicker2")] +[assembly: AssemblyCopyright("Copyright © 2012")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// + +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/LotteryNumberPicker/SevenSegmentLedController.cs b/LotteryNumberPicker/SevenSegmentLedController.cs new file mode 100644 index 0000000..0fee577 --- /dev/null +++ b/LotteryNumberPicker/SevenSegmentLedController.cs @@ -0,0 +1,121 @@ +using System; +using System.Threading; +using Microsoft.SPOT.Hardware; +using SecretLabs.NETMF.Hardware.NetduinoPlus; + +namespace LotteryNumberPicker2 +{ + /// + /// Controls a pair of Multiplexed 7-Segment Displays + /// + public sealed class SevenSegmentLedController + { + private readonly bool[][] _pins = new[] + { + new[] {true, true, true, true, true, true, false}, // 0 + new[] {true, false, false, false, true, false, false}, // 1 + new[] {false, true, true, true, true, false, true}, // 2 + new[] {true, false, true, true, true, false, true}, // 3 + new[] {true, false, false, false, true, true, true}, // 4 + new[] {true, false, true, true, false, true, true}, // 5 + new[] {true, true, true, true, false, true, true}, // 6 + new[] {true, false, false, true, true, false, false}, // 7 + new[] {true, true, true, true, true, true, true}, // 8 + new[] {true, false, false, true, true, true, true}, // 9 + }; + + private readonly OutputPort[] _outputPorts; + private readonly OutputPort[] _controlPorts; + + private int _number; + + public SevenSegmentLedController() + { + _outputPorts = new OutputPort[7]; + _outputPorts[0] = new OutputPort(Pins.GPIO_PIN_D0, false); + _outputPorts[1] = new OutputPort(Pins.GPIO_PIN_D1, false); + _outputPorts[2] = new OutputPort(Pins.GPIO_PIN_D2, false); + _outputPorts[3] = new OutputPort(Pins.GPIO_PIN_D3, false); + _outputPorts[4] = new OutputPort(Pins.GPIO_PIN_D4, false); + _outputPorts[5] = new OutputPort(Pins.GPIO_PIN_D5, false); + _outputPorts[6] = new OutputPort(Pins.GPIO_PIN_D6, false); + + _controlPorts = new OutputPort[2]; + _controlPorts[0] = new OutputPort(Pins.GPIO_PIN_D12, false); + _controlPorts[1] = new OutputPort(Pins.GPIO_PIN_D13, false); + } + + public void SetNumber(int number) + { + if (number < 0 || number > 99) + { + throw new Exception(); + } + _number = number; + } + + public void Render() + { + int digitLeft = _number / 10; + int digitRight = _number % 10; + + SetBankOff(); + if (digitLeft == 0) + { + SetPinsOff(); + } + else + { + SetDigit(digitLeft); + } + SetBank(true); + RenderSleep(); + + SetBankOff(); + if (digitLeft == 0 && digitRight == 0) + { + SetPinsOff(); + } + else + { + SetDigit(digitRight); + } + SetBank(false); + RenderSleep(); + } + + private static void RenderSleep() + { + Thread.Sleep(3); + } + + private void SetPinsOff() + { + for (int i = 0; i < 7; i++) + { + _outputPorts[i].Write(false); + } + } + + private void SetDigit(int number) + { + var pins = _pins[number]; + for (int i = 0; i < 7; i++) + { + _outputPorts[i].Write(pins[i]); + } + } + + private void SetBankOff() + { + _controlPorts[0].Write(false); + _controlPorts[1].Write(false); + } + + private void SetBank(bool left) + { + _controlPorts[0].Write(!left); + _controlPorts[1].Write(left); + } + } +} \ No newline at end of file