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
main.cpp@2:cf295dad3192, 2015-01-03 (annotated)
- Committer:
- WiredHome
- Date:
- Sat Jan 03 15:41:51 2015 +0000
- Revision:
- 2:cf295dad3192
- Parent:
- 1:0fdc10700ed2
- Child:
- 3:3b214426761d
A simple mbed Paint program to demonstrate the RA8875 Touchscreen interface.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:326a3f29e21b | 108 | |
WiredHome | 0:326a3f29e21b | 109 | // Some APIs |
WiredHome | 0:326a3f29e21b | 110 | extern "C" void mbed_reset(); |
WiredHome | 0:326a3f29e21b | 111 | int GetScreenCapture(void); |
WiredHome | 0:326a3f29e21b | 112 | void CalibrateTS(void); |
WiredHome | 0:326a3f29e21b | 113 | void ShowSampleRGB(void); |
WiredHome | 0:326a3f29e21b | 114 | void InitDisplay(void); |
WiredHome | 0:326a3f29e21b | 115 | |
WiredHome | 0:326a3f29e21b | 116 | Menu::menu_item_t file_menu[] = { |
WiredHome | 0:326a3f29e21b | 117 | // Prompt, onPress, onHold, OnRelease, parameter, child menu |
WiredHome | 0:326a3f29e21b | 118 | { "New...", File_New, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 119 | { "Save...", File_Save, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 120 | { "Save all", File_Save_All, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 121 | { "Calibrate", File_Cal, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 122 | { "Reset", NULL, NULL, File_Reset, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 123 | { NULL, NULL, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 124 | }; |
WiredHome | 0:326a3f29e21b | 125 | |
WiredHome | 0:326a3f29e21b | 126 | Menu::menu_item_t pen_menu[] = { |
WiredHome | 0:326a3f29e21b | 127 | { "1 pix", NULL, NULL, PenSize, 1, NULL }, |
WiredHome | 0:326a3f29e21b | 128 | { "2 pix", NULL, NULL, PenSize, 2, NULL }, |
WiredHome | 0:326a3f29e21b | 129 | { "4 pix", NULL, NULL, PenSize, 4, NULL }, |
WiredHome | 0:326a3f29e21b | 130 | { "6 pix", NULL, NULL, PenSize, 6, NULL }, |
WiredHome | 0:326a3f29e21b | 131 | { "8 pix", NULL, NULL, PenSize, 8, NULL }, |
WiredHome | 0:326a3f29e21b | 132 | { NULL, NULL, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 133 | }; |
WiredHome | 0:326a3f29e21b | 134 | |
WiredHome | 0:326a3f29e21b | 135 | Menu::menu_item_t tools_menu[] = { |
WiredHome | 1:0fdc10700ed2 | 136 | { "point", NULL, NULL, Tools_Type, dot, NULL }, |
WiredHome | 1:0fdc10700ed2 | 137 | { "line", NULL, NULL, Tools_Type, line, NULL }, |
WiredHome | 1:0fdc10700ed2 | 138 | { "join", NULL, NULL, Tools_Type, join, NULL }, |
WiredHome | 0:326a3f29e21b | 139 | { NULL, NULL, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 140 | }; |
WiredHome | 0:326a3f29e21b | 141 | |
WiredHome | 0:326a3f29e21b | 142 | Menu::menu_item_t menudata[] = { |
WiredHome | 0:326a3f29e21b | 143 | { "File", File, NULL, NULL, 0, file_menu }, |
WiredHome | 0:326a3f29e21b | 144 | { "Pen", NULL, NULL, NULL, 0, pen_menu }, |
WiredHome | 0:326a3f29e21b | 145 | { "Tools", NULL, NULL, NULL, 0, tools_menu }, |
WiredHome | 0:326a3f29e21b | 146 | { NULL, NULL, NULL, NULL, 0, NULL }, |
WiredHome | 0:326a3f29e21b | 147 | }; |
WiredHome | 0:326a3f29e21b | 148 | |
WiredHome | 0:326a3f29e21b | 149 | Menu menu(lcd, menudata); |
WiredHome | 0:326a3f29e21b | 150 | |
WiredHome | 0:326a3f29e21b | 151 | Menu::post_fnc_action_t File(uint32_t v) |
WiredHome | 0:326a3f29e21b | 152 | { |
WiredHome | 1:0fdc10700ed2 | 153 | INFO("File"); |
WiredHome | 0:326a3f29e21b | 154 | return Menu::no_action; |
WiredHome | 0:326a3f29e21b | 155 | } |
WiredHome | 0:326a3f29e21b | 156 | Menu::post_fnc_action_t File_New(uint32_t v) |
WiredHome | 0:326a3f29e21b | 157 | { |
WiredHome | 1:0fdc10700ed2 | 158 | INFO("File_New"); |
WiredHome | 0:326a3f29e21b | 159 | InitDisplay(); |
WiredHome | 0:326a3f29e21b | 160 | return Menu::no_action; |
WiredHome | 0:326a3f29e21b | 161 | } |
WiredHome | 0:326a3f29e21b | 162 | Menu::post_fnc_action_t File_Save(uint32_t v) |
WiredHome | 0:326a3f29e21b | 163 | { |
WiredHome | 1:0fdc10700ed2 | 164 | INFO("File_Save"); |
WiredHome | 0:326a3f29e21b | 165 | RA8875::LayerMode_T l = lcd.GetLayerMode(); |
WiredHome | 0:326a3f29e21b | 166 | lcd.SetLayerMode(RA8875::ShowLayer0); |
WiredHome | 0:326a3f29e21b | 167 | GetScreenCapture(); |
WiredHome | 0:326a3f29e21b | 168 | lcd.SetLayerMode(RA8875::TransparentMode); |
WiredHome | 0:326a3f29e21b | 169 | return Menu::close_menu; |
WiredHome | 0:326a3f29e21b | 170 | } |
WiredHome | 0:326a3f29e21b | 171 | Menu::post_fnc_action_t File_Save_All(uint32_t v) |
WiredHome | 0:326a3f29e21b | 172 | { |
WiredHome | 1:0fdc10700ed2 | 173 | INFO("File_Save_All"); |
WiredHome | 0:326a3f29e21b | 174 | GetScreenCapture(); |
WiredHome | 0:326a3f29e21b | 175 | return Menu::close_menu; |
WiredHome | 0:326a3f29e21b | 176 | } |
WiredHome | 0:326a3f29e21b | 177 | Menu::post_fnc_action_t File_Cal(uint32_t v) |
WiredHome | 0:326a3f29e21b | 178 | { |
WiredHome | 1:0fdc10700ed2 | 179 | INFO("Tools_Cal"); |
WiredHome | 0:326a3f29e21b | 180 | CalibrateTS(); |
WiredHome | 0:326a3f29e21b | 181 | return Menu::no_action; |
WiredHome | 0:326a3f29e21b | 182 | } |
WiredHome | 0:326a3f29e21b | 183 | Menu::post_fnc_action_t File_Reset(uint32_t v) |
WiredHome | 0:326a3f29e21b | 184 | { |
WiredHome | 1:0fdc10700ed2 | 185 | INFO("rebooting now..."); |
WiredHome | 0:326a3f29e21b | 186 | wait_ms(1000); |
WiredHome | 0:326a3f29e21b | 187 | mbed_reset(); |
WiredHome | 0:326a3f29e21b | 188 | return Menu::no_action; |
WiredHome | 0:326a3f29e21b | 189 | } |
WiredHome | 0:326a3f29e21b | 190 | Menu::post_fnc_action_t Edit(uint32_t v) |
WiredHome | 0:326a3f29e21b | 191 | { |
WiredHome | 1:0fdc10700ed2 | 192 | INFO("Edit"); |
WiredHome | 0:326a3f29e21b | 193 | return Menu::no_action; |
WiredHome | 0:326a3f29e21b | 194 | } |
WiredHome | 0:326a3f29e21b | 195 | Menu::post_fnc_action_t Tools(uint32_t v) |
WiredHome | 0:326a3f29e21b | 196 | { |
WiredHome | 1:0fdc10700ed2 | 197 | INFO("Tools"); |
WiredHome | 0:326a3f29e21b | 198 | return Menu::no_action; |
WiredHome | 0:326a3f29e21b | 199 | } |
WiredHome | 0:326a3f29e21b | 200 | Menu::post_fnc_action_t PenSize(uint32_t v) |
WiredHome | 0:326a3f29e21b | 201 | { |
WiredHome | 0:326a3f29e21b | 202 | pensize = v; |
WiredHome | 0:326a3f29e21b | 203 | if (pensize < 1) |
WiredHome | 0:326a3f29e21b | 204 | pensize = 1; |
WiredHome | 0:326a3f29e21b | 205 | else if (pensize > 8) |
WiredHome | 0:326a3f29e21b | 206 | pensize = 8; |
WiredHome | 1:0fdc10700ed2 | 207 | INFO("PenSize(%d)", pensize); |
WiredHome | 0:326a3f29e21b | 208 | ShowSampleRGB(); |
WiredHome | 0:326a3f29e21b | 209 | return Menu::close_menu; |
WiredHome | 0:326a3f29e21b | 210 | } |
WiredHome | 0:326a3f29e21b | 211 | Menu::post_fnc_action_t Tools_Type(uint32_t v) |
WiredHome | 0:326a3f29e21b | 212 | { |
WiredHome | 0:326a3f29e21b | 213 | switch (v) { |
WiredHome | 1:0fdc10700ed2 | 214 | case dot: |
WiredHome | 1:0fdc10700ed2 | 215 | case line: |
WiredHome | 1:0fdc10700ed2 | 216 | case join: |
WiredHome | 0:326a3f29e21b | 217 | selectedtooltype = (tooltype_t)v; |
WiredHome | 0:326a3f29e21b | 218 | break; |
WiredHome | 0:326a3f29e21b | 219 | default: |
WiredHome | 0:326a3f29e21b | 220 | break; |
WiredHome | 0:326a3f29e21b | 221 | } |
WiredHome | 0:326a3f29e21b | 222 | ShowSampleRGB(); |
WiredHome | 0:326a3f29e21b | 223 | return Menu::close_menu; |
WiredHome | 0:326a3f29e21b | 224 | } |
WiredHome | 0:326a3f29e21b | 225 | |
WiredHome | 0:326a3f29e21b | 226 | void ShowSampleRGB(void) |
WiredHome | 0:326a3f29e21b | 227 | { |
WiredHome | 1:0fdc10700ed2 | 228 | loc_t middle = (RGBList[3].p1.y + RGBList[3].p2.y)/2; |
WiredHome | 0:326a3f29e21b | 229 | lcd.fillrect(RGBList[3], Black); |
WiredHome | 0:326a3f29e21b | 230 | lcd.fillrect(RGBList[3], rgb); |
WiredHome | 1:0fdc10700ed2 | 231 | if (selectedtooltype == dot) { |
WiredHome | 1:0fdc10700ed2 | 232 | lcd.fillcircle((RGBList[3].p1.x + RGBList[3].p2.x)/2, |
WiredHome | 1:0fdc10700ed2 | 233 | middle, pensize, rgb); |
WiredHome | 0:326a3f29e21b | 234 | } else { |
WiredHome | 1:0fdc10700ed2 | 235 | lcd.fillrect(RGBList[3].p1.x,middle-pensize/2, RGBList[3].p2.x,middle+pensize/2, rgb); |
WiredHome | 0:326a3f29e21b | 236 | } |
WiredHome | 0:326a3f29e21b | 237 | } |
WiredHome | 0:326a3f29e21b | 238 | |
WiredHome | 0:326a3f29e21b | 239 | void CalibrateTS(void) |
WiredHome | 0:326a3f29e21b | 240 | { |
WiredHome | 0:326a3f29e21b | 241 | FILE * fh; |
WiredHome | 0:326a3f29e21b | 242 | tpMatrix_t matrix; |
WiredHome | 0:326a3f29e21b | 243 | RetCode_t r; |
WiredHome | 0:326a3f29e21b | 244 | |
WiredHome | 0:326a3f29e21b | 245 | r = lcd.TouchPanelCalibrate("Calibrate the touch panel", &matrix); |
WiredHome | 1:0fdc10700ed2 | 246 | INFO(" ret: %d", r); |
WiredHome | 0:326a3f29e21b | 247 | if (r == noerror) { |
WiredHome | 0:326a3f29e21b | 248 | fh = fopen("/local/tpcal.cfg", "wb"); |
WiredHome | 0:326a3f29e21b | 249 | if (fh) { |
WiredHome | 0:326a3f29e21b | 250 | fwrite(&matrix, sizeof(tpMatrix_t), 1, fh); |
WiredHome | 0:326a3f29e21b | 251 | fclose(fh); |
WiredHome | 1:0fdc10700ed2 | 252 | INFO(" tp cal written."); |
WiredHome | 0:326a3f29e21b | 253 | } else { |
WiredHome | 1:0fdc10700ed2 | 254 | WARN(" couldn't open tpcal file."); |
WiredHome | 0:326a3f29e21b | 255 | } |
WiredHome | 0:326a3f29e21b | 256 | } else { |
WiredHome | 1:0fdc10700ed2 | 257 | ERR("error return: %d", r); |
WiredHome | 0:326a3f29e21b | 258 | } |
WiredHome | 0:326a3f29e21b | 259 | } |
WiredHome | 0:326a3f29e21b | 260 | |
WiredHome | 0:326a3f29e21b | 261 | |
WiredHome | 0:326a3f29e21b | 262 | void InitTS(void) |
WiredHome | 0:326a3f29e21b | 263 | { |
WiredHome | 0:326a3f29e21b | 264 | FILE * fh; |
WiredHome | 0:326a3f29e21b | 265 | tpMatrix_t matrix; |
WiredHome | 0:326a3f29e21b | 266 | |
WiredHome | 0:326a3f29e21b | 267 | fh = fopen("/local/tpcal.cfg", "rb"); |
WiredHome | 0:326a3f29e21b | 268 | if (fh) { |
WiredHome | 0:326a3f29e21b | 269 | fread(&matrix, sizeof(tpMatrix_t), 1, fh); |
WiredHome | 0:326a3f29e21b | 270 | fclose(fh); |
WiredHome | 0:326a3f29e21b | 271 | lcd.TouchPanelSetMatrix(&matrix); |
WiredHome | 1:0fdc10700ed2 | 272 | INFO(" tp cal loaded."); |
WiredHome | 0:326a3f29e21b | 273 | } else { |
WiredHome | 0:326a3f29e21b | 274 | CalibrateTS(); |
WiredHome | 0:326a3f29e21b | 275 | } |
WiredHome | 0:326a3f29e21b | 276 | } |
WiredHome | 0:326a3f29e21b | 277 | |
WiredHome | 0:326a3f29e21b | 278 | |
WiredHome | 0:326a3f29e21b | 279 | void InitDisplay(void) |
WiredHome | 0:326a3f29e21b | 280 | { |
WiredHome | 0:326a3f29e21b | 281 | lcd.cls(3); // both layers |
WiredHome | 0:326a3f29e21b | 282 | lcd.SelectDrawingLayer(CANVAS); |
WiredHome | 0:326a3f29e21b | 283 | lcd.fillrect(RGBList[0], Red); |
WiredHome | 0:326a3f29e21b | 284 | lcd.fillrect(RGBList[1], Green); |
WiredHome | 0:326a3f29e21b | 285 | lcd.fillrect(RGBList[2], Blue); |
WiredHome | 0:326a3f29e21b | 286 | lcd.foreground(Blue); |
WiredHome | 0:326a3f29e21b | 287 | lcd.background(Black); |
WiredHome | 0:326a3f29e21b | 288 | } |
WiredHome | 0:326a3f29e21b | 289 | |
WiredHome | 0:326a3f29e21b | 290 | int GetScreenCapture(void) |
WiredHome | 0:326a3f29e21b | 291 | { |
WiredHome | 0:326a3f29e21b | 292 | char fqfn[50]; |
WiredHome | 0:326a3f29e21b | 293 | int i = 0; |
WiredHome | 0:326a3f29e21b | 294 | |
WiredHome | 1:0fdc10700ed2 | 295 | INFO("Screen Capture... "); |
WiredHome | 0:326a3f29e21b | 296 | for (i=1; i< 100; i++) { |
WiredHome | 0:326a3f29e21b | 297 | snprintf(fqfn, sizeof(fqfn), "/local/Screen%02d.bmp", i); |
WiredHome | 0:326a3f29e21b | 298 | FILE * fh = fopen(fqfn, "rb"); |
WiredHome | 0:326a3f29e21b | 299 | if (!fh) { |
WiredHome | 1:0fdc10700ed2 | 300 | lcd.PrintScreen(0,0,lcd.width(),lcd.height(),fqfn); |
WiredHome | 1:0fdc10700ed2 | 301 | INFO(" as /local/Screen%02d.bmp", i); |
WiredHome | 0:326a3f29e21b | 302 | return i; |
WiredHome | 0:326a3f29e21b | 303 | } else { |
WiredHome | 0:326a3f29e21b | 304 | fclose(fh); // close this and try the next |
WiredHome | 0:326a3f29e21b | 305 | } |
WiredHome | 0:326a3f29e21b | 306 | } |
WiredHome | 0:326a3f29e21b | 307 | return 0; |
WiredHome | 0:326a3f29e21b | 308 | } |
WiredHome | 0:326a3f29e21b | 309 | |
WiredHome | 1:0fdc10700ed2 | 310 | void SeeIfUserSelectingRGBValues(point_t p, TouchCode_t touchcode) |
WiredHome | 0:326a3f29e21b | 311 | { |
WiredHome | 0:326a3f29e21b | 312 | // See if the touch is setting new RGB values |
WiredHome | 0:326a3f29e21b | 313 | for (int i=0; i<3; i++) { |
WiredHome | 0:326a3f29e21b | 314 | if (lcd.Intersect(RGBList[i], p)) { |
WiredHome | 0:326a3f29e21b | 315 | uint8_t mag = (255 * (p.x - RGBList[i].p1.x)) / (RGBList[i].p2.x - RGBList[i].p1.x); |
WiredHome | 0:326a3f29e21b | 316 | rgbVal[i] = mag; |
WiredHome | 0:326a3f29e21b | 317 | // update the RGB values |
WiredHome | 0:326a3f29e21b | 318 | lcd.SelectDrawingLayer(MENUS); |
WiredHome | 1:0fdc10700ed2 | 319 | lcd.SetTextCursor(lcd.width() - 100, lcd.height() - 18); |
WiredHome | 0:326a3f29e21b | 320 | lcd.foreground(Blue); |
WiredHome | 0:326a3f29e21b | 321 | lcd.printf("(%02X,%02X,%02X)", rgbVal[0], rgbVal[1], rgbVal[2]); |
WiredHome | 0:326a3f29e21b | 322 | // show sample |
WiredHome | 0:326a3f29e21b | 323 | rgb = RGB(rgbVal[0], rgbVal[1], rgbVal[2]); |
WiredHome | 0:326a3f29e21b | 324 | ShowSampleRGB(); |
WiredHome | 0:326a3f29e21b | 325 | // |
WiredHome | 0:326a3f29e21b | 326 | lcd.SelectDrawingLayer(CANVAS); |
WiredHome | 0:326a3f29e21b | 327 | lcd.foreground(rgb); |
WiredHome | 0:326a3f29e21b | 328 | break; |
WiredHome | 0:326a3f29e21b | 329 | } |
WiredHome | 0:326a3f29e21b | 330 | } |
WiredHome | 0:326a3f29e21b | 331 | } |
WiredHome | 0:326a3f29e21b | 332 | |
WiredHome | 1:0fdc10700ed2 | 333 | void ThickLine(point_t origin, point_t p) |
WiredHome | 1:0fdc10700ed2 | 334 | { |
WiredHome | 2:cf295dad3192 | 335 | double angleN; |
WiredHome | 2:cf295dad3192 | 336 | loc_t dy; |
WiredHome | 2:cf295dad3192 | 337 | loc_t dx; |
WiredHome | 1:0fdc10700ed2 | 338 | point_t s = { 0, 0 }; |
WiredHome | 1:0fdc10700ed2 | 339 | point_t e = { 0, 0 }; |
WiredHome | 1:0fdc10700ed2 | 340 | |
WiredHome | 1:0fdc10700ed2 | 341 | lcd.line(origin,p, rgb); |
WiredHome | 1:0fdc10700ed2 | 342 | INFO(" End @ (%3d,%3d) - (%3d,%3d) [%d]", origin.x, origin.y, p.x, p.y, pensize); |
WiredHome | 1:0fdc10700ed2 | 343 | #define PI 3.14159 |
WiredHome | 1:0fdc10700ed2 | 344 | dy = p.y - origin.y; |
WiredHome | 1:0fdc10700ed2 | 345 | dx = p.x - origin.x; |
WiredHome | 1:0fdc10700ed2 | 346 | INFO("delta (%+3d,%+3d)", dx,dy); |
WiredHome | 2:cf295dad3192 | 347 | angleN = atan2((double)(dy), (double)(dx)); |
WiredHome | 1:0fdc10700ed2 | 348 | if (pensize == 1) { |
WiredHome | 1:0fdc10700ed2 | 349 | lcd.line(origin, p, rgb); |
WiredHome | 1:0fdc10700ed2 | 350 | } else { |
WiredHome | 1:0fdc10700ed2 | 351 | int thickness = pensize/2; |
WiredHome | 1:0fdc10700ed2 | 352 | for (int l=0; l<=pensize; l++) { |
WiredHome | 1:0fdc10700ed2 | 353 | s.x = origin.x + (l - thickness) * cos(angleN+PI/2); |
WiredHome | 1:0fdc10700ed2 | 354 | s.y = origin.y + (l - thickness) * sin(angleN+PI/2); |
WiredHome | 1:0fdc10700ed2 | 355 | e.x = p.x + (l - thickness) * cos(angleN+PI/2); |
WiredHome | 1:0fdc10700ed2 | 356 | e.y = p.y + (l - thickness) * sin(angleN+PI/2); |
WiredHome | 1:0fdc10700ed2 | 357 | lcd.line(s, e, rgb); |
WiredHome | 1:0fdc10700ed2 | 358 | INFO(" %+d @ (%3d,%3d) - (%3d,%3d) a:%+3.2f:%+3.2f", |
WiredHome | 1:0fdc10700ed2 | 359 | l, s.x,s.y, e.x,e.y, angleN, angleN+PI/2); |
WiredHome | 1:0fdc10700ed2 | 360 | } |
WiredHome | 1:0fdc10700ed2 | 361 | } |
WiredHome | 1:0fdc10700ed2 | 362 | } |
WiredHome | 1:0fdc10700ed2 | 363 | |
WiredHome | 1:0fdc10700ed2 | 364 | |
WiredHome | 0:326a3f29e21b | 365 | void SeeIfUserDrawingOnCanvas(point_t p, TouchCode_t touchcode) |
WiredHome | 0:326a3f29e21b | 366 | { |
WiredHome | 0:326a3f29e21b | 367 | if (lcd.Intersect(canvas_rect, p)) { |
WiredHome | 0:326a3f29e21b | 368 | switch (selectedtooltype) { |
WiredHome | 0:326a3f29e21b | 369 | case dot: |
WiredHome | 1:0fdc10700ed2 | 370 | lcd.fillcircle(p, (pensize == 1) ? pensize : pensize/2, rgb); |
WiredHome | 0:326a3f29e21b | 371 | break; |
WiredHome | 0:326a3f29e21b | 372 | case line: |
WiredHome | 0:326a3f29e21b | 373 | if (touchcode == touch) { |
WiredHome | 0:326a3f29e21b | 374 | lcd.fillcircle(p, 1, rgb); |
WiredHome | 0:326a3f29e21b | 375 | origin = p; |
WiredHome | 1:0fdc10700ed2 | 376 | INFO("Origin @ (%3d,%3d)", p.x, p.y); |
WiredHome | 0:326a3f29e21b | 377 | } else if (touchcode == release) { |
WiredHome | 1:0fdc10700ed2 | 378 | ThickLine(origin, p); |
WiredHome | 0:326a3f29e21b | 379 | } |
WiredHome | 0:326a3f29e21b | 380 | break; |
WiredHome | 1:0fdc10700ed2 | 381 | case join: |
WiredHome | 1:0fdc10700ed2 | 382 | if (touchcode == touch) { |
WiredHome | 1:0fdc10700ed2 | 383 | lcd.fillcircle(p, 1, rgb); |
WiredHome | 1:0fdc10700ed2 | 384 | origin = p; |
WiredHome | 1:0fdc10700ed2 | 385 | INFO("Origin @ (%3d,%3d)", p.x, p.y); |
WiredHome | 1:0fdc10700ed2 | 386 | } else if (touchcode == release) { |
WiredHome | 1:0fdc10700ed2 | 387 | ThickLine(origin, p); |
WiredHome | 1:0fdc10700ed2 | 388 | } else if (touchcode == held) { |
WiredHome | 1:0fdc10700ed2 | 389 | ThickLine(origin, p); |
WiredHome | 1:0fdc10700ed2 | 390 | origin = p; |
WiredHome | 1:0fdc10700ed2 | 391 | INFO(" held @ (%3d,%3d)", p.x, p.y); |
WiredHome | 1:0fdc10700ed2 | 392 | } |
WiredHome | 1:0fdc10700ed2 | 393 | break; |
WiredHome | 0:326a3f29e21b | 394 | default: |
WiredHome | 0:326a3f29e21b | 395 | break; |
WiredHome | 0:326a3f29e21b | 396 | } |
WiredHome | 0:326a3f29e21b | 397 | } |
WiredHome | 0:326a3f29e21b | 398 | } |
WiredHome | 0:326a3f29e21b | 399 | |
WiredHome | 0:326a3f29e21b | 400 | |
WiredHome | 0:326a3f29e21b | 401 | int main() |
WiredHome | 0:326a3f29e21b | 402 | { |
WiredHome | 0:326a3f29e21b | 403 | pc.baud(460800); // I like a snappy terminal, so crank it up! |
WiredHome | 0:326a3f29e21b | 404 | pc.printf("\r\nRA8875 Menu - Build " __DATE__ " " __TIME__ "\r\n"); |
WiredHome | 0:326a3f29e21b | 405 | |
WiredHome | 1:0fdc10700ed2 | 406 | INFO("Turning on display"); |
WiredHome | 0:326a3f29e21b | 407 | lcd.init(); |
WiredHome | 0:326a3f29e21b | 408 | menu.init(); |
WiredHome | 0:326a3f29e21b | 409 | InitTS(); |
WiredHome | 0:326a3f29e21b | 410 | InitDisplay(); |
WiredHome | 0:326a3f29e21b | 411 | |
WiredHome | 1:0fdc10700ed2 | 412 | INFO("processing loop..."); |
WiredHome | 0:326a3f29e21b | 413 | |
WiredHome | 0:326a3f29e21b | 414 | for (;;) { |
WiredHome | 0:326a3f29e21b | 415 | point_t p; |
WiredHome | 0:326a3f29e21b | 416 | TouchCode_t touchcode = lcd.TouchPanelReadable(&p); |
WiredHome | 0:326a3f29e21b | 417 | |
WiredHome | 0:326a3f29e21b | 418 | if (touchcode != no_touch) { |
WiredHome | 0:326a3f29e21b | 419 | int curLayer = lcd.GetDrawingLayer(); |
WiredHome | 0:326a3f29e21b | 420 | lcd.SelectDrawingLayer(MENUS); |
WiredHome | 0:326a3f29e21b | 421 | lcd.foreground(Blue); |
WiredHome | 0:326a3f29e21b | 422 | lcd.SetTextCursor(0, 255); |
WiredHome | 0:326a3f29e21b | 423 | lcd.printf("(%3d,%3d) - (%3d,%3d)", origin.x, origin.y, p.x, p.y); |
WiredHome | 0:326a3f29e21b | 424 | lcd.SelectDrawingLayer(curLayer); |
WiredHome | 0:326a3f29e21b | 425 | |
WiredHome | 0:326a3f29e21b | 426 | bool menuHandledIt = menu.HandledTouch(p, touchcode); |
WiredHome | 0:326a3f29e21b | 427 | if (menuHandledIt) { |
WiredHome | 0:326a3f29e21b | 428 | // menu handled it |
WiredHome | 0:326a3f29e21b | 429 | } else { |
WiredHome | 0:326a3f29e21b | 430 | // app to handle the touch |
WiredHome | 0:326a3f29e21b | 431 | if (!menu.isVisible()) { |
WiredHome | 1:0fdc10700ed2 | 432 | SeeIfUserSelectingRGBValues(p, touchcode); |
WiredHome | 0:326a3f29e21b | 433 | SeeIfUserDrawingOnCanvas(p, touchcode); |
WiredHome | 0:326a3f29e21b | 434 | } else { /* MENU */ |
WiredHome | 1:0fdc10700ed2 | 435 | WARN("on menu - invalid x,y"); |
WiredHome | 0:326a3f29e21b | 436 | menu.Hide(); |
WiredHome | 0:326a3f29e21b | 437 | } |
WiredHome | 0:326a3f29e21b | 438 | } |
WiredHome | 0:326a3f29e21b | 439 | } else { |
WiredHome | 0:326a3f29e21b | 440 | //non-touch |
WiredHome | 0:326a3f29e21b | 441 | } |
WiredHome | 0:326a3f29e21b | 442 | } |
WiredHome | 0:326a3f29e21b | 443 | } |
WiredHome | 0:326a3f29e21b | 444 | #include <stdarg.h> |
WiredHome | 0:326a3f29e21b | 445 | //Custom override for error() |
WiredHome | 0:326a3f29e21b | 446 | void error(const char* format, ...) |
WiredHome | 0:326a3f29e21b | 447 | { |
WiredHome | 0:326a3f29e21b | 448 | char sprintf_buffer[128]; |
WiredHome | 0:326a3f29e21b | 449 | |
WiredHome | 0:326a3f29e21b | 450 | va_list arg; |
WiredHome | 0:326a3f29e21b | 451 | va_start(arg, format); |
WiredHome | 0:326a3f29e21b | 452 | vsprintf(sprintf_buffer, format, arg); |
WiredHome | 0:326a3f29e21b | 453 | va_end(arg); |
WiredHome | 0:326a3f29e21b | 454 | |
WiredHome | 0:326a3f29e21b | 455 | fprintf(stderr, "SW err: %s", sprintf_buffer); |
WiredHome | 0:326a3f29e21b | 456 | } |
WiredHome | 0:326a3f29e21b | 457 | |
WiredHome | 0:326a3f29e21b | 458 | // Reset_Handler |
WiredHome | 0:326a3f29e21b | 459 | // NMI_Handler |
WiredHome | 0:326a3f29e21b | 460 | // HardFault_Handler |
WiredHome | 0:326a3f29e21b | 461 | // MemManage_Handler |
WiredHome | 0:326a3f29e21b | 462 | // BusFault_Handler |
WiredHome | 0:326a3f29e21b | 463 | // UsageFault_Handler |
WiredHome | 0:326a3f29e21b | 464 | extern "C" void HardFault_Handler() |
WiredHome | 0:326a3f29e21b | 465 | { |
WiredHome | 0:326a3f29e21b | 466 | printf("\r\n\r\nHard Fault!\r\n"); |
WiredHome | 0:326a3f29e21b | 467 | wait_ms(500); |
WiredHome | 0:326a3f29e21b | 468 | NVIC_SystemReset(); |
WiredHome | 0:326a3f29e21b | 469 | } |
WiredHome | 0:326a3f29e21b | 470 | extern "C" void NMI_Handler() |
WiredHome | 0:326a3f29e21b | 471 | { |
WiredHome | 0:326a3f29e21b | 472 | printf("\r\n\r\nNMI Fault!\r\n"); |
WiredHome | 0:326a3f29e21b | 473 | wait_ms(500); |
WiredHome | 0:326a3f29e21b | 474 | NVIC_SystemReset(); |
WiredHome | 0:326a3f29e21b | 475 | } |
WiredHome | 0:326a3f29e21b | 476 | extern "C" void MemManage_Handler() |
WiredHome | 0:326a3f29e21b | 477 | { |
WiredHome | 0:326a3f29e21b | 478 | printf("\r\n\r\nMemManage Fault!\r\n"); |
WiredHome | 0:326a3f29e21b | 479 | wait_ms(500); |
WiredHome | 0:326a3f29e21b | 480 | NVIC_SystemReset(); |
WiredHome | 0:326a3f29e21b | 481 | } |
WiredHome | 0:326a3f29e21b | 482 | extern "C" void BusFault_Handler() |
WiredHome | 0:326a3f29e21b | 483 | { |
WiredHome | 0:326a3f29e21b | 484 | printf("\r\n\r\nBusFault Fault!\r\n"); |
WiredHome | 0:326a3f29e21b | 485 | wait_ms(500); |
WiredHome | 0:326a3f29e21b | 486 | NVIC_SystemReset(); |
WiredHome | 0:326a3f29e21b | 487 | } |
WiredHome | 0:326a3f29e21b | 488 | extern "C" void UsageFault_Handler() |
WiredHome | 0:326a3f29e21b | 489 | { |
WiredHome | 0:326a3f29e21b | 490 | printf("\r\n\r\nUsageFault Fault!\r\n"); |
WiredHome | 0:326a3f29e21b | 491 | wait_ms(500); |
WiredHome | 0:326a3f29e21b | 492 | NVIC_SystemReset(); |
WiredHome | 0:326a3f29e21b | 493 | } |