Exemple #1
0
        public bool decodePulseModulation(PulseModulationEncoding encoding, ref uint32_t value)
        {
            _currentIndex = 0;
            value = 0;

            // Skip leading space
            getNextInterval();

            // Match the header
            if (encoding.headerMark != 0) {
                if (!matchMark(getNextInterval(), encoding.headerMark)) {
                    return false;
                }

                if (!matchSpace(getNextInterval(), encoding.headerSpace)) {
                    return false;
                }
            }

            // Match the data
            for (int i = 0; i < (int)encoding.numBits; i++) {
                uint16_t mark = getNextInterval();
                uint16_t space = getNextInterval();

                if (matchMark(mark, encoding.oneMark) &&
                    matchSpace(space, encoding.oneSpace)) {
                    value = (value << 1) | 1;
                } else if (matchMark(mark, encoding.zeroMark) &&
                           matchSpace(space, encoding.zeroSpace)) {
                    value <<= 1;
                } else {
                    return false;
                }
            }

            // Match the stop mark
            if (encoding.stopMark != 0) {
                if (!matchMark(getNextInterval(), encoding.stopMark)) {
                    return false;
                }
            }

            // Success!
            return true;
        }
        static int encodePulseModulation(PulseModulationEncoding encoding,
                                            uint32_t data, uint8_t[] rawData, uint8_t maxLen)
        {

            // Index 0 is a space. Set it to 0 length.
            rawData[0] = 0;
            int length = 1;

            if ((encoding.headerMark != 0) && (length + 2 < maxLen))
            {
                rawData[length++] = (byte)(encoding.headerMark / USECPERTICK);
                rawData[length++] = (byte)(encoding.headerSpace / USECPERTICK);
            }

            for (int i = encoding.numBits - 1; i >= 0 && length + 2 < maxLen; i--)
            {
                if ((data & (1 << i)) != 0)
                {
                    rawData[length++] = (byte)(encoding.oneMark / USECPERTICK);
                    rawData[length++] = (byte)(encoding.oneSpace / USECPERTICK);
                }
                else {
                    rawData[length++] = (byte)(encoding.zeroMark / USECPERTICK);
                    rawData[length++] = (byte)(encoding.zeroSpace / USECPERTICK);
                }
            }

            if ((encoding.stopMark != 0) && length < maxLen)
            {
                rawData[length++] = (byte)(encoding.stopMark / USECPERTICK);
            }

            return length;
        }