mbed Paint for the RA8875 display with based touch screen.

Dependencies:   menu mbed RA8875

mPaint

mbed Paint - a demo for the (480x272) RA8875 Display with touchscreen.

Touch Screen

Having had several of the 4.3" WQVGA displays (that use the RA8875), I created the RA8875 driver library (initially derived derived from the work of others). Absent for some time was support for the touch-screen interface. Support first appeared with the contributions of others, and then a major update (due to a recent acquisition of a touch-screen version). This is now much more fully developed and it is part of the standard library.

Demo the Touch Screen

How to demonstrate the touch screen support? mPaint was created for just that purpose.

Screen Shots

Here's a couple of screen shots - you can capture the "canvas" (left) or the composite image (right). As you see, the composite picks up the menu information, however it remains subdued because of the transparency setting.

/media/uploads/WiredHome/mpaint_screen01.png/media/uploads/WiredHome/mpaint_screen02.png

Program Build Info

I'm sometimes a bit skeptical of the reported metrics (perhaps because most of my mbed applications have Ethernet), but here is the reported information from the build of this program.

mPaint Build Infoblinky Build Info
/media/uploads/WiredHome/mpaint_buildinfo.png/media/uploads/WiredHome/blinky_buildinfo.png
RA8875 Graphics library is the primary user.blinky is almost all "startup code" and standard libs

How does mPaint and the graphics library do this in about 1 kB RAM?

The answer is that the display is used as a "write-only" memory, and it has enough RAM hosted in the RA8875 for two full screens (in the WQVGA mode).

mPaint features

  • RGB color selection using touch, with a visible sample.
  • Pen size selection.
    From a limited set.
  • Tool choices.
    Dot, Line, Joined lines.
  • Save your creation.
    Select File | Save..., and it reads the display memory as it creates the BMP file on the local file system.
    This is rather slow (due to both the display read and the local file system).
  • Sorry, no undo.
    If you don't like what "ink" you put down, you can draw over it (much like other paint programs).
