public Button ReadButton(byte button, int maxNumKeyCodes) { Button key = null; try { var sendBuffer = new byte[3]; sendBuffer[0] = SerialStartByte; sendBuffer[1] = ReadButtonCommand; sendBuffer[2] = button; var numBytes = SendCommand(sendBuffer, out byte[] replyBuffer); if (numBytes > 0) { if (replyBuffer[0] == SerialStartByte && replyBuffer[1] == ReadButtonCommand) { key = new Button { ButtonIndex = button }; byte bufferIndex = 3; for (int keycodeIndex = 0; keycodeIndex < maxNumKeyCodes; keycodeIndex++) { if (replyBuffer[bufferIndex] == 0) { //no further data break; } var keyStroke = new KeyStroke { KeyCodeType = (KeyCodeType)replyBuffer[bufferIndex], KeyCode = (UInt16)((replyBuffer[bufferIndex + 1] << 8) | replyBuffer[bufferIndex + 2]) }; bufferIndex += 3; key.KeyStrokes.Add(keyStroke); } } } } catch (Exception) { } return(key); }
public EncoderControl[] ReadEncoder(byte encoderIndex, byte maxNumKeystrokesPerButton) { var encoderControls = new List <EncoderControl>(); try { var sendBuffer = new byte[3]; sendBuffer[0] = SerialStartByte; sendBuffer[1] = ReadEncoderCommand; sendBuffer[2] = encoderIndex; var numBytes = SendCommand(sendBuffer, out byte[] replyBuffer); if (numBytes > 0) { if (replyBuffer[0] == SerialStartByte && replyBuffer[1] == ReadEncoderCommand) { //do the button var encoderControl = new EncoderControl { EncoderIndex = encoderIndex, EncoderControlType = EncoderControlType.Button }; byte bufferIndex = 3; for (int keystrokeIndex = 0; keystrokeIndex < maxNumKeystrokesPerButton; keystrokeIndex++) { if (replyBuffer[bufferIndex] != 0) { var keyStroke = new KeyStroke { KeyCodeType = (KeyCodeType)replyBuffer[bufferIndex], KeyCode = (UInt16)((replyBuffer[bufferIndex + 1] << 8) | replyBuffer[bufferIndex + 2]) }; encoderControl.KeyStrokes.Add(keyStroke); } bufferIndex += 3; } //add the button encoderControls.Add(encoderControl); //do the clockwise control encoderControl = new EncoderControl { EncoderIndex = encoderIndex, EncoderControlType = EncoderControlType.Clockwise }; for (int keystrokeIndex = 0; keystrokeIndex < maxNumKeystrokesPerButton; keystrokeIndex++) { if (replyBuffer[bufferIndex] != 0) { var keyStroke = new KeyStroke { KeyCodeType = (KeyCodeType)replyBuffer[bufferIndex], KeyCode = (UInt16)((replyBuffer[bufferIndex + 1] << 8) | replyBuffer[bufferIndex + 2]) }; encoderControl.KeyStrokes.Add(keyStroke); } bufferIndex += 3; } //add the clockwise control encoderControls.Add(encoderControl); //do the anticlockwise control encoderControl = new EncoderControl { EncoderIndex = encoderIndex, EncoderControlType = EncoderControlType.AntiClockwise }; for (int keystrokeIndex = 0; keystrokeIndex < maxNumKeystrokesPerButton; keystrokeIndex++) { if (replyBuffer[bufferIndex] != 0) { var keyStroke = new KeyStroke { KeyCodeType = (KeyCodeType)replyBuffer[bufferIndex], KeyCode = (UInt16)((replyBuffer[bufferIndex + 1] << 8) | replyBuffer[bufferIndex + 2]) }; encoderControl.KeyStrokes.Add(keyStroke); } bufferIndex += 3; } //add the anticlockwise control encoderControls.Add(encoderControl); } } } catch (Exception) { } return(encoderControls.ToArray()); }