-1

I am working on a program that communicates with a device connected through USB. When my program reads in data sent from the device (button presses, for example), it is sent as a byte array. I have key value pairs stored as a dictionary for the button press byte array and a string for the name of the button, which I display whenever it is pressed. This device also has sliders which sends byte arrays, but it has a full range, each slider is a different digit in the array

{ 3, 0, 0, 0, FF, 0, 0, 0 }

The range goes from 00 to FF in hex which I convert to decimal.

byte[] slider1rangeLower = { 3, 0, 0, 0, 00, 0, 0, 0 }
byte[] slider1rangeUpper = { 3, 0, 0, 0, 255, 0, 0, 0 }
byte[] slider2rangeLower = { 3, 0, 0, 00, 0, 0, 0, 0 }
byte[] slider2rangeUpper = { 3, 0, 0, 255, 0, 0, 0, 0 }

I need to know what slider is being used, so every value between 0-255 comes through as "slider1" or whatever the name might be. Is there any way to store a whole range of values for one object, as opposed to making 256 entries per slider?

jacksonSD
  • 677
  • 2
  • 13
  • 27
  • 1
    What happens if they press a button and move a slider at the same time? Will your buttons look-up fail? I think the dictionary / every-possible-combination is the incorrect approach, but without knowing more about the data format its difficult to recommend a better one. – Ron Beyer Apr 24 '15 at 20:47
  • @RonBeyer perhaps right about it being the wrong approach, but not sure of a better solution. This device is a composite usb device with multiple endpoints for each part of the device (buttons, sliders, encoders, LEDs, etc.) each have their own endpoint. the "3" at the first index of the byte array indicates which endpoint it is. Buttons are 1, sliders 3, etc. When they are pressed and moved at the same time, it just goes from one to the other as it cant output different values in the first index of the array at the same time. – jacksonSD Apr 24 '15 at 22:33

1 Answers1

0

C# is an object oriented language, and I would create an object for the device with all of its properties. Since I don't know much about the device, I had to guess a lot, but here is what I came up with and it should be easily extensible to what you need to use it for.

public class MyDevice
{
    private const int PayloadIdByte = 0;
    private const int PayloadSliderId = 3;
    private const int PayloadButtonId = 4;
    private const int Button1Byte = 1;
    private const int Button2Byte = 2;
    private const int Button3Byte = 3;
    private const int Slider1Byte = 4;
    private const int Slider2Byte = 3;
    private const int PayloadSize = 8;

    public bool Button1 { get; set; }
    public bool Button2 { get; set; }
    public bool Button3 { get; set; }
    public byte Slider1 { get; set; }
    public byte Slider2 { get; set; }

    public MyDevice()
    {

    }

    public void Update(byte[] payload)
    {
        if (payload.Length != PayloadSize)
            throw new ArgumentException("payload");

        if (payload[PayloadIdByte] == PayloadSliderId)
        {
            Slider1 = payload[Slider1Byte];
            Slider2 = payload[Slider2Byte];
        }

        if (payload[PayloadIdByte] == PayloadButtonId)
        {
            Button1 = payload[Button1Byte] > 0;
            Button2 = payload[Button2Byte] > 0;
            Button3 = payload[Button3Byte] > 0;
        }
    }
}

You can add events when the object is updated, additional features, etc. When you read a new payload from the USB, just pass it to the Update function and let the function parse the payload into the object.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37