Created
April 10, 2013 19:59
-
-
Save rossdargan/5357928 to your computer and use it in GitHub Desktop.
A program to control a PacMan remote control
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO.Ports; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PacMan.Lib | |
{ | |
public class PacManManager : IDisposable | |
{ | |
readonly SerialPort _serialPort; | |
public enum Command | |
{ | |
Speed =0, | |
Red = 4, | |
Yellow =5, | |
Off=8, | |
Blue=12, | |
Purple=14, | |
Green=20, | |
On=24, | |
Fade=25, | |
Flash=26, | |
White=28, | |
Smooth=29, | |
Strobe=30 | |
} | |
public PacManManager(string comPort) | |
{ | |
_serialPort = new SerialPort(comPort); | |
_serialPort.Open(); | |
_serialPort.DataReceived += _serialPort_DataReceived; | |
} | |
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) | |
{ | |
SerialPort sp = (SerialPort)sender; | |
while (sp.BytesToRead > 0) | |
{ | |
try | |
{ | |
Debug.Write(sp.ReadLine()); | |
} | |
catch (TimeoutException) | |
{ | |
break; | |
} | |
} | |
} | |
public void DoCommand(Command command) | |
{ | |
int commandNumber = (int) command; | |
byte[] intBytes = new byte[] { (byte)commandNumber }; | |
_serialPort.Write(intBytes, 0, intBytes.Length); | |
} | |
public void Dispose() | |
{ | |
DoCommand(Command.Off); | |
_serialPort.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment