- /* wait until photo is off, if it takes too long time,
- * return. */
- while (READ_PHOTO() != 0) {
- diff = TCNT0 - time2;
- if (diff > MAX_PHOTO_TIME)
+/* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
+ * in this case, the 'when' pointed area is assigned to the time when
+ * IR signal should be sent. The 'laserdiff' pointer is the time
+ * between the 2 lasers, in timer unit. */
+static inline int8_t wait_laser(uint16_t *when, uint16_t *laserdiff)
+{
+ uint16_t time1, time2;
+ uint16_t diff;
+
+#ifdef SPEED_40RPS
+ /* set timer to 16Mhz, we will use ICP */
+ TCCR1A = 0;
+ TCCR1B = _BV(CS10);
+#else /* 10 RPS */
+ /* set timer to 2Mhz, we will use ICP */
+ TCCR1A = 0;
+ TCCR1B = _BV(CS11);
+#endif
+
+ /* wait until all is off (inverted logic) */
+ while (READ_PHOTO() == 0);
+ TIFR = _BV(ICF1);
+
+ /* wait falling edge */
+ while ((TIFR & _BV(ICF1)) == 0);
+ time1 = ICR1;
+
+ LED1_ON();
+
+ /* wait a bit to avoid oscillations */
+ while ((uint16_t)(TCNT1-time1) < MIN_INTER_TIME);
+ TIFR = _BV(ICF1);
+
+ /* wait next falling edge + timeout */
+ while ((TIFR & _BV(ICF1)) == 0) {
+ diff = TCNT1 - time1;
+ if (diff > MAX_INTER_TIME)