Exemple #1
0
        public HidReport(int reportSize, HidDeviceData deviceData)
        {
            _status = deviceData.Status;

            Array.Resize(ref _data, reportSize - 1);

            if ((deviceData.Data != null))
            {

                if (deviceData.Data.Length > 0)
                {
                    _reportId = deviceData.Data[0];
                    Exists = true;

                    if (deviceData.Data.Length > 1)
                    {
                        var dataLength = reportSize - 1;
                        if (deviceData.Data.Length < reportSize - 1) dataLength = deviceData.Data.Length;
                        Array.Copy(deviceData.Data, 1, _data, 0, dataLength);
                    }
                }
                else Exists = false;
            }
            else Exists = false;
        }
Exemple #2
0
        public HidReport(int reportSize, HidDeviceData deviceData)
        {
            _status = deviceData.Status;

            Array.Resize(ref _data, reportSize - 1);

            if ((deviceData.Data != null))
            {
                if (deviceData.Data.Length > 0)
                {
                    _reportId = deviceData.Data[0];
                    Exists    = true;

                    if (deviceData.Data.Length > 1)
                    {
                        var dataLength = reportSize - 1;
                        if (deviceData.Data.Length < reportSize - 1)
                        {
                            dataLength = deviceData.Data.Length;
                        }
                        Array.Copy(deviceData.Data, 1, _data, 0, dataLength);
                    }
                }
                else
                {
                    Exists = false;
                }
            }
            else
            {
                Exists = false;
            }
        }
Exemple #3
0
        /// <summary>
        /// Reads an input report from the Control channel.  This method provides access to report data for devices that
        /// do not use the interrupt channel to communicate for specific usages.
        /// </summary>
        /// <param name="reportId">The report ID to read from the device</param>
        /// <returns>The HID report that is read.  The report will contain the success status of the read request</returns>
        ///
        public HidReport ReadReportSync(byte reportId)
        {
            byte[] cmdBuffer = new byte[Capabilities.InputReportByteLength];
            cmdBuffer[0] = reportId;
            bool          bSuccess   = NativeMethods.HidD_GetInputReport(ReadHandle, cmdBuffer, cmdBuffer.Length);
            HidDeviceData deviceData = new HidDeviceData(cmdBuffer, bSuccess ? HidDeviceData.ReadStatus.Success : HidDeviceData.ReadStatus.NoDataRead);

            return(new HidReport(Capabilities.InputReportByteLength, deviceData));
        }
Exemple #4
0
        protected static void EndRead(IAsyncResult ar)
        {
            HidAsyncState hidAsyncState = (HidAsyncState)ar.AsyncState;
            ReadDelegate  readDelegate  = (ReadDelegate)hidAsyncState.CallerDelegate;
            ReadCallback  readCallback  = (ReadCallback)hidAsyncState.CallbackDelegate;
            HidDeviceData data          = readDelegate.EndInvoke(ar);

            readCallback?.Invoke(data);
        }
        private void ReadCallback(HidDeviceData data)
        {
            Log.Debug("Got a ReadCallback of length {0}", data.Data.Length);

            _device.Read(ReadCallback);

            if (DataReceived == null)
                return;

            var bytes = data.Data;
            var dataString = _parser.CurrentParser.InterpretPacket(bytes);
            var args = new DataReceivedEventArgs(dataString);
            DataReceived(this, args);
        }
        private void ReadCallback(HidDeviceData data)
        {
            LogTo.Debug("Got a ReadCallback of length {0}", data.Data.Length);

            if(open)
                _device.Read(ReadCallback);
            else
            {
                LogTo.Trace("Ignored incoming data, device is closed");
                return;
            }

            if (DataReceived == null)
                return;

            var bytes = data.Data;
            var preparsed = _preparser.InterpretPacket(bytes);
            var dataString = _parser.CurrentParser.InterpretPacket(preparsed, bytes);
            var args = new DataReceivedEventArgs(dataString);
            DataReceived(this, args);
        }
        private void readButtonData(HidDeviceData InData)
        {
            timerHibernation.Enabled = false;

            if ((InData.Status == HidDeviceData.ReadStatus.Success) && (InData.Data[0] == 1))
            {
                SleepState = RemoteBtStates.Awake;
                timerFindRemote.Interval = 1500;

                DebugLog.write("Read button data: " + String.Join(",", InData.Data));

                if ((InData.Data[10] == 0) || (InData.Data[4] == 255)) // button released
                {
                    timerSleepButton.Stop();
                    if (ButtonReleased != null && isButtonDown) ButtonReleased(this, new ButtonData(lastButton));

                }
                else // button pressed
                {
                    byte[] bCode = { InData.Data[1], InData.Data[2], InData.Data[3], InData.Data[4] };

                    int i, j;

                    for (j = 0; j < 56; j++)
                    {
                        for (i = 0; i < 4; i++)
                        {
                            if (bCode[i] != buttonCodes[j][i]) break;
                        }

                        if (i == 4) break;
                    }

                    if (j != 56)
                    {
                        lastButton = (Button)j;
                        isButtonDown = true;

                        if (ButtonDown != null) ButtonDown(this, new ButtonData(lastButton));
                        if (lastButton == Button.Playstation) timerSleepButton.Start();
                        else timerSleepButton.Stop();
                    }
                }

                byte batteryReading = (byte)(InData.Data[11] * 20);

                if (batteryReading != _batteryLife) //Check battery life reading.
                {
                    _batteryLife = batteryReading;

                    if (BatteryLifeChanged != null) BatteryLifeChanged(this, new EventArgs());
                }

                if (_hibernationEnabled) timerHibernation.Start();

                hidRemote.Read(readButtonData); //Read next button pressed.

                return;
            }

            DebugLog.write("Read remote data: " + String.Join(",", InData.Data));

            if (Disconnected != null) Disconnected(this, new EventArgs());

            hidRemote.Dispose(); //Dispose of current remote.

            hidRemote = null;

            timerFindRemote.Enabled = true; //Try to reconnect.
            if (_hibernationEnabled) timerHibernation.Enabled = false;
        }
