136 lines
4.8 KiB
C#
136 lines
4.8 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api.Devices;
|
|
using System;
|
|
using System.Text;
|
|
|
|
namespace VisionLight {
|
|
/// <summary>
|
|
/// [상시 조명 컨트롤러 - 16CH / 시리얼 통신][Series]
|
|
/// Range : (0,255)
|
|
/// Baudrate : 9600 bps
|
|
/// DataBit : 8 bit
|
|
/// StopBit : 1 bit
|
|
/// ParityBit : None
|
|
/// </summary>
|
|
internal class Light_LEESOS_LPD : LightBase_Serial {
|
|
public const byte CMD_SINGLE_CHANNEL_ON_OFF = 0x48; // H
|
|
public const byte CMD_SINGLE_CHANNEL_BRIGHTNESS = 0x43; // C
|
|
|
|
public const byte BYTE_F = 0x46; // 70
|
|
public const byte BYTE_G = 0x47; // 71
|
|
public const byte BYTE_N = 0x4E; // 78
|
|
public const byte BYTE_O = 0x4F; // 79
|
|
public const byte BYTE_R = 0x52; // 82
|
|
public const byte BYTE_CR = 0x0D; // 13
|
|
public const byte BYTE_LF = 0x0A; // 10
|
|
public const byte BYTE_0 = 0x30; // 48
|
|
|
|
public const byte LENGTH_SINGLE_CHANNEL_OFF_REQUEST_MSG = 6;
|
|
public const byte LENGTH_SINGLE_CHANNEL_BRIGHTNESS_REQUEST_MSG = 6;
|
|
|
|
public Light_LEESOS_LPD(string deviceId) : base(deviceId) {
|
|
}
|
|
|
|
protected override eLightResult SetOnImpl(int channel) {
|
|
return LightOnOff(channel, true);
|
|
}
|
|
|
|
protected override eLightResult SetOffImpl(int channel) {
|
|
return LightOnOff(channel, false);
|
|
}
|
|
|
|
private eLightResult LightOnOff(int channel, bool isOnOff) {
|
|
if (!IsValidChannel(channel)) {
|
|
LOG.E(string.Format("Argument Error! Ch : {0}", channel.ToString()));
|
|
return eLightResult.NG;
|
|
}
|
|
|
|
lock (LockThis) {
|
|
byte[] requestMsg = CreateSingleChannelOnOffReqMsg(channel, isOnOff);
|
|
if (!SerialConn.Write(requestMsg)) {
|
|
return eLightResult.NG;
|
|
}
|
|
}
|
|
|
|
return eLightResult.OK;
|
|
}
|
|
|
|
private static byte[] CreateSingleChannelOnOffReqMsg(int iChannel, bool isOnOff) {
|
|
byte[] requestMsg = new byte[LENGTH_SINGLE_CHANNEL_OFF_REQUEST_MSG];
|
|
|
|
requestMsg[0] = CMD_SINGLE_CHANNEL_ON_OFF;
|
|
requestMsg[1] = ConvertChannelIntToByte(iChannel);
|
|
requestMsg[2] = BYTE_O;
|
|
requestMsg[3] = (isOnOff) ? BYTE_N : BYTE_F;
|
|
requestMsg[requestMsg.Length - 2] = BYTE_CR;
|
|
requestMsg[requestMsg.Length - 1] = BYTE_LF;
|
|
|
|
return requestMsg;
|
|
}
|
|
|
|
protected override eLightResult SetLevelImpl(int level, int channel) {
|
|
try {
|
|
if (!IsValidChannel(channel) || !IsValidBrightness(level)) {
|
|
LOG.E(string.Format("Argument Error! Ch : {0}, Value : {1}", channel, level));
|
|
return eLightResult.NG;
|
|
}
|
|
|
|
lock (LockThis) {
|
|
byte[] requestMsg = CreateSingleChannelBrightnessReqMsg(channel, level);
|
|
if (!SerialConn.Write(requestMsg)) {
|
|
return eLightResult.NG;
|
|
}
|
|
}
|
|
|
|
return eLightResult.OK;
|
|
}
|
|
catch (Exception e) {
|
|
LOG.FuncE(e.Message);
|
|
}
|
|
return eLightResult.NG;
|
|
}
|
|
|
|
private bool IsValidChannel(int channel) {
|
|
return IsValidValue(channel, LightConfig.Channels.Min, LightConfig.Channels.Max);
|
|
}
|
|
|
|
private bool IsValidBrightness(int value) {
|
|
return IsValidValue(value, LightConfig.DimRange.Min, LightConfig.DimRange.Max);
|
|
}
|
|
|
|
private static bool IsValidValue(int value, int min, int max) {
|
|
return min <= value && value <= max;
|
|
}
|
|
|
|
private static byte[] CreateSingleChannelBrightnessReqMsg(int iChannel, int iValue) {
|
|
byte[] requestMsg = new byte[LENGTH_SINGLE_CHANNEL_BRIGHTNESS_REQUEST_MSG];
|
|
byte[] valueByte = ConvertBrightnessIntToBytes(iValue);
|
|
|
|
requestMsg[0] = CMD_SINGLE_CHANNEL_BRIGHTNESS;
|
|
requestMsg[1] = ConvertChannelIntToByte(iChannel);
|
|
Array.Copy(valueByte, 0, requestMsg, 2, valueByte.Length);
|
|
requestMsg[requestMsg.Length - 2] = BYTE_CR;
|
|
requestMsg[requestMsg.Length - 1] = BYTE_LF;
|
|
|
|
return requestMsg;
|
|
}
|
|
|
|
private static byte ConvertChannelIntToByte(int iChannel) {
|
|
if (iChannel == 16) {
|
|
return BYTE_G;
|
|
}
|
|
else {
|
|
var bytes = Encoding.Default.GetBytes(iChannel.ToString("X1"));
|
|
return bytes[0];
|
|
}
|
|
}
|
|
|
|
private static byte[] ConvertBrightnessIntToBytes(int iValue) {
|
|
return Encoding.Default.GetBytes(iValue.ToString("X2"));
|
|
}
|
|
}
|
|
}
|