vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / projects / microb2010 / mainboard / strat_db.c
index c3608d6..dfc5693 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <aversive.h>
 #include <aversive/pgmspace.h>
+#include <aversive/error.h>
 
 #include <ax12.h>
 #include <uart.h>
@@ -159,10 +160,42 @@ int8_t ijcoord_to_xycoord(uint8_t i, uint8_t j, int16_t *x, int16_t *y)
        return 0;
 }
 
+/* return the nearest waypoint (any type) */
+int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp)
+{
+       int16_t x, y;
+       uint8_t i, j;
+
+       x = *xp;
+       y = *yp;
+
+       x -= OFFSET_CORN_X;
+       x += (STEP_CORN_X/2);
+       i = x / STEP_CORN_X;
+
+       y = COLOR_Y(y); /* Y depends on color */
+       y -= OFFSET_CORN_Y;
+       y += STEP_CORN_Y/2;
+
+       if ((i & 1) == 1)
+               y -= STEP_CORN_Y/2;
+       j = y / STEP_CORN_Y;
+
+       if (ijcoord_to_xycoord(i, j, &x, &y) < 0)
+               return -1;
+
+       *xp = x;
+       *yp = y;
+       *ip = i;
+       *jp = j;
+
+       return 0;
+}
+
 /* return the nearest waypoint that is not a corn: xp and yp contains
  * the input and output, and ip, jp are only outputs. return 0 on
  * success. */
-int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp)
+int8_t xycoord_to_ijcoord_not_corn(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp)
 {
        int16_t x, y;
        uint8_t i, j;
@@ -174,7 +207,7 @@ int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp)
        x += (STEP_CORN_X/2);
        i = x / STEP_CORN_X;
 
-       y = COLOR_Y(y);
+       y = COLOR_Y(y); /* Y depends on color */
        y -= OFFSET_CORN_Y;
        if ((i & 1) == 1) {
                j = y / STEP_CORN_Y;
@@ -206,8 +239,7 @@ int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp)
 
 /******** CORN */
 
-/* return the index of a corn given its i,j coords. */
-int8_t ijcoord_to_corn_idx(uint8_t i, uint8_t j)
+static int8_t early_ijcoord_to_corn_idx(uint8_t i, uint8_t j)
 {
        uint8_t n;
        for (n = 0; n < CORN_NB; n ++) {
@@ -218,6 +250,14 @@ int8_t ijcoord_to_corn_idx(uint8_t i, uint8_t j)
        return -1;
 }
 
+/* return the index of a corn given its i,j coords. */
+int8_t ijcoord_to_corn_idx(uint8_t i, uint8_t j)
+{
+       if (strat_db.wp_table[i][j].type != WP_TYPE_CORN)
+               return -1;
+       return strat_db.wp_table[i][j].corn.idx;
+}
+
 /* return the i,j coords of a corn given its index */
 int8_t corn_idx_to_ijcoord(uint8_t idx, uint8_t *i, uint8_t *j)
 {
@@ -492,7 +532,7 @@ void strat_db_init(void)
                        }
 
                        /* corn */
-                       idx = ijcoord_to_corn_idx(i, j);
+                       idx = early_ijcoord_to_corn_idx(i, j);
                        if (idx >= 0) {
                                wp->type = WP_TYPE_CORN;
                                wp->present = 1;