- uint8_t photos;
- uint8_t time1, time2;
- uint8_t diff;
-
- /*
- wait photo on
- wait photo off
- wait photo on or timeout
- wait photo off
- should return the amount of time to wait
- */
-
- /* wait until all is off */
- while (READ_PHOTO() != 0);
-
- /* wait until photo is on */
- while (READ_PHOTO() != 1);
- time1 = TCNT0;
+ uint16_t x, cksum;
+
+ x = (val & 0xfff);
+ /* add the three 4-bits blocks of x together */
+ cksum = x & 0xf;
+ x = x >> 4;
+ cksum += x & 0xf;
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+ x = x >> 4;
+ cksum += x & 0xf;
+ cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
+ cksum = (~cksum) & 0xf;
+ return (cksum << 12) + (val & 0xfff);
+}
+
+/* get the frame from laser time difference, return 0 on error (this
+ * frame cannot be sent). */
+static uint32_t get_frame(uint16_t laserdiff)
+{
+ uint32_t frame = 0;
+ double a, d;
+ uint16_t frame_dist;
+
+ frame |= ((uint32_t)BEACON_ID << BEACON_ID_SHIFT);
+
+ /* process angle from laserdiff time */
+#ifdef SPEED_10RPS
+ /* timer = 2Mhz */
+ a = ((double)laserdiff / (2000000./10.)) * 2. * M_PI;
+#else
+ /* timer = 16Mhz */
+ a = ((double)laserdiff / (16000000./40.)) * 2. * M_PI;
+#endif
+ /* get distance from angle */
+ d = LASER_DIST / sin(a/2);
+
+ /* scale it between 0 and 511 */
+ if (d <= MIN_DIST)
+ return 0;
+ if (d >= MAX_DIST)
+ return 0;
+ d -= MIN_DIST;
+ d /= (MAX_DIST-MIN_DIST);
+ d *= 512;
+ frame_dist = (uint16_t)d;
+ if (frame_dist >= 512) /* should not happen... */
+ return 0;
+
+ frame |= ((uint32_t)(frame_dist & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
+
+ /* process cksum and return */
+ return do_cksum(frame);
+}
+
+/* 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;
+