我有一个项目,它在我们机器人的两个轮子上都有编码器(试图计算多少个刻度,转动轮子) 在Gpios中有一个GpioName.ValueChanged事件,当某个gpio将其“读取”值从“高”变为“低”或“从”到“低”时,它会调用一个函数。我的问题是,如果我创建2个事件来电者,我不确定我的代码是如何工作的。比如..
TicksRightPin.ValueChanged + = CountRight; TicksLeftPin.ValueChanged + = CountLeft;
但是当我评论(不使用代码)时,其中任何一个,未注释的另一行都有效。例如:
// TicksRightPin.ValueChanged + = CountRight; TicksLeftPin.ValueChanged + = CountLeft;
或
TicksRightPin.ValueChanged + = CountRight; //TicksLeftPin.ValueChanged + = CountLeft;
我希望他们两个都工作.. 这是我的代码。我希望我没有任何困惑......
public MainPage()
{
this.InitializeComponent();
ImageMap.Source = bp;
DisableButtons();
var gpio = GpioController.GetDefault();
if (gpio == null)
{
CheckStatusText.Text = "There is no GPIO connected";
}
else
{
//Initializing all pins!!
InitGpios();
StartStop_Bool = false;
RunBrake_Bool = false;
CwCcw_Bool = true;
}
TicksRightPin.DebounceTimeout = TimeSpan.FromMilliseconds(10);
TicksLeftPin.DebounceTimeout = TimeSpan.FromMilliseconds(15);
TicksRightPin.ValueChanged += CountRight;
TicksLeftPin.ValueChanged += CountLeft;
}
private async void CountLeft(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (TicksLeftPin.Read() == GpioPinValue.High && Debouncing_Left == 0)
{
LeftMotor_TickCounter++;
Debouncing_Left = 1;
if (LeftMotor_TickCounter > 1 && CwCcwPinLeft.Read() == GpioPinValue.Low)
{
finalLeftCount++;
LeftMotor_TickCounter = 0;
}
else if (LeftMotor_TickCounter > 1 && CwCcwPinLeft.Read() == GpioPinValue.High)
{
finalLeftCount--;
LeftMotor_TickCounter = 0;
}
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
CounterLabelLeft.Text = Convert.ToString(finalLeftCount);
});
}
if (TicksLeftPin.Read() == GpioPinValue.Low && Debouncing_Left == 1)
{
Debouncing_Left = 0;
}
}
private async void CountRight(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (TicksRightPin.Read() == GpioPinValue.High && Debouncing_Right == 0)
{
RightMotor_TickCounter++;
Debouncing_Right = 1;
if (RightMotor_TickCounter > 0 && CwCcwPinRight.Read() == GpioPinValue.High)
{
finalRightCount++;
RightMotor_TickCounter = 0;
}
if (RightMotor_TickCounter > 0 && CwCcwPinRight.Read() == GpioPinValue.Low)
{
finalRightCount--;
RightMotor_TickCounter = 0;
}
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
CounterLabelRight.Text = Convert.ToString(finalRightCount);
});
}
if (TicksRightPin.Read() == GpioPinValue.Low && Debouncing_Right == 1)
{
Debouncing_Right = 0;
}
}