Exemple #8
0
        public ScaleWeightStatus GetWeight(out decimal? weight)
        {
            weight = null;

              // Byte 0 == Report ID?
              // Byte 1 == Scale Status (1 == Fault, 2 == Stable @ 0, 3 == In Motion, 4 == Stable, 5 == Under 0, 6 == Over Weight, 7 == Requires Calibration, 8 == Requires Re-Zeroing)
              // Byte 2 == Weight Unit
              // Byte 3 == Data Scaling (decimal placement) - signed byte is power of 10
              // Byte 4 == Weight LSB
              // Byte 5 == Weight MSB
              long startTime = millisecondsSinceEpoch();
              // The argument to scale.Read() is a timeout value for the USB
              // connection, not the actual Scale Status, so I'm leaving it at 250 as
              // it was originally and waiting 100ms between the 250ms tries. Hope that
              // makes sense.
              inData = scale.Read(250);
              while ((ScaleWeightStatus)inData.Data[1] != ScaleWeightStatus.Stable
             && millisecondsSinceEpoch() - startTime < this.timeoutLength)
              {
            inData = scale.Read(250);
            Thread.Sleep(this.retryTime);
              }
              if ((ScaleWeightStatus)inData.Data[1] != ScaleWeightStatus.Stable)
              {
            return (ScaleWeightStatus)inData.Data[1];
              }

              // Convert weight into pounds always
              weight = (Convert.ToDecimal(inData.Data[4]) +
              Convert.ToDecimal(inData.Data[5]) * 256) *
              Convert.ToDecimal(Math.Pow(10, (sbyte)inData.Data[3]));

              switch (Convert.ToInt16(inData.Data[2]))
              {
            case 3:  // Kilos
              weight = weight * (decimal?)2.2;
              break;
            case 11: // Ounces
              weight = weight * (decimal?)0.0625;
              break;
            case 12: // Pounds
              // already in pounds, do nothing
              break;
              }
              return ScaleWeightStatus.Stable;
        }
        public void TestNotifyUsbDeviceRespondsNak()
        {
            LightsManager lightsManager = null;
            try
            {
                // Mocks
                Mock<IHidDevice> mockDevice = this.mockFactory.CreateMock<IHidDevice>();
                byte[] mockDataBytes = Encoding.ASCII.GetBytes(UsbResponse.Nak);
                HidDeviceData mockData = new HidDeviceData(mockDataBytes, HidDeviceData.ReadStatus.Success);
                HidReport mockReport = new HidReport(1, mockData);

                // Expectations
                mockDevice.Expects.AtLeastOne.GetProperty(x => x.IsConnected).WillReturn(true);
                using (this.mockFactory.Ordered)
                {
                    mockDevice.Expects.One.GetProperty(x => x.IsOpen).WillReturn(false);
                    mockDevice.Expects.AtLeastOne.GetProperty(x => x.IsOpen).WillReturn(true);
                }

                mockDevice.Expects.AtLeastOne.Method(x => x.OpenDevice());
                mockDevice.Expects.AtLeastOne.Method(x => x.CreateReport()).WillReturn(mockReport);
                mockDevice.Expects.AtLeastOne.Method(x => x.WriteReport(null)).WithAnyArguments().WillReturn(true);
                mockDevice.Expects.AtLeastOne.Method(x => x.Read()).WillReturn(mockData);

                // Run
                LightsDeviceController lightsDeviceController = new LightsDeviceController(0, 0, 0, 0, 1000, UsbControlTransferType.Raw) {
                                                                                                                                                 Device = mockDevice.MockObject
                                                                                                                                         };
                lightsManager = new LightsManager(lightsDeviceController, UsbProtocolType.DasBlinkenlichten);
                BuildActiveRequest buildActiveRequest = new BuildActiveRequest(true);

                // Test
                Assert.That(lightsManager.NotifyLightsDevice(buildActiveRequest), NUnit.Framework.Is.EqualTo(LightsDeviceResult.Nak));
                this.mockFactory.VerifyAllExpectationsHaveBeenMet();
            }
            finally
            {
                if (lightsManager != null)
                {
                    lightsManager.Dispose();
                }
            }
        }
Exemple #10
0
        public void GetWeight(out decimal? weight, out bool? isStable)
        {
            weight = null;
              isStable = false;

              if (scale.IsConnected)
              {
            inData = scale.Read(250);
            // Byte 0 == Report ID?
            // Byte 1 == Scale Status (1 == Fault, 2 == Stable @ 0, 3 == In Motion, 4 == Stable, 5 == Under 0, 6 == Over Weight, 7 == Requires Calibration, 8 == Requires Re-Zeroing)
            // Byte 2 == Weight Unit
            // Byte 3 == Data Scaling (decimal placement)
            // Byte 4 == Weight LSB
            // Byte 5 == Weight MSB

            // FIXME: dividing by 100 probably wont work with
            // every scale, need to figure out what to do with
            // Byte 3
            weight = (Convert.ToDecimal(inData.Data[4]) +
            Convert.ToDecimal(inData.Data[5]) * 256) / 100;

            switch (Convert.ToInt16(inData.Data[2]))
            {
              case 3:  // Kilos
            weight = weight * (decimal?)2.2;
            break;
              case 11: // Ounces
            weight = weight * (decimal?)0.625;
            break;
              case 12: // Pounds
            // already in pounds, do nothing
            break;
            }
            isStable = inData.Data[1] == 0x4;
              }
        }
Exemple #11
0
        public void GetWeight(out decimal? weight, out bool? isStable)
        {
            weight = null;
            isStable = false;

            if (scale.IsConnected)
            {
                inData = scale.Read(250);

                weight = (Convert.ToDecimal(inData.Data[4]) +
                            Convert.ToDecimal(inData.Data[5]) * 256) *
                            Convert.ToDecimal(Math.Pow(10, (sbyte)inData.Data[3]));

                Convert.ToInt16(inData.Data[2]);

                isStable = inData.Data[1] == 0x4;
            }
        }
Exemple #12
0
    public void GetWeight (out decimal? weight, out bool? isStable)
    {
      weight = null;
      isStable = false;

      if (scale.IsConnected)
      {
        inData = scale.Read(250);
        // Byte 0 == Report ID?
        // Byte 1 == Scale Status (1 == Fault, 2 == Stable @ 0, 3 == In Motion, 4 == Stable, 5 == Under 0, 6 == Over Weight, 7 == Requires Calibration, 8 == Requires Re-Zeroing)
        // Byte 2 == Weight Unit
        // Byte 3 == Data Scaling (decimal placement) - signed byte is power of 10
        // Byte 4 == Weight LSB
        // Byte 5 == Weight MSB

        weight = (Convert.ToDecimal(inData.Data[4]) + 
            Convert.ToDecimal(inData.Data[5]) * 256) *
            Convert.ToDecimal(Math.Pow(10, (sbyte)inData.Data[3]));

        switch (Convert.ToInt16(inData.Data[2]))
        {
          case 3:  // Kilos
            weight = weight * (decimal?)2.2;
            break;
          case 11: // Ounces
            weight = weight * (decimal?)0.625;
            break;
          case 12: // Pounds
            // already in pounds, do nothing
            break;
        }
        isStable = inData.Data[1] == 0x4;
      }
    }
        private void OnRead(HidDeviceData data)
        {
            double interval;
            string time = "0";
            if (data.Data[1] != 0)
            {
                if (timer.IsRunning)
                {
                    interval = timer.ElapsedMilliseconds;
                    time = Convert.ToString(interval - prevtime);
                    prevtime = interval;
                }
                else
                {
                    timer.Start();
                    time = DateTime.Now.ToString("HH:mm:ss");
                }

                byte[] inbuffer = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                Array.Copy(data.Data, 1, inbuffer,0,64);
                if (setup == true)
                {
                    setupRoutine(inbuffer);
                }
                DecodeCan(inbuffer, time, "usbEvent");
                ReadSettings(inbuffer);
            }
            if (_device.IsOpen) _device.Read(OnRead);
        }