48 lines
1.3 KiB
C#
48 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
|
|||
|
{
|
|||
|
private SerialPort _serialPort;
|
|||
|
public bool Conected { get; }
|
|||
|
|
|||
|
public ArduinoBoard(string portName, int baudRate = 9600)
|
|||
|
{
|
|||
|
Conected = false;
|
|||
|
_serialPort = new SerialPort();
|
|||
|
_serialPort.PortName = portName;
|
|||
|
_serialPort.BaudRate = baudRate;
|
|||
|
_serialPort.Open();
|
|||
|
if (_serialPort.IsOpen)
|
|||
|
Conected = 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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|