Created
October 29, 2014 15:35
-
-
Save stevecooperorg/bc34491db41cae9d8717 to your computer and use it in GitHub Desktop.
Blinky lights for the build yak (2014-10-29 15:34)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// uses .Net Micro Framework 4.2, runs on Netduino 2 Plus. | |
using System; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
using Microsoft.SPOT; | |
using Microsoft.SPOT.Hardware; | |
using SecretLabs.NETMF.Hardware; | |
using SecretLabs.NETMF.Hardware.Netduino; | |
namespace BlinkyLights | |
{ | |
public class Program | |
{ | |
public class Led | |
{ | |
private readonly OutputPort redPort; | |
private readonly OutputPort greenPort; | |
private readonly OutputPort bluePort; | |
public Led(Cpu.Pin redPin, Cpu.Pin greenPin, Cpu.Pin bluePin) | |
{ | |
this.redPort = new OutputPort(redPin, false); | |
this.greenPort = new OutputPort(greenPin, false); | |
this.bluePort = new OutputPort(bluePin, false); | |
} | |
public void Red() { SetColor(true, false, false); } | |
public void Green() { SetColor(false, true, false); } | |
public void Blue() { SetColor(false, false, true); } | |
public void Off() { SetColor(false, false, false); } | |
private void SetColor(bool red, bool green, bool blue) | |
{ | |
this.redPort.Write(red); | |
this.greenPort.Write(green); | |
this.bluePort.Write(blue); | |
} | |
public void SetColor(LedColor color) | |
{ | |
bool r = false; | |
bool g = false; | |
bool b = false; | |
switch (color) | |
{ | |
case LedColor.Red: | |
r = true; | |
break; | |
case LedColor.Blue: | |
b = true; | |
break; | |
case LedColor.Green: | |
g = true; | |
break; | |
} | |
SetColor(r, g, b); | |
} | |
} | |
public static void Main() | |
{ | |
// write your code here | |
var rifle = new Led(Pins.GPIO_PIN_D13, Pins.GPIO_PIN_D12, Pins.GPIO_PIN_D11); | |
var eyes = new Led(Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D8); | |
Swoop(new [] { eyes, rifle }, 10); | |
rifle.Off(); | |
eyes.Off(); | |
} | |
public enum LedColor | |
{ | |
Red, | |
Green, | |
Blue, | |
Off | |
} | |
public static void Swoop(Led[] leds, int seconds) | |
{ | |
int stepMs = 1; | |
LedColor startColor = LedColor.Red; | |
LedColor endColor = LedColor.Blue; | |
var r = new Random(); | |
var now = DateTime.Now; | |
int totalMs = seconds * 1000; | |
var endSwoopAt = now.AddMilliseconds(totalMs); | |
for(var ms = 0; ms < totalMs; ms += stepMs) | |
{ | |
double fractionOfEndColor = (totalMs - 1.0d * ms) / totalMs; | |
bool isEndColor = r.NextDouble() > fractionOfEndColor; | |
var color = isEndColor ? endColor : startColor; | |
foreach (var led in leds) | |
{ | |
led.SetColor(color); | |
} | |
Wait(stepMs); | |
}; | |
} | |
private static void Wait(int p) | |
{ | |
Thread.Sleep(p); | |
} | |
private static void BlinkPin(OutputPort port, int p) | |
{ | |
port.Write(true); | |
Wait(p); | |
port.Write(false); | |
} | |
//private void Red | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment