2020-03-25 07:54 AM
Here my solution to create a slidebar on this evaluation board, it should run also in other STM32 modules.
//######################################################################################
//# Slide bar
//######################################################################################
#define SLIDE_X (400)
#define SLIDE_Y (20)
#define EMPTY_Y (15)
typedef struct slidebar_tag_Struct {
uint16_t slide_start_x; // to set
uint16_t slide_start_y; // to set
uint16_t slide_empty_y; // to set (diff from top image to top slide)
uint16_t first; // to set to 1
uint32_t width_slide; // to set to 0
uint32_t height_slide; // to set to 0
uint32_t width_cursor; // to set to 0
uint32_t height_cursor; // to set to 0
int prev_cursor_Ypos; // to set to 0
float Ymin;
float Ymax;
int save_curr_x;
int save_curr_y;
uint16_t cursor_start_x;
uint16_t cursor_end_x;
uint16_t cursor_start_y;
uint16_t cursor_end_y;
uint32_t index;
uint32_t bit_pixel;
uint16_t curr_pos;
uint32_t * save_area;
} slidebar_Struct;
slidebar_Struct slidestr1 = { SLIDE_X, SLIDE_Y, EMPTY_Y, 1, 0, 0, 0, 0, 0 };
slidebar_Struct slidestr2 = { SLIDE_X - 60, SLIDE_Y, EMPTY_Y, 1, 0, 0, 0, 0, 0 };
void BSP_LCD_CreateSlideBar(slidebar_Struct *, uint8_t *, uint8_t *, int, uint16_t);
> 2 parameter the pointer to the slibar image
> 3 parameter the pointer to the cursor image
void BSP_LCD_CreateSlideBar(slidebar_Struct * s, uint8_t * slide, uint8_t * cursor, int pos, uint16_t perc)
{
//static uint16_t first = 1;
uint16_t i, m, k;
float Xcursor, Ycursor;
if (s->first == 1)
{
/* Read slide bitmap width */
s->width_slide = *(uint16_t *) (slide + 18);
s->width_slide |= (*(uint16_t *) (slide + 20)) << 16;
/* Read slide bitmap height */
s->height_slide = *(uint16_t *) (slide + 22);
s->height_slide |= (*(uint16_t *) (slide + 24)) << 16;
/* Read slide bitmap width */
s->width_cursor = *(uint16_t *) (cursor + 18);
s->width_cursor |= (*(uint16_t *) (cursor + 20)) << 16;
/* Read cursor bitmap height */
s->height_cursor = *(uint16_t *) (cursor + 22);
s->height_cursor |= (*(uint16_t *) (cursor + 24)) << 16;
/* Get bitmap data address offset */
s->index = cursor[10] + (cursor[11] << 8) + (cursor[12] << 16) + (cursor[13] << 24);
/* Read bit/pixel */
s->bit_pixel = cursor[28] + (cursor[29] << 8);
/* Calculate the pixel range of cursor */
s->Ymin = s->slide_start_y + s->slide_empty_y - (s->height_cursor / 2);
s->Ymax = s->Ymin + (s->height_slide - (2 * s->slide_empty_y));
/* Drar slidebar without cursor */
BSP_LCD_DrawBitmap(s->slide_start_x, s->slide_start_y, slide);
s->first = 0;
}
else
{
/* Restore original area under the cursor */
for ( i = k = 0; i < s->height_cursor; i++)
{
for ( m = 0; m < s->width_cursor; m++, k++)
{
BSP_LCD_DrawPixel(s->save_curr_x + m, s->save_curr_y + i, s->save_area[k]);
}
}
/* free original save area */
free(s->save_area);
}
/* Calculate the new cursor position */
Xcursor = s->slide_start_x + (s->width_slide / 2) - (s->width_cursor / 2);
/* If has been specified a percentage */
if (pos == 0 && perc <= 100)
{
Ycursor = s->slide_start_y + s->slide_empty_y - (s->height_cursor/2);
Ycursor += (perc / 100.0) * (s->height_slide - (2 * s->slide_empty_y));
}
else /* If has been specified a pixel step */
{
Ycursor = (uint16_t)(s->save_curr_y + pos);
if (Ycursor < s->Ymin || Ycursor > s->Ymax)
Ycursor = (uint16_t)(s->save_curr_y);
uint16_t rel1 = (uint16_t)Ycursor - s->slide_start_y - s->slide_empty_y + (s->height_cursor/2);
s->curr_pos = (uint16_t)(100.0 * (rel1 / (float)(s->Ymax - s->Ymin)));
//LCD_LOG_State("curr %d\%", s->curr_pos);
}
/* Save the area under the new cursor */
s->save_curr_x = (int)Xcursor;
s->save_curr_y = (int)Ycursor;
s->save_area = calloc(s->height_cursor * s->width_cursor, sizeof(uint32_t));
if (s->save_area == NULL)
{
LCD_LOG_State("BSP_LCD_CreateSlideBar no memory");
return;
}
for ( i = k = 0; i < s->height_cursor; i++)
{
for ( m = 0; m < s->width_cursor; m++, k++)
{
s->save_area[k] = BSP_LCD_ReadPixel((uint16_t)Xcursor + m, (uint16_t)Ycursor + i);
}
}
/* Draw the cursor in the new position */
BSP_LCD_DrawBitmap((uint16_t)Xcursor, (uint16_t)Ycursor, cursor);
#define ENLANGE_TOUCH_AREA (20)
/* Calculate the touchscreen area for the cursor */
s->cursor_start_x = (uint16_t)Xcursor - ENLANGE_TOUCH_AREA;
s->cursor_end_x = (uint16_t)Xcursor + s->width_cursor + ENLANGE_TOUCH_AREA;
s->cursor_start_y = (uint16_t)Ycursor - ENLANGE_TOUCH_AREA;
s->cursor_end_y = (uint16_t)Ycursor + s->height_cursor + ENLANGE_TOUCH_AREA;
}
void BSP_Pointer_Update(void)
{
static TS_StateTypeDef prev_state; /*previous touch state from the touch sensor used from BSP package*/
TS_StateTypeDef ts; /*actual touch state from the touch sensor used from BSP package*/
int xDiff, yDiff; /*difference in postitions between touch states*/
BSP_TS_GetState(&ts);
xDiff = ts.touchX[0] - prev_state.touchX[0];
yDiff = ts.touchY[0] - prev_state.touchY[0];
if (prev_state.touchDetected != ts.touchDetected)
{
prev_state.touchDetected = ts.touchDetected;
if ((ts.touchX[0] != 0) && (ts.touchY[0] != 0))
{
prev_state.touchX[0] = ts.touchX[0];
prev_state.touchY[0] = ts.touchY[0];
}
}
else if (yDiff > 5 || yDiff < 5)
{
if (ts.touchX[0] > slidestr1.cursor_start_x && ts.touchX[0] < slidestr1.cursor_end_x &&
ts.touchY[0] > slidestr1.cursor_start_y && ts.touchY[0] < slidestr1.cursor_end_y)
{
if (yDiff < 100 && yDiff > -100 && yDiff != 0)
{
BSP_LCD_CreateSlideBar(&slidestr1, (uint8_t *)(&slide1), (uint8_t *)(&cursor1), yDiff, 0);
LCD_LOG_State("Cursor 1 now at position %d%%", slidestr1.curr_pos);
}
}
if (ts.touchX[0] > slidestr2.cursor_start_x && ts.touchX[0] < slidestr2.cursor_end_x &&
ts.touchY[0] > slidestr2.cursor_start_y && ts.touchY[0] < slidestr2.cursor_end_y)
{
if (yDiff < 100 && yDiff > -100 && yDiff != 0)
{
BSP_LCD_CreateSlideBar(&slidestr2, (uint8_t *)(&slide1), (uint8_t *)(&cursor1), yDiff, 0);
LCD_LOG_State("Cursor 2 now at position %d%%", slidestr2.curr_pos);
}
}
if ((ts.touchX[0] != 0) && (ts.touchY[0] != 0))
{
prev_state.touchX[0] = ts.touchX[0];
prev_state.touchY[0] = ts.touchY[0];
}
//TS_State.Layer = 0;
//TS_State.x = prev_state.touchX[0];
//TS_State.y = prev_state.touchY[0];
//GUI_TOUCH_StoreStateEx(&TS_State);
}