Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ColorDetection (
- input CLK ,
- input ENABLE1 ,
- input ENABLE2 ,
- input ENABLE3 ,
- input ENABLE4 ,
- input ENABLE5 ,
- input ENABLE6 ,
- input ENABLE7 ,
- input ENABLE8 ,
- input ENABLE9 ,
- input ENABLE10 ,
- input ENABLEALL,
- input ENABLE12 ,
- input ENABLE13 ,
- input ENABLE14 ,
- input ENABLE15 ,
- input ENABLE16 ,
- input ENABLE17 ,
- input [ 7:0] R_IN ,
- input [ 7:0] G_IN ,
- input [ 7:0] B_IN ,
- input [12:0] VGA_V_CNT,
- input [12:0] VGA_H_CNT,
- output [ 7:0] R_OUT ,
- output [ 7:0] G_OUT ,
- output [ 7:0] B_OUT ,
- output [ 5:0] BALL_X ,
- output [ 5:0] BALL_Y ,
- output [ 5:0] BALL_X1 ,
- output [ 5:0] BALL_Y1
- );
- parameter MARGIN = 1;
- parameter FONT_WIDTH = 3;
- parameter FONT_HEIGHT = 5;
- reg [14:0] font[0:10] = '{
- 15'b111_101_101_101_111, // 0
- 15'b110_010_010_010_111, // 1
- 15'b111_001_111_100_111, // 2
- 15'b111_001_111_001_111, // 3
- 15'b101_101_111_001_001, // 4
- 15'b111_100_111_001_111, // 5
- 15'b111_100_111_101_111, // 6
- 15'b111_001_001_001_001, // 7
- 15'b111_101_111_101_111, // 8
- 15'b111_101_111_001_111, // 9
- 15'b000_000_111_000_000 // -
- };
- function font_pixel;
- input [3:0] digit;
- input [15:0] x_pos;
- input [15:0] y_pos;
- begin
- case (digit)
- 0 : font_pixel = font[0][x_pos + y_pos * FONT_WIDTH];
- 1 : font_pixel = font[1][x_pos + y_pos * FONT_WIDTH];
- 2 : font_pixel = font[2][x_pos + y_pos * FONT_WIDTH];
- 3 : font_pixel = font[3][x_pos + y_pos * FONT_WIDTH];
- 4 : font_pixel = font[4][x_pos + y_pos * FONT_WIDTH];
- 5 : font_pixel = font[5][x_pos + y_pos * FONT_WIDTH];
- 6 : font_pixel = font[6][x_pos + y_pos * FONT_WIDTH];
- 7 : font_pixel = font[7][x_pos + y_pos * FONT_WIDTH];
- 8 : font_pixel = font[8][x_pos + y_pos * FONT_WIDTH];
- 9 : font_pixel = font[9][x_pos + y_pos * FONT_WIDTH];
- 10 : font_pixel = font[10][x_pos + y_pos * FONT_WIDTH];
- endcase
- end
- endfunction
- parameter rows = 30; //default 30
- parameter rowstest = 10;
- parameter cols = 40; //default 40
- parameter block_size = 16; // 640/40=16 480/30=16 so block_size = 16x16 350
- shortint unsigned modes = 0;
- byte unsigned row [cols]; // Store the count of green pixels of a single row
- byte unsigned max_count_per_row[rows]; // Store the max green count per row
- byte unsigned max_x_per_row [rows]; // Store the x of the max green per row
- // The max x/y/count so far in for loops for getting the max of the array above
- shortint unsigned max_x, max_y, max_count = 0;
- // Current x/y
- shortint unsigned x, y;
- // Current x/y on the grid
- shortint unsigned x_grid, y_grid;
- shortint unsigned prev_y_grid = rows; // Previous y on the grid
- // (default = imposible so first clock tick the array is reset)
- // HSL values
- shortint unsigned hue, lightness, saturation;
- // HSL tmp values
- shortint unsigned max, min, delta, R, G, B;
- shortint unsigned y_block_min, y_block_max, x_block_min, x_block_max, side;
- // Is the current pixel green or not
- reg is_green = 0;
- // Is the current pixel blue or not
- reg is_blue = 0;
- // Is the current pixel blue or not
- reg is_red = 0;
- // Is the current pixel blue or not
- reg is_white = 0;
- // Is the current pixel blue or not
- reg is_yellow = 0;
- // Is the current pixel blue or not
- reg is_orange = 0;
- reg is_greenblock = 0;
- reg is_blueblock = 0;
- shortint unsigned is_blockcolor = 0; //0 blue, 1 green, 2 rood, 3 wit, 4 geel, 5 oranje
- always @(posedge CLK) begin
- // if (VGA_H_CNT < X_START + 155 || (VGA_H_CNT - X_START) > VGA_WIDTH - 370 ||
- // VGA_V_CNT < Y_START + 52 || (VGA_V_CNT - Y_START) > VGA_HEIGHT - 320) begin
- if (VGA_H_CNT < X_START || (VGA_H_CNT - X_START) > VGA_WIDTH ||
- VGA_V_CNT < Y_START || (VGA_V_CNT - Y_START) > VGA_HEIGHT) begin
- R_OUT <= R_IN;
- G_OUT <= G_IN;
- B_OUT <= B_IN;
- end else begin
- // Calculate the real x/y values
- x = VGA_H_CNT - X_START;
- y = VGA_V_CNT - Y_START;
- // Calculate the x/y on the grid
- x_grid = x / block_size;
- y_grid = y / block_size;
- //blokken voor detectie
- y_block_min = 0;
- y_block_max = 30;
- x_block_min = 0;
- x_block_max = 40;
- if (ENABLE10) begin
- y_block_min = 3;
- y_block_max = 9;
- x_block_min = 9;
- x_block_max = 15;
- end else if (ENABLE12) begin
- y_block_min = 3;
- y_block_max = 9;
- x_block_min = 16;
- x_block_max = 22;
- end else if (ENABLE13) begin
- y_block_min = 3;
- y_block_max = 9;
- x_block_min = 24;
- x_block_max = 30;
- end else if (ENABLE14) begin
- y_block_min = 11;
- y_block_max = 17;
- x_block_min = 9;
- x_block_max = 15;
- end else if (ENABLE15) begin
- y_block_min = 11;
- y_block_max = 17;
- x_block_min = 17;
- x_block_max = 23;
- end
- if (y_grid >= y_block_min && y_grid < y_block_max && x_grid >= x_block_min && x_grid < x_block_max) begin //if (y_grid > 17 && y_grid < 24 && x_grid > 22 && x_grid < 29)
- // Reset the grid to zero's
- if(y_grid != prev_y_grid) begin
- prev_y_grid = y_grid;
- for (int for_x = 0; for_x < cols; for_x++) begin
- row[for_x] = 0;
- end
- end
- // convert 0-255 to 0-100 (normally 0-1 but no floats bruh)
- R = R_IN * 100 / 255;
- G = G_IN * 100 / 255;
- B = B_IN * 100 / 255;
- // Calculate max/min (without helper functions, say whut)
- max = (R > G) ? (R > B ? R : B) : (G > B ? G : B);
- min = (R < G) ? (R < B ? R : B) : (G < B ? G : B);
- delta = max - min;
- lightness = (max + min) / 2;
- saturation = (lightness > 50) ? (delta * 100) / (200 - max - min) : (delta * 100) / (max + min);
- if (max == R) hue = 60 * ((G - B) / delta % 6);
- if (max == G) hue = 60 * ((B - R) / delta + 2);
- if (max == B) hue = 60 * ((R - G) / delta + 4);
- if (max == min) begin // achromatic
- hue = 0;
- saturation = 0;
- end
- if (hue > 360) hue = hue - 360;
- if (hue < 0) hue = hue + 360;
- if(ENABLEALL) begin
- is_blue = hue > 160 && hue < 200 && // was 160 200
- saturation > 20; //default 20
- // lightness > 10 && lightness < 90;
- is_green = hue > 70 && hue < 100 && // was 50 90
- saturation > 22; //default 20
- // lightness > 10 && lightness < 90;
- is_red = (hue > 320 || hue < 20) && //was 345 20
- saturation < 70 && lightness < 60; //default 20
- // lightness > 10 && lightness < 90;
- is_white = lightness > 95 && saturation < 50;
- is_yellow = hue > 35 && hue < 70 && // was 25 60
- saturation > 20; //default 20
- // lightness > 10 && lightness < 90;
- is_orange = hue > 15 && hue < 35 && //was 0 35//////// 10 35 werkt, maar pakt te vaak over rood
- saturation > 60; //was 50
- // lightness > 10 && lightness < 90;
- //hier worden de hoeveelheid groene puntjes berekend
- // If hue is green
- // And value is still valid
- if (is_blue && row[x_grid] < 255) begin
- // Increase by one
- // row[x_grid] = 20; //default ++
- row[x_grid]++;
- end
- // if (is_green && row[x_grid] < 255) begin
- // // Increase by one
- // // row[x_grid] = 20; //default ++
- // row[x_grid]++;
- // end
- // if (is_red && row[x_grid] < 255) begin
- // // Increase by one
- // // row[x_grid] = 20; //default ++
- // row[x_grid]++;
- // end
- end
- R_OUT <= 0;
- G_OUT <= 0;
- B_OUT <= 0;
- if(ENABLE1 && y % block_size < FONT_HEIGHT && x_grid > 0 && x_grid < cols - 1) begin
- shortint unsigned x_tgt, block_x, block_y, dig, x_pos, y_pos;
- // Calculate the start x/y of the block
- block_x = x_grid * block_size + 1;
- block_y = y_grid * block_size;
- for (int i = 0; i < 3; i++) begin
- x_tgt = block_x + i * (FONT_WIDTH + MARGIN); //debug letters
- if (x >= x_tgt && x < (x_tgt + FONT_WIDTH)) begin
- byte unsigned x_pos, y_pos;
- x_pos = (x - x_tgt);
- y_pos = (y - block_y);
- case (i)
- 0 : dig = row[x_grid] % 1000 / 100;
- 1 : dig = row[x_grid] % 100 / 10;
- 2 : dig = row[x_grid] % 10;
- endcase
- if (font_pixel(dig, FONT_WIDTH - 1 - x_pos, FONT_HEIGHT - 1 - y_pos)) begin
- R_OUT <= 255;
- G_OUT <= 255;
- B_OUT <= 255;
- end
- end
- end
- end else if(BALL_X == x_grid && BALL_Y == y_grid && 1 == 0) begin
- // Show the hotbox
- R_OUT <= 0;
- G_OUT <= 255;
- B_OUT <= 0;
- end else if(BALL_X1 == x_grid && BALL_Y1 == y_grid && 1==0) begin
- // Show the hotbox
- R_OUT <= 0;
- G_OUT <= 0;
- B_OUT <= 255;
- end else if(ENABLE4 && is_green) begin //end else if(ENABLE2 && is_green) begin
- // Show all the greens with hue 100 > hue < 140
- R_OUT <= 0;
- G_OUT <= 255;
- B_OUT <= 0;
- end else if(ENABLE5 && is_blue) begin //end else if(ENABLE5 && is_blue) begin
- // Show all the greens with hue 100 > hue < 140
- R_OUT <= 0;
- G_OUT <= 0;
- B_OUT <= 255;
- end else if(ENABLE6 && is_red) begin
- // Show all the greens with hue 100 > hue < 140
- R_OUT <= 255;
- G_OUT <= 0;
- B_OUT <= 0;
- end else if(ENABLE7 && is_white) begin
- // Show all the greens with hue 100 > hue < 140
- R_OUT <= 0;
- G_OUT <= 0;
- B_OUT <= 0;
- end else if(ENABLE8 && is_yellow) begin
- // Show all the greens with hue 100 > hue < 140
- R_OUT <= 255;
- G_OUT <= 255;
- B_OUT <= 0;
- end else if(ENABLE9 && is_orange) begin
- // Show all the greens with hue 100 > hue < 140
- R_OUT <= 255;
- G_OUT <= 125;
- B_OUT <= 0;
- end else if(~ENABLE3) begin
- R_OUT <= R_IN;
- G_OUT <= G_IN;
- B_OUT <= B_IN;
- end else if((is_blue && x == 100 && y == 100) || (is_blue && x == 101 && y == 100) || (is_blue && x == 100 && y == 101) || (is_blue && x == 101 && y == 101)) begin //
- if(1) begin //is_green ||
- R_OUT <= 255;
- G_OUT <= 0;
- B_OUT <= 220;
- is_blueblock = 1;
- is_blockcolor = 0;//
- end
- end else if ((is_green && x == 100 && y == 100) || (is_green && x == 101 && y == 100) || (is_green && x == 100 && y == 101) || (is_green && x == 101 && y == 101)) begin
- is_greenblock = 1;
- is_blockcolor = 1;
- // end else if ((~is_green && x == 100 && y == 100) && (~is_green && x == 101 && y == 100) && (~is_green && x == 100 && y == 101) && (~is_green && x == 101 && y == 101) || ENABLE16) begin
- // //DEZE MOET UIT
- //
- // if (1) begin //~is_green ||
- // is_greenblock = 0;
- // is_blueblock = 0;
- // is_blockcolor = 2;
- // end
- end else if ((is_red && x == 100 && y == 100) || (is_red && x == 101 && y == 100) || (is_red && x == 100 && y == 101) || (is_red && x == 101 && y == 101)) begin
- is_blockcolor = 2;
- end else if ((is_white && x == 100 && y == 100) || (is_white && x == 101 && y == 100) || (is_white && x == 100 && y == 101) || (is_white && x == 101 && y == 101)) begin
- is_blockcolor = 3;
- end else if ((is_yellow && x == 100 && y == 100) || (is_yellow && x == 101 && y == 100) || (is_yellow && x == 100 && y == 101) || (is_yellow && x == 101 && y == 101)) begin
- is_blockcolor = 4;
- end else if ((is_orange && x == 100 && y == 100) || (is_orange && x == 101 && y == 100) || (is_orange && x == 100 && y == 101) || (is_orange && x == 101 && y == 101)) begin
- is_blockcolor = 5;
- end else if (is_blockcolor == 2 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_greenblock == 0
- //rood betekend kleur groen niet herkend
- R_OUT <= 255;
- G_OUT <= 0;
- B_OUT <= 0;
- end else if (is_blockcolor == 0 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_greenblock && ~is_blueblock
- //blauw
- R_OUT <= 0;
- G_OUT <= 0;
- B_OUT <= 255;
- end else if (is_blockcolor == 1 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
- //groen
- R_OUT <= 0;
- G_OUT <= 255;
- B_OUT <= 0;
- end else if (is_blockcolor == 2 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
- //rood
- R_OUT <= 255;
- G_OUT <= 0;
- B_OUT <= 0;
- end else if (is_blockcolor == 3 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
- //wit
- R_OUT <= 255;
- G_OUT <= 255;
- B_OUT <= 255;
- end else if (is_blockcolor == 4 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
- //geel
- R_OUT <= 255;
- G_OUT <= 255;
- B_OUT <= 0;
- end else if (is_blockcolor == 5 && x < 300 && x > 200 && y < 200 && y > 100) begin //is_blueblock && ~is_greenblock
- //oranje
- R_OUT <= 255;
- G_OUT <= 125;
- B_OUT <= 0;
- end
- // end else if (ENABLE16) begin
- // is_greenblock = 0;
- // end
- // If this pixel is the last of the row
- // (x == max of screen and y == on last pixel of row)
- if(x == VGA_WIDTH - 1 && y % block_size == block_size - 1) begin
- max_count = 0;
- // For every value in the row
- // HIER VALLLEN DE ACTIEVE COLUMNS AAN TE PASSEN
- for (int for_x = 0; for_x < cols; for_x++) begin // for (int for_x = 0; for_x < cols; for_x++) begin
- // If it's bigger than the previous found one
- if(row[for_x] > max_count &&
- row[for_x] > (block_size * block_size / 2)) begin
- // Store it
- max_count = row[for_x];
- max_x = for_x;
- end
- row[for_x] = 0; // Always reset the cell
- end
- max_count_per_row[y_grid] = max_count;
- max_x_per_row[y_grid] = max_x;
- // On the last row
- if(y_grid == rows - 1) begin
- max_count = 0;
- for (int for_y = 0; for_y < rows; for_y++) begin
- if(max_count_per_row[for_y] > max_count) begin
- max_count = max_count_per_row[for_y];
- max_y = for_y;
- max_x = max_x_per_row[for_y];
- end
- end
- // Set the new hotbox for the ball
- BALL_X = max_x;
- BALL_Y = max_y;
- end
- end
- // // If this pixel is the last of the row
- // // (x == max of screen and y == on last pixel of row)
- // if(x == VGA_WIDTH - 1 && y % block_size == block_size - 1) begin
- // max_count = 0;
- //
- // // For every value in the row
- // // HIER VALLLEN DE ACTIEVE COLUMNS AAN TE PASSEN
- // for (int for_x = 10; for_x < 20; for_x++) begin // for (int for_x = 0; for_x < cols; for_x++) begin
- // // If it's bigger than the previous found one
- // if(row[for_x] > max_count &&
- // row[for_x] > (block_size * block_size / 2)) begin
- // // Store it
- // max_count = row[for_x];
- // max_x = for_x;
- // end
- //
- // row[for_x] = 0; // Always reset the cell
- // end
- //
- // max_count_per_row[y_grid] = max_count;
- // max_x_per_row[y_grid] = max_x;
- //
- // // On the last row
- // if(y_grid == rows - 1) begin
- // max_count = 0;
- //
- // for (int for_y = 0; for_y < rows; for_y++) begin
- // if(max_count_per_row[for_y] > max_count) begin
- // max_count = max_count_per_row[for_y];
- // max_y = for_y;
- // max_x = max_x_per_row[for_y];
- // end
- // end
- //
- // // Set the new hotbox for the ball
- // BALL_X = max_x;
- // BALL_Y = max_y;
- // end
- // end
- end else begin
- R_OUT <= 255;
- G_OUT <= 255;
- B_OUT <= 255;
- end
- end
- end
- endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement