I am trying to draw a Checkerboard pattern on a LCD using a GUI library called emWin. I have actually managed to draw it using the following code. But having this many loops in the program body for a single task, that too in the internal flash of the microcontroller is not a good idea.
For those who have not worked with emWin, I will try and explain a few things before we go for the actual logic.
GUI_RECT
is a structure which id define source files of emWin and I am blind to it.Rect
,Rect2
,Rect3
.. and so on untilRect10
are objects.- Elements of the
Rect
array are{x0,y0,x1,y1}
, wherex0
,y0
are starting locations of the rectangle in the X-Y plane and x1, y1 are end locations of the Rectangle in the X-Y plane.
Rect={0,0,79,79}
is a rectangle that starts at the top left of the LCD and is up to (79,79), so it's basically a square.
- The function
GUI_setBkColor(int color);
sets the color of the background. - The function
GUI_setColor(int color);
sets the color of the foreground. GUI_WHITE
andDM_CHECKERBOARD_COLOR
are two color values,#define
edGUI_FillRectEx(&Rect);
will draw the rectangle.
The code below works fine but I want to make it smarter.
GUI_RECT Rect = {0, 0, 79, 79};
GUI_RECT Rect2 = {80, 0, 159, 79};
GUI_RECT Rect3 = {160, 0, 239, 79};
GUI_RECT Rect4 = {240, 0, 319, 79};
GUI_RECT Rect5 = {320, 0, 399, 79};
GUI_RECT Rect6 = {400, 0, 479, 79};
GUI_RECT Rect7 = {480, 0, 559, 79};
GUI_RECT Rect8 = {560, 0, 639, 79};
GUI_RECT Rect9 = {640, 0, 719, 79};
GUI_RECT Rect10 = {720, 0, 799, 79};
WM_SelectWindow(Win_DM_Main);
GUI_SetBkColor(GUI_BLACK);
GUI_Clear();
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(GUI_WHITE);
else
GUI_SetColor(DM_CHECKERBOARD_COLOR);
GUI_FillRectEx(&Rect);
Rect.y0 += 80;
Rect.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(DM_CHECKERBOARD_COLOR);
else
GUI_SetColor(GUI_WHITE);
GUI_FillRectEx(&Rect2);
Rect2.y0 += 80;
Rect2.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(GUI_WHITE);
else
GUI_SetColor(DM_CHECKERBOARD_COLOR);
GUI_FillRectEx(&Rect3);
Rect3.y0 += 80;
Rect3.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(DM_CHECKERBOARD_COLOR);
else
GUI_SetColor(GUI_WHITE);
GUI_FillRectEx(&Rect4);
Rect4.y0 += 80;
Rect4.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(GUI_WHITE);
else
GUI_SetColor(DM_CHECKERBOARD_COLOR);
GUI_FillRectEx(&Rect5);
Rect5.y0 += 80;
Rect5.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(DM_CHECKERBOARD_COLOR);
else
GUI_SetColor(GUI_WHITE);
GUI_FillRectEx(&Rect6);
Rect6.y0 += 80;
Rect6.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(GUI_WHITE);
else
GUI_SetColor(DM_CHECKERBOARD_COLOR);
GUI_FillRectEx(&Rect7);
Rect7.y0 += 80;
Rect7.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(DM_CHECKERBOARD_COLOR);
else
GUI_SetColor(GUI_WHITE);
GUI_FillRectEx(&Rect8);
Rect8.y0 += 80;
Rect8.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(GUI_WHITE);
else
GUI_SetColor(DM_CHECKERBOARD_COLOR);
GUI_FillRectEx(&Rect9);
Rect9.y0 += 80;
Rect9.y1 += 80;
}
for(i = 0; i < 6; i++)
{
if(i%2 == 0)
GUI_SetColor(DM_CHECKERBOARD_COLOR);
else
GUI_SetColor(GUI_WHITE);
GUI_FillRectEx(&Rect10);
Rect10.y0 += 80;
Rect10.y1 += 80;
}