David Smart
/
PUB_RA875_Dial
Demo touching the perimeter of a circle
Diff: main.cpp
- Revision:
- 2:3077639f94fa
- Parent:
- 1:e4f92c21e329
- Child:
- 3:d259be26795b
diff -r e4f92c21e329 -r 3077639f94fa main.cpp --- a/main.cpp Sun Apr 04 17:17:02 2021 +0000 +++ b/main.cpp Sun Apr 04 20:15:47 2021 +0000 @@ -64,6 +64,95 @@ const dim_t radius = 125; const dim_t touch_tolerance = 30; + +// +// Checkbox to turn on/off the touch-point +// +const rect_t check_tonoff = {400, 150, LCD_W-1, 182}; // Height based on double-size font +const char * check_tonoff_msg = "[%c] Visible touchpoints"; +bool touchVisible = false; // default hidden + + +// +// Radio buttons to select between pie pieces +// and a dial-indent +// +const rect_t radio_pie = {400, 250, LCD_W-1, 282}; +const char * radio_pie_msg = "(%c) Pie-slice rendering"; +const rect_t radio_dial = {400, 300, LCD_W-1, 332}; +const char * radio_dial_msg = "(%c) Dial rendering"; +int renderOption = 0; // pie-slice + +color_t edgeColor = BrightBlue; +color_t fillColor = Black; + +void DrawCircle() { + // Draw a thickened circle + lcd.fillellipse(center.x, center.y, radius, radius, fillColor); + for (int r = 0; r <= 3; r++) { + lcd.ellipse(center.x, center.y, radius + r, radius + r, edgeColor); + } +} + +void ShowUserControls() { + lcd.SetTextFontSize(2); // Bigger font; easier to touch + // + // [X] Enable visible touch-points + // + lcd.foreground(White); + lcd.SetTextCursor(check_tonoff.p1); + lcd.printf(check_tonoff_msg, (touchVisible) ? 'X' : ' '); + lcd.rect(check_tonoff, Gray); + // + // (*) Pie-slice rendering + // ( ) Dial rendering + // + lcd.foreground(White); + lcd.SetTextCursor(radio_pie.p1); + lcd.printf(radio_pie_msg, (renderOption == 0) ? '*' : ' '); + lcd.rect(radio_pie, Gray); + lcd.foreground(White); + lcd.SetTextCursor(radio_dial.p1); + lcd.printf(radio_dial_msg, (renderOption == 1) ? '*' : ' '); + lcd.rect(radio_dial, Gray); + lcd.SetTextFontSize(); +} + +void DrawInitialScreen() { + lcd.cls(); + lcd.foreground(White); // Change to white + lcd.printf("RA8875 Touch Dial Example - Build " __DATE__ " " __TIME__ "\r\n"); + lcd.printf("MBED v%d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); + ShowUserControls(); + DrawCircle(); +} + +void TestToggleHit(point_t t) { + if (lcd.Intersect(check_tonoff, t)) { + touchVisible = !touchVisible; + DrawInitialScreen(); + } +} + +void TestRenderHit(point_t t) { + bool update = false; + if (lcd.Intersect(radio_pie, t)) { + renderOption = 0; + edgeColor = BrightBlue; + fillColor = Black; + update = true; + } else if (lcd.Intersect(radio_dial, t)) { + renderOption = 1; + edgeColor = BrightBlue; + fillColor = Gray; + update = true; + } + if (update) { + DrawInitialScreen(); + } +} + + #define PI 3.14159265359f // --------------- @@ -123,22 +212,24 @@ lcd.init(LCD_W,LCD_H,LCD_C,BL_NORM); lcd.TouchPanelInit(); - lcd.foreground(White); // Change to white - lcd.printf("RA8875 Touch Dial Example - Build " __DATE__ " " __TIME__ "\r\n"); - lcd.printf("MBED v%d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); - - // Draw a thickened circle - for (int r = 0; r <= 3; r++) { - lcd.ellipse(center.x, center.y, radius + r, radius + r, BrightBlue); - } + DrawInitialScreen(); while (1) { TouchCode_t touched; - + static point_t lastTouch = {LCD_W, LCD_H}; // Init off-screen for easy detection touched = lcd.TouchPanelReadable(); if (touched) { point_t Touch = lcd.TouchCoordinates(); + if (touched == touch) { // Only "on-touch" + TestToggleHit(Touch); + TestRenderHit(Touch); + } + + lcd.foreground(White); + lcd.SetTextCursor(LCD_W - 8 * 9, 0); + lcd.printf("(%3d,%3d)", Touch.x,Touch.y); + // If the touch is near the drawn circle (+/- 30 pixels), // compute the angle to the touch from the center of the circle if (abs(GetRadius(Touch, center) - radius) <= touch_tolerance) { @@ -146,20 +237,37 @@ printf("Touch at (%4d,%4d) is %3d degrees from (%4d,%4d)\r\n", Touch.x, Touch.y, angle, center.x, center.y); - // Fill the circle using 6° pie slices up to the angle of the touch - point_t lastP = ProjectPoint(center, 90, radius); // seed it at angle 0 - for (int a=6; a<=360; a+=6) { - point_t p = ProjectPoint(center, 90 - a, radius); - color_t fillColor = (a <= angle) ? Blue : Black; - //if ((a <= angle)) { // show the triangle coordinates (only the fill) - // printf(" (%3d,%3d), (%3d,%3d), (%3d,%3d)\r\n", - // lastP.x,lastP.y, p.x,p.y, center.x,center.y); - //} - lcd.filltriangle(center, p, lastP, fillColor); - lastP = p; + point_t lastP; + switch (renderOption) { + default: + case 0: + // Fill the circle using 6° pie slices up to the angle of the touch + lastP = ProjectPoint(center, 90, radius); // seed it at angle 0 + for (int a=6; a<=360; a+=6) { + point_t p = ProjectPoint(center, 90 - a, radius); + color_t fillColor = (a <= angle) ? Blue : Black; + //if ((a <= angle)) { // show the triangle coordinates (only the fill) + // printf(" (%3d,%3d), (%3d,%3d), (%3d,%3d)\r\n", + // lastP.x,lastP.y, p.x,p.y, center.x,center.y); + //} + lcd.filltriangle(center, p, lastP, fillColor); + lastP = p; + } + lastTouch.x = LCD_W; // reset to untouched for a change to the dial + break; + case 1: + lastP = ProjectPoint(center, 90 - angle, 0.75 * radius); + if (lastTouch.x != LCD_W) { + lcd.fillellipse(lastTouch, radius/5,radius/5, fillColor); + } + lcd.fillellipse(lastP, radius/5,radius/5, Black); + lastTouch = lastP; + break; } // Show the touch point (note we're not erasing the old one) - lcd.fillellipse(Touch.x, Touch.y, 5, 5, BrightRed); + if (touchVisible) { + lcd.fillellipse(Touch.x, Touch.y, 5, 5, BrightRed); + } } } }