private void PacketReceived(RF24Radio radio, int pipe, byte[] packet) { if (_isClosing) { return; } Dispatcher.Invoke(() => { analogStick.XDirection = ((double)(packet[0] - 127)) / 128; analogStick.YDirection = ((double)(packet[1] - 128)) / 128; bool upperButton = packet[2] != 0; bool lowerButton = packet[3] != 0; analogStick.Foreground = upperButton || lowerButton ? _stickPressedColor : _stickReleasedColor; }); }
public void OnDeviceConnected(WirekiteDevice device) { _device = device; _device.ResetConfiguration(); if (hasBuiltInLED) { _builtinLED = device.ConfigureDigitalOutputPin(13, DigitalOutputPinAttributes.Default); _ledTimer = new Timer(Blink, null, 300, 500); } if (useLEDBoard) { _redLED = device.ConfigureDigitalOutputPin(16, DigitalOutputPinAttributes.HighCurrent); _orangeLED = device.ConfigureDigitalOutputPin(17, DigitalOutputPinAttributes.HighCurrent); _greenLED = device.ConfigureDigitalOutputPin(21, DigitalOutputPinAttributes.HighCurrent); _switchPort = device.ConfigureDigitalInputPin(12, DigitalInputPinAttributes.Pullup | DigitalInputPinAttributes.TriggerRaising | DigitalInputPinAttributes.TriggerFalling, (port, value) => { Dispatcher.Invoke(new Action(() => { SetSwitchDisplay(value); })); }); SetSwitchDisplay(device.ReadDigitalPin(_switchPort)); _dutyCyclePin = device.ConfigureAnalogInputPin(AnalogPin.A4, 127, (port, value) => { device.WritePWMPin(_pwmOutputPin, value); Dispatcher.Invoke(new Action(() => { string text = String.Format("{0:##0.0} %", value * 100); dutyCycleValueLabel.Content = text; })); }); _frequencyPin = device.ConfigureAnalogInputPin(AnalogPin.A1, 149, (port, value) => { if (Math.Abs(value - _prevFrequencyValue) > 0.01) { _prevFrequencyValue = value; int frequency = (int)((Math.Exp(Math.Exp(value)) - Math.Exp(1)) * 900 + 10); _device.ConfigurePWMTimer(0, frequency, PWMTimerAttributes.Default); Dispatcher.Invoke(new Action(() => { string text = String.Format("{0} Hz", frequency); frequencyValueLabel.Content = text; })); } }); _voltageXPin = device.ConfigureAnalogInputPin(AnalogPin.A8, 137, (port, value) => { Dispatcher.Invoke(new Action(() => { analogStick.XDirection = 1.0 - value * 2; })); }); _voltageYPin = device.ConfigureAnalogInputPin(AnalogPin.A9, 139, (port, value) => { Dispatcher.Invoke(new Action(() => { analogStick.YDirection = 1.0 - value * 2; })); }); _stickSwitchPin = device.ConfigureDigitalInputPin(20, DigitalInputPinAttributes.TriggerRaising | DigitalInputPinAttributes.TriggerFalling | DigitalInputPinAttributes.Pullup, (port, value) => { Dispatcher.Invoke(new Action(() => { analogStick.Foreground = value ? _stickReleasedColor : _stickPressedColor; })); }); analogStick.Foreground = device.ReadDigitalPin(_stickSwitchPin) ? _stickReleasedColor : _stickPressedColor; _pwmOutputPin = device.ConfigurePWMOutputPin(10); } if (useI2CBoard) { _i2cPort = _device.ConfigureI2CMaster(I2CPins.I2CPinsSCL16_SDA17, 400000); _gyro = new GyroMPU6050(_device, _i2cPort); _gyro.StartCalibration(); _gyroTimer = new Timer(ReadGyro, null, 400, 400); _ammeter = new Ammeter(_device, _i2cPort); _ammeterTimer = new Timer(ReadAmmeter, null, 300, 350); _display = new OLEDDisplay(_device, _i2cPort) { DisplayOffset = 2 }; StartOLEDShow(); } if (useSpiTFTBoard) { if (_device.GetBoardInfo(BoardInfo.Board) == WirekiteDevice.BoardTeensyLC) { _spiPort = _device.ConfigureSPIMaster(14, 11, WirekiteDevice.InvalidPortId, 16000000, SPIAttributes.Default); } else { _device.ConfigureFlowControl(memorySize: 20000, maxOutstandingRequests: 100); _spiPort = _device.ConfigureSPIMaster(14, 11, WirekiteDevice.InvalidPortId, 18000000, SPIAttributes.Default); } _colorTFT = new ColorTFT(_device, _spiPort, 6, 4, 5); StartColorShow(); } if (useSpiRFBoard) { if (_device.GetBoardInfo(BoardInfo.Board) == WirekiteDevice.BoardTeensy3_2) { _device.ConfigureFlowControl(memorySize: 20000, maxOutstandingRequests: 100); } _spiPort = _device.ConfigureSPIMaster(14, 11, 12, 10000000, SPIAttributes.Default); _radio = new RF24Radio(_device, _spiPort, 18, 19); _radio.InitModule(); _radio.RFChannel = 0x52; _radio.AutoAck = false; _radio.OutputPower = RF24Radio.RFOutputPower.Low; _radio.ConfigureIRQPin(10, 10, PacketReceived); _radio.OpenTransmitPipe(0x389f30cc1b); _radio.OpenReceivePipe(1, 0x38a8bb7201); _radio.StartListening(); _radioThread = new Thread(SendTime); _radioThread.Start(); } }