Skip to content

Commit

Permalink
Added LotteryNumber Picker program
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyWideFoot committed Jan 2, 2012
1 parent 5b35878 commit ed1fe17
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 0 deletions.
57 changes: 57 additions & 0 deletions LotteryNumberPicker/Button.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
47 changes: 47 additions & 0 deletions LotteryNumberPicker/LotteryNumberPicker2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyName>LotteryNumberPicker2</AssemblyName>
<OutputType>Exe</OutputType>
<RootNamespace>LotteryNumberPicker2</RootNamespace>
<ProjectTypeGuids>{b69e3092-b931-443c-abe7-7e7b65f2a37f};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{F42FB074-AF2E-4A19-8D07-3AFB9686E50A}</ProjectGuid>
<TargetFrameworkVersion>v4.1</TargetFrameworkVersion>
<NetMfTargetsBaseDir Condition="'$(NetMfTargetsBaseDir)'==''">$(MSBuildExtensionsPath32)\Microsoft\.NET Micro Framework\</NetMfTargetsBaseDir>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<Import Project="$(NetMfTargetsBaseDir)$(TargetFrameworkVersion)\CSharp.Targets" />
<ItemGroup>
<Compile Include="Button.cs" />
<Compile Include="NoParamEventHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Program.cs" />
<Compile Include="SevenSegmentLedController.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.SPOT.Hardware" />
<Reference Include="Microsoft.SPOT.Native" />
<Reference Include="Microsoft.SPOT.Net" />
<Reference Include="SecretLabs.NETMF.Hardware" />
<Reference Include="SecretLabs.NETMF.Hardware.NetduinoPlus" />
<Reference Include="System" />
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions LotteryNumberPicker/LotteryNumberPicker2.sln
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions LotteryNumberPicker/NoParamEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace LotteryNumberPicker2
{
public delegate void NoParamEventHandler();
}
65 changes: 65 additions & 0 deletions LotteryNumberPicker/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
25 changes: 25 additions & 0 deletions LotteryNumberPicker/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
121 changes: 121 additions & 0 deletions LotteryNumberPicker/SevenSegmentLedController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace LotteryNumberPicker2
{
/// <summary>
/// Controls a pair of Multiplexed 7-Segment Displays
/// </summary>
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);
}
}
}

0 comments on commit ed1fe17

Please sign in to comment.