Committer:
WiredHome
Date:
Sun Mar 29 18:19:01 2020 +0000
Revision:
11:fe3e6b51f4ee
Parent:
10:52b0f7ccbc40
Pick up a bug-fix on jpeg rendering

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 1:0fdc10700ed2 1 /// mPaint is a simple drawing program, used to explore the touch
WiredHome 1:0fdc10700ed2 2 /// APIs of the RA8875 display library.
WiredHome 1:0fdc10700ed2 3 ///
WiredHome 1:0fdc10700ed2 4 /// @note Copyright © 2015 by Smartware Computing, all rights reserved.
WiredHome 1:0fdc10700ed2 5 /// Individuals may use this application for evaluation or non-commercial
WiredHome 1:0fdc10700ed2 6 /// purposes. Within this restriction, changes may be made to this application
WiredHome 1:0fdc10700ed2 7 /// as long as this copyright notice is retained. The user shall make
WiredHome 1:0fdc10700ed2 8 /// clear that their work is a derived work, and not the original.
WiredHome 1:0fdc10700ed2 9 /// Users of this application and sources accept this application "as is" and
WiredHome 1:0fdc10700ed2 10 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 1:0fdc10700ed2 11 /// using this application - whether real or imagined.
WiredHome 1:0fdc10700ed2 12 ///
WiredHome 1:0fdc10700ed2 13 /// @author David Smart, Smartware Computing
WiredHome 0:326a3f29e21b 14 //
WiredHome 0:326a3f29e21b 15 //
WiredHome 7:e8a7c28c6ebf 16 #include "mbed.h" // tested with v112
WiredHome 7:e8a7c28c6ebf 17 #include "RA8875.h" // tested with v102
WiredHome 7:e8a7c28c6ebf 18 #include "menu.h" // v3
WiredHome 0:326a3f29e21b 19
WiredHome 8:412e2819092e 20 #define DEBUG "mPaint"
WiredHome 1:0fdc10700ed2 21 // ...
WiredHome 1:0fdc10700ed2 22 // INFO("Stuff to show %d", var); // new-line is automatically appended
WiredHome 1:0fdc10700ed2 23 //
WiredHome 1:0fdc10700ed2 24 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 1:0fdc10700ed2 25 #define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 1:0fdc10700ed2 26 #define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 1:0fdc10700ed2 27 #define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 1:0fdc10700ed2 28 #else
WiredHome 1:0fdc10700ed2 29 #define INFO(x, ...)
WiredHome 1:0fdc10700ed2 30 #define WARN(x, ...)
WiredHome 1:0fdc10700ed2 31 #define ERR(x, ...)
WiredHome 1:0fdc10700ed2 32 #define HexDump(a, b, c)
WiredHome 1:0fdc10700ed2 33 #endif
WiredHome 1:0fdc10700ed2 34
WiredHome 8:412e2819092e 35 // Define this for 800x480 panel, undefine for 480x272
WiredHome 10:52b0f7ccbc40 36 //#define BIG_SCREEN
WiredHome 8:412e2819092e 37
WiredHome 8:412e2819092e 38 // Define this for Cap touch panel, undefine for resistive
WiredHome 10:52b0f7ccbc40 39 //#define CAP_TOUCH
WiredHome 8:412e2819092e 40
WiredHome 8:412e2819092e 41 #ifdef CAP_TOUCH
WiredHome 8:412e2819092e 42 RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, I2C:{SDA,SCL,/IRQ}, name
WiredHome 8:412e2819092e 43 #else
WiredHome 8:412e2819092e 44 RA8875 lcd(p5, p6, p7, p12, NC, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, name
WiredHome 8:412e2819092e 45 // LocalFileSystem local("local"); // if not otherwise needed; access to calibration file for resistive touch.
WiredHome 8:412e2819092e 46 #endif
WiredHome 8:412e2819092e 47
WiredHome 8:412e2819092e 48 #define PC_BAUD 460800 // I like the serial communications to be very fast
WiredHome 0:326a3f29e21b 49
WiredHome 8:412e2819092e 50 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 8:412e2819092e 51 // End of Configuration Section
WiredHome 8:412e2819092e 52 // // // // // // // // // // // // // // // // // // // // // // // //
WiredHome 8:412e2819092e 53
WiredHome 8:412e2819092e 54 #ifdef BIG_SCREEN
WiredHome 8:412e2819092e 55 #define LCD_W 800
WiredHome 8:412e2819092e 56 #define LCD_H 480
WiredHome 8:412e2819092e 57 #define LCD_C 8 // color - bits per pixel
WiredHome 8:412e2819092e 58 #define DEF_RADIUS 50 // default radius of the fingerprint
WiredHome 8:412e2819092e 59 #define BL_NORM 25 // Backlight Normal setting (0 to 255)
WiredHome 8:412e2819092e 60 #else
WiredHome 8:412e2819092e 61 #define LCD_W 480
WiredHome 8:412e2819092e 62 #define LCD_H 272
WiredHome 8:412e2819092e 63 #define LCD_C 8 // color - bits per pixel
WiredHome 8:412e2819092e 64 #define DEF_RADIUS 20 // default radius of the fingerprint
WiredHome 8:412e2819092e 65 #define BL_NORM 25 // Backlight Normal setting (0 to 255)
WiredHome 8:412e2819092e 66 #endif
WiredHome 0:326a3f29e21b 67
WiredHome 0:326a3f29e21b 68 // A monitor port for the SW developer.
WiredHome 0:326a3f29e21b 69 Serial pc(USBTX, USBRX);
WiredHome 0:326a3f29e21b 70
WiredHome 8:412e2819092e 71 LocalFileSystem local("local");
WiredHome 8:412e2819092e 72
WiredHome 1:0fdc10700ed2 73 // list of tools (dots, lines, joined lines).
WiredHome 0:326a3f29e21b 74 typedef enum {
WiredHome 1:0fdc10700ed2 75 dot, // draw dots at the point
WiredHome 1:0fdc10700ed2 76 line, // connected line from touch(point) to release(point)
WiredHome 1:0fdc10700ed2 77 join // connected lines while held(point)
WiredHome 0:326a3f29e21b 78 } tooltype_t; // what tool are we using to draw
WiredHome 0:326a3f29e21b 79
WiredHome 1:0fdc10700ed2 80 color_t rgb = Black; // the composite color value to draw in.
WiredHome 1:0fdc10700ed2 81 uint8_t rgbVal[3] = { 0, 0, 0 }; // hosts each of the individual values
WiredHome 1:0fdc10700ed2 82 uint32_t pensize = 1; // pensize is user selectable within a small range.
WiredHome 1:0fdc10700ed2 83 tooltype_t selectedtooltype = dot; // 0:dot, 1:line, 2:join
WiredHome 0:326a3f29e21b 84 point_t origin = { 0, 0}; // tracks origin when drawing a line
WiredHome 0:326a3f29e21b 85
WiredHome 8:412e2819092e 86 // 0 200 400 500 600 700 800
WiredHome 8:412e2819092e 87 // | | | | | | | | |
WiredHome 8:412e2819092e 88 // 0 100 200 300 400 500
WiredHome 8:412e2819092e 89 // | | | | | |
WiredHome 8:412e2819092e 90 // +---------------------------------------------------+ 0
WiredHome 8:412e2819092e 91 // | File Edit Pen Tools [smpl](o) [rrr][ggg][bbb] |
WiredHome 8:412e2819092e 92 // +---------------------------------------------------+
WiredHome 8:412e2819092e 93 // | | 16 32
WiredHome 8:412e2819092e 94 // | canvas |
WiredHome 8:412e2819092e 95 // | |
WiredHome 8:412e2819092e 96 // | |
WiredHome 8:412e2819092e 97 // | |
WiredHome 8:412e2819092e 98 // | |
WiredHome 8:412e2819092e 99 // | |
WiredHome 8:412e2819092e 100 // | |
WiredHome 8:412e2819092e 101 // | |
WiredHome 8:412e2819092e 102 // | |
WiredHome 8:412e2819092e 103 // +---------------------------------------------------+ 255 463
WiredHome 8:412e2819092e 104 // | (xxx,yyy) - (xxx,yyy) rgb (RR,GG,BB) |
WiredHome 8:412e2819092e 105 // +---------------------------------------------------+ 271 479
WiredHome 8:412e2819092e 106 // 0 479
WiredHome 8:412e2819092e 107 // 0 799
WiredHome 0:326a3f29e21b 108
WiredHome 8:412e2819092e 109 // Adjust the following if using the 800x480 v 480x272 display
WiredHome 8:412e2819092e 110 #if LCD_W == 800
WiredHome 8:412e2819092e 111 const rect_t RGBList[] = { // regions on the display for special tools
WiredHome 8:412e2819092e 112 { 425,0, 425+50,30 }, // show selected color
WiredHome 8:412e2819092e 113 { 500,0, 500+80,30 }, // R
WiredHome 8:412e2819092e 114 { 600,0, 600+80,30 }, // G
WiredHome 8:412e2819092e 115 { 700,0, 700+80,30 }, // B
WiredHome 0:326a3f29e21b 116 };
WiredHome 0:326a3f29e21b 117 const rect_t canvas_rect = { // the drawing surface
WiredHome 8:412e2819092e 118 0,32, LCD_W-1,LCD_H-1
WiredHome 0:326a3f29e21b 119 };
WiredHome 8:412e2819092e 120 #else
WiredHome 8:412e2819092e 121 const rect_t RGBList[] = { // regions on the display for special tools
WiredHome 8:412e2819092e 122 { 249,0, 249+50,15 }, // show selected color
WiredHome 8:412e2819092e 123 { 309,0, 309+50,15 }, // R
WiredHome 8:412e2819092e 124 { 369,0, 369+50,15 }, // G
WiredHome 8:412e2819092e 125 { 429,0, 429+50,15 }, // B
WiredHome 8:412e2819092e 126 };
WiredHome 8:412e2819092e 127 const rect_t canvas_rect = { // the drawing surface
WiredHome 8:412e2819092e 128 0,16, LCD_W-1,LCD_H-1
WiredHome 8:412e2819092e 129 };
WiredHome 8:412e2819092e 130 #endif
WiredHome 0:326a3f29e21b 131
WiredHome 0:326a3f29e21b 132
WiredHome 0:326a3f29e21b 133 // File Pen Tools
WiredHome 0:326a3f29e21b 134 // New... Pensize 1 Dot
WiredHome 0:326a3f29e21b 135 // Save... Pensize 2 Line
WiredHome 0:326a3f29e21b 136 // Calibrate Pensize 4
WiredHome 0:326a3f29e21b 137 // Reset Pensize 6
WiredHome 0:326a3f29e21b 138 // Pensize 8
WiredHome 0:326a3f29e21b 139 //
WiredHome 0:326a3f29e21b 140 Menu::post_fnc_action_t File(uint32_t v);
WiredHome 0:326a3f29e21b 141 Menu::post_fnc_action_t File_New(uint32_t v);
WiredHome 0:326a3f29e21b 142 Menu::post_fnc_action_t File_Save(uint32_t v);
WiredHome 0:326a3f29e21b 143 Menu::post_fnc_action_t File_Save_All(uint32_t v);
WiredHome 0:326a3f29e21b 144 Menu::post_fnc_action_t File_Cal(uint32_t v);
WiredHome 0:326a3f29e21b 145 Menu::post_fnc_action_t File_Reset(uint32_t v);
WiredHome 0:326a3f29e21b 146 Menu::post_fnc_action_t Edit(uint32_t v);
WiredHome 0:326a3f29e21b 147 Menu::post_fnc_action_t Edit_Clear(uint32_t v);
WiredHome 0:326a3f29e21b 148 Menu::post_fnc_action_t Tools(uint32_t v);
WiredHome 0:326a3f29e21b 149 Menu::post_fnc_action_t Tools_Type(uint32_t v);
WiredHome 0:326a3f29e21b 150 Menu::post_fnc_action_t PenSize(uint32_t v);
WiredHome 3:3b214426761d 151 Menu::post_fnc_action_t HideMenu(uint32_t v);
WiredHome 0:326a3f29e21b 152
WiredHome 0:326a3f29e21b 153 // Some APIs
WiredHome 0:326a3f29e21b 154 extern "C" void mbed_reset();
WiredHome 0:326a3f29e21b 155 int GetScreenCapture(void);
WiredHome 0:326a3f29e21b 156 void CalibrateTS(void);
WiredHome 0:326a3f29e21b 157 void ShowSampleRGB(void);
WiredHome 0:326a3f29e21b 158 void InitDisplay(void);
WiredHome 0:326a3f29e21b 159
WiredHome 0:326a3f29e21b 160 Menu::menu_item_t file_menu[] = {
WiredHome 0:326a3f29e21b 161 // Prompt, onPress, onHold, OnRelease, parameter, child menu
WiredHome 0:326a3f29e21b 162 { "New...", File_New, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 163 { "Save...", File_Save, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 164 { "Save all", File_Save_All, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 165 { "Calibrate", File_Cal, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 166 { "Reset", NULL, NULL, File_Reset, 0, NULL },
WiredHome 0:326a3f29e21b 167 { NULL, NULL, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 168 };
WiredHome 0:326a3f29e21b 169
WiredHome 0:326a3f29e21b 170 Menu::menu_item_t pen_menu[] = {
WiredHome 0:326a3f29e21b 171 { "1 pix", NULL, NULL, PenSize, 1, NULL },
WiredHome 0:326a3f29e21b 172 { "2 pix", NULL, NULL, PenSize, 2, NULL },
WiredHome 0:326a3f29e21b 173 { "4 pix", NULL, NULL, PenSize, 4, NULL },
WiredHome 0:326a3f29e21b 174 { "6 pix", NULL, NULL, PenSize, 6, NULL },
WiredHome 0:326a3f29e21b 175 { "8 pix", NULL, NULL, PenSize, 8, NULL },
WiredHome 0:326a3f29e21b 176 { NULL, NULL, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 177 };
WiredHome 0:326a3f29e21b 178
WiredHome 0:326a3f29e21b 179 Menu::menu_item_t tools_menu[] = {
WiredHome 1:0fdc10700ed2 180 { "point", NULL, NULL, Tools_Type, dot, NULL },
WiredHome 1:0fdc10700ed2 181 { "line", NULL, NULL, Tools_Type, line, NULL },
WiredHome 1:0fdc10700ed2 182 { "join", NULL, NULL, Tools_Type, join, NULL },
WiredHome 0:326a3f29e21b 183 { NULL, NULL, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 184 };
WiredHome 0:326a3f29e21b 185
WiredHome 0:326a3f29e21b 186 Menu::menu_item_t menudata[] = {
WiredHome 0:326a3f29e21b 187 { "File", File, NULL, NULL, 0, file_menu },
WiredHome 0:326a3f29e21b 188 { "Pen", NULL, NULL, NULL, 0, pen_menu },
WiredHome 0:326a3f29e21b 189 { "Tools", NULL, NULL, NULL, 0, tools_menu },
WiredHome 3:3b214426761d 190 { "Hide", NULL, NULL, HideMenu, 0, NULL },
WiredHome 0:326a3f29e21b 191 { NULL, NULL, NULL, NULL, 0, NULL },
WiredHome 0:326a3f29e21b 192 };
WiredHome 0:326a3f29e21b 193
WiredHome 0:326a3f29e21b 194 Menu menu(lcd, menudata);
WiredHome 0:326a3f29e21b 195
WiredHome 0:326a3f29e21b 196 Menu::post_fnc_action_t File(uint32_t v)
WiredHome 0:326a3f29e21b 197 {
WiredHome 3:3b214426761d 198 (void)v;
WiredHome 1:0fdc10700ed2 199 INFO("File");
WiredHome 0:326a3f29e21b 200 return Menu::no_action;
WiredHome 0:326a3f29e21b 201 }
WiredHome 0:326a3f29e21b 202 Menu::post_fnc_action_t File_New(uint32_t v)
WiredHome 0:326a3f29e21b 203 {
WiredHome 3:3b214426761d 204 (void)v;
WiredHome 1:0fdc10700ed2 205 INFO("File_New");
WiredHome 0:326a3f29e21b 206 InitDisplay();
WiredHome 0:326a3f29e21b 207 return Menu::no_action;
WiredHome 0:326a3f29e21b 208 }
WiredHome 0:326a3f29e21b 209 Menu::post_fnc_action_t File_Save(uint32_t v)
WiredHome 0:326a3f29e21b 210 {
WiredHome 3:3b214426761d 211 (void)v;
WiredHome 1:0fdc10700ed2 212 INFO("File_Save");
WiredHome 0:326a3f29e21b 213 RA8875::LayerMode_T l = lcd.GetLayerMode();
WiredHome 0:326a3f29e21b 214 lcd.SetLayerMode(RA8875::ShowLayer0);
WiredHome 0:326a3f29e21b 215 GetScreenCapture();
WiredHome 0:326a3f29e21b 216 lcd.SetLayerMode(RA8875::TransparentMode);
WiredHome 0:326a3f29e21b 217 return Menu::close_menu;
WiredHome 0:326a3f29e21b 218 }
WiredHome 0:326a3f29e21b 219 Menu::post_fnc_action_t File_Save_All(uint32_t v)
WiredHome 0:326a3f29e21b 220 {
WiredHome 3:3b214426761d 221 (void)v;
WiredHome 1:0fdc10700ed2 222 INFO("File_Save_All");
WiredHome 0:326a3f29e21b 223 GetScreenCapture();
WiredHome 0:326a3f29e21b 224 return Menu::close_menu;
WiredHome 0:326a3f29e21b 225 }
WiredHome 0:326a3f29e21b 226 Menu::post_fnc_action_t File_Cal(uint32_t v)
WiredHome 0:326a3f29e21b 227 {
WiredHome 3:3b214426761d 228 (void)v;
WiredHome 1:0fdc10700ed2 229 INFO("Tools_Cal");
WiredHome 0:326a3f29e21b 230 CalibrateTS();
WiredHome 0:326a3f29e21b 231 return Menu::no_action;
WiredHome 0:326a3f29e21b 232 }
WiredHome 0:326a3f29e21b 233 Menu::post_fnc_action_t File_Reset(uint32_t v)
WiredHome 0:326a3f29e21b 234 {
WiredHome 3:3b214426761d 235 (void)v;
WiredHome 1:0fdc10700ed2 236 INFO("rebooting now...");
WiredHome 0:326a3f29e21b 237 wait_ms(1000);
WiredHome 0:326a3f29e21b 238 mbed_reset();
WiredHome 0:326a3f29e21b 239 return Menu::no_action;
WiredHome 0:326a3f29e21b 240 }
WiredHome 0:326a3f29e21b 241 Menu::post_fnc_action_t Edit(uint32_t v)
WiredHome 0:326a3f29e21b 242 {
WiredHome 3:3b214426761d 243 (void)v;
WiredHome 1:0fdc10700ed2 244 INFO("Edit");
WiredHome 0:326a3f29e21b 245 return Menu::no_action;
WiredHome 0:326a3f29e21b 246 }
WiredHome 0:326a3f29e21b 247 Menu::post_fnc_action_t Tools(uint32_t v)
WiredHome 0:326a3f29e21b 248 {
WiredHome 3:3b214426761d 249 (void)v;
WiredHome 1:0fdc10700ed2 250 INFO("Tools");
WiredHome 0:326a3f29e21b 251 return Menu::no_action;
WiredHome 0:326a3f29e21b 252 }
WiredHome 0:326a3f29e21b 253 Menu::post_fnc_action_t PenSize(uint32_t v)
WiredHome 0:326a3f29e21b 254 {
WiredHome 0:326a3f29e21b 255 pensize = v;
WiredHome 0:326a3f29e21b 256 if (pensize < 1)
WiredHome 0:326a3f29e21b 257 pensize = 1;
WiredHome 0:326a3f29e21b 258 else if (pensize > 8)
WiredHome 0:326a3f29e21b 259 pensize = 8;
WiredHome 1:0fdc10700ed2 260 INFO("PenSize(%d)", pensize);
WiredHome 0:326a3f29e21b 261 ShowSampleRGB();
WiredHome 0:326a3f29e21b 262 return Menu::close_menu;
WiredHome 0:326a3f29e21b 263 }
WiredHome 0:326a3f29e21b 264 Menu::post_fnc_action_t Tools_Type(uint32_t v)
WiredHome 0:326a3f29e21b 265 {
WiredHome 0:326a3f29e21b 266 switch (v) {
WiredHome 1:0fdc10700ed2 267 case dot:
WiredHome 1:0fdc10700ed2 268 case line:
WiredHome 1:0fdc10700ed2 269 case join:
WiredHome 0:326a3f29e21b 270 selectedtooltype = (tooltype_t)v;
WiredHome 0:326a3f29e21b 271 break;
WiredHome 0:326a3f29e21b 272 default:
WiredHome 0:326a3f29e21b 273 break;
WiredHome 0:326a3f29e21b 274 }
WiredHome 0:326a3f29e21b 275 ShowSampleRGB();
WiredHome 0:326a3f29e21b 276 return Menu::close_menu;
WiredHome 0:326a3f29e21b 277 }
WiredHome 3:3b214426761d 278 Menu::post_fnc_action_t HideMenu(uint32_t v)
WiredHome 3:3b214426761d 279 {
WiredHome 3:3b214426761d 280 (void)v;
WiredHome 3:3b214426761d 281 return Menu::close_menu;
WiredHome 3:3b214426761d 282 }
WiredHome 0:326a3f29e21b 283
WiredHome 0:326a3f29e21b 284 void ShowSampleRGB(void)
WiredHome 0:326a3f29e21b 285 {
WiredHome 8:412e2819092e 286 loc_t middle = (RGBList[0].p1.y + RGBList[0].p2.y)/2;
WiredHome 8:412e2819092e 287 lcd.fillrect(RGBList[0], Black);
WiredHome 1:0fdc10700ed2 288 if (selectedtooltype == dot) {
WiredHome 8:412e2819092e 289 lcd.fillcircle((RGBList[0].p1.x + RGBList[0].p2.x)/2,
WiredHome 8:412e2819092e 290 middle, pensize, rgb);
WiredHome 0:326a3f29e21b 291 } else {
WiredHome 8:412e2819092e 292 lcd.fillrect(RGBList[0], rgb);
WiredHome 0:326a3f29e21b 293 }
WiredHome 0:326a3f29e21b 294 }
WiredHome 0:326a3f29e21b 295
WiredHome 0:326a3f29e21b 296 void CalibrateTS(void)
WiredHome 0:326a3f29e21b 297 {
WiredHome 0:326a3f29e21b 298 FILE * fh;
WiredHome 0:326a3f29e21b 299 tpMatrix_t matrix;
WiredHome 0:326a3f29e21b 300 RetCode_t r;
WiredHome 0:326a3f29e21b 301
WiredHome 0:326a3f29e21b 302 r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix);
WiredHome 1:0fdc10700ed2 303 INFO(" ret: %d", r);
WiredHome 0:326a3f29e21b 304 if (r == noerror) {
WiredHome 0:326a3f29e21b 305 fh = fopen("/local/tpcal.cfg", "wb");
WiredHome 0:326a3f29e21b 306 if (fh) {
WiredHome 0:326a3f29e21b 307 fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 0:326a3f29e21b 308 fclose(fh);
WiredHome 1:0fdc10700ed2 309 INFO(" tp cal written.");
WiredHome 0:326a3f29e21b 310 } else {
WiredHome 1:0fdc10700ed2 311 WARN(" couldn't open tpcal file.");
WiredHome 0:326a3f29e21b 312 }
WiredHome 0:326a3f29e21b 313 } else {
WiredHome 1:0fdc10700ed2 314 ERR("error return: %d", r);
WiredHome 0:326a3f29e21b 315 }
WiredHome 0:326a3f29e21b 316 }
WiredHome 0:326a3f29e21b 317
WiredHome 0:326a3f29e21b 318
WiredHome 0:326a3f29e21b 319 void InitTS(void)
WiredHome 0:326a3f29e21b 320 {
WiredHome 0:326a3f29e21b 321 FILE * fh;
WiredHome 0:326a3f29e21b 322 tpMatrix_t matrix;
WiredHome 0:326a3f29e21b 323
WiredHome 0:326a3f29e21b 324 fh = fopen("/local/tpcal.cfg", "rb");
WiredHome 0:326a3f29e21b 325 if (fh) {
WiredHome 0:326a3f29e21b 326 fread(&matrix, sizeof(tpMatrix_t), 1, fh);
WiredHome 0:326a3f29e21b 327 fclose(fh);
WiredHome 0:326a3f29e21b 328 lcd.TouchPanelSetMatrix(&matrix);
WiredHome 1:0fdc10700ed2 329 INFO(" tp cal loaded.");
WiredHome 0:326a3f29e21b 330 } else {
WiredHome 0:326a3f29e21b 331 CalibrateTS();
WiredHome 0:326a3f29e21b 332 }
WiredHome 0:326a3f29e21b 333 }
WiredHome 0:326a3f29e21b 334
WiredHome 0:326a3f29e21b 335
WiredHome 0:326a3f29e21b 336 void InitDisplay(void)
WiredHome 0:326a3f29e21b 337 {
WiredHome 0:326a3f29e21b 338 lcd.SelectDrawingLayer(CANVAS);
WiredHome 3:3b214426761d 339 lcd.cls();
WiredHome 0:326a3f29e21b 340 lcd.foreground(Blue);
WiredHome 0:326a3f29e21b 341 lcd.background(Black);
WiredHome 0:326a3f29e21b 342 }
WiredHome 0:326a3f29e21b 343
WiredHome 0:326a3f29e21b 344 int GetScreenCapture(void)
WiredHome 0:326a3f29e21b 345 {
WiredHome 0:326a3f29e21b 346 char fqfn[50];
WiredHome 0:326a3f29e21b 347 int i = 0;
WiredHome 0:326a3f29e21b 348
WiredHome 1:0fdc10700ed2 349 INFO("Screen Capture... ");
WiredHome 0:326a3f29e21b 350 for (i=1; i< 100; i++) {
WiredHome 0:326a3f29e21b 351 snprintf(fqfn, sizeof(fqfn), "/local/Screen%02d.bmp", i);
WiredHome 0:326a3f29e21b 352 FILE * fh = fopen(fqfn, "rb");
WiredHome 0:326a3f29e21b 353 if (!fh) {
WiredHome 8:412e2819092e 354 INFO("Saving as %s", fqfn);
WiredHome 1:0fdc10700ed2 355 lcd.PrintScreen(0,0,lcd.width(),lcd.height(),fqfn);
WiredHome 1:0fdc10700ed2 356 INFO(" as /local/Screen%02d.bmp", i);
WiredHome 0:326a3f29e21b 357 return i;
WiredHome 0:326a3f29e21b 358 } else {
WiredHome 0:326a3f29e21b 359 fclose(fh); // close this and try the next
WiredHome 0:326a3f29e21b 360 }
WiredHome 0:326a3f29e21b 361 }
WiredHome 0:326a3f29e21b 362 return 0;
WiredHome 0:326a3f29e21b 363 }
WiredHome 0:326a3f29e21b 364
WiredHome 3:3b214426761d 365 void ShowRGBSelectors(void)
WiredHome 0:326a3f29e21b 366 {
WiredHome 3:3b214426761d 367 uint16_t curLayer = lcd.GetDrawingLayer();
WiredHome 3:3b214426761d 368 lcd.SelectDrawingLayer(MENUS);
WiredHome 8:412e2819092e 369 lcd.fillrect(RGBList[1], Red);
WiredHome 8:412e2819092e 370 lcd.fillrect(RGBList[2], Green);
WiredHome 8:412e2819092e 371 lcd.fillrect(RGBList[3], Blue);
WiredHome 3:3b214426761d 372 lcd.SelectDrawingLayer(curLayer);
WiredHome 3:3b214426761d 373 }
WiredHome 3:3b214426761d 374
WiredHome 3:3b214426761d 375 bool SeeIfUserSelectingRGBValues(point_t p, TouchCode_t touchcode)
WiredHome 3:3b214426761d 376 {
WiredHome 3:3b214426761d 377 static bool wasIn = false;
WiredHome 8:412e2819092e 378
WiredHome 0:326a3f29e21b 379 // See if the touch is setting new RGB values
WiredHome 8:412e2819092e 380 for (int i=1; i<=3; i++) {
WiredHome 0:326a3f29e21b 381 if (lcd.Intersect(RGBList[i], p)) {
WiredHome 0:326a3f29e21b 382 uint8_t mag = (255 * (p.x - RGBList[i].p1.x)) / (RGBList[i].p2.x - RGBList[i].p1.x);
WiredHome 3:3b214426761d 383 wasIn = true;
WiredHome 3:3b214426761d 384 if (touchcode == touch)
WiredHome 3:3b214426761d 385 menu.Show();
WiredHome 3:3b214426761d 386 else if (touchcode == release)
WiredHome 3:3b214426761d 387 menu.Hide();
WiredHome 8:412e2819092e 388 rgbVal[i-1] = mag;
WiredHome 0:326a3f29e21b 389 // update the RGB values
WiredHome 0:326a3f29e21b 390 lcd.SelectDrawingLayer(MENUS);
WiredHome 8:412e2819092e 391 lcd.SetTextCursor(lcd.width() - 10*lcd.fontwidth(), lcd.height() - lcd.fontheight());
WiredHome 0:326a3f29e21b 392 lcd.foreground(Blue);
WiredHome 0:326a3f29e21b 393 lcd.printf("(%02X,%02X,%02X)", rgbVal[0], rgbVal[1], rgbVal[2]);
WiredHome 0:326a3f29e21b 394 // show sample
WiredHome 0:326a3f29e21b 395 rgb = RGB(rgbVal[0], rgbVal[1], rgbVal[2]);
WiredHome 0:326a3f29e21b 396 ShowSampleRGB();
WiredHome 0:326a3f29e21b 397 //
WiredHome 0:326a3f29e21b 398 lcd.SelectDrawingLayer(CANVAS);
WiredHome 0:326a3f29e21b 399 lcd.foreground(rgb);
WiredHome 3:3b214426761d 400 return true;
WiredHome 0:326a3f29e21b 401 }
WiredHome 0:326a3f29e21b 402 }
WiredHome 3:3b214426761d 403 if (wasIn)
WiredHome 3:3b214426761d 404 menu.Hide();
WiredHome 3:3b214426761d 405 return false;
WiredHome 0:326a3f29e21b 406 }
WiredHome 0:326a3f29e21b 407
WiredHome 8:412e2819092e 408 int sgn(int x)
WiredHome 1:0fdc10700ed2 409 {
WiredHome 8:412e2819092e 410 if (sgn < 0)
WiredHome 8:412e2819092e 411 return -1;
WiredHome 8:412e2819092e 412 else if (sgn > 0)
WiredHome 8:412e2819092e 413 return 1;
WiredHome 8:412e2819092e 414 else
WiredHome 8:412e2819092e 415 return 0;
WiredHome 8:412e2819092e 416 }
WiredHome 1:0fdc10700ed2 417
WiredHome 1:0fdc10700ed2 418
WiredHome 1:0fdc10700ed2 419
WiredHome 0:326a3f29e21b 420 void SeeIfUserDrawingOnCanvas(point_t p, TouchCode_t touchcode)
WiredHome 0:326a3f29e21b 421 {
WiredHome 0:326a3f29e21b 422 if (lcd.Intersect(canvas_rect, p)) {
WiredHome 0:326a3f29e21b 423 switch (selectedtooltype) {
WiredHome 0:326a3f29e21b 424 case dot:
WiredHome 1:0fdc10700ed2 425 lcd.fillcircle(p, (pensize == 1) ? pensize : pensize/2, rgb);
WiredHome 0:326a3f29e21b 426 break;
WiredHome 0:326a3f29e21b 427 case line:
WiredHome 0:326a3f29e21b 428 if (touchcode == touch) {
WiredHome 0:326a3f29e21b 429 lcd.fillcircle(p, 1, rgb);
WiredHome 0:326a3f29e21b 430 origin = p;
WiredHome 1:0fdc10700ed2 431 INFO("Origin @ (%3d,%3d)", p.x, p.y);
WiredHome 0:326a3f29e21b 432 } else if (touchcode == release) {
WiredHome 8:412e2819092e 433 lcd.ThickLine(origin, p, pensize, rgb);
WiredHome 0:326a3f29e21b 434 }
WiredHome 0:326a3f29e21b 435 break;
WiredHome 1:0fdc10700ed2 436 case join:
WiredHome 1:0fdc10700ed2 437 if (touchcode == touch) {
WiredHome 1:0fdc10700ed2 438 lcd.fillcircle(p, 1, rgb);
WiredHome 1:0fdc10700ed2 439 origin = p;
WiredHome 1:0fdc10700ed2 440 INFO("Origin @ (%3d,%3d)", p.x, p.y);
WiredHome 1:0fdc10700ed2 441 } else if (touchcode == release) {
WiredHome 8:412e2819092e 442 lcd.ThickLine(origin, p, pensize, rgb);
WiredHome 1:0fdc10700ed2 443 } else if (touchcode == held) {
WiredHome 8:412e2819092e 444 lcd.ThickLine(origin, p, pensize, rgb);
WiredHome 1:0fdc10700ed2 445 origin = p;
WiredHome 1:0fdc10700ed2 446 INFO(" held @ (%3d,%3d)", p.x, p.y);
WiredHome 1:0fdc10700ed2 447 }
WiredHome 8:412e2819092e 448 break;
WiredHome 0:326a3f29e21b 449 default:
WiredHome 0:326a3f29e21b 450 break;
WiredHome 0:326a3f29e21b 451 }
WiredHome 0:326a3f29e21b 452 }
WiredHome 0:326a3f29e21b 453 }
WiredHome 0:326a3f29e21b 454
WiredHome 0:326a3f29e21b 455
WiredHome 0:326a3f29e21b 456 int main()
WiredHome 0:326a3f29e21b 457 {
WiredHome 0:326a3f29e21b 458 pc.baud(460800); // I like a snappy terminal, so crank it up!
WiredHome 0:326a3f29e21b 459 pc.printf("\r\nRA8875 Menu - Build " __DATE__ " " __TIME__ "\r\n");
WiredHome 0:326a3f29e21b 460
WiredHome 1:0fdc10700ed2 461 INFO("Turning on display");
WiredHome 8:412e2819092e 462 lcd.init(LCD_W,LCD_H,LCD_C);
WiredHome 8:412e2819092e 463 lcd.TouchPanelInit();
WiredHome 5:942133f7bb12 464 lcd.frequency(10000000);
WiredHome 8:412e2819092e 465 lcd.SetTextFontSize(2);
WiredHome 8:412e2819092e 466 #ifndef CAP_TOUCH
WiredHome 8:412e2819092e 467 InitTS(); // resistive touch calibration
WiredHome 8:412e2819092e 468 #endif
WiredHome 0:326a3f29e21b 469 InitDisplay();
WiredHome 3:3b214426761d 470 menu.init();
WiredHome 3:3b214426761d 471 ShowRGBSelectors();
WiredHome 0:326a3f29e21b 472
WiredHome 1:0fdc10700ed2 473 INFO("processing loop...");
WiredHome 0:326a3f29e21b 474
WiredHome 0:326a3f29e21b 475 for (;;) {
WiredHome 0:326a3f29e21b 476 point_t p;
WiredHome 0:326a3f29e21b 477 TouchCode_t touchcode = lcd.TouchPanelReadable(&p);
WiredHome 0:326a3f29e21b 478
WiredHome 0:326a3f29e21b 479 if (touchcode != no_touch) {
WiredHome 8:412e2819092e 480 //int curLayer = lcd.GetDrawingLayer();
WiredHome 5:942133f7bb12 481 // This is nice feedback, but certainly slows the drawing.
WiredHome 5:942133f7bb12 482 //lcd.SelectDrawingLayer(MENUS);
WiredHome 5:942133f7bb12 483 //lcd.foreground(Blue);
WiredHome 5:942133f7bb12 484 //lcd.SetTextCursor(0, lcd.height() - 16);
WiredHome 5:942133f7bb12 485 //lcd.printf("(%3d,%3d) - (%3d,%3d)", origin.x, origin.y, p.x, p.y);
WiredHome 8:412e2819092e 486 //lcd.SelectDrawingLayer(curLayer);
WiredHome 0:326a3f29e21b 487
WiredHome 0:326a3f29e21b 488 bool menuHandledIt = menu.HandledTouch(p, touchcode);
WiredHome 0:326a3f29e21b 489 if (menuHandledIt) {
WiredHome 0:326a3f29e21b 490 // menu handled it
WiredHome 3:3b214426761d 491 } else if (SeeIfUserSelectingRGBValues(p, touchcode)) {
WiredHome 3:3b214426761d 492 // that handled it.
WiredHome 0:326a3f29e21b 493 } else {
WiredHome 3:3b214426761d 494 SeeIfUserDrawingOnCanvas(p, touchcode);
WiredHome 0:326a3f29e21b 495 }
WiredHome 0:326a3f29e21b 496 } else {
WiredHome 0:326a3f29e21b 497 //non-touch
WiredHome 0:326a3f29e21b 498 }
WiredHome 0:326a3f29e21b 499 }
WiredHome 0:326a3f29e21b 500 }
WiredHome 8:412e2819092e 501
WiredHome 0:326a3f29e21b 502 #include <stdarg.h>
WiredHome 0:326a3f29e21b 503 //Custom override for error()
WiredHome 0:326a3f29e21b 504 void error(const char* format, ...)
WiredHome 0:326a3f29e21b 505 {
WiredHome 0:326a3f29e21b 506 char sprintf_buffer[128];
WiredHome 0:326a3f29e21b 507
WiredHome 0:326a3f29e21b 508 va_list arg;
WiredHome 0:326a3f29e21b 509 va_start(arg, format);
WiredHome 0:326a3f29e21b 510 vsprintf(sprintf_buffer, format, arg);
WiredHome 0:326a3f29e21b 511 va_end(arg);
WiredHome 0:326a3f29e21b 512
WiredHome 0:326a3f29e21b 513 fprintf(stderr, "SW err: %s", sprintf_buffer);
WiredHome 0:326a3f29e21b 514 }
WiredHome 0:326a3f29e21b 515
WiredHome 0:326a3f29e21b 516 // Reset_Handler
WiredHome 0:326a3f29e21b 517 // NMI_Handler
WiredHome 0:326a3f29e21b 518 // HardFault_Handler
WiredHome 0:326a3f29e21b 519 // MemManage_Handler
WiredHome 0:326a3f29e21b 520 // BusFault_Handler
WiredHome 0:326a3f29e21b 521 // UsageFault_Handler
WiredHome 0:326a3f29e21b 522 extern "C" void HardFault_Handler()
WiredHome 0:326a3f29e21b 523 {
WiredHome 0:326a3f29e21b 524 printf("\r\n\r\nHard Fault!\r\n");
WiredHome 0:326a3f29e21b 525 wait_ms(500);
WiredHome 0:326a3f29e21b 526 NVIC_SystemReset();
WiredHome 0:326a3f29e21b 527 }
WiredHome 0:326a3f29e21b 528 extern "C" void NMI_Handler()
WiredHome 0:326a3f29e21b 529 {
WiredHome 0:326a3f29e21b 530 printf("\r\n\r\nNMI Fault!\r\n");
WiredHome 0:326a3f29e21b 531 wait_ms(500);
WiredHome 0:326a3f29e21b 532 NVIC_SystemReset();
WiredHome 0:326a3f29e21b 533 }
WiredHome 0:326a3f29e21b 534 extern "C" void MemManage_Handler()
WiredHome 0:326a3f29e21b 535 {
WiredHome 0:326a3f29e21b 536 printf("\r\n\r\nMemManage Fault!\r\n");
WiredHome 0:326a3f29e21b 537 wait_ms(500);
WiredHome 0:326a3f29e21b 538 NVIC_SystemReset();
WiredHome 0:326a3f29e21b 539 }
WiredHome 0:326a3f29e21b 540 extern "C" void BusFault_Handler()
WiredHome 0:326a3f29e21b 541 {
WiredHome 0:326a3f29e21b 542 printf("\r\n\r\nBusFault Fault!\r\n");
WiredHome 0:326a3f29e21b 543 wait_ms(500);
WiredHome 0:326a3f29e21b 544 NVIC_SystemReset();
WiredHome 0:326a3f29e21b 545 }
WiredHome 0:326a3f29e21b 546 extern "C" void UsageFault_Handler()
WiredHome 0:326a3f29e21b 547 {
WiredHome 0:326a3f29e21b 548 printf("\r\n\r\nUsageFault Fault!\r\n");
WiredHome 0:326a3f29e21b 549 wait_ms(500);
WiredHome 0:326a3f29e21b 550 NVIC_SystemReset();
WiredHome 0:326a3f29e21b 551 }