public BigRedButtonState? QueryState()
        {
            var statusQuery = new HidReport(1) {Data = StatusReport};

            var isQueryWritten = device.WriteReport(statusQuery);

            if (!isQueryWritten)
            {
                return null;
            }

            var readReport = device.ReadReport(100);

            var readData = readReport.Data;

            if (readReport.ReadStatus != HidDeviceData.ReadStatus.Success)
            {
                return null;
            }

            var statusByte = readData[0];

            Debug.WriteLine(Convert.ToString(readData[0],2));

            var buttonIsPressed = (statusByte & (1 << 0)) == 0;
            var lidIsOpen = (statusByte & (1 << 1)) != 0;

            return new BigRedButtonState(buttonIsPressed, lidIsOpen);
        }
예제 #2
0
        public void WriteReport(HidReport report, WriteCallback callback)
        {
            WriteReportDelegate writeReportDelegate = WriteReport;
            HidAsyncState       @object             = new HidAsyncState(writeReportDelegate, callback);

            writeReportDelegate.BeginInvoke(report, EndWriteReport, @object);
        }
예제 #3
0
        private static void OnReport(HidReport report)
        {
            if (_attached == false) { return; }

            if (report.Data.Length >= 4)
            {
                var message = MessageFactory.CreateMessage(_currentProductId, report.Data);
                if (message.Depress) { KeyDepressed(); }
                else if (message.MultiplePressed == false)
                {
                    if (message.UpPressed) { KeyPressed(1); }
                    if (message.DownPressed) { KeyPressed(2); }
                    if (message.LeftPressed) { KeyPressed(3); }
                    if (message.RightPressed) { KeyPressed(4); }
                    if (message.Button1Pressed) { KeyPressed(5); }
                    if (message.Button2Pressed) { KeyPressed(6); }
                    if (message.Button3Pressed) { KeyPressed(7); }
                    if (message.Button4Pressed) { KeyPressed(8); }
                    if (message.Button5Pressed) { KeyPressed(9); }
                    if (message.Button6Pressed) { KeyPressed(10); }
                    if (message.Button7Pressed) { KeyPressed(11); }
                    if (message.Button8Pressed) { KeyPressed(12); }
                    if (message.Button9Pressed) { KeyPressed(13); }
                    if (message.Button10Pressed) { KeyPressed(14); }
                }
            }

            _device.ReadReport(OnReport);
        }
        private void OnReport(HidReport report)
        {
            if (_attached == false) { return; }

            if (report.Data.Length >= 4)
            {
                var message = MessageFactory.CreateMessage(_currentProductId, report.Data);
                OnButtonEvent.Invoke(message);
                //if (message.Depress) { KeyDepressed(); }
                //else
                //{
                //    OnButtonEvent.Invoke(message);

                //    //if (message.UpPressed) { KeyPressed("Up"); }
                //    //if (message.DownPressed) { KeyPressed("Down"); }
                //    //if (message.LeftPressed) { KeyPressed("Left"); }
                //    //if (message.RightPressed) { KeyPressed("Right"); }
                //    //if (message.Button1Pressed) { KeyPressed("1"); }
                //    //if (message.Button2Pressed) { KeyPressed("2"); }
                //    //if (message.ButtonAPressed) { KeyPressed("A"); }
                //    //if (message.ButtonBPressed) { KeyPressed("B"); }
                //    //if (message.ButtonPlusPressed) { KeyPressed("Plus"); }
                //    //if (message.ButtonMinusPressed) { KeyPressed("Minus"); }
                //    //if (message.ButtonRedPressed) { KeyPressed("Red"); }
                //    //if (message.ButtonYellowPressed) { KeyPressed("Yellow"); }
                //    //if (message.ButtonBluePressed) { KeyPressed("Blue"); }
                //    //if (message.ButtonGreenPressed) { KeyPressed("Green"); }
                //    //if (message.ButtonPedalPressed) { KeyPressed("Pedal"); }
                //    //_press = true;
                //}
            }

            _device.ReadReport(OnReport);
        }
예제 #5
0
        public void WriteReport(HidReport report, WriteCallback callback, int timeout)
        {
            var writeReportDelegate = new WriteReportDelegate(WriteReport);
            var asyncState          = new HidAsyncState(writeReportDelegate, callback);

            writeReportDelegate.BeginInvoke(report, timeout, EndWriteReport, asyncState);
        }
예제 #6
0
        protected static void EndReadReport(IAsyncResult ar)
        {
            HidAsyncState      hidAsyncState      = (HidAsyncState)ar.AsyncState;
            ReadReportDelegate readReportDelegate = (ReadReportDelegate)hidAsyncState.CallerDelegate;
            ReadReportCallback readReportCallback = (ReadReportCallback)hidAsyncState.CallbackDelegate;
            HidReport          report             = readReportDelegate.EndInvoke(ar);

            readReportCallback?.Invoke(report);
        }
예제 #7
0
 public void OnReport(HidReport report)
 {
     if (report.ReportId == 4) {
         WriteLog("Report! " + report.Data[1] + ", " + report.Data[2] + ",  " + report.Data[5] + ", " + report.Data[10]);
     }
     if (_running) {
         _device.ReadReport(OnReport);
     }
 }
예제 #8
0
        private static void OnReport(HidReport report)
        {
            if (!_device.IsConnected) { return; }

            var cardData = new Data(report.Data);

            Console.WriteLine(!cardData.Error ? Encoding.ASCII.GetString(cardData.CardData) : cardData.ErrorMessage);

            _device.ReadReport(OnReport);
        }
예제 #9
0
        public async Task <bool> WriteReportAsync(HidReport report, int timeout = 0)
        {
            var writeReportDelegate = new WriteReportDelegate(WriteReport);

#if NET20 || NET35
            return(await Task <bool> .Factory.StartNew(() => writeReportDelegate.Invoke(report, timeout)));
#else
            return(await Task <bool> .Factory.FromAsync(writeReportDelegate.BeginInvoke, writeReportDelegate.EndInvoke, report, timeout, null));
#endif
        }
 private void ReadReport(HidReport report)
 {
     if (onDataReceived != null && report.ReadStatus == HidDeviceData.ReadStatus.Success)
     {
         int length = report.Data[0];
         byte[] data = new byte[length];
         Array.Copy(report.Data, 1, data, 0, length);
         onDataReceived(data);
     }
     dev.ReadReport(ReadReport);
 }
예제 #11
0
 /// <summary>
 /// Handle data transfers on the control channel.  This method places data on the control channel for devices
 /// that do not support the interupt transfers
 /// </summary>
 /// <param name="report">The outbound HID report</param>
 /// <returns>The result of the tranfer request: true if successful otherwise false</returns>
 ///
 public bool WriteReportSync(HidReport report)
 {
     if (null != report)
     {
         byte[] buffer = report.GetBytes();
         return(NativeMethods.HidD_SetOutputReport(WriteHandle, buffer, buffer.Length));
     }
     else
     {
         throw new ArgumentException("The output report is null, it must be allocated before you call this method", "report");
     }
 }
예제 #12
0
 public void ChronOverride(byte deviceId, byte deviceState)
 {
     if (connectedToDriver)
     {
         byte[] data = new byte[63];
         data[0] = (byte)0x00;
         data[1] = (byte)12; // mao ni ang 0 sa PIC
         data[2] = deviceId; // 1
         data[3] = deviceState; // 2
         HidReport report = new HidReport(63, new HidDeviceData(data, HidDeviceData.ReadStatus.Success)); //7
         device.WriteReport(report);
         //ReceiveReport(report);
     }
 }
        private void OnReportReceived(HidReport report)
        {
            if (Device.IsConnected == false)
            {
                return;
            }

            if (report.Data.Length == JoystickDataParser.DataSize)
            {
                ReportReceived(JoystickDataParser.Parse(report.Data));
            }

            Device.ReadReport(OnReportReceived);
        }
예제 #14
0
 public void WriteFeatureReport(HidReport report)
 {
     if (dev.OpenDeviceForWrite())
     {
         if (report.Length == GetReportLength(HidReportType.Feature))
         {
             if (!NativeApi.HidD_SetFeature(dev.Handle, report.Bytes, report.Length))
             {
                 throw new Exception("SetFeatureReport: " + Marshal.GetLastWin32Error().ToString());
             }
         }
     }
     else
     {
         throw new Exception("Unable to open device for Write");
     }
 }
        public bool write(byte[] data)
        {
            int count = 0; int size = data.Length;
            if (!dev.IsConnected)
                return false;

            while (size > 0)
            {
                int byteToWrite = Math.Min(maxSize - 2, size); 
                HidReport rp = new HidReport(byteToWrite + 2);
                rp.ReportId = 2;
                Array.Copy(data, count, rp.Data, 1, byteToWrite);
                rp.Data[0] = (byte)byteToWrite;
                size -= byteToWrite;
                count += byteToWrite;
                bool rv;
                rv = dev.WriteReport(rp);
            }
            
            return true;
        }
예제 #16
0
        public Report(HidReport hidReport)
        {
            _status = hidReport.ReadStatus;
            ReportId = hidReport.ReportId;
            Exists = hidReport.Exists;

            if (hidReport.Data.Length > 0) Length = hidReport.Data[0];
            if (hidReport.Data.Length > 1) AimSymbologyId0 = hidReport.Data[1];
            if (hidReport.Data.Length > 2) AimSymbologyId1 = hidReport.Data[2];
            if (hidReport.Data.Length > 3) AimSymbologyId2 = hidReport.Data[3];

            if (hidReport.Data.Length > Length + 3)
            {
                Array.Resize(ref _data, Length);
                Array.Copy(hidReport.Data, 4, _data, 0, Length);
            }

            if (hidReport.Data.Length > 60) HhpSymbologyId = hidReport.Data[60];
            if (hidReport.Data.Length > 61) Reserved = hidReport.Data[61];
            if (hidReport.Data.Length > 62) MoreData = hidReport.Data[62] == 1;
        }
예제 #17
0
        public void GetCronData()
        {
            if (connectedToDriver)
            {
                byte[] data = new byte[2];
                data[0] = 0x00;
                data[1] = 0x01;
                HidReport report = new HidReport(2, new HidDeviceData(data, HidDeviceData.ReadStatus.Success));
                device.WriteReport(report);
                ChronState state = new ChronState();
                if (report.Data.Length > 2)
                {
                    if (report.Data[0] == 0 && report.Data[1] == 2)
                    {
                        state.Diagnostics = "Received Chron Entry " + cron_entries_received + ": ";
                        state.Read_MoreToParse = true;
                        cron_entries_received++;
                        //this.GetCronDataContinuation();
                    }

                    if (report.Data[0] == 0 && report.Data[1] == 3)
                    {
                        state.Diagnostics = "Chron Entry Comms Ended: ";
                        cron_entries_received = 0;
                    }

                    if (debugPrintRawMessages)
                    {
                        for (int i = 0; i < report.Data.Length; i++)
                        {
                            state.Diagnostics += report.Data[i] + ", ";
                        }

                        System.Diagnostics.Debug.WriteLine(state.Diagnostics);
                    }

                    this.OnEntryReceived(state);
                }
            }
        }
예제 #18
0
        private void OnReport(HidReport report)
        {
            if (!_isAttached)
             {
            return;
             }

             var data = report.Data;
             PrintOutputLine(string.Format("recv {0} bytes:", data.Length));

             string outputString = string.Empty;
             for (int i = 0; i < data.Length; i++)
             {
            outputString += string.Format("{0:X2} ", data[i]);
            if (i % 16 == 15 && i < data.Length)
            {
               PrintOutputLine(outputString);
               outputString = string.Empty;
            }
             }
             PrintOutputLine("\n");

             _device.ReadReport(OnReport);
        }
예제 #19
0
 public void WriteOutPipeReport(HidReport report)
 {
     if (dev.OpenDeviceForWrite())
     {
         if (report.Length == GetReportLength(HidReportType.Output))
         {
             using (FileStream stream = new FileStream(dev.Handle, FileAccess.Write))
             {
                 try
                 {
                     stream.Write(report.Bytes, 0, report.Bytes.Length);
                 }
                 catch (Exception e)
                 {
                     throw new Exception("Write to outpipe failed: " + e.Message);
                 }
             }
         }
     }
     else
     {
         throw new Exception("Unable to open device for Write");
     }
 }
예제 #20
0
        private void OnReport(HidReport report)
        {
            ChronState state = new ChronState();

            if (attached == false) { return; }

            if (report.Data.Length > 2)
            {
                if (report.Data[0] == 0 && report.Data[1] == 0)
                {
                    state.Diagnostics = "Received Time and Data Reset " + cron_entries_received + ": ";
                    state.Read_MoreToParse = true;
                    cron_entries_received++;
                }

                if (report.Data[0] == 0 && report.Data[1] == 2)
                {
                    state.Diagnostics = "Received Chron Entry " + cron_entries_received + ": ";
                    state.Read_MoreToParse = true;
                    cron_entries_received++;
                    this.GetCronDataContinuation();
                }

                if (report.Data[0] == 0 && report.Data[1] == 3)
                {
                    state.Diagnostics = "Chron Entry Comms Ended: ";
                    cron_entries_received = 0;
                }

                if (report.Data[0] == 0 && report.Data[1] == 6)
                {
                    state.Diagnostics = "Written Chron Entry " + cron_entries_written + ": ";
                    state.Read_MoreToParse = true;
                    cron_entries_written++;
                    this.WriteCronDataContinuation();
                }

                if (report.Data[0] == 0 && report.Data[1] == 5)
                {
                    state.Diagnostics = "Chron Write Comms Ended: ";
                    cron_entries_written = 0;
                }

                // Override Control Reply
                if (report.Data[0] == 0 && report.Data[1] == 13)
                {
                    state.Diagnostics = "Override Control Response: ";
                }

                if (debugPrintRawMessages)
                {
                    for (int i = 0; i < report.Data.Length; i++)
                    {
                        state.Diagnostics += report.Data[i] + ", ";
                    }

                    byte[] data = new byte[9];
                    //for (int i = 4; i < 11; i++)
                    //{
                    //	data[i] = report.Data[i];
                    //}

                    state.SetData(data);

                    System.Diagnostics.Debug.WriteLine(state.Diagnostics);
                }

                this.OnEntryReceived(state);
            }

            device.ReadReport(OnReport);
        }
예제 #21
0
 private void ReadProcess(HidReport report)
 {
     BeginInvoke(new ReadHandlerDelegate(ReadHandler), new object[] { report });
 }
예제 #22
0
        private void ReceiveReport(HidReport report)
        {
            ChronState state = new ChronState();
            if (report.Data.Length > 2)
            {
                if (report.Data[0] == 0 && report.Data[1] == 2)
                {
                    state.Diagnostics = "Received Chron Entry " + cron_entries_received + ": ";
                    state.Read_MoreToParse = true;
                    cron_entries_received++;
                    //this.GetCronDataContinuation();
                }

                if (report.Data[0] == 0 && report.Data[1] == 3)
                {
                    state.Diagnostics = "Chron Entry Comms Ended: ";
                    cron_entries_received = 0;
                }

                if (debugPrintRawMessages)
                {
                    for (int i = 0; i < report.Data.Length; i++)
                    {
                        state.Diagnostics += report.Data[i] + ", ";
                    }

                    System.Diagnostics.Debug.WriteLine(state.Diagnostics);
                }

                this.OnEntryReceived(state);
            }
        }
예제 #23
0
        private static HidReport PrepareReport(int addr, byte[] data, BoardDefinition BoardDef)
        {
            var report = new HidReport(BoardDef.BlockSize + BoardDef.DataOffset + 1);
            report.ReportId = 0;

            // Copy address bytes into report. Use function stored in board definition
            BoardDef.AddrCopy(report.Data, BitConverter.GetBytes(addr));

            // Copy datablock into report
            data.CopyTo(report.Data, BoardDef.DataOffset);

            return report;
        }
예제 #24
0
 private void ReadHandler(HidReport report)
 {
     Output.Clear();
     Output.AppendText(System.Text.Encoding.ASCII.GetString(report.Data));
 }
		public override Boolean GetInfoBlock (byte id, out byte[] data)
		{
			if (id == 2 || id == 3) {
				if (connectedToDriver && device.ReadFeatureData (id, out data))
				{
                    HidReport report = new HidReport(33, new HidDeviceData(data, HidDeviceData.ReadStatus.Success));
                    data = report.Data;
					return true;
				}
				else
				{
					data = new byte[0];
					return false;
				}
			} else {
				throw new Exception("Invalid info block id");
			}
		}
예제 #26
0
        private void sendButton_Click(object sender, EventArgs e)
        {
            const int size = 64;
             var data = new byte[size];
             byte result;
             if (byte.TryParse(valueTextBox.Text, out result))
             {
            data[1] = result;
             }
             else
             {
            PrintOutputLine("Invalid value, expecting byte value");
            return;
             }

             var report = new HidReport(64, new HidDeviceData(data, HidDeviceData.ReadStatus.Success));
             var status = _device.WriteReport(report);
             return;
        }
예제 #27
0
 public void WriteReport(HidReport report, WriteCallback callback)
 {
     var writeReportDelegate = new WriteReportDelegate(WriteReport);
     var asyncState = new HidAsyncState(writeReportDelegate, callback);
     writeReportDelegate.BeginInvoke(report, EndWriteReport, asyncState);
 }
예제 #28
0
 public bool WriteReport(HidReport report, int timeout)
 {
     return Write(report.GetBytes(), timeout);
 }
예제 #29
0
        private void ReadReportCallback(HidReport report)
        {
            Log.Debug("Got a ReadReportCallback of length {0}", report.Data.Length);

            _device.ReadReport(ReadReportCallback);

            if (DataReceived == null)
                return;

            var data = report.GetBytes();
            var dataString = _parser.CurrentParser.InterpretPacket(data);
            var args = new DataReceivedEventArgs(dataString);
            DataReceived(this, args);
        }
