【发布时间】:2018-07-19 06:32:25
【问题描述】:
我是 C# 和对象编码的新手,所以请温柔....
我有一个班级呼叫 LED,见下文:
public sealed class LED
{
public GpioPinValue ReqPinValue { get; set; } //Enable/Disable LED
public bool Flashing { get; set; } //Does the LED flash
public GpioPin Pin { get; set; }
public int flashingPeriod { get; set; } //Period to flash in seconds
private GpioPinValue value; //Pin value (high/low)
private int flashCount = 0; //Times we have entered the timer loop
public LED()
{
}
public void UpdateLED()
{
int timesToCycle = 0;
if (ReqPinValue == GpioPinValue.Low)
{
if (Flashing)
{
timesToCycle = flashingPeriod * 2;
if (flashCount == timesToCycle)
{
value = (value == GpioPinValue.High) ? GpioPinValue.Low : GpioPinValue.High;
Pin.Write(value);
flashCount = 0;
}
else
flashCount++;
}
else
{
Pin.Write(GpioPinValue.Low);
}
}
else
{
Pin.Write(GpioPinValue.High);
}
}
}
在另一个类中,我为 4 个不同的状态 LED 创建了该 LED 类的四个实例。
public sealed class StatusLED
{
private const int RUN_LED = 4;
private const int IO_LED = 17;
private const int NET_LED = 27;
private const int FAULT_LED = 22;
public LED RunLed = new LED();
public LED IOLed = new LED();
public LED NetLed = new LED();
public LED FaultLed = new LED();
private GPIO GPIO = new GPIO();
private GpioController gpioController;
private ThreadPoolTimer timer;
public void InitStatusLED()
{
gpioController = GPIO.InitGPIO();
if (gpioController == null)
{
Debug.WriteLine("Failed to find GPIO Controller!");
//TODO proper error handling although this should never happen
}
else
{
//Setup the default parameters for the LEDS (ie flashing or non-flashing)
RunLed.Flashing = false;
IOLed.Flashing = false;
NetLed.Flashing = false;
FaultLed.Flashing = false;
RunLed.flashingPeriod = 0;
IOLed.flashingPeriod = 0;
NetLed.flashingPeriod = 0;
FaultLed.flashingPeriod = 0;
RunLed.Pin = GPIO.InitOutputPin(gpioController, RUN_LED);
IOLed.Pin = GPIO.InitOutputPin(gpioController, IO_LED);
NetLed.Pin = GPIO.InitOutputPin(gpioController, NET_LED);
FaultLed.Pin = GPIO.InitOutputPin(gpioController, FAULT_LED);
//Turn the LED's on to Start
RunLed.ReqPinValue = GpioPinValue.Low;
IOLed.ReqPinValue = GpioPinValue.Low;
NetLed.ReqPinValue = GpioPinValue.Low;
FaultLed.ReqPinValue = GpioPinValue.Low;
timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(500));
}
}
private void Timer_Tick(ThreadPoolTimer timer)
{
RunLed.UpdateLED();
IOLed.UpdateLED();
NetLed.UpdateLED();
FaultLed.UpdateLED();
}
}
我现在想使用下面的代码从另一个类中为 StatusLED 类中的这些实例设置字段“ReqPinValue”
private StatusLED statusLED = new StatusLED();
statusLED.RunLed.ReqPinValue = GpioPinValue.Low;
我收到以下错误:
错误:类型“....”包含外部可见字段“....”字段 只能通过结构暴露。
我可以看到它不喜欢下面的行被公开,我怎样才能在不公开的情况下从另一个类访问这个实例的参数?
public LED RunLed = new LED();
【问题讨论】:
标签: c# class parameters