RGBController/RGBController2/Boards/ArduinoBoard.cs

49 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
namespace RGBController2.Boards
{
public class ArduinoBoard : IBoard
{
public bool Connected { get; }
private SerialPort _serialPort;
public ArduinoBoard(string portName, int baudRate = 9600)
{
Connected = false;
_serialPort = new SerialPort();
_serialPort.PortName = portName;
_serialPort.BaudRate = baudRate;
_serialPort.Open();
if (_serialPort.IsOpen)
Connected = true;
}
public void SetAllLeds(byte red, byte green, byte blue)
{
string command = "a";
command += ByteToHexString(red);
command += ByteToHexString(green);
command += ByteToHexString(blue);
command += ';';
_serialPort.WriteLine(command);
}
public void TurnOffAllLeds()
{
string command = "a000000;";
_serialPort.WriteLine(command);
}
private static string ByteToHexString(byte b)
{
StringBuilder hex = new StringBuilder(2);
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
}
}