+ /* if we didn't found beacon 0 and 1, return */
+ if (found != 0x3)
+ return;
+
+ /* update ring head */
+ status->head = head;
+
+ /* beacon 0 */
+ dist0 = data0;
+ dist0 /= 512.;
+ dist0 *= (MAX_DIST-MIN_DIST);
+ dist0 += MIN_DIST;
+
+ time0 = time0 - ref_time0;
+ angle0 = (double)time0 / (double)current_motor_period;
+ if (angle0 > 1.)
+ angle0 -= 1.;
+ if (angle0 > 1.)
+ return; /* fail */
+ angle0 *= (2 * M_PI);
+ if (angle0 > M_PI)
+ angle0 -= M_PI;
+
+ /* beacon 1 */
+ dist1 = data1;
+ dist1 /= 512.;
+ dist1 *= (MAX_DIST-MIN_DIST);
+ dist1 += MIN_DIST;
+
+ time1 = time1 - ref_time1;
+ angle1 = (double)time1 / (double)current_motor_period;
+ if (angle1 > 1.)
+ angle1 -= 1.;
+ if (angle1 > 1.)
+ return; /* fail */
+ angle1 *= (2 * M_PI);
+ if (angle0 > M_PI)
+ angle0 -= M_PI;
+
+ /* display if needed */
+ if (beacon_tsop.debug_frame) {
+ printf("STA ID=%d dist0=%2.2f angle0=%2.2f dist1=%2.2f angle1=%2.2f\r\n",
+ beacon_id, dist0, angle0 * 180. / M_PI, dist1, angle1 * 180. / M_PI);
+ }
+
+ if (ad_to_posxya(&pos, &a, 0, &beacon0, &beacon2, angle0, dist0,
+ angle1, dist1) < 0)
+ return;
+
+ /* /!\ angle is between 0 and 3600 */
+ xmit_static((uint16_t)pos.x, (uint16_t)pos.y, (uint16_t)(a*10));
+}
+
+static int8_t check_opp_frame(uint16_t frame, uint16_t time)
+{
+ int8_t beacon_id = -1;
+ uint16_t cksum;
+
+ /* ignore bad cksum */
+ cksum = verify_cksum(frame);
+ if (cksum == 0xFFFF)
+ goto fail;
+
+ beacon_id = frame & TSOP_OPP_BEACON_ID_MASK;
+ if (beacon_id != TSOP_OPP_BEACON_ID)
+ goto fail;
+
+ /* if motor speed is not good, skip values */
+ if (current_motor_period < MOTOR_PERIOD_MIN)
+ goto fail;
+ if (current_motor_period > MOTOR_PERIOD_MAX)
+ goto fail;
+
+ return beacon_id;
+ fail:
+ /* display if needed */
+ if (beacon_tsop.debug_frame) {
+ printf("OPP ID=%d frame=%x time=%d cksum=%x d=%d\r\n",
+ beacon_id, frame, time, cksum,
+ (frame & TSOP_OPP_FRAME_DATA_MASK) >>
+ TSOP_OPP_FRAME_DATA_SHIFT);
+ }
+ return -1;
+}
+
+/* process the received frame ring */
+static void process_opp_ring(struct frame_status *status)
+{
+ uint8_t head_next;
+ uint16_t frame;
+ uint8_t found = 0;
+ uint16_t data, time, ref_time;
+ double angle;
+ double dist;
+
+ /* after CS, check if we have a new frame in ring */
+ while (status->head != status->tail) {
+ head_next = (status->head+1) & FRAME_RING_MASK;
+ frame = status->ring[status->head].frame;
+
+ if (check_opp_frame(frame, status->ring[status->head].time) < 0) {
+ status->head = head_next;
+ continue;
+ }
+
+ found = 1;
+ data = (frame & TSOP_OPP_FRAME_DATA_MASK) >> TSOP_OPP_FRAME_DATA_SHIFT;
+ time = status->ring[status->head].time;
+ ref_time = status->ring[status->head].ref_time;
+
+ status->head = head_next;
+ }
+
+ if (found == 0)
+ return;
+
+ dist = data;
+ dist /= 512.;
+ dist *= (MAX_DIST-MIN_DIST);
+ dist += MIN_DIST;
+
+ time = time - ref_time;
+ angle = (double)time / (double)current_motor_period;
+ if (angle > 1.)
+ angle -= 1.;
+ if (angle > 1.)
+ return; /* fail */
+ angle *= 3600; /* angle in 1/10 deg */
+
+ /* display if needed */
+ if (beacon_tsop.debug_frame) {
+ printf("OPP dist=%2.2f angle=%2.2f\r\n", dist, angle/10);
+ }
+ xmit_opp((uint16_t)dist, (uint16_t)angle);