Demo of RA8875 TFT touch display on Freescale FRDM-K64F Forked from David Smart https://developer.mbed.org/users/WiredHome/

Dependencies:   RA8875 menu SDFileSystem mbed

Fork of PUB_RA8875_mPaint by David Smart

Committer:
unix_guru
Date:
Fri Oct 02 19:47:06 2015 +0000
Revision:
5:3afc5780b966
Parent:
3:3b214426761d
First functional commit

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