static void circular_buffer_test(circular_buffer_t cb) { int i; test_t t; for (i = 0; i < 7; ++i) { memset(t.buf, 0, sizeof(t.buf)); t.index = i + 1; sprintf(t.buf, "the test buffer with id=>%d\n", t.index); t.len = strlen(t.buf); circular_buffer_put(cb, (const char*)&t, sizeof(t)); } fprintf(stdout, "size : %d\n", circular_buffer_size(cb)); fprintf(stdout, "circular buffer get=>%d\n", circular_buffer_get(cb, sizeof(t), (char*)&t)); fprintf(stdout, "size : %d\n", circular_buffer_size(cb)); test_show(t); for (i = 0; i < 1; ++i) { memset(t.buf, 0, sizeof(t.buf)); t.index = (i + 7) * (i + 7); sprintf(t.buf, "the test buffer with id=>%d\n", t.index); t.len = strlen(t.buf); circular_buffer_put(cb, (const char*)&t, sizeof(t)); } fprintf(stdout, "size : %d\n", circular_buffer_size(cb)); while (!circular_buffer_empty(cb)) { circular_buffer_get(cb, sizeof(t), (char*)&t); test_show(t); } fprintf(stdout, "size : %d\n", circular_buffer_size(cb)); }
/** * Removes item from the buffer. * Returns zero if buffer is empty. */ uint8_t circular_buffer_pop(CircularBuffer* buf) { uint8_t data; if (circular_buffer_empty(buf)) { return 0; } data = buf->buf[buf->back]; buf->back++; if (buf->back >= BUFSIZE) { buf->back = 0; } return data; }
/* function name: draw_contour * Input Parameter * maze : maze array wich has maze information * map : array to draw contour map * type : maze type to run mouse * pos : current mouse location in the maze */ void draw_contour(unsigned char *maze, unsigned char *map, unsigned int type, unsigned char pos) { int i, contour_lvl = 1; int index; unsigned int item; char found_mouse = 0; struct circular_buffer *cb, contour_buffer; cb = &contour_buffer; circular_buffer_init(cb, contour_cb_buffer, CONTOUR_CB_BUFFER_MAX); /* Uninitialized contour map */ memset(map, 0, MAZEMAX); /* Seed value 1 as a goal */ switch (type) { case TO_GOAL_4X4: map[get_index(3, 3)] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x33)); break; case TO_GOAL_8X8: map[get_index(7, 7)] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x77)); break; case TO_GOAL_16X16: map[get_index(7, 7)] = contour_lvl; map[get_index(8, 7)] = contour_lvl; map[get_index(7, 8)] = contour_lvl; map[get_index(8, 8)] = contour_lvl; /* Add list of same level contour value */ circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x77)); circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x78)); circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x87)); circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x88)); break; case TO_START_4X4: case TO_START_8X8: case TO_START_16X16: map[get_index(0, 0)] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x00)); break; default: if (type < MAZEMAX) { map[type] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, type)); } else { print_exit("Invalid target goal index!\n"); } break; } /* Get one contour number from circular buffer. * Put next higher value in next block if there is * no wall to there and save it inti circular buffer. * If contour map reaches to current mouse or * circular buffer is empty then it's done. */ while (!circular_buffer_empty(cb) && !found_mouse) { circular_buffer_read(cb, &item); index = get_contour_index(item); contour_lvl = get_contour_lvl(item) + 1; /* Calculate contour lvl around current cube */ for (i = NI; i <= WI; i++) { if (!(maze[index] & wall_bit(i)) && (map[index + maze_dxy[i]] == 0)) { map[index + maze_dxy[i]] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, index + maze_dxy[i])); if (index + maze_dxy[i] == pos) found_mouse = 1; } } #ifdef DEBUG if (debug_flag & DEBUG_CONTOUR) { print_map(map); usleep(20000); } #endif } if (!found_mouse) { /* Mouse alorighm should never hit this location */ print_map(map); print_exit("%s couldn't find mouse location\n", __func__); } }