void TM_ILI9341_DrawFilledCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) { int16_t f = 1 - r; int16_t ddF_x = 1; int16_t ddF_y = -2 * r; int16_t x = 0; int16_t y = r; TM_ILI9341_DrawPixel(x0, y0 + r, color); TM_ILI9341_DrawPixel(x0, y0 - r, color); TM_ILI9341_DrawPixel(x0 + r, y0, color); TM_ILI9341_DrawPixel(x0 - r, y0, color); TM_ILI9341_DrawLine(x0 - r, y0, x0 + r, y0, color); while (x < y) { if (f >= 0) { y--; ddF_y += 2; f += ddF_y; } x++; ddF_x += 2; f += ddF_x; TM_ILI9341_DrawLine(x0 - x, y0 + y, x0 + x, y0 + y, color); TM_ILI9341_DrawLine(x0 + x, y0 - y, x0 - x, y0 - y, color); TM_ILI9341_DrawLine(x0 + y, y0 + x, x0 - y, y0 + x, color); TM_ILI9341_DrawLine(x0 + y, y0 - x, x0 - y, y0 - x, color); } }
void TM_ILI9341_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color) { /* Code by dewoller: https://github.com/dewoller */ int16_t dx, dy, sx, sy, err, e2; dx = (x0 < x1) ? (x1 - x0) : (x0 - x1); dy = (y0 < y1) ? (y1 - y0) : (y0 - y1); sx = (x0 < x1) ? 1 : -1; sy = (y0 < y1) ? 1 : -1; err = ((dx > dy) ? dx : -dy) / 2; while (1) { TM_ILI9341_DrawPixel(x0, y0, color); if (x0 == x1 && y0 == y1) { break; } e2 = err; if (e2 > -dx) { err -= dy; x0 += sx; } if (e2 < dy) { err += dx; y0 += sy; } } }
void TM_ILI9341_Putc(uint16_t x, uint16_t y, char c, TM_FontDef_t *font, uint16_t foreground, uint16_t background) { uint32_t i, b, j; if ((ILI9341_x + font->FontWidth) > ILI9341_Opts.Width) { //If at the end of a line of display, go to new line and set x to 0 position ILI9341_y += font->FontHeight; ILI9341_x = 0; } for (i = 0; i < font->FontHeight; i++) { b = font->data[(c - 32) * font->FontHeight + i]; for (j = 0; j < font->FontWidth; j++) { if ((b << j) & 0x8000) { TM_ILI9341_DrawPixel(ILI9341_x + j, (ILI9341_y + i), foreground); } else { TM_ILI9341_DrawPixel(ILI9341_x + j, (ILI9341_y + i), background); } } } ILI9341_x += font->FontWidth; }
ErrorStatus TM_ILI9341_Button_Draw(uint8_t id) { uint16_t fontWidth, fontHeight, x, y, i, j; if ((TM_ILI9341_Buttons[id].flags & TM_BUTTON_FLAG_USED) == 0) { //Button not enabled return ERROR; } //Draw label if (TM_ILI9341_Buttons[id].flags & TM_BUTTON_FLAG_IMAGE) { //Draw picture for (i = 0; i < TM_ILI9341_Buttons[id].width; i++) { for (j = 0; j < TM_ILI9341_Buttons[id].height; j++) { TM_ILI9341_DrawPixel( TM_ILI9341_Buttons[id].x + i, TM_ILI9341_Buttons[id].y + j, *(uint16_t *)(TM_ILI9341_Buttons[id].image + j * TM_ILI9341_Buttons[id].width + i) ); } } } else { //Background TM_ILI9341_DrawFilledRectangle( TM_ILI9341_Buttons[id].x, TM_ILI9341_Buttons[id].y, TM_ILI9341_Buttons[id].x + TM_ILI9341_Buttons[id].width, TM_ILI9341_Buttons[id].y + TM_ILI9341_Buttons[id].height, TM_ILI9341_Buttons[id].background ); } //Display label if ((TM_ILI9341_Buttons[id].flags & TM_BUTTON_FLAG_NOLABEL) == 0) { TM_ILI9341_GetStringSize(TM_ILI9341_Buttons[id].label, TM_ILI9341_Buttons[id].font, &fontWidth, &fontHeight); x = TM_ILI9341_Buttons[id].x + TM_ILI9341_Buttons[id].width / 2 - (fontWidth / 2); y = TM_ILI9341_Buttons[id].y + TM_ILI9341_Buttons[id].height / 2 - (fontHeight / 2); TM_ILI9341_Puts(x, y, TM_ILI9341_Buttons[id].label, TM_ILI9341_Buttons[id].font, TM_ILI9341_Buttons[id].color, TM_ILI9341_Buttons[id].background); } //Border if ((TM_ILI9341_Buttons[id].flags & TM_BUTTON_FLAG_NOBORDER) == 0) { //Border enabled TM_ILI9341_DrawRectangle( TM_ILI9341_Buttons[id].x, TM_ILI9341_Buttons[id].y, TM_ILI9341_Buttons[id].x + TM_ILI9341_Buttons[id].width, TM_ILI9341_Buttons[id].y + TM_ILI9341_Buttons[id].height, TM_ILI9341_Buttons[id].borderColor ); } return SUCCESS; }
int main(void) { char str[30]; /* Create TouchData struct */ TM_STMPE811_TouchData touchData; /* Initialize system */ SystemInit(); /* Initialize LCD */ TM_ILI9341_Init(); /* Fill with orange color */ TM_ILI9341_Fill(ILI9341_COLOR_ORANGE); /* Rotate LCD */ TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); /* Initialize Touch */ if (TM_STMPE811_Init() != TM_STMPE811_State_Ok) { TM_ILI9341_Puts(20, 20, "STMPE811 Error", &TM_Font_11x18, ILI9341_COLOR_ORANGE, ILI9341_COLOR_BLACK); while (1); } /* Select touch screen orientation */ touchData.orientation = TM_STMPE811_Orientation_Portrait_2; /* Print some text */ TM_ILI9341_Puts(20, 20, "Press on LCD", &TM_Font_11x18, ILI9341_COLOR_ORANGE, ILI9341_COLOR_BLACK); TM_ILI9341_Puts(93, 310, "stm32f4-discovery.com", &TM_Font_7x10, ILI9341_COLOR_GREEN, ILI9341_COLOR_BLACK); while (1) { if (TM_STMPE811_ReadTouch(&touchData) == TM_STMPE811_State_Pressed) { /* Touch valid */ sprintf(str, "Pressed \n\nX: %03d\nY: %03d", touchData.x, touchData.y); TM_ILI9341_Puts(20, 80, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); /* Draw pixel on touch location */ TM_ILI9341_DrawPixel(touchData.x, touchData.y, 0x0000); } else { sprintf(str, "Not Pressed\n\n \n "); TM_ILI9341_Puts(20, 80, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); } } }
int main(void) { char str[30]; //Create TouchData struct TM_STMPE811_TouchData touchData; //Initialize system SystemInit(); //Initialize LCD TM_ILI9341_Init(); //Fill with orange color TM_ILI9341_Fill(ILI9341_COLOR_ORANGE); //Rotate LCD TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); //Initialize Touch TM_STMPE811_Init(); //Select touch screen orientation touchData.orientation = TM_STMPE811_Orientation_Portrait_2; //Print some text TM_ILI9341_Puts(20, 20, "Press on LCD", &TM_Font_11x18, ILI9341_COLOR_ORANGE, ILI9341_COLOR_BLACK); TM_ILI9341_Puts(168, 308, "majerle.eu", &TM_Font_7x10, ILI9341_COLOR_GREEN, ILI9341_COLOR_BLACK); while (1) { if (TM_STMPE811_ReadTouch(&touchData) == TM_STMPE811_State_Pressed) { //Touch valid sprintf(str, "Pressed \n\nX: %03d\nY: %03d", touchData.x, touchData.y); TM_ILI9341_Puts(20, 80, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); TM_ILI9341_DrawPixel(touchData.x, touchData.y, 0x0000); } else { sprintf(str, "Not Pressed\n\n \n "); TM_ILI9341_Puts(20, 80, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_ORANGE); } } }
void TM_ILI9341_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color) { short dx, dy; short temp; if (x0 > x1) { temp = x1; x1 = x0; x0 = temp; } if (y0 > y1) { temp = y1; y1 = y0; y0 = temp; } dx = x1 - x0; dy = y1 - y0; if (dx == 0) { do { TM_ILI9341_DrawPixel(x0, y0, color); y0++; } while (y1 >= y0); return; } if (dy == 0) { do { TM_ILI9341_DrawPixel(x0, y0, color); x0++; } while (x1 >= x0); return; } /* Based on Bresenham's line algorithm */ if (dx > dy) { temp = 2 * dy - dx; while (x0 != x1) { TM_ILI9341_DrawPixel(x0, y0, color); x0++; if (temp > 0) { y0++; temp += 2 * dy - 2 * dx; } else { temp += 2 * dy; } } TM_ILI9341_DrawPixel(x0, y0, color); } else { temp = 2 * dx - dy; while (y0 != y1) { TM_ILI9341_DrawPixel(x0, y0, color); y0++; if (temp > 0) { x0++; temp += 2 * dy - 2 * dx; } else { temp += 2 * dy; } } TM_ILI9341_DrawPixel(x0, y0, color); } }