/* * box_move_opt.c * * Created on: 11/19/26 * Author: johnsontimoj * * Box demo using nios_pixel hardware * * OBVIOUSLY - most of this should be in functions * */ ///////////////// // Includes ///////////////// #include "altera_up_avalon_video_pixel_buffer_dma.h" #include #include #include #include int main(void){ printf("Processor Started Successfully - Entered Main of box.c\n\n"); // define a pointer of type pixel_buffer... // to use as a reference in the dma functions // alt_up_pixel_buffer_dma_dev * pixel_buf_dma_dev; // open the Pixel Buffer port // - command is in drivers/inc/alter...video_pixel_buffer_dma.h // name reference is in system.h // - "/dev/video_pixel_buffer_dma_0" // pixel_buf_dma_dev = alt_up_pixel_buffer_dma_open_dev ("/dev/video_pixel_buffer_dma_0"); // Check for error and output to the console // if ( pixel_buf_dma_dev == NULL) printf ("Error: could not open pixel buffer device \n"); else printf ("Opened pixel buffer device \n"); // Clear the screen // - command is in drivers/inc/alter...video_pixel_buffer_dma.h // - wait until done before continuing // alt_up_pixel_buffer_dma_clear_screen(pixel_buf_dma_dev, 0); alt_up_pixel_buffer_dma_clear_screen(pixel_buf_dma_dev, 1); usleep(1000000); // 1sec ////////////////////////////////////////// // establish variables for 3 boxes // these could be variables, arrays or structures ///////////////////////////////////////// // VARIABLES alt_16 x; alt_16 x_old; alt_16 y; alt_16 y_old; alt_16 xydim; alt_8 xy_x_dir; alt_8 xy_y_dir; x = 100; x_old = 100; y = 100; y_old = 100; xydim = 50; xy_x_dir = 1; xy_y_dir = 1; // ARRAY // x i_loc, j_loc, i_old, j_old, dim, i_dir, j_dir alt_16 ij[7] = {40, 40, 40, 40, 25, 1, -1}; // STRUCTURE typedef struct{ alt_16 p_loc; alt_16 q_loc; alt_16 p_new; alt_16 q_new; alt_16 pq_dim; alt_16 p_dir; alt_8 q_dir; } pq_block; pq_block blk3 = {.p_loc = 150, .q_loc = 150, .p_new = 150, .q_new = 150, .pq_dim = 75, .p_dir = -1, .q_dir = 1 }; //////////////////////////////////////// //////////////////////////////// // setup a counter 10 ticks // to allow different motion rates // using 1-10 instead of 0-9 ///////////////////////////////// alt_u8 count10; count10 = 1; /////////////////////////////// ////////////////////////////////////////// ////////////////////////////////////////// // // THIS CODE IS NOT COMPLETE // // IT DOES NOT WORRY ABOUT COLLISIONS // IT DOES NOT FULLY HANDLE WRAPS // IT HAS FIXED MOTION STEP SIZES AND DIRECTIONS // THESE WOULD BE CALCULATED IN A REAL SYSTEM // // IT IS AN EXAMPLE // ///////////////////////////////////////// ////////////////////////////////////////// // draw initial boxes alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x, y, x + xydim, y + xydim, 0xF800, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, ij[0], ij[1], ij[0] + ij[4], ij[1] + ij[4], 0x07E0, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, blk3.p_loc, blk3.q_loc, blk3.p_loc + blk3.pq_dim, blk3.q_loc + blk3.pq_dim, 0x001F, 1); // motion while(1){ ////////////////////////////////// // update, erase, shift, redraw, swap, erase, redraw ////////////////////////////////// ///////////////////////////////// // big box - only erase and redraw what moves // update at every other cycle ///////////////////////////////// if(count10 % 2){ x_old = x; // update position y_old = y ; if((x_old + xy_x_dir) >= 320) x = 0; else if((x_old + xy_x_dir) < 0) x = (320 - 1); else x = x_old + xy_x_dir; if((y_old + xy_y_dir) >= 240) y = 0; else if((y_old + xy_y_dir) < 0) y = (320 - 1); else y = y_old + xy_y_dir; alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x_old, y_old, x_old + xydim, y_old, 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x_old, y_old, x_old, y_old + xydim, 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x, y + xydim, x + xydim , y + xydim, 0xF800, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x + xydim, y, x + xydim , y + xydim, 0xF800, 1); } //////////////////////////////////// // small box erase and redraw it all // update every cycle /////////////////////////////////// ij[2] = ij[0]; // update position ij[3] = ij[1]; if((ij[2] + ij[5]) >= 320) ij[0] = 0; else if((ij[2] + ij[5]) < 0) ij[0] = (320 - 1); else ij[0] += ij[5]; if((ij[3] + ij[6]) >= 240) ij[1] = 0; else if((ij[3] + ij[6]) < 0) ij[1] = (240 - 1); else ij[1] += ij[6]; alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, ij[2], ij[3], ij[2] + ij[4], ij[3] + ij[4], 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, ij[0], ij[1], ij[0] + ij[4], ij[1] + ij[4], 0x07E0, 1); ///////////////////////////////////// // big box - but I got lazy and redraw it all // update every 5th cycle ///////////////////////////////////// if(!(count10 % 5)){ blk3.p_loc = blk3.p_new; // update position blk3.q_loc = blk3.q_new; if((blk3.p_loc + blk3.p_dir) >= 320) blk3.p_new = 0; else if((blk3.p_loc + blk3.p_dir) < 0) blk3.p_new = (320 - 1); else blk3.p_new = blk3.p_loc + blk3.p_dir; if((blk3.q_loc + blk3.q_dir) >= 240) blk3.q_new = 0; else if((blk3.q_loc + blk3.q_dir) < 0) blk3.q_new = (240 - 1); else blk3.q_new = blk3.q_loc + blk3.q_dir; alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, blk3.p_loc, blk3.q_loc, blk3.p_loc + blk3.pq_dim, blk3.q_loc + blk3.pq_dim, 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, blk3.p_new, blk3.q_new, blk3.p_new + blk3.pq_dim, blk3.q_new + blk3.pq_dim, 0x001F, 1); } //////////////////////////////////// // swap and wait //////////////////////////////////// alt_up_pixel_buffer_dma_swap_buffers(pixel_buf_dma_dev); while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buf_dma_dev)){ ;// spin } ///////////////////////////////// // big box - only erase and redraw what moves // update at every other cycle ///////////////////////////////// if(count10 % 2){ alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x_old, y_old, x_old + xydim, y_old, 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x_old, y_old, x_old, y_old + xydim, 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x, y + xydim, x + xydim , y + xydim, 0xF800, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, x + xydim, y, x + xydim , y + xydim, 0xF800, 1); } //////////////////////////////////// // small box erase and redraw it all // update every cycle /////////////////////////////////// alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, ij[2], ij[3], ij[2] + ij[4], ij[3] + ij[4], 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, ij[0], ij[1], ij[0] + ij[4], ij[1] + ij[4], 0x07E0, 1); ///////////////////////////////////// // big box - but I got lazy and redraw it all // update every 5th cycle ///////////////////////////////////// if(!(count10 % 5)){ // big box - but I got lazy and redraw it all alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, blk3.p_loc, blk3.q_loc, blk3.p_loc + blk3.pq_dim, blk3.q_loc + blk3.pq_dim, 0x0000, 1); alt_up_pixel_buffer_dma_draw_box(pixel_buf_dma_dev, blk3.p_new, blk3.q_new, blk3.p_new + blk3.pq_dim, blk3.q_new + blk3.pq_dim, 0x001F, 1); } //////////////////////// // limit the count //////////////////////// if(count10 >= 10) count10 = 1; else count10++; }// end while(1) loop // print program complete message printf ("Program Complete \n"); return 0; }