예제 #30
0
	    private void ReadReport(HidReport report)
	    {
		    var scannerReport = new Report(report);
            var data = new byte[] { };
		    var currentPosition = 0;
		    var readRequired = false;

		    if (scannerReport.Length > 0 & scannerReport.ReadStatus == HidDeviceData.ReadStatus.Success) 
            {
			    do 
                {
				    if (scannerReport.MoreData && readRequired) scannerReport = new Report(_scanner.ReadReport());
				    else readRequired = true;

                    if (!scannerReport.Exists) continue;

                    Array.Resize(ref data, data.GetUpperBound(0) + scannerReport.Data.Length + 1);
                    Array.Copy(scannerReport.Data, 0, data, currentPosition, scannerReport.Data.Length);
                    currentPosition += scannerReport.Data.Length;

                } while (scannerReport.MoreData && scannerReport.Exists);

			    if (IsListening && data.Length > 0 && DataRecieved != null) DataRecieved(data);
		    }

		    if (scannerReport.ReadStatus != HidDeviceData.ReadStatus.NotConnected) _scanner.ReadReport(ReadReport);
            else _isReading = 0;
	    }
        private void OnReport(HidReport report)
        {
            if (attached == false) { return; }

				/*
            if (report.Data.Length >= 6)
            {
                PowerMateState state = ParseState(report.Data);
                if (!state.IsValid)
                {
                    System.Diagnostics.Debug.WriteLine("Invalid PowerMate state");
                }
                else
                {
                    GenerateEvents(state);

                    if (debugPrintRawMessages)
                    {
                        System.Diagnostics.Debug.Write("PowerMate raw data: ");
                        for (int i = 0; i < report.Data.Length; i++)
                        {
                            System.Diagnostics.Debug.Write(String.Format("{0:000} ", report.Data[i]));
                        }
                        System.Diagnostics.Debug.WriteLine("");
                    }
                }
            }
*/

            //device.ReadReport(OnReport);
        }
예제 #32
0
 public bool WriteReport(HidReport report, int timeout)
 {
     return(Write(report.GetBytes(), timeout));
 }
        public override void SetLedColor(byte r, byte g, byte b)
        {
            if (connectedToDriver)
            {
                byte [] data = new byte[4];
                data[0] = 1;
                data[1] = r;
                data[2] = g;
                data[3] = b;

                HidReport report = new HidReport(4, new HidDeviceData(data, HidDeviceData.ReadStatus.Success));
                device.WriteFeatureData(data);
            }
        }
예제 #34
0
 public bool WriteReport(HidReport report)
 {
     return WriteReport(report, 0);
 }
예제 #35
0
        private void ReadHandler(HidReport report)
        {
            Output.Clear();
            Output.AppendText(String.Join(" ", report.Data.Select(d => d.ToString("X2"))));

            _deviceList[Devices.SelectedIndex].ReadReport(ReadProcess);
        }
예제 #36
0
 public bool WriteReport(HidReport report)
 {
     return(WriteReport(report, 0));
 }
		protected override void SetInfoBlock (byte id, byte[] data)
		{
			if (id == 2 || id == 3) {
				if (data.Length > 32)
				{
		            Array.Resize(ref data, 32);
				}
				else if (data.Length < 32)
				{
					int size = data.Length;

		            Array.Resize(ref data, 32);

					//pad with zeros
					for (int i = size; i < 32; i++)
					{
						data[i] = 0;
					}
				}

                Array.Resize(ref data, 33);


                for (int i = 32; i >0; i--)
                {
                    data[i] = data[i-1];
				}

                data[0] = id;

                HidReport report = new HidReport(33, new HidDeviceData(data, HidDeviceData.ReadStatus.Success));
                device.WriteFeatureData(data);
			} else {
				throw new Exception("Invalid info block id");
			}
		}
예제 #38
0
 public void WriteReport(HidReport report, WriteCallback callback)
 {
     WriteReport(report, callback, 0);
 }
예제 #39
0
 public void WriteReport(HidReport report, WriteCallback callback)
 {
     WriteReport(report, callback, 0);
 }
예제 #40
0
        public async Task <bool> WriteReportAsync(HidReport report, int timeout = 0)
        {
            var writeReportDelegate = new WriteReportDelegate(WriteReport);

            return(await Task <bool> .Factory.FromAsync(writeReportDelegate.BeginInvoke, writeReportDelegate.EndInvoke, report, timeout, null));
        }
예제 #41
0
 public async Task<bool> WriteReportAsync(HidReport report, int timeout = 0)
 {
     var writeReportDelegate = new WriteReportDelegate(WriteReport);
     return await Task<bool>.Factory.FromAsync(writeReportDelegate.BeginInvoke, writeReportDelegate.EndInvoke, report, timeout, null);
 }