+static void poll_input_capture(void)
+{
+ uint16_t icp, diff;
+ uint8_t rising;
+
+ /* no new sample, return */
+ if ((TIFR1 & _BV(ICF1)) == 0)
+ return;
+
+ sei();
+ icp = ICR1;
+ cli();
+
+ rising = TCCR1B & _BV(ICES1);
+
+ /* change the edge type */
+ TCCR1B ^= _BV(ICES1);
+
+ /* clear the flag */
+ TIFR1 = TIFR1 | _BV(ICF1);
+
+ diff = icp - icp_prev;
+ icp_prev = icp;
+
+ /* a rising edge with at least 2ms of state 0 means that we
+ * get the first servo */
+ if (rising == 1 && diff > 2000) {
+ icp_idx = 0;
+ return;
+ }
+
+ /* get the value for the servo */
+ if (rising == 0 && icp_idx < NB_SERVO) {
+ if (diff < 1000)
+ icp_servos[icp_idx] = 0;
+ else
+ icp_servos[icp_idx] = diff - 1000;
+ icp_idx++;
+ }
+}
+
+static void poll(void)
+{
+ poll_spi();
+ poll_input_capture();
+}
+