|
| 1 | +// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Text; |
| 8 | +using nanoFramework.Hardware.Esp32.Rmt; |
| 9 | + |
| 10 | +namespace InfraredRemote |
| 11 | +{ |
| 12 | + public abstract class Decoder |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Defines constructor of <see cref="Decoder"/>.</typeparam> |
| 16 | + /// </summary> |
| 17 | + /// <param name="signalLengthTolerance">Tolerance of signal length represented as fraction value. </param> |
| 18 | + protected Decoder(double signalLengthTolerance) |
| 19 | + { |
| 20 | + SignalLengthTolerance = signalLengthTolerance; |
| 21 | + } |
| 22 | + |
| 23 | + protected SignalData lastData = null; |
| 24 | + protected abstract int PulseLengthInMicroseconds { get; } |
| 25 | + protected abstract int HeaderMark { get; } |
| 26 | + protected abstract int HeaderSpace { get; } |
| 27 | + protected abstract int RepeatSpace { get; } |
| 28 | + protected abstract int ZeroSpace { get; } |
| 29 | + protected abstract int OneSpace { get; } |
| 30 | + protected abstract Protocol Protocol { get; } |
| 31 | + protected double SignalLengthTolerance { get; private set; } = 0.2; |
| 32 | + protected abstract int SignalLength { get; } |
| 33 | + protected abstract bool UseLessSignificantBitFirst { get; } |
| 34 | + protected abstract int AddressBits { get; } |
| 35 | + protected abstract int CommandBits { get; } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Decodes Rmt signal into SignalData. |
| 39 | + /// </summary> |
| 40 | + /// <param name="receivedSignal">Array representing decoded signal.</param> |
| 41 | + /// <returns>SignalData object.</returns> |
| 42 | + public virtual SignalData Decode(RmtCommand[] receivedSignal) |
| 43 | + { |
| 44 | + var firstPulse = receivedSignal[0]; |
| 45 | + var lastPulse = receivedSignal[receivedSignal.Length - 1]; |
| 46 | + bool isHeaderMark = Match(firstPulse.Duration0, HeaderMark); |
| 47 | + bool isSpaceMark = Match(firstPulse.Duration1, HeaderSpace); |
| 48 | + bool isEndOfTransmission = Match(lastPulse.Duration0, PulseLengthInMicroseconds) && !lastPulse.Level0 && lastPulse.Duration1 == 0 && |
| 49 | + lastPulse.Level1; |
| 50 | + bool isRepeat = receivedSignal.Length == 2 && Match(firstPulse.Duration0, HeaderMark) && !firstPulse.Level0 && Match(firstPulse.Duration1, RepeatSpace) && firstPulse.Level1; |
| 51 | + |
| 52 | + if (isRepeat) |
| 53 | + { |
| 54 | + return lastData; |
| 55 | + } |
| 56 | + |
| 57 | + SignalData signalData = null; |
| 58 | + if (isHeaderMark && isSpaceMark && (receivedSignal.Length == SignalLength) && isEndOfTransmission) |
| 59 | + { |
| 60 | + var message = ExtractRawPayload(receivedSignal); |
| 61 | + var address = ExtractAddress(message); |
| 62 | + var command = ExtractCommand(message); |
| 63 | + int addressNumber = 0; |
| 64 | + int commandNumber = 0; |
| 65 | + if (UseLessSignificantBitFirst) |
| 66 | + { |
| 67 | + addressNumber = Convert.ToInt32(Reverse(address), 2); |
| 68 | + commandNumber = ToInt32(Reverse(command)); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + addressNumber = ToInt32(address); |
| 73 | + commandNumber = ToInt32(command); |
| 74 | + } |
| 75 | + |
| 76 | + signalData = new SignalData(addressNumber, commandNumber, address, command, Protocol, message); |
| 77 | + lastData = signalData; |
| 78 | + } |
| 79 | + |
| 80 | + return signalData; |
| 81 | + } |
| 82 | + |
| 83 | + protected virtual string ExtractCommand(string message) |
| 84 | + { |
| 85 | + return message.Substring(AddressBits, CommandBits); |
| 86 | + } |
| 87 | + |
| 88 | + protected virtual string ExtractAddress(string message) |
| 89 | + { |
| 90 | + return message.Substring(0, AddressBits); |
| 91 | + } |
| 92 | + |
| 93 | + private string ExtractRawPayload(RmtCommand[] response) |
| 94 | + { |
| 95 | + StringBuilder sb = new StringBuilder(); |
| 96 | + for (int i = 1; i < response.Length - 1; i++) |
| 97 | + { |
| 98 | + var rmtCommand = response[i]; |
| 99 | + if (Match(rmtCommand.Duration1, OneSpace)) |
| 100 | + { |
| 101 | + sb.Append("1"); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + sb.Append("0"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return sb.ToString(); |
| 110 | + } |
| 111 | + |
| 112 | + protected bool Match(int tick, int expectedTick) |
| 113 | + { |
| 114 | + var ticksLow = expectedTick * (1 - SignalLengthTolerance); |
| 115 | + var ticksHigh = expectedTick * (1 + SignalLengthTolerance); |
| 116 | + return (tick >= ticksLow && |
| 117 | + tick <= ticksHigh); |
| 118 | + } |
| 119 | + |
| 120 | + private string Reverse(string valueToBeReversed) |
| 121 | + { |
| 122 | + StringBuilder sb = new StringBuilder(); |
| 123 | + for (int i = valueToBeReversed.Length - 1; i >= 0; i--) |
| 124 | + { |
| 125 | + sb.Append(valueToBeReversed[i]); |
| 126 | + } |
| 127 | + |
| 128 | + return sb.ToString(); |
| 129 | + } |
| 130 | + |
| 131 | + private int ToInt32(string binaryValue) |
| 132 | + { |
| 133 | + int dec_value = 0; |
| 134 | + |
| 135 | + int base1 = 1; |
| 136 | + |
| 137 | + for (int i = binaryValue.Length - 1; i >= 0; i--) |
| 138 | + { |
| 139 | + if (binaryValue[i] == '1') |
| 140 | + { |
| 141 | + dec_value += base1; |
| 142 | + } |
| 143 | + base1 = base1 * 2; |
| 144 | + } |
| 145 | + |
| 146 | + return dec_value; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments