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:
Sat Jan 03 17:12:02 2015 +0000
Revision:
3:3b214426761d
Parent:
2:cf295dad3192
Child:
5:942133f7bb12
Move RGB selection to the other layer - less visual interference with the canvas drawing.

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