/** ****************************************************************************** * @file palette.c * @author fire * @version V1.0 * @date 2016-xx-xx * @brief 触摸画板应用函数 ****************************************************************************** * @attention * * 实验平台:野火 STM32 H750 开发板 * 论坛 :http://www.firebbs.cn * 淘宝 :http://firestm32.taobao.com * ****************************************************************************** */ #include "./touch/palette.h" #include "./touch/bsp_touch_gtxx.h" #include "./lcd/bsp_lcd.h" extern uint32_t ActiveLayer; extern LCD_DrawPropTypeDef DrawProp[MAX_LAYER_NUMBER]; /*按钮结构体数组*/ Touch_Button button[BUTTON_NUM]; /*画笔参数*/ Brush_Style brush; static void Draw_Color_Button(void *btn); static void Draw_Clear_Button(void *btn); static void Draw_Shape_Button(void *btn); static void Command_Select_Color(void *btn); static void Command_Select_Brush(void *btn); static void Command_Clear_Palette(void *btn); static void LCD_DrawUniLineCircle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint8_t thick ); /** * @brief Palette_Init 画板初始化 * @param 无 * @retval 无 */ void Palette_Init(void) { uint8_t i; /* 整屏清为白色 */ LCD_Clear(LCD_COLOR_WHITE); /* 清屏,显示全黑 */ /* 初始化按钮 */ Touch_Button_Init(); /* 描绘按钮 */ for(i=0;i=button[i].start_y && x>=button[i].start_x ) { if(button[i].touch_flag == 0) /*原本的状态为没有按下,则更新状态*/ { button[i].touch_flag = 1; /* 记录按下标志 */ button[i].draw_btn(&button[i]); /*重绘按钮*/ } } else if(button[i].touch_flag == 1) /* 触摸移出了按键的范围且之前有按下按钮 */ { button[i].touch_flag = 0; /* 清除按下标志,判断为误操作*/ button[i].draw_btn(&button[i]); /*重绘按钮*/ } } } /** * @brief Touch_Button_Up 按键被释放时调用的函数,由触摸屏调用 * @param x 触摸最后释放时的x坐标 * @param y 触摸最后释放时的y坐标 * @retval 无 */ void Touch_Button_Up(uint16_t x,uint16_t y) { uint8_t i; for(i=0;ibutton[i].start_x && ybutton[i].start_y)) { button[i].touch_flag = 0; /*释放触摸标志*/ button[i].draw_btn(&button[i]); /*重绘按钮*/ button[i].btn_command(&button[i]); /*执行按键的功能命令*/ break; } } } /** * @brief Draw_Trail 在画板区域描绘触摸轨迹 * @param pre_x 上一点的x坐标 * @param pre_y 上一点的y坐标 * @param x 最新一点的x坐标 * @param y 最新一点的y坐标 * @param brush 画刷参数 * @retval 无 */ void Draw_Trail(int16_t pre_x,int16_t pre_y,int16_t x,int16_t y,Brush_Style* brush) { /*设置画板区域为活动窗口,bsp_lcd.c驱动还没有这样的函数,用于限制绘画窗口*/ // RA8875_SetActiveWindow(PALETTE_START_X,PALETTE_START_Y,PALETTE_END_X,PALETTE_END_Y); /*触摸位置在画板区域*/ if(x>PALETTE_START_X && pre_x>PALETTE_START_X ) { switch(brush->shape) /*根据画刷参数描绘不同的轨迹*/ { /* 描绘1像素宽度的轨迹线 */ case LINE_SINGLE_PIXCEL: if(pre_x< 0 || pre_y < 0) //新的笔迹 { LCD_DrawPixel(x, y, DrawProp[ActiveLayer].TextColor); } else //继续上一次的笔迹 { LCD_DrawLine(pre_x,pre_y,x,y); } break; case LINE_2_PIXCEL: if(x-1LCD_PIXEL_WIDTH || x-20<0 || //液晶左右边界 y+20>LCD_PIXEL_HEIGHT || y-20<0) //液晶上下边界 break; // if(x>PALETTE_START_X+20) { LCD_SetColors(LCD_COLOR_WHITE,LCD_COLOR_WHITE); LCD_FillRect( x-40/2, y-40/2, 40, 40); } break; } } /*退出局限画板的绘图窗口,bsp_lcd.c驱动还没有这样的函数,用于限制绘画窗口*/ // RA8875_SetActiveWindow(0,0,LCD_PIXEL_WIDTH,LCD_PIXEL_HEIGHT); } /** * @brief Draw_Color_Button 颜色按钮的描绘函数 * @param btn Touch_Button 类型的按键参数 * @retval 无 */ static void Draw_Color_Button(void *btn) { Touch_Button *ptr = (Touch_Button *)btn; /*释放按键*/ if(ptr->touch_flag == 0) { /*背景为功能键相应的颜色*/ LCD_SetColors(ptr->para,LCD_COLOR_WHITE); LCD_FillRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); } else /*按键按下*/ { /*白色背景*/ LCD_SetColors(LCD_COLOR_WHITE,LCD_COLOR_WHITE); LCD_FillRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); } /*按钮边框*/ LCD_SetColors(LCD_COLOR_DARKBLUE,LCD_COLOR_WHITE); LCD_DrawRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); } /** * @brief Draw_Clear_Button 清屏按钮的描绘函数 * @param btn Touch_Button 类型的按键参数 * @retval 无 */ static void Draw_Clear_Button(void *btn) { Touch_Button *ptr = (Touch_Button *)btn; /*释放按键*/ if(ptr->touch_flag == 0) { LCD_SetColors(LCD_COLOR_LIGHTGRAY,LCD_COLOR_WHITE); LCD_FillRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); LCD_SetColors(LCD_COLOR_RED,LCD_COLOR_LIGHTGRAY); /*选择字体,使用中英文显示时,尽量把英文选择成16*24的字体, *中文字体大小是24*24的,需要其它字体请自行制作字模*/ /*这个函数只对英文字体起作用*/ LCD_SetFont(&LCD_DEFAULT_FONT); LCD_DispString_EN_CH( (ptr->end_y - ptr->start_y)/2 + ptr->start_y - 12, (ptr->end_x - ptr->start_x)/2 - 6, (uint8_t*)"C");//清屏 } else /*按键按下*/ { LCD_SetColors(LCD_COLOR_WHITE,LCD_COLOR_WHITE); LCD_FillRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); LCD_SetColors(LCD_COLOR_RED,LCD_COLOR_WHITE); /*选择字体,使用中英文显示时,尽量把英文选择成16*24的字体, *中文字体大小是24*24的,需要其它字体请自行制作字模*/ /*这个函数只对英文字体起作用*/ LCD_SetFont(&LCD_DEFAULT_FONT); // LCD_DispString_EN_CH( ptr->start_y+25, // ptr->start_x + (ptr->end_x - ptr->start_x - 24*2 )/2, // (uint8_t*)"清屏"); } /*按钮边框*/ LCD_SetColors(LCD_COLOR_DARKBLUE,LCD_COLOR_WHITE); LCD_DrawRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); } /** * @brief Draw_Shape_Button 笔刷按钮的描绘函数 * @param btn Touch_Button 类型的按键参数 * @retval 无 */ static void Draw_Shape_Button(void *btn) { Touch_Button *ptr = (Touch_Button *)btn; uint16_t i; /* 背景颜色 没按下时为灰色,按下时为白色*/ if(ptr->touch_flag ==0 ) { LCD_SetColors(LCD_COLOR_LIGHTGRAY,LCD_COLOR_WHITE); LCD_FillRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); /*显示文字时的背景颜色*/ LCD_SetColors(LCD_COLOR_DARKBLUE,LCD_COLOR_LIGHTGRAY); LCD_DrawRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); } else { LCD_SetColors(LCD_COLOR_WHITE,LCD_COLOR_WHITE); LCD_FillRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); /*显示文字时的背景颜色*/ LCD_SetColors(LCD_COLOR_DARKBLUE,LCD_COLOR_WHITE); LCD_DrawRect(ptr->start_x, ptr->start_y, ptr->end_x - ptr->start_x, ptr->end_y - ptr->start_y); } LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_WHITE); /*根据画刷形状描绘按钮图案*/ switch(ptr->para) { case LINE_SINGLE_PIXCEL: LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_WHITE); LCD_DrawLine(ptr->start_x+20, ptr->start_y+(ptr->end_y-ptr->start_y)/2, ptr->end_x-20, ptr->start_y+(ptr->end_y-ptr->start_y)/2); break; case LINE_2_PIXCEL: LCD_DrawUniLineCircle(ptr->start_x+20, ptr->start_y+(ptr->end_y-ptr->start_y)/2,ptr->end_x-20, ptr->start_y+(ptr->end_y-ptr->start_y)/2, 1); break; case LINE_4_PIXCEL: LCD_DrawUniLineCircle(ptr->start_x+20, ptr->start_y+(ptr->end_y-ptr->start_y)/2,ptr->end_x-20, ptr->start_y+(ptr->end_y-ptr->start_y)/2, 2); break; case LINE_6_PIXCEL: LCD_DrawUniLineCircle(ptr->start_x+20, ptr->start_y+(ptr->end_y-ptr->start_y)/2,ptr->end_x-20, ptr->start_y+(ptr->end_y-ptr->start_y)/2, 3); break; case LINE_8_PIXCEL: LCD_DrawUniLineCircle(ptr->start_x+20, ptr->start_y+(ptr->end_y-ptr->start_y)/2,ptr->end_x-20, ptr->start_y+(ptr->end_y-ptr->start_y)/2, 4); break; case LINE_16_PIXCEL: LCD_DrawUniLineCircle(ptr->start_x+20, ptr->start_y+(ptr->end_y-ptr->start_y)/2,ptr->end_x-20, ptr->start_y+(ptr->end_y-ptr->start_y)/2, 8 ); break; case LINE_20_PIXCEL: LCD_DrawUniLineCircle(ptr->start_x+20, ptr->start_y+(ptr->end_y-ptr->start_y)/2,ptr->end_x-20, ptr->start_y+(ptr->end_y-ptr->start_y)/2, 10); break; case LINE_WITH_CIRCLE: LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_WHITE); LCD_DrawLine(ptr->start_x+5, ptr->start_y+(ptr->end_y-ptr->start_y)/2, ptr->end_x-5, ptr->start_y+(ptr->end_y-ptr->start_y)/2); for(i=0;i<((ptr->end_x - ptr->start_x-10)/10);i++) { LCD_FillCircle(ptr->start_x+5+i*10, ptr->start_y+(ptr->end_y-ptr->start_y)/2, 3); } break; case RUBBER: // LCD_SetColors(LCD_COLOR_WHITE,LCD_COLOR_BLACK); // LCD_FillRect( ptr->start_x+((ptr->end_x - ptr->start_x -40)/2), // ptr->start_y+ ((ptr->end_y - ptr->start_y-40 -30)/2), // 40, // 40 ); // 画矩形框 LCD_SetColors(LCD_COLOR_RED,LCD_COLOR_LIGHTGRAY); /*选择字体,使用中英文显示时,尽量把英文选择成16*24的字体, *中文字体大小是24*24的,需要其它字体请自行制作字模*/ /*这个函数只对英文字体起作用*/ LCD_SetFont(&Font24); LCD_DispString_EN_CH((ptr->end_y - ptr->start_y)/2 + ptr->start_y - 12, ptr->start_x + (ptr->end_x - ptr->start_x)/2 - 6, (uint8_t*)"R");//橡皮 break; } } /** * @brief Command_Select_Color 切换画刷颜色,颜色按键的功能执行函数 * @param btn Touch_Button 类型的按键参数 * @retval 无 */ static void Command_Select_Color(void *btn) { Touch_Button *ptr = (Touch_Button *)btn; brush.color = ptr->para; LCD_SetColors(brush.color,LCD_COLOR_WHITE); if(brush.shape == RUBBER) { brush.shape = LINE_SINGLE_PIXCEL; } } /** * @brief Command_Select_Brush 切换画刷颜色,画刷按键的功能执行函数 * @param btn Touch_Button 类型的按键参数 * @retval 无 */ static void Command_Select_Brush(void *btn) { Touch_Button *ptr = (Touch_Button *)btn; brush.shape =(SHAPE) ptr->para; LCD_SetColors(brush.color,LCD_COLOR_WHITE); } /** * @brief Command_Select_Brush 切换画刷颜色,清屏按键的功能执行函数 * @param btn Touch_Button 类型的按键参数 * @retval 无 */ static void Command_Clear_Palette(void *btn) { LCD_SetColors(LCD_COLOR_WHITE,LCD_COLOR_WHITE); LCD_FillRect(PALETTE_START_X, PALETTE_START_Y, PALETTE_END_X-(PALETTE_START_X+1), PALETTE_END_Y-PALETTE_START_Y ); } #define ABS(X) ((X) > 0 ? (X) : -(X)) /** * @brief 在两点之间描绘轨迹 * @param x1: specifies the point 1 x position. * @param y1: specifies the point 1 y position. * @param x2: specifies the point 2 x position. * @param y2: specifies the point 2 y position. * @retval None */ static void LCD_DrawUniLineCircle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint8_t thick ) { int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0, yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0, curpixel = 0; deltax = ABS(x2 - x1); /* The difference between the x's */ deltay = ABS(y2 - y1); /* The difference between the y's */ x = x1; /* Start x off at the first pixel */ y = y1; /* Start y off at the first pixel */ if (x2 >= x1) /* The x-values are increasing */ { xinc1 = 1; xinc2 = 1; } else /* The x-values are decreasing */ { xinc1 = -1; xinc2 = -1; } if (y2 >= y1) /* The y-values are increasing */ { yinc1 = 1; yinc2 = 1; } else /* The y-values are decreasing */ { yinc1 = -1; yinc2 = -1; } if (deltax >= deltay) /* There is at least one x-value for every y-value */ { xinc1 = 0; /* Don't change the x when numerator >= denominator */ yinc2 = 0; /* Don't change the y for every iteration */ den = deltax; num = deltax / 2; numadd = deltay; numpixels = deltax; /* There are more x-values than y-values */ } else /* There is at least one y-value for every x-value */ { xinc2 = 0; /* Don't change the x for every iteration */ yinc1 = 0; /* Don't change the y when numerator >= denominator */ den = deltay; num = deltay / 2; numadd = deltax; numpixels = deltay; /* There are more y-values than x-values */ } for (curpixel = 0; curpixel <= numpixels; curpixel++) { //判断边界 if(x+thick>LCD_PIXEL_WIDTH || x-thick<0 || //液晶左右边界 y+thick>LCD_PIXEL_HEIGHT || y-thick<0 ) //液晶上下边界 continue; LCD_FillCircle(x,y,thick); /* Draw the current pixel */ num += numadd; /* Increase the numerator by the top of the fraction */ if (num >= den) /* Check if numerator >= denominator */ { num -= den; /* Calculate the new numerator value */ x += xinc1; /* Change the x as appropriate */ y += yinc1; /* Change the y as appropriate */ } x += xinc2; /* Change the x as appropriate */ y += yinc2; /* Change the y as appropriate */ } } /* ------------------------------------------end of file---------------------------------------- */