12 fChannelCount(channelCount),
13 fChannel(new uint16_t[fChannelCount]),
14 fLastChannel(new uint16_t[fChannelCount])
22 delete[] fLastChannel;
29 fCurrent = fChannelCount;
30 memset(fChannel,
'\0',
sizeof(fChannelCount)*
sizeof(uint16_t));
31 memset(fLastChannel,
'\0',
sizeof(fChannelCount)*
sizeof(uint16_t));
41 uint32_t pulse = readPulse(fPin);
44 if (fCurrent == fChannelCount)
51 fChannel[fCurrent++] = pulse;
52 if (fCurrent == fChannelCount)
54 for (
unsigned i = 0; i < fChannelCount; i++)
56 if (fChannel[i] > 2000 || fChannel[i] < 100)
58 fChannel[i] = fLastChannel[i];
62 fChannel[i] = (fLastChannel[i] + fChannel[i]) / 2;
68 for (
unsigned i = 0; i < fChannelCount; i++)
77 fLastChannel[i] = fChannel[i];
90 uint16_t
channel(
unsigned ch,
unsigned minvalue,
unsigned maxvalue,
unsigned neutralvalue)
92 unsigned pulse = (ch < fChannelCount) ? fChannel[ch] : 0;
94 return map(max(min(pulse, 1600u), 600u), 600u, 1600u, minvalue, maxvalue);
103 unsigned fChannelCount;
104 uint32_t fRisingTime;
106 uint16_t* fLastChannel;
108 uint32_t readPulse(uint8_t pin)
110 uint8_t state = digitalRead(pin);
111 uint32_t pulseLength = 0;
114 if (fPinState == LOW && state == HIGH)
116 fRisingTime = micros();
120 if (fPinState == HIGH && state == LOW)
122 unsigned long fallingTime = micros();
123 pulseLength = fallingTime - fRisingTime;