Hello,
I'm working on a custom widget that would use motion (sort of horizontal listwheel or swipelist).
And I'd like to reduce time for snapping the items to the snap position just like listwheel widget does .
As far as I understand there is a variable Period in WM_MOTION_INFO structure that controls motion time depending on the start moving force by a PID.
When I start moving the variable Period need to be a quite big value for a long time motion.
When I want to stop my widget by a PID the same variable Period also makes a long snapping time before final stop.
I tried to measure the speed in WM_TIMER and change Period value in WM_MOTION_MOVE when speed is quite low but it is not working.
Apparently Period value need to be changed only in WM_MOTION_INIT.
Here is my sample code:
Display All
What could be done to get it right?
Thank you.
Alex.
I'm working on a custom widget that would use motion (sort of horizontal listwheel or swipelist).
And I'd like to reduce time for snapping the items to the snap position just like listwheel widget does .
As far as I understand there is a variable Period in WM_MOTION_INFO structure that controls motion time depending on the start moving force by a PID.
When I start moving the variable Period need to be a quite big value for a long time motion.
When I want to stop my widget by a PID the same variable Period also makes a long snapping time before final stop.
I tried to measure the speed in WM_TIMER and change Period value in WM_MOTION_MOVE when speed is quite low but it is not working.
Apparently Period value need to be changed only in WM_MOTION_INIT.
Here is my sample code:
C Source Code
- #include "DIALOG.h"
- #include "stdio.h"
- WM_HWIN hWin;
- LISTWHEEL_Handle hLstwhl;
- int i, xPos;
- char str[2];
- // Callback for the desktop window
- void _cbBk(WM_MESSAGE * pMsg) {
- int i;
- GUI_RECT Rect = { 0, 150, 30, 451 };
- switch (pMsg->MsgId) {
- case WM_PAINT:
- GUI_SetBkColor(GUI_DARKGRAY);
- GUI_Clear();
- // Horizontal listwheel text
- GUI_SetColor(GUI_WHITE);
- GUI_SetFont(GUI_FONT_20_ASCII);
- GUI_DispStringHCenterAt("Custom horizontal listwheel (wrap off)", 200, 10);
- GUI_SetFont(GUI_FONT_16_ASCII);
- GUI_DispStringHCenterAt("Snap points", 200, 30);
- // Draw snap markers for horizontal listwheel
- for (i = 60; i <= 360; i += 50)
- GUI_DrawVLine(i, 52, 57);
- // Draw arrow
- GUI_DrawLine(365, 85, 375, 80);
- GUI_DrawLine(365, 85, 375, 90);
- GUI_DrawHLine(85, 365, 400);
- // Some notes about the problem
- GUI_DispStringAt("When accelerating it to long time rotation by a PID and then stop,", 410, 77);
- GUI_DispStringAt("then items are snapping with the same period as of they are starting rotation (quite slow)", 410, 95);
- // Listwheel by EmWin text
- GUI_SetFont(GUI_FONT_20_ASCII);
- GUI_DispStringInRectEx("Listwheel by EmWin", &Rect, GUI_TA_HCENTER | GUI_TA_BOTTOM, 18, GUI_ROTATE_CCW);
- Rect.x0 = 32;
- Rect.x1 = 47;
- GUI_SetFont(GUI_FONT_16_ASCII);
- GUI_DispStringInRectEx("Snap points", &Rect, GUI_TA_HCENTER | GUI_TA_BOTTOM, 11, GUI_ROTATE_CCW);
- // Draw snap markers
- for (i = 150; i <= 450; i += 50)
- GUI_DrawHLine(i, 52, 57);
- // Draw arrow
- GUI_DrawLine(115, 300, 125, 295);
- GUI_DrawLine(115, 300, 125, 305);
- GUI_DrawHLine(300, 115, 150);
- // Some notes about my goal as listwheel by EmWin does
- GUI_DispStringAt("When accelerating it to long time rotation by a PID and then stop,", 160, 292);
- GUI_DispStringAt("then items are snapping quite fast", 160, 310);
- break;
- default:
- WM_DefaultProc(pMsg);
- break;
- }
- }
- // Callback for the horizontal listwheel
- void _cbWin(WM_MESSAGE * pMsg) {
- GUI_RECT Rect;
- int i;
- char str[10];
- WM_MOTION_INFO* pInfo;
- switch (pMsg->MsgId) {
- case WM_MOTION:
- pInfo = (WM_MOTION_INFO*)pMsg->Data.p;
- switch (pInfo->Cmd) {
- case WM_MOTION_INIT:
- // Start motion, set snap, overlap and period values
- pInfo->Flags = WM_CF_MOTION_X | WM_MOTION_MANAGE_BY_WINDOW;
- pInfo->SnapX = 50;
- pInfo->Overlap = 25;
- pInfo->Period = 1000;
- break;
- case WM_MOTION_MOVE:
- // Some code when motion is acting (limiting and stopping the motion)
- xPos += pInfo->dx;
- if (pInfo->IsDragging)
- pInfo->IsOutside = (xPos < -4720) | (xPos > 0);
- if (xPos < -4720) {
- xPos = -4720;
- pInfo->StopMotion = pInfo->IsDragging ^ 1;
- }
- if (xPos > 20) {
- xPos = 20;
- pInfo->StopMotion = pInfo->IsDragging ^ 1;
- }
- // Call WM_PAINT
- WM_Invalidate(pMsg->hWin);
- break;
- case WM_MOTION_GETPOS:
- pInfo->xPos = xPos;
- break;
- }
- break;
- case WM_PAINT:
- // Draw horizontal listwheel with 100 items depending on motion position
- GUI_SetBkColor(GUI_RED);
- GUI_Clear();
- Rect.x0 = xPos;
- Rect.x1 = xPos + 50;
- Rect.y0 = 0;
- Rect.y1 = 49;
- GUI_SetColor(GUI_WHITE);
- GUI_SetFont(GUI_FONT_20_ASCII);
- for (i = 1; i <= 100; i++) {
- GUI_DrawRectEx(&Rect);
- sprintf(str, "%d", i);
- GUI_DispStringInRect(str, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER);
- Rect.x0 += 50;
- Rect.x1 += 50;
- }
- break;
- default:
- WM_DefaultProc(pMsg);
- break;
- }
- }
- // Some additional draw on the listwheel items
- static int _LISTWHEEL_CustomDraw(const WIDGET_ITEM_DRAW_INFO* pDrawItemInfo) {
- switch (pDrawItemInfo->Cmd) {
- case WIDGET_ITEM_DRAW:
- LISTWHEEL_OwnerDraw(pDrawItemInfo);
- GUI_SetColor(GUI_WHITE);
- GUI_DrawRect(pDrawItemInfo->x0, pDrawItemInfo->y0, pDrawItemInfo->x1, pDrawItemInfo->y1 + 1);
- break;
- default:
- return LISTWHEEL_OwnerDraw(pDrawItemInfo);
- break;
- }
- return 0;
- }
- void MainTask(void) {
- GUI_Init();
- WM_MULTIBUF_Enable(1);
- // Set callback for the desktop window
- WM_SetCallback(WM_HBKWIN, _cbBk);
- WM_MOTION_Enable(1);
- // Create a "horizontal listwheel", set callback
- hWin = WM_CreateWindowAsChild(60, 60, 301, 50, WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);
- // Create a listwheel by EmWin, set some parameters
- hLstwhl = LISTWHEEL_CreateEx(60, 150, 50, 301, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_LISTWHEEL0, NULL);
- LISTWHEEL_SetLineHeight(hLstwhl, 50);
- LISTWHEEL_SetFont(hLstwhl, GUI_FONT_20_ASCII);
- LISTWHEEL_SetTextAlign(hLstwhl, GUI_TA_HCENTER | GUI_TA_VCENTER);
- LISTWHEEL_SetTextColor(hLstwhl, LISTWHEEL_CI_UNSEL, GUI_WHITE);
- LISTWHEEL_SetTextColor(hLstwhl, LISTWHEEL_CI_SEL, GUI_WHITE);
- LISTWHEEL_SetBkColor(hLstwhl, LISTWHEEL_CI_UNSEL, GUI_RED);
- LISTWHEEL_SetBkColor(hLstwhl, LISTWHEEL_CI_SEL, GUI_RED);
- LISTWHEEL_SetOwnerDraw(hLstwhl, _LISTWHEEL_CustomDraw);
- // Set 100 items
- for (i = 1; i <= 100; i++) {
- sprintf(str, "%d", i);
- LISTWHEEL_AddString(hLstwhl, str);
- }
- while(1) {
- GUI_Delay(50);
- }
- }
Thank you.
Alex.