示例#1
0
        public void Send(byte protocol, uint data)
        {

            byte[] rawData = new byte[128];
            IREncoding encoder = new IREncoding();

            int rawLen = encoder.IREncode(protocol, data, rawData, 128);

            if (rawLen > 0)
            {
                SendRaw(rawData);
            }
        }
示例#2
0
        public override void ProcessEvent(byte code, ushort eventData)
        {
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                int i = 0;
                while (i < eventData)
                {
                    byte[] recvd = transfer(kFUNCTION_RECEIVE, new byte[] { (byte)i, (byte)16 }, 16);
                    if (recvd != null)
                    {
                        ms.Write(recvd, 0, recvd.Length);
                        i += recvd.Length;
                    }
                }
                transfer(kFUNCTION_CLEAR_READ, null, 0);
                data = ms.ToArray();
            }
            ushort[] expandedData = new ushort[data.Length];
            ushort expandedLen = 0;
            for (int i = 0; i < data.Length; i++)
                {
                    ushort value = 0;
                    if ( data[i] == 0 && ( i+2 < data.Length) ) {
                    value = (ushort)( data[i + 1] + (data[i + 2] << 8));
                    i += 2;
                }
                else
                {
                    value = data[i];
                }

                // Due to sensor lag, each mark tends to be approx 1 tick long and
                // each space tends to be 1 tick short. Adjust for that here.
                /*
                        if (expandedLen%2) {
                            value--;
                        } else {
                            value++;
                        }
                */

                expandedData[expandedLen++] = value;

            }
            // If the length is less than 2, it's a surpious signal that should be ignored.
            if (expandedLen <= 2)
            {
                return;
            }
            {
                sbyte protocol = -1;
                uint value = 0;
                IREncoding encoder = new IREncoding();
                encoder.IRDecode(expandedData, expandedLen, ref protocol, ref value);

                if (dataReceivedCallback != null)
                {
                    dataReceivedCallback(this,protocol, (int)value, expandedData, expandedLen);
                }
            }
        }