KSM edits to RA8875

Dependents:   Liz_Test_Code

Committer:
kerrysmartin
Date:
Thu Jul 11 14:02:46 2019 +0000
Revision:
177:8620cdfcdbf2
Parent:
176:4ab96d33a8ec
Initial check in

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 152:a013ac0133e4 1 // RA8875 Display Controller Library.
WiredHome 152:a013ac0133e4 2 //
WiredHome 152:a013ac0133e4 3 // This is being created for a Raio RA8875-based display from buydisplay.com,
WiredHome 152:a013ac0133e4 4 // which is either 480 x 272 or 800 x 480, using a 4-wire SPI interface.
WiredHome 152:a013ac0133e4 5 // Support is provided for both a keypad and a resistive touch-screen.
WiredHome 152:a013ac0133e4 6 //
WiredHome 152:a013ac0133e4 7 // See the RA8875.h file for full details.
WiredHome 152:a013ac0133e4 8 //
WiredHome 152:a013ac0133e4 9 // 20161106: Updated the initialization to set the various registers based on
WiredHome 152:a013ac0133e4 10 // the BuyDisplay.com example code. This altered several registers
WiredHome 152:a013ac0133e4 11 // for the 800x480 display driver.
WiredHome 152:a013ac0133e4 12 //
WiredHome 19:3f82c1161fd2 13 #include "RA8875.h"
WiredHome 19:3f82c1161fd2 14
WiredHome 145:5eb2492acdda 15 //#include "Utility.h" // private memory manager
WiredHome 145:5eb2492acdda 16 #ifndef UTILITY_H
WiredHome 145:5eb2492acdda 17 #define swMalloc malloc // use the standard
WiredHome 145:5eb2492acdda 18 #define swFree free
WiredHome 145:5eb2492acdda 19 #endif
WiredHome 145:5eb2492acdda 20
WiredHome 41:2956a0a221e5 21 //#define DEBUG "RAIO"
WiredHome 19:3f82c1161fd2 22 // ...
WiredHome 19:3f82c1161fd2 23 // INFO("Stuff to show %d", var); // new-line is automatically appended
WiredHome 19:3f82c1161fd2 24 //
WiredHome 19:3f82c1161fd2 25 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 98:ecebed9b80b2 26 #define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 98:ecebed9b80b2 27 #define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 98:ecebed9b80b2 28 #define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 98:ecebed9b80b2 29 static void HexDump(const char * title, const uint8_t * p, int count)
WiredHome 73:f22a18707b5e 30 {
WiredHome 73:f22a18707b5e 31 int i;
WiredHome 73:f22a18707b5e 32 char buf[100] = "0000: ";
WiredHome 73:f22a18707b5e 33
WiredHome 73:f22a18707b5e 34 if (*title)
WiredHome 73:f22a18707b5e 35 INFO("%s", title);
WiredHome 73:f22a18707b5e 36 for (i=0; i<count; ) {
WiredHome 73:f22a18707b5e 37 sprintf(buf + strlen(buf), "%02X ", *(p+i));
WiredHome 73:f22a18707b5e 38 if ((++i & 0x0F) == 0x00) {
WiredHome 73:f22a18707b5e 39 INFO("%s", buf);
WiredHome 73:f22a18707b5e 40 if (i < count)
WiredHome 73:f22a18707b5e 41 sprintf(buf, "%04X: ", i);
WiredHome 73:f22a18707b5e 42 else
WiredHome 73:f22a18707b5e 43 buf[0] = '\0';
WiredHome 73:f22a18707b5e 44 }
WiredHome 73:f22a18707b5e 45 }
WiredHome 73:f22a18707b5e 46 if (strlen(buf))
WiredHome 73:f22a18707b5e 47 INFO("%s", buf);
WiredHome 73:f22a18707b5e 48 }
WiredHome 19:3f82c1161fd2 49 #else
WiredHome 19:3f82c1161fd2 50 #define INFO(x, ...)
WiredHome 19:3f82c1161fd2 51 #define WARN(x, ...)
WiredHome 19:3f82c1161fd2 52 #define ERR(x, ...)
WiredHome 73:f22a18707b5e 53 #define HexDump(a, b, c)
WiredHome 19:3f82c1161fd2 54 #endif
WiredHome 19:3f82c1161fd2 55
WiredHome 109:7b94f06f085b 56 // Defaults. Users can override this with the init() method.
WiredHome 19:3f82c1161fd2 57 #define RA8875_DISPLAY_WIDTH 480
WiredHome 19:3f82c1161fd2 58 #define RA8875_DISPLAY_HEIGHT 272
WiredHome 43:3becae133285 59 #define RA8875_COLORDEPTH_BPP 16 /* Not an API */
WiredHome 19:3f82c1161fd2 60
WiredHome 19:3f82c1161fd2 61 #ifdef PERF_METRICS
WiredHome 19:3f82c1161fd2 62 #define PERFORMANCE_RESET performance.reset()
WiredHome 19:3f82c1161fd2 63 #define REGISTERPERFORMANCE(a) RegisterPerformance(a)
WiredHome 68:ab08efabfc88 64 #define COUNTIDLETIME(a) CountIdleTime(a)
WiredHome 73:f22a18707b5e 65 static const char *metricsName[] = {
WiredHome 109:7b94f06f085b 66 "Cls", "Pixel", "Pixel Stream", "Boolean Stream",
WiredHome 41:2956a0a221e5 67 "Read Pixel", "Read Pixel Stream",
WiredHome 73:f22a18707b5e 68 "Line",
WiredHome 73:f22a18707b5e 69 "Rectangle", "Rounded Rectangle",
WiredHome 68:ab08efabfc88 70 "Triangle", "Circle", "Ellipse"
WiredHome 19:3f82c1161fd2 71 };
WiredHome 91:ca5f829e6d27 72 uint16_t commandsUsed[256]; // track which commands are used with simple counter of number of hits.
WiredHome 19:3f82c1161fd2 73 #else
WiredHome 19:3f82c1161fd2 74 #define PERFORMANCE_RESET
WiredHome 19:3f82c1161fd2 75 #define REGISTERPERFORMANCE(a)
WiredHome 68:ab08efabfc88 76 #define COUNTIDLETIME(a)
WiredHome 19:3f82c1161fd2 77 #endif
WiredHome 19:3f82c1161fd2 78
WiredHome 73:f22a18707b5e 79 // When it is going to poll a register for completion, how many
WiredHome 19:3f82c1161fd2 80 // uSec should it wait between each polling activity.
WiredHome 19:3f82c1161fd2 81 #define POLLWAITuSec 10
WiredHome 19:3f82c1161fd2 82
WiredHome 75:ca78388cfd77 83 // Private RawKeyMap for the Keyboard interface
WiredHome 79:544eb4964795 84 static const uint8_t DefaultKeyMap[22] = {
WiredHome 77:9206c13aa527 85 0,
WiredHome 77:9206c13aa527 86 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
WiredHome 75:ca78388cfd77 87 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
WiredHome 75:ca78388cfd77 88 255
WiredHome 77:9206c13aa527 89 };
WiredHome 75:ca78388cfd77 90
WiredHome 79:544eb4964795 91 static const char * ErrMessages[] = {
WiredHome 79:544eb4964795 92 "noerror", ///< no errors, command completed successfully
WiredHome 89:04575562c961 93 "bad parameter", ///< one or more parameters are invalid
WiredHome 89:04575562c961 94 "file not found", ///< specified file could not be found
WiredHome 89:04575562c961 95 "not bmp format", ///< file is not a .bmp file
WiredHome 89:04575562c961 96 "not ico format", ///< file is not a .ico file
WiredHome 89:04575562c961 97 "not supported format", ///< file format is not yet supported
WiredHome 89:04575562c961 98 "image too big", ///< image is too large for the screen
WiredHome 89:04575562c961 99 "not enough ram", ///< could not allocate ram for scanline
WiredHome 125:7a0b70f56550 100 "touch cal. timeout", ///< calibration could not complete in time
WiredHome 125:7a0b70f56550 101 "external abort", ///< during an idle callback, the user code initiated an abort
WiredHome 79:544eb4964795 102 };
WiredHome 19:3f82c1161fd2 103
WiredHome 163:17526689a3ed 104 typedef struct {
WiredHome 163:17526689a3ed 105 uint8_t b;
WiredHome 163:17526689a3ed 106 uint8_t g;
WiredHome 163:17526689a3ed 107 uint8_t r;
WiredHome 163:17526689a3ed 108 uint8_t a;
WiredHome 163:17526689a3ed 109 } rgbTrio_t;
WiredHome 163:17526689a3ed 110
WiredHome 164:76edd7d9cb68 111 /// This is defined as a "Web-Safe" color palette of 216 colors.
WiredHome 164:76edd7d9cb68 112 ///
WiredHome 164:76edd7d9cb68 113 /// It is defined so it can be emitted into a BMP file as the color palette, and it is then used
WiredHome 164:76edd7d9cb68 114 /// for downscaling from higher resolution color depth to an 8-bit format.
WiredHome 164:76edd7d9cb68 115 ///
WiredHome 163:17526689a3ed 116 static const rgbTrio_t WebColorPalette[] = {
WiredHome 163:17526689a3ed 117 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x33,0xFF}, {0x00,0x00,0x66,0xFF}, {0x00,0x00,0x99,0xFF}, {0x00,0x00,0xCC,0xFF}, {0x00,0x00,0xFF,0xFF},
WiredHome 163:17526689a3ed 118 {0x00,0x33,0x00,0xFF}, {0x00,0x33,0x33,0xFF}, {0x00,0x33,0x66,0xFF}, {0x00,0x33,0x99,0xFF}, {0x00,0x33,0xCC,0xFF}, {0x00,0x33,0xFF,0xFF},
WiredHome 163:17526689a3ed 119 {0x00,0x66,0x00,0xFF}, {0x00,0x66,0x33,0xFF}, {0x00,0x66,0x66,0xFF}, {0x00,0x66,0x99,0xFF}, {0x00,0x66,0xCC,0xFF}, {0x00,0x66,0xFF,0xFF},
WiredHome 163:17526689a3ed 120 {0x00,0x99,0x00,0xFF}, {0x00,0x99,0x33,0xFF}, {0x00,0x99,0x66,0xFF}, {0x00,0x99,0x99,0xFF}, {0x00,0x99,0xCC,0xFF}, {0x00,0x99,0xFF,0xFF},
WiredHome 163:17526689a3ed 121 {0x00,0xCC,0x00,0xFF}, {0x00,0xCC,0x33,0xFF}, {0x00,0xCC,0x66,0xFF}, {0x00,0xCC,0x99,0xFF}, {0x00,0xCC,0xCC,0xFF}, {0x00,0xCC,0xFF,0xFF},
WiredHome 163:17526689a3ed 122 {0x00,0xFF,0x00,0xFF}, {0x00,0xFF,0x33,0xFF}, {0x00,0xFF,0x66,0xFF}, {0x00,0xFF,0x99,0xFF}, {0x00,0xFF,0xCC,0xFF}, {0x00,0xFF,0xFF,0xFF},
WiredHome 163:17526689a3ed 123 {0x33,0x00,0x00,0xFF}, {0x33,0x00,0x33,0xFF}, {0x33,0x00,0x66,0xFF}, {0x33,0x00,0x99,0xFF}, {0x33,0x00,0xCC,0xFF}, {0x33,0x00,0xFF,0xFF},
WiredHome 163:17526689a3ed 124 {0x33,0x33,0x00,0xFF}, {0x33,0x33,0x33,0xFF}, {0x33,0x33,0x66,0xFF}, {0x33,0x33,0x99,0xFF}, {0x33,0x33,0xCC,0xFF}, {0x33,0x33,0xFF,0xFF},
WiredHome 163:17526689a3ed 125 {0x33,0x66,0x00,0xFF}, {0x33,0x66,0x33,0xFF}, {0x33,0x66,0x66,0xFF}, {0x33,0x66,0x99,0xFF}, {0x33,0x66,0xCC,0xFF}, {0x33,0x66,0xFF,0xFF},
WiredHome 163:17526689a3ed 126 {0x33,0x99,0x00,0xFF}, {0x33,0x99,0x33,0xFF}, {0x33,0x99,0x66,0xFF}, {0x33,0x99,0x99,0xFF}, {0x33,0x99,0xCC,0xFF}, {0x33,0x99,0xFF,0xFF},
WiredHome 163:17526689a3ed 127 {0x33,0xCC,0x00,0xFF}, {0x33,0xCC,0x33,0xFF}, {0x33,0xCC,0x66,0xFF}, {0x33,0xCC,0x99,0xFF}, {0x33,0xCC,0xCC,0xFF}, {0x33,0xCC,0xFF,0xFF},
WiredHome 163:17526689a3ed 128 {0x33,0xFF,0x00,0xFF}, {0x33,0xFF,0x33,0xFF}, {0x33,0xFF,0x66,0xFF}, {0x33,0xFF,0x99,0xFF}, {0x33,0xFF,0xCC,0xFF}, {0x33,0xFF,0xFF,0xFF},
WiredHome 163:17526689a3ed 129 {0x66,0x00,0x00,0xFF}, {0x66,0x00,0x33,0xFF}, {0x66,0x00,0x66,0xFF}, {0x66,0x00,0x99,0xFF}, {0x66,0x00,0xCC,0xFF}, {0x66,0x00,0xFF,0xFF},
WiredHome 163:17526689a3ed 130 {0x66,0x33,0x00,0xFF}, {0x66,0x33,0x33,0xFF}, {0x66,0x33,0x66,0xFF}, {0x66,0x33,0x99,0xFF}, {0x66,0x33,0xCC,0xFF}, {0x66,0x33,0xFF,0xFF},
WiredHome 163:17526689a3ed 131 {0x66,0x66,0x00,0xFF}, {0x66,0x66,0x33,0xFF}, {0x66,0x66,0x66,0xFF}, {0x66,0x66,0x99,0xFF}, {0x66,0x66,0xCC,0xFF}, {0x66,0x66,0xFF,0xFF},
WiredHome 163:17526689a3ed 132 {0x66,0x99,0x00,0xFF}, {0x66,0x99,0x33,0xFF}, {0x66,0x99,0x66,0xFF}, {0x66,0x99,0x99,0xFF}, {0x66,0x99,0xCC,0xFF}, {0x66,0x99,0xFF,0xFF},
WiredHome 163:17526689a3ed 133 {0x66,0xCC,0x00,0xFF}, {0x66,0xCC,0x33,0xFF}, {0x66,0xCC,0x66,0xFF}, {0x66,0xCC,0x99,0xFF}, {0x66,0xCC,0xCC,0xFF}, {0x66,0xCC,0xFF,0xFF},
WiredHome 163:17526689a3ed 134 {0x66,0xFF,0x00,0xFF}, {0x66,0xFF,0x33,0xFF}, {0x66,0xFF,0x66,0xFF}, {0x66,0xFF,0x99,0xFF}, {0x66,0xFF,0xCC,0xFF}, {0x66,0xFF,0xFF,0xFF},
WiredHome 163:17526689a3ed 135 {0x99,0x00,0x00,0xFF}, {0x99,0x00,0x33,0xFF}, {0x99,0x00,0x66,0xFF}, {0x99,0x00,0x99,0xFF}, {0x99,0x00,0xCC,0xFF}, {0x99,0x00,0xFF,0xFF},
WiredHome 163:17526689a3ed 136 {0x99,0x33,0x00,0xFF}, {0x99,0x33,0x33,0xFF}, {0x99,0x33,0x66,0xFF}, {0x99,0x33,0x99,0xFF}, {0x99,0x33,0xCC,0xFF}, {0x99,0x33,0xFF,0xFF},
WiredHome 163:17526689a3ed 137 {0x99,0x66,0x00,0xFF}, {0x99,0x66,0x33,0xFF}, {0x99,0x66,0x66,0xFF}, {0x99,0x66,0x99,0xFF}, {0x99,0x66,0xCC,0xFF}, {0x99,0x66,0xFF,0xFF},
WiredHome 163:17526689a3ed 138 {0x99,0x99,0x00,0xFF}, {0x99,0x99,0x33,0xFF}, {0x99,0x99,0x66,0xFF}, {0x99,0x99,0x99,0xFF}, {0x99,0x99,0xCC,0xFF}, {0x99,0x99,0xFF,0xFF},
WiredHome 163:17526689a3ed 139 {0x99,0xCC,0x00,0xFF}, {0x99,0xCC,0x33,0xFF}, {0x99,0xCC,0x66,0xFF}, {0x99,0xCC,0x99,0xFF}, {0x99,0xCC,0xCC,0xFF}, {0x99,0xCC,0xFF,0xFF},
WiredHome 163:17526689a3ed 140 {0x99,0xFF,0x00,0xFF}, {0x99,0xFF,0x33,0xFF}, {0x99,0xFF,0x66,0xFF}, {0x99,0xFF,0x99,0xFF}, {0x99,0xFF,0xCC,0xFF}, {0x99,0xFF,0xFF,0xFF},
WiredHome 163:17526689a3ed 141 {0xCC,0x00,0x00,0xFF}, {0xCC,0x00,0x33,0xFF}, {0xCC,0x00,0x66,0xFF}, {0xCC,0x00,0x99,0xFF}, {0xCC,0x00,0xCC,0xFF}, {0xCC,0x00,0xFF,0xFF},
WiredHome 163:17526689a3ed 142 {0xCC,0x33,0x00,0xFF}, {0xCC,0x33,0x33,0xFF}, {0xCC,0x33,0x66,0xFF}, {0xCC,0x33,0x99,0xFF}, {0xCC,0x33,0xCC,0xFF}, {0xCC,0x33,0xFF,0xFF},
WiredHome 163:17526689a3ed 143 {0xCC,0x66,0x00,0xFF}, {0xCC,0x66,0x33,0xFF}, {0xCC,0x66,0x66,0xFF}, {0xCC,0x66,0x99,0xFF}, {0xCC,0x66,0xCC,0xFF}, {0xCC,0x66,0xFF,0xFF},
WiredHome 163:17526689a3ed 144 {0xCC,0x99,0x00,0xFF}, {0xCC,0x99,0x33,0xFF}, {0xCC,0x99,0x66,0xFF}, {0xCC,0x99,0x99,0xFF}, {0xCC,0x99,0xCC,0xFF}, {0xCC,0x99,0xFF,0xFF},
WiredHome 163:17526689a3ed 145 {0xCC,0xCC,0x00,0xFF}, {0xCC,0xCC,0x33,0xFF}, {0xCC,0xCC,0x66,0xFF}, {0xCC,0xCC,0x99,0xFF}, {0xCC,0xCC,0xCC,0xFF}, {0xCC,0xCC,0xFF,0xFF},
WiredHome 163:17526689a3ed 146 {0xCC,0xFF,0x00,0xFF}, {0xCC,0xFF,0x33,0xFF}, {0xCC,0xFF,0x66,0xFF}, {0xCC,0xFF,0x99,0xFF}, {0xCC,0xFF,0xCC,0xFF}, {0xCC,0xFF,0xFF,0xFF},
WiredHome 163:17526689a3ed 147 {0xFF,0x00,0x00,0xFF}, {0xFF,0x00,0x33,0xFF}, {0xFF,0x00,0x66,0xFF}, {0xFF,0x00,0x99,0xFF}, {0xFF,0x00,0xCC,0xFF}, {0xFF,0x00,0xFF,0xFF},
WiredHome 163:17526689a3ed 148 {0xFF,0x33,0x00,0xFF}, {0xFF,0x33,0x33,0xFF}, {0xFF,0x33,0x66,0xFF}, {0xFF,0x33,0x99,0xFF}, {0xFF,0x33,0xCC,0xFF}, {0xFF,0x33,0xFF,0xFF},
WiredHome 163:17526689a3ed 149 {0xFF,0x66,0x00,0xFF}, {0xFF,0x66,0x33,0xFF}, {0xFF,0x66,0x66,0xFF}, {0xFF,0x66,0x99,0xFF}, {0xFF,0x66,0xCC,0xFF}, {0xFF,0x66,0xFF,0xFF},
WiredHome 163:17526689a3ed 150 {0xFF,0x99,0x00,0xFF}, {0xFF,0x99,0x33,0xFF}, {0xFF,0x99,0x66,0xFF}, {0xFF,0x99,0x99,0xFF}, {0xFF,0x99,0xCC,0xFF}, {0xFF,0x99,0xFF,0xFF},
WiredHome 163:17526689a3ed 151 {0xFF,0xCC,0x00,0xFF}, {0xFF,0xCC,0x33,0xFF}, {0xFF,0xCC,0x66,0xFF}, {0xFF,0xCC,0x99,0xFF}, {0xFF,0xCC,0xCC,0xFF}, {0xFF,0xCC,0xFF,0xFF},
WiredHome 163:17526689a3ed 152 {0xFF,0xFF,0x00,0xFF}, {0xFF,0xFF,0x33,0xFF}, {0xFF,0xFF,0x66,0xFF}, {0xFF,0xFF,0x99,0xFF}, {0xFF,0xFF,0xCC,0xFF}, {0xFF,0xFF,0xFF,0xFF},
WiredHome 163:17526689a3ed 153
WiredHome 163:17526689a3ed 154 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF},
WiredHome 163:17526689a3ed 155 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF},
WiredHome 163:17526689a3ed 156 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF},
WiredHome 163:17526689a3ed 157 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF},
WiredHome 163:17526689a3ed 158 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF},
WiredHome 163:17526689a3ed 159 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF},
WiredHome 163:17526689a3ed 160 {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF}, {0x00,0x00,0x00,0xFF},
WiredHome 163:17526689a3ed 161 };
WiredHome 163:17526689a3ed 162
WiredHome 164:76edd7d9cb68 163 #define sqr(a) ((a) * (a))
WiredHome 164:76edd7d9cb68 164
WiredHome 164:76edd7d9cb68 165 /// Find the nearest color match in the lookup table.
WiredHome 164:76edd7d9cb68 166 ///
WiredHome 164:76edd7d9cb68 167 /// The typical process is to find the difference between a given color and each entry in
WiredHome 164:76edd7d9cb68 168 /// the table. The difference is defined as:
WiredHome 164:76edd7d9cb68 169 /// diff = sqrt(sqr(r - table[i].r) + sqr(g - table[i].g) + sqr(b - table[i].b))
WiredHome 164:76edd7d9cb68 170 /// The square root function is very CPU intensive, especially w/o a floating point unit,
WiredHome 164:76edd7d9cb68 171 /// so that step is omitted to speed it up a bit.
WiredHome 164:76edd7d9cb68 172 ///
WiredHome 163:17526689a3ed 173 static int FindNearestWebColor(uint8_t r, uint8_t g, uint8_t b) {
WiredHome 163:17526689a3ed 174 int bestNdx = 0;
WiredHome 164:76edd7d9cb68 175 float bestDiff = (sqr(r - WebColorPalette[0].r) + sqr(g - WebColorPalette[0].g) + sqr(b - WebColorPalette[0].b));
WiredHome 163:17526689a3ed 176 for (int i=1; i<216; i++) {
WiredHome 164:76edd7d9cb68 177 float thisDiff = (sqr(r - WebColorPalette[i].r) + sqr(g - WebColorPalette[i].g) + sqr(b - WebColorPalette[i].b));
WiredHome 163:17526689a3ed 178 if (thisDiff < bestDiff) {
WiredHome 163:17526689a3ed 179 bestDiff = thisDiff;
WiredHome 163:17526689a3ed 180 bestNdx = i;
WiredHome 163:17526689a3ed 181 }
WiredHome 163:17526689a3ed 182 }
WiredHome 163:17526689a3ed 183 return bestNdx;
WiredHome 163:17526689a3ed 184 }
WiredHome 163:17526689a3ed 185
WiredHome 166:53fd4a876dac 186 // Non-Touch, or Resistive Touch when later initialized that way
WiredHome 166:53fd4a876dac 187 //
WiredHome 124:1690a7ae871c 188 RA8875::RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset,
WiredHome 124:1690a7ae871c 189 const char *name)
WiredHome 58:26658a56112a 190 : GraphicsDisplay(name)
WiredHome 58:26658a56112a 191 , spi(mosi, miso, sclk)
WiredHome 19:3f82c1161fd2 192 , cs(csel)
WiredHome 19:3f82c1161fd2 193 , res(reset)
WiredHome 19:3f82c1161fd2 194 {
WiredHome 176:4ab96d33a8ec 195 touchInfo = (touchInfo_T *)malloc(RESISTIVE_TOUCH_POINTS * sizeof(touchInfo_T));
WiredHome 176:4ab96d33a8ec 196 if (touchInfo)
WiredHome 176:4ab96d33a8ec 197 useTouchPanel = TP_RES;
WiredHome 176:4ab96d33a8ec 198 else
WiredHome 176:4ab96d33a8ec 199 useTouchPanel = TP_NONE; // unfortnately a silent failure, but unlikely
WiredHome 157:1565f38ca44b 200 tpFQFN = NULL;
WiredHome 157:1565f38ca44b 201 tpCalMessage = NULL;
WiredHome 124:1690a7ae871c 202 m_irq = NULL;
WiredHome 124:1690a7ae871c 203 m_i2c = NULL;
WiredHome 165:695c24cc5197 204 m_wake = NULL;
WiredHome 96:40b74dd3695b 205 c_callback = NULL;
WiredHome 96:40b74dd3695b 206 obj_callback = NULL;
WiredHome 96:40b74dd3695b 207 method_callback = NULL;
WiredHome 123:2f45e80fec5f 208 idle_callback = NULL;
WiredHome 153:8a85efb3eb71 209 fontScaleX = fontScaleY = 1;
WiredHome 19:3f82c1161fd2 210 }
WiredHome 19:3f82c1161fd2 211
WiredHome 166:53fd4a876dac 212 // Touch, based on FT5206 Controller Chip
WiredHome 166:53fd4a876dac 213 //
WiredHome 124:1690a7ae871c 214 RA8875::RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset,
WiredHome 124:1690a7ae871c 215 PinName sda, PinName scl, PinName irq, const char * name)
WiredHome 124:1690a7ae871c 216 : GraphicsDisplay(name)
WiredHome 124:1690a7ae871c 217 , spi(mosi, miso, sclk)
WiredHome 124:1690a7ae871c 218 , cs(csel)
WiredHome 124:1690a7ae871c 219 , res(reset)
WiredHome 124:1690a7ae871c 220 {
WiredHome 157:1565f38ca44b 221 tpFQFN = NULL;
WiredHome 157:1565f38ca44b 222 tpCalMessage = NULL;
WiredHome 124:1690a7ae871c 223 m_irq = new InterruptIn(irq);
WiredHome 124:1690a7ae871c 224 m_i2c = new I2C(sda, scl);
WiredHome 165:695c24cc5197 225
WiredHome 165:695c24cc5197 226 // Cap touch panel config
WiredHome 166:53fd4a876dac 227 touchInfo = (touchInfo_T *)malloc(FT5206_TOUCH_POINTS * sizeof(touchInfo_T));
WiredHome 166:53fd4a876dac 228 if (touchInfo)
WiredHome 166:53fd4a876dac 229 useTouchPanel = TP_FT5206;
WiredHome 166:53fd4a876dac 230 else
WiredHome 166:53fd4a876dac 231 useTouchPanel = TP_NONE; // unfortnately a silent failure, but unlikely
WiredHome 165:695c24cc5197 232 m_addr = (FT5206_I2C_ADDRESS << 1);
WiredHome 165:695c24cc5197 233 m_i2c->frequency(FT5206_I2C_FREQUENCY);
WiredHome 165:695c24cc5197 234 m_wake = NULL; // not used for FT5206
WiredHome 165:695c24cc5197 235
WiredHome 124:1690a7ae871c 236 c_callback = NULL;
WiredHome 124:1690a7ae871c 237 obj_callback = NULL;
WiredHome 124:1690a7ae871c 238 method_callback = NULL;
WiredHome 124:1690a7ae871c 239 idle_callback = NULL;
WiredHome 153:8a85efb3eb71 240 fontScaleX = fontScaleY = 1;
WiredHome 124:1690a7ae871c 241
WiredHome 124:1690a7ae871c 242 // Interrupt
WiredHome 124:1690a7ae871c 243 m_irq->mode(PullUp);
WiredHome 124:1690a7ae871c 244 m_irq->enable_irq();
WiredHome 155:b3f225ae572c 245 #if MBED_VERSION >= MBED_ENCODE_VERSION(5,8,0)
WiredHome 155:b3f225ae572c 246 eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
WiredHome 155:b3f225ae572c 247 m_irq->fall(queue.event(callback(this, &RA8875::TouchPanelISR)));
WiredHome 155:b3f225ae572c 248 #elif (MBED_MAJOR_VERSION >= 5) || (MBED_LIBRARY_VERSION > 128) // Is this the right version?
WiredHome 145:5eb2492acdda 249 m_irq->fall(callback(this, &RA8875::TouchPanelISR));
WiredHome 150:35a4db3081c1 250 #else
WiredHome 150:35a4db3081c1 251 m_irq->fall(this, &RA8875::TouchPanelISR);
WiredHome 150:35a4db3081c1 252 #endif
WiredHome 124:1690a7ae871c 253 TouchPanelInit();
WiredHome 124:1690a7ae871c 254 }
WiredHome 124:1690a7ae871c 255
WiredHome 166:53fd4a876dac 256
WiredHome 166:53fd4a876dac 257 // Touch, based on GSL1680 Controller Chip
WiredHome 166:53fd4a876dac 258 //
WiredHome 165:695c24cc5197 259 RA8875::RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset,
WiredHome 165:695c24cc5197 260 PinName sda, PinName scl, PinName wake, PinName irq, const char * name)
WiredHome 165:695c24cc5197 261 : GraphicsDisplay(name)
WiredHome 165:695c24cc5197 262 , spi(mosi, miso, sclk)
WiredHome 165:695c24cc5197 263 , cs(csel)
WiredHome 165:695c24cc5197 264 , res(reset)
WiredHome 165:695c24cc5197 265 {
WiredHome 165:695c24cc5197 266 tpFQFN = NULL;
WiredHome 165:695c24cc5197 267 tpCalMessage = NULL;
WiredHome 165:695c24cc5197 268 m_irq = new InterruptIn(irq);
WiredHome 165:695c24cc5197 269 m_i2c = new I2C(sda, scl);
WiredHome 165:695c24cc5197 270
WiredHome 165:695c24cc5197 271 // Cap touch panel config
WiredHome 166:53fd4a876dac 272 touchInfo = (touchInfo_T *)malloc(FT5206_TOUCH_POINTS * sizeof(touchInfo_T));
WiredHome 166:53fd4a876dac 273 if (touchInfo)
WiredHome 166:53fd4a876dac 274 useTouchPanel = TP_GSL1680;
WiredHome 166:53fd4a876dac 275 else
WiredHome 166:53fd4a876dac 276 useTouchPanel = TP_NONE; // unfortnately a silent failure, but unlikely
WiredHome 165:695c24cc5197 277 m_addr = (GSL1680_I2C_ADDRESS << 1);
WiredHome 165:695c24cc5197 278 m_i2c->frequency(GSL1680_I2C_FREQUENCY);
WiredHome 165:695c24cc5197 279 m_wake = new DigitalOut(wake);
WiredHome 165:695c24cc5197 280
WiredHome 165:695c24cc5197 281 c_callback = NULL;
WiredHome 165:695c24cc5197 282 obj_callback = NULL;
WiredHome 165:695c24cc5197 283 method_callback = NULL;
WiredHome 165:695c24cc5197 284 idle_callback = NULL;
WiredHome 165:695c24cc5197 285 fontScaleX = fontScaleY = 1;
WiredHome 165:695c24cc5197 286
WiredHome 165:695c24cc5197 287 // Interrupt
WiredHome 165:695c24cc5197 288 m_irq->mode(PullUp);
WiredHome 165:695c24cc5197 289 m_irq->enable_irq();
WiredHome 165:695c24cc5197 290 #if MBED_VERSION >= MBED_ENCODE_VERSION(5,8,0)
WiredHome 165:695c24cc5197 291 eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
WiredHome 165:695c24cc5197 292 m_irq->fall(queue.event(callback(this, &RA8875::TouchPanelISR)));
WiredHome 165:695c24cc5197 293 #elif (MBED_MAJOR_VERSION >= 5) || (MBED_LIBRARY_VERSION > 128) // Is this the right version?
WiredHome 165:695c24cc5197 294 m_irq->fall(callback(this, &RA8875::TouchPanelISR));
WiredHome 165:695c24cc5197 295 #else
WiredHome 165:695c24cc5197 296 m_irq->fall(this, &RA8875::TouchPanelISR);
WiredHome 165:695c24cc5197 297 #endif
WiredHome 165:695c24cc5197 298 TouchPanelInit();
WiredHome 165:695c24cc5197 299 }
WiredHome 165:695c24cc5197 300
WiredHome 165:695c24cc5197 301
WiredHome 124:1690a7ae871c 302
WiredHome 19:3f82c1161fd2 303 //RA8875::~RA8875()
WiredHome 19:3f82c1161fd2 304 //{
WiredHome 19:3f82c1161fd2 305 //}
WiredHome 19:3f82c1161fd2 306
WiredHome 154:ad2450fc3dc3 307
WiredHome 131:5bd6ba2ee4a1 308 RetCode_t RA8875::init(int width, int height, int color_bpp, uint8_t poweron, bool keypadon, bool touchscreenon)
WiredHome 79:544eb4964795 309 {
WiredHome 98:ecebed9b80b2 310 font = NULL; // no external font, use internal.
WiredHome 98:ecebed9b80b2 311 pKeyMap = DefaultKeyMap; // set default key map
WiredHome 98:ecebed9b80b2 312 _select(false); // deselect the display
WiredHome 98:ecebed9b80b2 313 frequency(RA8875_DEFAULT_SPI_FREQ); // data rate
WiredHome 79:544eb4964795 314 Reset();
WiredHome 134:f028ed71a0af 315 // Set PLL based on display size from buy-display.com sample code
WiredHome 134:f028ed71a0af 316 if (width == 800) {
WiredHome 175:7be3a1fb7fc2 317 WriteCommand(RA8875_PLLC1, 0x0C); // PLLC1 - Phase Lock Loop registers
WiredHome 134:f028ed71a0af 318 } else {
WiredHome 175:7be3a1fb7fc2 319 WriteCommand(RA8875_PLLC1, 0x0B); // PLLC1 - Phase Lock Loop registers
WiredHome 134:f028ed71a0af 320 }
WiredHome 79:544eb4964795 321 wait_ms(1);
WiredHome 175:7be3a1fb7fc2 322 WriteCommand(RA8875_PLLC2, 0x02);
WiredHome 79:544eb4964795 323 wait_ms(1);
WiredHome 79:544eb4964795 324
WiredHome 79:544eb4964795 325 // System Config Register (SYSR)
WiredHome 105:4f116006ba1f 326 screenbpp = color_bpp;
WiredHome 79:544eb4964795 327 if (color_bpp == 16) {
WiredHome 175:7be3a1fb7fc2 328 WriteCommand(RA8875_SYSR, 0x0C); // 16-bpp (65K colors) color depth, 8-bit interface
WiredHome 79:544eb4964795 329 } else { // color_bpp == 8
WiredHome 175:7be3a1fb7fc2 330 WriteCommand(RA8875_SYSR, 0x00); // 8-bpp (256 colors)
WiredHome 79:544eb4964795 331 }
WiredHome 134:f028ed71a0af 332
WiredHome 134:f028ed71a0af 333 // Set Pixel Clock Setting Register (PCSR) based on display size from buy-display.com sample code
WiredHome 134:f028ed71a0af 334 if (width == 800) {
WiredHome 175:7be3a1fb7fc2 335 WriteCommand(RA8875_PCSR, 0x81); // PDAT on PCLK falling edge, PCLK = 4 x System Clock
WiredHome 134:f028ed71a0af 336 wait_ms(1);
WiredHome 134:f028ed71a0af 337
WiredHome 134:f028ed71a0af 338 // Horizontal Settings
WiredHome 134:f028ed71a0af 339 screenwidth = width;
WiredHome 175:7be3a1fb7fc2 340 WriteCommand(RA8875_HDWR, width/8 - 1); //HDWR//Horizontal Display Width Setting Bit[6:0]
WiredHome 175:7be3a1fb7fc2 341 WriteCommand(RA8875_HNDFTR, 0x00); //HNDFCR//Horizontal Non-Display Period fine tune Bit[3:0]
WiredHome 175:7be3a1fb7fc2 342 WriteCommand(RA8875_HNDR, 0x03); //HNDR//Horizontal Non-Display Period Bit[4:0]
WiredHome 175:7be3a1fb7fc2 343 WriteCommand(RA8875_HSTR, 0x03); //HSTR//HSYNC Start Position[4:0]
WiredHome 175:7be3a1fb7fc2 344 WriteCommand(RA8875_HPWR, 0x0B); //HPWR//HSYNC Polarity ,The period width of HSYNC.
WiredHome 134:f028ed71a0af 345
WiredHome 134:f028ed71a0af 346 // Vertical Settings
WiredHome 134:f028ed71a0af 347 screenheight = height;
WiredHome 175:7be3a1fb7fc2 348 WriteCommand(RA8875_VDHR0, (height-1)&0xFF); //VDHR0 //Vertical Display Height Bit [7:0]
WiredHome 175:7be3a1fb7fc2 349 WriteCommand(RA8875_VDHR1, (height-1)>>8); //VDHR1 //Vertical Display Height Bit [8]
WiredHome 175:7be3a1fb7fc2 350 WriteCommand(RA8875_VNDR0, 0x20); //VNDR0 //Vertical Non-Display Period Bit [7:0]
WiredHome 175:7be3a1fb7fc2 351 WriteCommand(RA8875_VNDR1, 0x00); //VNDR1 //Vertical Non-Display Period Bit [8]
WiredHome 175:7be3a1fb7fc2 352 WriteCommand(RA8875_VSTR0, 0x16); //VSTR0 //VSYNC Start Position[7:0]
WiredHome 175:7be3a1fb7fc2 353 WriteCommand(RA8875_VSTR1, 0x00); //VSTR1 //VSYNC Start Position[8]
WiredHome 175:7be3a1fb7fc2 354 WriteCommand(RA8875_VPWR, 0x01); //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]
WiredHome 134:f028ed71a0af 355 } else {
WiredHome 175:7be3a1fb7fc2 356 WriteCommand(RA8875_PCSR, 0x82); // PDAT on PCLK falling edge, PCLK = 4 x System Clock
WiredHome 134:f028ed71a0af 357 wait_ms(1);
WiredHome 134:f028ed71a0af 358
WiredHome 134:f028ed71a0af 359 // Horizontal Settings
WiredHome 134:f028ed71a0af 360 screenwidth = width;
WiredHome 175:7be3a1fb7fc2 361 WriteCommand(RA8875_HDWR, width/8 - 1); //HDWR//Horizontal Display Width Setting Bit[6:0]
WiredHome 175:7be3a1fb7fc2 362 WriteCommand(RA8875_HNDFTR, 0x02); //HNDFCR//Horizontal Non-Display Period fine tune Bit[3:0]
WiredHome 175:7be3a1fb7fc2 363 WriteCommand(RA8875_HNDR, 0x03); //HNDR//Horizontal Non-Display Period Bit[4:0]
WiredHome 175:7be3a1fb7fc2 364 WriteCommand(RA8875_HSTR, 0x01); //HSTR//HSYNC Start Position[4:0]
WiredHome 175:7be3a1fb7fc2 365 WriteCommand(RA8875_HPWR, 0x03); //HPWR//HSYNC Polarity ,The period width of HSYNC.
WiredHome 134:f028ed71a0af 366
WiredHome 134:f028ed71a0af 367 // Vertical Settings
WiredHome 134:f028ed71a0af 368 screenheight = height;
WiredHome 175:7be3a1fb7fc2 369 WriteCommand(RA8875_VDHR0, (height-1)&0xFF); //VDHR0 //Vertical Display Height Bit [7:0]
WiredHome 175:7be3a1fb7fc2 370 WriteCommand(RA8875_VDHR1, (height-1)>>8); //VDHR1 //Vertical Display Height Bit [8]
WiredHome 175:7be3a1fb7fc2 371 WriteCommand(RA8875_VNDR0, 0x0F); //VNDR0 //Vertical Non-Display Period Bit [7:0]
WiredHome 175:7be3a1fb7fc2 372 WriteCommand(RA8875_VNDR1, 0x00); //VNDR1 //Vertical Non-Display Period Bit [8]
WiredHome 175:7be3a1fb7fc2 373 WriteCommand(RA8875_VSTR0, 0x0e); //VSTR0 //VSYNC Start Position[7:0]
WiredHome 175:7be3a1fb7fc2 374 WriteCommand(RA8875_VSTR1, 0x06); //VSTR1 //VSYNC Start Position[8]
WiredHome 175:7be3a1fb7fc2 375 WriteCommand(RA8875_VPWR, 0x01); //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]
WiredHome 134:f028ed71a0af 376 }
WiredHome 79:544eb4964795 377
WiredHome 100:0b084475d5a9 378 portraitmode = false;
WiredHome 133:e36dcfc2d756 379
WiredHome 79:544eb4964795 380 if (width >= 800 && height >= 480 && color_bpp > 8) {
WiredHome 175:7be3a1fb7fc2 381 WriteCommand(RA8875_DPCR, 0x00); // DPCR - 1-layer mode when the resolution is too high
WiredHome 79:544eb4964795 382 } else {
WiredHome 175:7be3a1fb7fc2 383 WriteCommand(RA8875_DPCR, 0x80); // DPCR - 2-layer mode
WiredHome 79:544eb4964795 384 }
WiredHome 79:544eb4964795 385
WiredHome 79:544eb4964795 386 // Set display image to Blue on Black as default
WiredHome 79:544eb4964795 387 window(0,0, width, height); // Initialize to full screen
WiredHome 79:544eb4964795 388 SetTextCursorControl();
WiredHome 79:544eb4964795 389 foreground(Blue);
WiredHome 79:544eb4964795 390 background(Black);
WiredHome 79:544eb4964795 391 cls(3);
WiredHome 79:544eb4964795 392
WiredHome 79:544eb4964795 393 Power(poweron);
WiredHome 131:5bd6ba2ee4a1 394 Backlight_u8(poweron);
WiredHome 81:01da2e34283d 395 if (keypadon)
WiredHome 81:01da2e34283d 396 KeypadInit();
WiredHome 124:1690a7ae871c 397 if (touchscreenon) {
WiredHome 124:1690a7ae871c 398 if (useTouchPanel == TP_NONE)
WiredHome 124:1690a7ae871c 399 useTouchPanel = TP_RES;
WiredHome 81:01da2e34283d 400 TouchPanelInit();
WiredHome 124:1690a7ae871c 401 }
WiredHome 79:544eb4964795 402 #ifdef PERF_METRICS
WiredHome 79:544eb4964795 403 performance.start();
WiredHome 79:544eb4964795 404 ClearPerformance();
WiredHome 79:544eb4964795 405 #endif
WiredHome 79:544eb4964795 406 return noerror;
WiredHome 79:544eb4964795 407 }
WiredHome 79:544eb4964795 408
WiredHome 79:544eb4964795 409
WiredHome 79:544eb4964795 410 RetCode_t RA8875::Reset(void)
WiredHome 79:544eb4964795 411 {
WiredHome 79:544eb4964795 412 RetCode_t ret;
WiredHome 79:544eb4964795 413
WiredHome 94:203729061e48 414 #if 0
WiredHome 94:203729061e48 415 if (res != (PinName)NC) {
WiredHome 94:203729061e48 416 res = 0; // Active low - assert reset
WiredHome 94:203729061e48 417 wait_ms(2); // must be > 1024 clock periods. (@25 MHz, this is 40.96 usec)
WiredHome 94:203729061e48 418 res = 1; // de-assert reset
WiredHome 94:203729061e48 419 }
WiredHome 94:203729061e48 420 #endif
WiredHome 175:7be3a1fb7fc2 421 ret = WriteCommand(RA8875_PWRR, 0x01); // Apply Display Off, Reset
WiredHome 94:203729061e48 422 wait_ms(2); // no idea if I need to wait, or how long
WiredHome 79:544eb4964795 423 if (ret == noerror) {
WiredHome 175:7be3a1fb7fc2 424 ret = WriteCommand(RA8875_PWRR, 0x00); // Display off, Remove reset
WiredHome 94:203729061e48 425 wait_ms(2); // no idea if I need to wait, or how long
WiredHome 79:544eb4964795 426 }
WiredHome 79:544eb4964795 427 return ret;
WiredHome 79:544eb4964795 428 }
WiredHome 79:544eb4964795 429
WiredHome 79:544eb4964795 430
WiredHome 79:544eb4964795 431 const char * RA8875::GetErrorMessage(RetCode_t code)
WiredHome 79:544eb4964795 432 {
WiredHome 79:544eb4964795 433 if (code >= LastErrCode)
WiredHome 79:544eb4964795 434 code = bad_parameter;
WiredHome 79:544eb4964795 435 return ErrMessages[code];
WiredHome 79:544eb4964795 436 }
WiredHome 79:544eb4964795 437
WiredHome 79:544eb4964795 438
WiredHome 61:8f3153bf0baa 439 uint16_t RA8875::GetDrawingLayer(void)
WiredHome 61:8f3153bf0baa 440 {
WiredHome 61:8f3153bf0baa 441 return (ReadCommand(0x41) & 0x01);
WiredHome 61:8f3153bf0baa 442 }
WiredHome 43:3becae133285 443
WiredHome 79:544eb4964795 444
WiredHome 143:e872d65a710d 445 RetCode_t RA8875::SelectDrawingLayer(uint16_t layer, uint16_t * prevLayer)
WiredHome 43:3becae133285 446 {
WiredHome 142:6e9bff59878a 447 unsigned char mwcr1 = ReadCommand(0x41); // retain all but the currently selected layer
WiredHome 143:e872d65a710d 448
WiredHome 143:e872d65a710d 449 if (prevLayer)
WiredHome 143:e872d65a710d 450 *prevLayer = mwcr1 & 1;
WiredHome 142:6e9bff59878a 451
WiredHome 142:6e9bff59878a 452 mwcr1 &= ~0x01; // remove the current layer
WiredHome 106:c80828f5dea4 453 if (screenwidth >= 800 && screenheight >= 480 && screenbpp > 8) {
WiredHome 142:6e9bff59878a 454 layer = 0;
WiredHome 43:3becae133285 455 } else if (layer > 1) {
WiredHome 142:6e9bff59878a 456 layer = 0;
WiredHome 43:3becae133285 457 }
WiredHome 175:7be3a1fb7fc2 458 return WriteCommand(RA8875_MWCR1, mwcr1 | layer);
WiredHome 43:3becae133285 459 }
WiredHome 43:3becae133285 460
WiredHome 44:207594dece70 461
WiredHome 82:f7d300f26540 462 RA8875::LayerMode_T RA8875::GetLayerMode(void)
WiredHome 82:f7d300f26540 463 {
WiredHome 82:f7d300f26540 464 return (LayerMode_T)(ReadCommand(0x52) & 0x7);
WiredHome 82:f7d300f26540 465 }
WiredHome 82:f7d300f26540 466
WiredHome 82:f7d300f26540 467
WiredHome 53:86d24b9480b9 468 RetCode_t RA8875::SetLayerMode(LayerMode_T mode)
WiredHome 44:207594dece70 469 {
WiredHome 53:86d24b9480b9 470 unsigned char ltpr0 = ReadCommand(0x52) & ~0x7; // retain all but the display layer mode
WiredHome 105:4f116006ba1f 471
WiredHome 53:86d24b9480b9 472 if (mode <= (LayerMode_T)6) {
WiredHome 175:7be3a1fb7fc2 473 WriteCommand(RA8875_LTPR0, ltpr0 | (mode & 0x7));
WiredHome 53:86d24b9480b9 474 return noerror;
WiredHome 53:86d24b9480b9 475 } else {
WiredHome 53:86d24b9480b9 476 return bad_parameter;
WiredHome 53:86d24b9480b9 477 }
WiredHome 44:207594dece70 478 }
WiredHome 44:207594dece70 479
WiredHome 44:207594dece70 480
WiredHome 44:207594dece70 481 RetCode_t RA8875::SetLayerTransparency(uint8_t layer1, uint8_t layer2)
WiredHome 44:207594dece70 482 {
WiredHome 44:207594dece70 483 if (layer1 > 8)
WiredHome 44:207594dece70 484 layer1 = 8;
WiredHome 44:207594dece70 485 if (layer2 > 8)
WiredHome 44:207594dece70 486 layer2 = 8;
WiredHome 175:7be3a1fb7fc2 487 WriteCommand(RA8875_LTPR1, ((layer2 & 0xF) << 4) | (layer1 & 0xF));
WiredHome 44:207594dece70 488 return noerror;
WiredHome 44:207594dece70 489 }
WiredHome 44:207594dece70 490
WiredHome 44:207594dece70 491
WiredHome 53:86d24b9480b9 492 RetCode_t RA8875::SetBackgroundTransparencyColor(color_t color)
WiredHome 53:86d24b9480b9 493 {
WiredHome 133:e36dcfc2d756 494 return _writeColorTrio(0x67, color);
WiredHome 53:86d24b9480b9 495 }
WiredHome 53:86d24b9480b9 496
WiredHome 79:544eb4964795 497
WiredHome 73:f22a18707b5e 498 color_t RA8875::GetBackgroundTransparencyColor(void)
WiredHome 73:f22a18707b5e 499 {
WiredHome 73:f22a18707b5e 500 RGBQUAD q;
WiredHome 105:4f116006ba1f 501
WiredHome 73:f22a18707b5e 502 q.rgbRed = ReadCommand(0x67);
WiredHome 73:f22a18707b5e 503 q.rgbGreen = ReadCommand(0x68);
WiredHome 73:f22a18707b5e 504 q.rgbBlue = ReadCommand(0x69);
WiredHome 73:f22a18707b5e 505 return RGBQuadToRGB16(&q, 0);
WiredHome 73:f22a18707b5e 506 }
WiredHome 73:f22a18707b5e 507
WiredHome 71:dcac8efd842d 508
WiredHome 77:9206c13aa527 509 RetCode_t RA8875::KeypadInit(bool scanEnable, bool longDetect, uint8_t sampleTime, uint8_t scanFrequency,
WiredHome 77:9206c13aa527 510 uint8_t longTimeAdjustment, bool interruptEnable, bool wakeupEnable)
WiredHome 71:dcac8efd842d 511 {
WiredHome 71:dcac8efd842d 512 uint8_t value = 0;
WiredHome 77:9206c13aa527 513
WiredHome 71:dcac8efd842d 514 if (sampleTime > 3 || scanFrequency > 7 || longTimeAdjustment > 3)
WiredHome 71:dcac8efd842d 515 return bad_parameter;
WiredHome 71:dcac8efd842d 516 value |= (scanEnable) ? 0x80 : 0x00;
WiredHome 71:dcac8efd842d 517 value |= (longDetect) ? 0x40 : 0x00;
WiredHome 71:dcac8efd842d 518 value |= (sampleTime & 0x03) << 4;
WiredHome 71:dcac8efd842d 519 value |= (scanFrequency & 0x07);
WiredHome 175:7be3a1fb7fc2 520 WriteCommand(RA8875_KSCR1, value); // KSCR1 - Enable Key Scan (and ignore possibility of an error)
WiredHome 77:9206c13aa527 521
WiredHome 71:dcac8efd842d 522 value = 0;
WiredHome 71:dcac8efd842d 523 value |= (wakeupEnable) ? 0x80 : 0x00;
WiredHome 71:dcac8efd842d 524 value |= (longTimeAdjustment & 0x03) << 2;
WiredHome 175:7be3a1fb7fc2 525 WriteCommand(RA8875_KSCR2, value); // KSCR2 - (and ignore possibility of an error)
WiredHome 77:9206c13aa527 526
WiredHome 75:ca78388cfd77 527 value = ReadCommand(0xF0); // (and ignore possibility of an error)
WiredHome 71:dcac8efd842d 528 value &= ~0x10;
WiredHome 71:dcac8efd842d 529 value |= (interruptEnable) ? 0x10 : 0x00;
WiredHome 175:7be3a1fb7fc2 530 return WriteCommand(RA8875_INTC1, value); // INT
WiredHome 71:dcac8efd842d 531 }
WiredHome 71:dcac8efd842d 532
WiredHome 79:544eb4964795 533
WiredHome 75:ca78388cfd77 534 RetCode_t RA8875::SetKeyMap(const uint8_t * CodeList)
WiredHome 75:ca78388cfd77 535 {
WiredHome 75:ca78388cfd77 536 pKeyMap = CodeList;
WiredHome 75:ca78388cfd77 537 return noerror;
WiredHome 75:ca78388cfd77 538 }
WiredHome 75:ca78388cfd77 539
WiredHome 79:544eb4964795 540
WiredHome 75:ca78388cfd77 541 bool RA8875::readable(void)
WiredHome 71:dcac8efd842d 542 {
WiredHome 71:dcac8efd842d 543 return (ReadCommand(0xF1) & 0x10); // check KS status - true if kbhit
WiredHome 71:dcac8efd842d 544 }
WiredHome 71:dcac8efd842d 545
WiredHome 79:544eb4964795 546
WiredHome 75:ca78388cfd77 547 uint8_t RA8875::getc(void)
WiredHome 71:dcac8efd842d 548 {
WiredHome 79:544eb4964795 549 //#define GETC_DEV // for development
WiredHome 77:9206c13aa527 550 #ifdef GETC_DEV
WiredHome 75:ca78388cfd77 551 uint8_t keyCode1, keyCode2;
WiredHome 77:9206c13aa527 552 #endif
WiredHome 75:ca78388cfd77 553 uint8_t keyCode3;
WiredHome 75:ca78388cfd77 554 static uint8_t count = 0;
WiredHome 75:ca78388cfd77 555 uint8_t col, row;
WiredHome 75:ca78388cfd77 556 uint8_t key;
WiredHome 123:2f45e80fec5f 557
WiredHome 75:ca78388cfd77 558 while (!readable()) {
WiredHome 71:dcac8efd842d 559 wait_us(POLLWAITuSec);
WiredHome 75:ca78388cfd77 560 // COUNTIDLETIME(POLLWAITuSec); // As it is voluntary to call the getc and pend. Don't tally it.
WiredHome 123:2f45e80fec5f 561 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 562 if (external_abort == (*idle_callback)(getc_wait, 0)) {
WiredHome 123:2f45e80fec5f 563 return 0;
WiredHome 123:2f45e80fec5f 564 }
WiredHome 123:2f45e80fec5f 565 }
WiredHome 71:dcac8efd842d 566 }
WiredHome 71:dcac8efd842d 567 // read the key press number
WiredHome 71:dcac8efd842d 568 uint8_t keyNumReg = ReadCommand(0xC1) & 0x03;
WiredHome 75:ca78388cfd77 569 count++;
WiredHome 75:ca78388cfd77 570 switch (keyNumReg) {
WiredHome 75:ca78388cfd77 571 case 0x01: // one key
WiredHome 75:ca78388cfd77 572 keyCode3 = ReadCommand(0xC2);
WiredHome 77:9206c13aa527 573 #ifdef GETC_DEV
WiredHome 75:ca78388cfd77 574 keyCode2 = 0;
WiredHome 75:ca78388cfd77 575 keyCode1 = 0;
WiredHome 77:9206c13aa527 576 #endif
WiredHome 75:ca78388cfd77 577 break;
WiredHome 75:ca78388cfd77 578 case 0x02: // two keys
WiredHome 75:ca78388cfd77 579 keyCode3 = ReadCommand(0xC3);
WiredHome 77:9206c13aa527 580 #ifdef GETC_DEV
WiredHome 75:ca78388cfd77 581 keyCode2 = ReadCommand(0xC2);
WiredHome 75:ca78388cfd77 582 keyCode1 = 0;
WiredHome 77:9206c13aa527 583 #endif
WiredHome 75:ca78388cfd77 584 break;
WiredHome 75:ca78388cfd77 585 case 0x03: // three keys
WiredHome 75:ca78388cfd77 586 keyCode3 = ReadCommand(0xC4);
WiredHome 77:9206c13aa527 587 #ifdef GETC_DEV
WiredHome 75:ca78388cfd77 588 keyCode2 = ReadCommand(0xC3);
WiredHome 75:ca78388cfd77 589 keyCode1 = ReadCommand(0xC2);
WiredHome 77:9206c13aa527 590 #endif
WiredHome 75:ca78388cfd77 591 break;
WiredHome 75:ca78388cfd77 592 default: // no keys (key released)
WiredHome 75:ca78388cfd77 593 keyCode3 = 0xFF;
WiredHome 77:9206c13aa527 594 #ifdef GETC_DEV
WiredHome 75:ca78388cfd77 595 keyCode2 = 0;
WiredHome 75:ca78388cfd77 596 keyCode1 = 0;
WiredHome 77:9206c13aa527 597 #endif
WiredHome 75:ca78388cfd77 598 break;
WiredHome 75:ca78388cfd77 599 }
WiredHome 75:ca78388cfd77 600 if (keyCode3 == 0xFF)
WiredHome 75:ca78388cfd77 601 key = pKeyMap[0]; // Key value 0
WiredHome 75:ca78388cfd77 602 else {
WiredHome 75:ca78388cfd77 603 row = (keyCode3 >> 4) & 0x03;
WiredHome 75:ca78388cfd77 604 col = (keyCode3 & 7);
WiredHome 75:ca78388cfd77 605 key = row * 5 + col + 1; // Keys value 1 - 20
WiredHome 75:ca78388cfd77 606 if (key > 21) {
WiredHome 75:ca78388cfd77 607 key = 21;
WiredHome 75:ca78388cfd77 608 }
WiredHome 75:ca78388cfd77 609 key = pKeyMap[key];
WiredHome 75:ca78388cfd77 610 key |= (keyCode3 & 0x80); // combine the key held flag
WiredHome 75:ca78388cfd77 611 }
WiredHome 77:9206c13aa527 612 #if GETC_DEV // for Development only
WiredHome 75:ca78388cfd77 613 SetTextCursor(0, 20);
WiredHome 75:ca78388cfd77 614 printf(" Reg: %02x\r\n", keyNumReg);
WiredHome 75:ca78388cfd77 615 printf(" key1: %02x\r\n", keyCode1);
WiredHome 75:ca78388cfd77 616 printf(" key2: %02x\r\n", keyCode2);
WiredHome 75:ca78388cfd77 617 printf(" key3: %02x\r\n", keyCode3);
WiredHome 75:ca78388cfd77 618 printf(" count: %02X\r\n", count);
WiredHome 75:ca78388cfd77 619 printf(" key: %02X\r\n", key);
WiredHome 77:9206c13aa527 620 #endif
WiredHome 175:7be3a1fb7fc2 621 WriteCommand(RA8875_INTC2, 0x10); // Clear KS status
WiredHome 75:ca78388cfd77 622 return key;
WiredHome 71:dcac8efd842d 623 }
WiredHome 71:dcac8efd842d 624
WiredHome 79:544eb4964795 625
WiredHome 19:3f82c1161fd2 626 #ifdef PERF_METRICS
WiredHome 19:3f82c1161fd2 627 void RA8875::ClearPerformance()
WiredHome 19:3f82c1161fd2 628 {
WiredHome 100:0b084475d5a9 629 int i;
WiredHome 100:0b084475d5a9 630
WiredHome 100:0b084475d5a9 631 for (i=0; i<METRICCOUNT; i++)
WiredHome 19:3f82c1161fd2 632 metrics[i] = 0;
WiredHome 75:ca78388cfd77 633 idletime_usec = 0;
WiredHome 100:0b084475d5a9 634 for (i=0; i<256; i++)
WiredHome 100:0b084475d5a9 635 commandsUsed[i] = 0;
WiredHome 19:3f82c1161fd2 636 }
WiredHome 19:3f82c1161fd2 637
WiredHome 79:544eb4964795 638
WiredHome 19:3f82c1161fd2 639 void RA8875::RegisterPerformance(method_e method)
WiredHome 19:3f82c1161fd2 640 {
WiredHome 19:3f82c1161fd2 641 unsigned long elapsed = performance.read_us();
WiredHome 73:f22a18707b5e 642
WiredHome 19:3f82c1161fd2 643 if (method < METRICCOUNT && elapsed > metrics[method])
WiredHome 19:3f82c1161fd2 644 metrics[method] = elapsed;
WiredHome 19:3f82c1161fd2 645 }
WiredHome 19:3f82c1161fd2 646
WiredHome 79:544eb4964795 647
WiredHome 66:468a11f05580 648 void RA8875::CountIdleTime(uint32_t t)
WiredHome 66:468a11f05580 649 {
WiredHome 75:ca78388cfd77 650 idletime_usec += t;
WiredHome 66:468a11f05580 651 }
WiredHome 44:207594dece70 652
WiredHome 79:544eb4964795 653
WiredHome 41:2956a0a221e5 654 void RA8875::ReportPerformance(Serial & pc)
WiredHome 19:3f82c1161fd2 655 {
WiredHome 100:0b084475d5a9 656 int i;
WiredHome 100:0b084475d5a9 657
WiredHome 41:2956a0a221e5 658 pc.printf("\r\nPerformance Metrics\r\n");
WiredHome 100:0b084475d5a9 659 for (i=0; i<METRICCOUNT; i++) {
WiredHome 41:2956a0a221e5 660 pc.printf("%10d uS %s\r\n", metrics[i], metricsName[i]);
WiredHome 66:468a11f05580 661 }
WiredHome 75:ca78388cfd77 662 pc.printf("%10d uS Idle time polling display for ready.\r\n", idletime_usec);
WiredHome 91:ca5f829e6d27 663 for (i=0; i<256; i++) {
WiredHome 91:ca5f829e6d27 664 if (commandsUsed[i])
WiredHome 100:0b084475d5a9 665 pc.printf("Command %02X used %5d times.\r\n", i, commandsUsed[i]);
WiredHome 91:ca5f829e6d27 666 }
WiredHome 19:3f82c1161fd2 667 }
WiredHome 19:3f82c1161fd2 668 #endif
WiredHome 19:3f82c1161fd2 669
WiredHome 44:207594dece70 670
WiredHome 82:f7d300f26540 671 bool RA8875::Intersect(rect_t rect, point_t p)
WiredHome 82:f7d300f26540 672 {
WiredHome 82:f7d300f26540 673 if (p.x >= min(rect.p1.x, rect.p2.x) && p.x <= max(rect.p1.x, rect.p2.x)
WiredHome 82:f7d300f26540 674 && p.y >= min(rect.p1.y, rect.p2.y) && p.y <= max(rect.p1.y, rect.p2.y))
WiredHome 82:f7d300f26540 675 return true;
WiredHome 82:f7d300f26540 676 else
WiredHome 82:f7d300f26540 677 return false;
WiredHome 82:f7d300f26540 678 }
WiredHome 82:f7d300f26540 679
WiredHome 82:f7d300f26540 680
WiredHome 131:5bd6ba2ee4a1 681 bool RA8875::Intersect(rect_t rect1, rect_t rect2)
WiredHome 131:5bd6ba2ee4a1 682 {
WiredHome 147:3494792458d9 683 #if 1
WiredHome 147:3494792458d9 684 // If one rectangle is on left side of other
WiredHome 147:3494792458d9 685 if (max(rect1.p1.x,rect1.p2.x) < min(rect2.p1.x,rect2.p2.x)
WiredHome 147:3494792458d9 686 || min(rect1.p1.x, rect1.p2.x) > max(rect2.p1.x, rect2.p2.x))
WiredHome 147:3494792458d9 687 return false;
WiredHome 147:3494792458d9 688 // If one rectangle is above other
WiredHome 147:3494792458d9 689 if (max(rect1.p1.y, rect1.p2.y) < min(rect2.p1.y, rect2.p2.y)
WiredHome 147:3494792458d9 690 || min(rect1.p1.y, rect1.p2.y) > max(rect2.p1.y, rect2.p2.y))
WiredHome 147:3494792458d9 691 return false;
WiredHome 147:3494792458d9 692 return true; // all that's left is they overlap
WiredHome 147:3494792458d9 693 #else
WiredHome 131:5bd6ba2ee4a1 694 point_t bl, tr;
WiredHome 131:5bd6ba2ee4a1 695 bl.x = rect2.p1.x;
WiredHome 131:5bd6ba2ee4a1 696 bl.y = rect2.p2.y;
WiredHome 131:5bd6ba2ee4a1 697 tr.x = rect2.p2.x;
WiredHome 131:5bd6ba2ee4a1 698 tr.y = rect2.p1.y;
WiredHome 131:5bd6ba2ee4a1 699 if (Intersect(rect1, rect2.p1) || Intersect(rect1, rect2.p2)
WiredHome 147:3494792458d9 700 || Intersect(rect1, bl) || Intersect(rect1, tr))
WiredHome 131:5bd6ba2ee4a1 701 return true;
WiredHome 131:5bd6ba2ee4a1 702 else
WiredHome 131:5bd6ba2ee4a1 703 return false;
WiredHome 147:3494792458d9 704 #endif
WiredHome 131:5bd6ba2ee4a1 705 }
WiredHome 131:5bd6ba2ee4a1 706
WiredHome 131:5bd6ba2ee4a1 707
WiredHome 147:3494792458d9 708 bool RA8875::Intersect(rect_t * pRect1, const rect_t * pRect2)
WiredHome 147:3494792458d9 709 {
WiredHome 147:3494792458d9 710 if (Intersect(*pRect1, *pRect2)) {
WiredHome 147:3494792458d9 711 rect_t iSect;
WiredHome 147:3494792458d9 712
WiredHome 148:33e99de1aca6 713 iSect.p1.x = max(min(pRect1->p1.x,pRect1->p2.x),min(pRect2->p1.x,pRect2->p2.x));
WiredHome 148:33e99de1aca6 714 iSect.p1.y = max(min(pRect1->p1.y,pRect1->p2.y),min(pRect2->p1.y,pRect2->p2.y));
WiredHome 148:33e99de1aca6 715 iSect.p2.x = min(max(pRect1->p1.x,pRect1->p2.x),max(pRect2->p1.x,pRect2->p2.x));
WiredHome 148:33e99de1aca6 716 iSect.p2.y = min(max(pRect1->p1.y,pRect1->p2.y),max(pRect2->p1.y,pRect2->p2.y));
WiredHome 147:3494792458d9 717 *pRect1 = iSect;
WiredHome 147:3494792458d9 718 return true;
WiredHome 147:3494792458d9 719 } else {
WiredHome 147:3494792458d9 720 return false;
WiredHome 147:3494792458d9 721 }
WiredHome 147:3494792458d9 722 }
WiredHome 147:3494792458d9 723
WiredHome 82:f7d300f26540 724
WiredHome 38:38d503b4fad6 725 RetCode_t RA8875::WriteCommandW(uint8_t command, uint16_t data)
WiredHome 38:38d503b4fad6 726 {
WiredHome 38:38d503b4fad6 727 WriteCommand(command, data & 0xFF);
WiredHome 38:38d503b4fad6 728 WriteCommand(command+1, data >> 8);
WiredHome 38:38d503b4fad6 729 return noerror;
WiredHome 38:38d503b4fad6 730 }
WiredHome 38:38d503b4fad6 731
WiredHome 44:207594dece70 732
WiredHome 19:3f82c1161fd2 733 RetCode_t RA8875::WriteCommand(unsigned char command, unsigned int data)
WiredHome 19:3f82c1161fd2 734 {
WiredHome 91:ca5f829e6d27 735 #ifdef PERF_METRICS
WiredHome 92:ce1ab76e8614 736 if (commandsUsed[command] < 65535)
WiredHome 91:ca5f829e6d27 737 commandsUsed[command]++;
WiredHome 91:ca5f829e6d27 738 #endif
WiredHome 79:544eb4964795 739 _select(true);
WiredHome 83:7bad0068cca0 740 _spiwrite(0x80); // RS:1 (Cmd/Status), RW:0 (Write)
WiredHome 79:544eb4964795 741 _spiwrite(command);
WiredHome 19:3f82c1161fd2 742 if (data <= 0xFF) { // only if in the valid range
WiredHome 79:544eb4964795 743 _spiwrite(0x00);
WiredHome 79:544eb4964795 744 _spiwrite(data);
WiredHome 19:3f82c1161fd2 745 }
WiredHome 79:544eb4964795 746 _select(false);
WiredHome 19:3f82c1161fd2 747 return noerror;
WiredHome 19:3f82c1161fd2 748 }
WiredHome 19:3f82c1161fd2 749
WiredHome 44:207594dece70 750
WiredHome 38:38d503b4fad6 751 RetCode_t RA8875::WriteDataW(uint16_t data)
WiredHome 38:38d503b4fad6 752 {
WiredHome 79:544eb4964795 753 _select(true);
WiredHome 83:7bad0068cca0 754 _spiwrite(0x00); // RS:0 (Data), RW:0 (Write)
WiredHome 79:544eb4964795 755 _spiwrite(data & 0xFF);
WiredHome 79:544eb4964795 756 _spiwrite(data >> 8);
WiredHome 79:544eb4964795 757 _select(false);
WiredHome 38:38d503b4fad6 758 return noerror;
WiredHome 38:38d503b4fad6 759 }
WiredHome 38:38d503b4fad6 760
WiredHome 44:207594dece70 761
WiredHome 19:3f82c1161fd2 762 RetCode_t RA8875::WriteData(unsigned char data)
WiredHome 19:3f82c1161fd2 763 {
WiredHome 79:544eb4964795 764 _select(true);
WiredHome 83:7bad0068cca0 765 _spiwrite(0x00); // RS:0 (Data), RW:0 (Write)
WiredHome 79:544eb4964795 766 _spiwrite(data);
WiredHome 79:544eb4964795 767 _select(false);
WiredHome 19:3f82c1161fd2 768 return noerror;
WiredHome 19:3f82c1161fd2 769 }
WiredHome 19:3f82c1161fd2 770
WiredHome 44:207594dece70 771
WiredHome 19:3f82c1161fd2 772 unsigned char RA8875::ReadCommand(unsigned char command)
WiredHome 19:3f82c1161fd2 773 {
WiredHome 19:3f82c1161fd2 774 WriteCommand(command);
WiredHome 19:3f82c1161fd2 775 return ReadData();
WiredHome 19:3f82c1161fd2 776 }
WiredHome 19:3f82c1161fd2 777
WiredHome 136:224e03d5c31f 778 uint16_t RA8875::ReadCommandW(unsigned char command)
WiredHome 136:224e03d5c31f 779 {
WiredHome 136:224e03d5c31f 780 WriteCommand(command);
WiredHome 136:224e03d5c31f 781 return ReadDataW();
WiredHome 136:224e03d5c31f 782 }
WiredHome 44:207594dece70 783
WiredHome 19:3f82c1161fd2 784 unsigned char RA8875::ReadData(void)
WiredHome 19:3f82c1161fd2 785 {
WiredHome 19:3f82c1161fd2 786 unsigned char data;
WiredHome 73:f22a18707b5e 787
WiredHome 79:544eb4964795 788 _select(true);
WiredHome 83:7bad0068cca0 789 _spiwrite(0x40); // RS:0 (Data), RW:1 (Read)
WiredHome 79:544eb4964795 790 data = _spiread();
WiredHome 79:544eb4964795 791 _select(false);
WiredHome 19:3f82c1161fd2 792 return data;
WiredHome 19:3f82c1161fd2 793 }
WiredHome 19:3f82c1161fd2 794
WiredHome 44:207594dece70 795
WiredHome 41:2956a0a221e5 796 uint16_t RA8875::ReadDataW(void)
WiredHome 41:2956a0a221e5 797 {
WiredHome 41:2956a0a221e5 798 uint16_t data;
WiredHome 73:f22a18707b5e 799
WiredHome 79:544eb4964795 800 _select(true);
WiredHome 83:7bad0068cca0 801 _spiwrite(0x40); // RS:0 (Data), RW:1 (Read)
WiredHome 79:544eb4964795 802 data = _spiread();
WiredHome 79:544eb4964795 803 data |= (_spiread() << 8);
WiredHome 79:544eb4964795 804 _select(false);
WiredHome 41:2956a0a221e5 805 return data;
WiredHome 41:2956a0a221e5 806 }
WiredHome 41:2956a0a221e5 807
WiredHome 44:207594dece70 808
WiredHome 19:3f82c1161fd2 809 unsigned char RA8875::ReadStatus(void)
WiredHome 19:3f82c1161fd2 810 {
WiredHome 19:3f82c1161fd2 811 unsigned char data;
WiredHome 73:f22a18707b5e 812
WiredHome 79:544eb4964795 813 _select(true);
WiredHome 83:7bad0068cca0 814 _spiwrite(0xC0); // RS:1 (Cmd/Status), RW:1 (Read) (Read STSR)
WiredHome 79:544eb4964795 815 data = _spiread();
WiredHome 79:544eb4964795 816 _select(false);
WiredHome 19:3f82c1161fd2 817 return data;
WiredHome 19:3f82c1161fd2 818 }
WiredHome 19:3f82c1161fd2 819
WiredHome 79:544eb4964795 820
WiredHome 66:468a11f05580 821 /// @todo add a timeout and return false, but how long
WiredHome 66:468a11f05580 822 /// to wait since some operations can be very long.
WiredHome 66:468a11f05580 823 bool RA8875::_WaitWhileBusy(uint8_t mask)
WiredHome 66:468a11f05580 824 {
WiredHome 66:468a11f05580 825 int i = 20000/POLLWAITuSec; // 20 msec max
WiredHome 66:468a11f05580 826
WiredHome 67:9f834f0ff97d 827 while (i-- && ReadStatus() & mask) {
WiredHome 66:468a11f05580 828 wait_us(POLLWAITuSec);
WiredHome 68:ab08efabfc88 829 COUNTIDLETIME(POLLWAITuSec);
WiredHome 123:2f45e80fec5f 830 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 831 if (external_abort == (*idle_callback)(status_wait, 0)) {
WiredHome 123:2f45e80fec5f 832 return false;
WiredHome 123:2f45e80fec5f 833 }
WiredHome 123:2f45e80fec5f 834 }
WiredHome 67:9f834f0ff97d 835 }
WiredHome 66:468a11f05580 836 if (i)
WiredHome 66:468a11f05580 837 return true;
WiredHome 66:468a11f05580 838 else
WiredHome 66:468a11f05580 839 return false;
WiredHome 66:468a11f05580 840 }
WiredHome 66:468a11f05580 841
WiredHome 79:544eb4964795 842
WiredHome 66:468a11f05580 843 /// @todo add a timeout and return false, but how long
WiredHome 66:468a11f05580 844 /// to wait since some operations can be very long.
WiredHome 66:468a11f05580 845 bool RA8875::_WaitWhileReg(uint8_t reg, uint8_t mask)
WiredHome 66:468a11f05580 846 {
WiredHome 66:468a11f05580 847 int i = 20000/POLLWAITuSec; // 20 msec max
WiredHome 66:468a11f05580 848
WiredHome 67:9f834f0ff97d 849 while (i-- && ReadCommand(reg) & mask) {
WiredHome 66:468a11f05580 850 wait_us(POLLWAITuSec);
WiredHome 68:ab08efabfc88 851 COUNTIDLETIME(POLLWAITuSec);
WiredHome 123:2f45e80fec5f 852 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 853 if (external_abort == (*idle_callback)(command_wait, 0)) {
WiredHome 123:2f45e80fec5f 854 return false;
WiredHome 123:2f45e80fec5f 855 }
WiredHome 123:2f45e80fec5f 856 }
WiredHome 67:9f834f0ff97d 857 }
WiredHome 66:468a11f05580 858 if (i)
WiredHome 66:468a11f05580 859 return true;
WiredHome 66:468a11f05580 860 else
WiredHome 66:468a11f05580 861 return false;
WiredHome 66:468a11f05580 862 }
WiredHome 66:468a11f05580 863
WiredHome 106:c80828f5dea4 864 // RRRR RGGG GGGB BBBB
WiredHome 106:c80828f5dea4 865 // 4321 0543 2104 3210
WiredHome 106:c80828f5dea4 866 // RRRG GGBB
WiredHome 106:c80828f5dea4 867 // 2102 1010
WiredHome 105:4f116006ba1f 868 uint8_t RA8875::_cvt16to8(color_t c16)
WiredHome 105:4f116006ba1f 869 {
WiredHome 105:4f116006ba1f 870 return ((c16 >> 8) & 0xE0)
WiredHome 105:4f116006ba1f 871 | ((c16 >> 6) & 0x1C)
WiredHome 105:4f116006ba1f 872 | ((c16 >> 3) & 0x03);
WiredHome 105:4f116006ba1f 873 }
WiredHome 105:4f116006ba1f 874
WiredHome 106:c80828f5dea4 875 // RRRG GGBB
WiredHome 106:c80828f5dea4 876 // 2102 1010
WiredHome 106:c80828f5dea4 877 // RRRR RGGG GGGB BBBB
WiredHome 106:c80828f5dea4 878 // 2101 0543 2104 3210
WiredHome 105:4f116006ba1f 879 color_t RA8875::_cvt8to16(uint8_t c8)
WiredHome 105:4f116006ba1f 880 {
WiredHome 106:c80828f5dea4 881 color_t c16;
WiredHome 106:c80828f5dea4 882 color_t temp = (color_t)c8;
WiredHome 106:c80828f5dea4 883
WiredHome 106:c80828f5dea4 884 c16 = ((temp & 0xE0) << 8)
WiredHome 106:c80828f5dea4 885 | ((temp & 0xC0) << 5)
WiredHome 106:c80828f5dea4 886 | ((temp & 0x1C) << 6)
WiredHome 106:c80828f5dea4 887 | ((temp & 0x1C) << 3)
WiredHome 106:c80828f5dea4 888 | ((temp & 0x03) << 3)
WiredHome 106:c80828f5dea4 889 | ((temp & 0x03) << 1)
WiredHome 106:c80828f5dea4 890 | ((temp & 0x03) >> 1);
WiredHome 106:c80828f5dea4 891 c16 = (c16 << 8) | (c16 >> 8);
WiredHome 105:4f116006ba1f 892 return c16;
WiredHome 105:4f116006ba1f 893 }
WiredHome 66:468a11f05580 894
WiredHome 133:e36dcfc2d756 895 RetCode_t RA8875::_writeColorTrio(uint8_t regAddr, color_t color)
WiredHome 133:e36dcfc2d756 896 {
WiredHome 133:e36dcfc2d756 897 RetCode_t rt = noerror;
WiredHome 133:e36dcfc2d756 898
WiredHome 133:e36dcfc2d756 899 if (screenbpp == 16) {
WiredHome 133:e36dcfc2d756 900 WriteCommand(regAddr+0, (color>>11)); // BGCR0
WiredHome 133:e36dcfc2d756 901 WriteCommand(regAddr+1, (unsigned char)(color>>5)); // BGCR1
WiredHome 133:e36dcfc2d756 902 rt = WriteCommand(regAddr+2, (unsigned char)(color)); // BGCR2
WiredHome 133:e36dcfc2d756 903 } else {
WiredHome 133:e36dcfc2d756 904 uint8_t r, g, b;
WiredHome 133:e36dcfc2d756 905
WiredHome 133:e36dcfc2d756 906 // RRRR RGGG GGGB BBBB RGB
WiredHome 133:e36dcfc2d756 907 // RRR GGG B B
WiredHome 133:e36dcfc2d756 908 r = (uint8_t)((color) >> 13);
WiredHome 133:e36dcfc2d756 909 g = (uint8_t)((color) >> 8);
WiredHome 133:e36dcfc2d756 910 b = (uint8_t)((color) >> 3);
WiredHome 133:e36dcfc2d756 911 WriteCommand(regAddr+0, r); // BGCR0
WiredHome 133:e36dcfc2d756 912 WriteCommand(regAddr+1, g); // BGCR1
WiredHome 133:e36dcfc2d756 913 rt = WriteCommand(regAddr+2, b); // BGCR2
WiredHome 133:e36dcfc2d756 914 }
WiredHome 133:e36dcfc2d756 915 return rt;
WiredHome 133:e36dcfc2d756 916 }
WiredHome 133:e36dcfc2d756 917
WiredHome 133:e36dcfc2d756 918 color_t RA8875::_readColorTrio(uint8_t regAddr)
WiredHome 133:e36dcfc2d756 919 {
WiredHome 133:e36dcfc2d756 920 color_t color;
WiredHome 133:e36dcfc2d756 921 uint8_t r, g, b;
WiredHome 133:e36dcfc2d756 922
WiredHome 133:e36dcfc2d756 923 r = ReadCommand(regAddr+0);
WiredHome 133:e36dcfc2d756 924 g = ReadCommand(regAddr+1);
WiredHome 133:e36dcfc2d756 925 b = ReadCommand(regAddr+2);
WiredHome 133:e36dcfc2d756 926 if (screenbpp == 16) {
WiredHome 133:e36dcfc2d756 927 // 000R RRRR 00GG GGGG 000B BBBB
WiredHome 133:e36dcfc2d756 928 // RRRR RGGG GGGB BBBB
WiredHome 133:e36dcfc2d756 929 color = (r & 0x1F) << 11;
WiredHome 133:e36dcfc2d756 930 color |= (g & 0x3F) << 5;
WiredHome 133:e36dcfc2d756 931 color |= (b & 0x1F);
WiredHome 133:e36dcfc2d756 932 } else {
WiredHome 133:e36dcfc2d756 933 // RRRG GGBB
WiredHome 133:e36dcfc2d756 934 // RRRR RGGG GGGB BBBB
WiredHome 133:e36dcfc2d756 935 color = (r & 0x07) << 13;
WiredHome 133:e36dcfc2d756 936 color |= (g & 0x07) << 8;
WiredHome 133:e36dcfc2d756 937 color |= (b & 0x03) << 3;
WiredHome 133:e36dcfc2d756 938 }
WiredHome 133:e36dcfc2d756 939 return color;
WiredHome 133:e36dcfc2d756 940 }
WiredHome 133:e36dcfc2d756 941
WiredHome 133:e36dcfc2d756 942
WiredHome 37:f19b7e7449dc 943 dim_t RA8875::fontwidth(void)
WiredHome 19:3f82c1161fd2 944 {
WiredHome 19:3f82c1161fd2 945 if (font == NULL)
WiredHome 55:dfbabef7003e 946 return (((ReadCommand(0x22) >> 2) & 0x3) + 1) * 8;
WiredHome 19:3f82c1161fd2 947 else
WiredHome 98:ecebed9b80b2 948 return extFontWidth;
WiredHome 19:3f82c1161fd2 949 }
WiredHome 19:3f82c1161fd2 950
WiredHome 44:207594dece70 951
WiredHome 37:f19b7e7449dc 952 dim_t RA8875::fontheight(void)
WiredHome 19:3f82c1161fd2 953 {
WiredHome 19:3f82c1161fd2 954 if (font == NULL)
WiredHome 23:a50ded45dbaf 955 return (((ReadCommand(0x22) >> 0) & 0x3) + 1) * 16;
WiredHome 19:3f82c1161fd2 956 else
WiredHome 98:ecebed9b80b2 957 return extFontHeight;
WiredHome 19:3f82c1161fd2 958 }
WiredHome 19:3f82c1161fd2 959
WiredHome 44:207594dece70 960
WiredHome 37:f19b7e7449dc 961 RetCode_t RA8875::locate(textloc_t column, textloc_t row)
WiredHome 19:3f82c1161fd2 962 {
WiredHome 32:0e4f2ae512e2 963 return SetTextCursor(column * fontwidth(), row * fontheight());
WiredHome 19:3f82c1161fd2 964 }
WiredHome 19:3f82c1161fd2 965
WiredHome 44:207594dece70 966
WiredHome 19:3f82c1161fd2 967 int RA8875::columns(void)
WiredHome 19:3f82c1161fd2 968 {
WiredHome 105:4f116006ba1f 969 return screenwidth / fontwidth();
WiredHome 19:3f82c1161fd2 970 }
WiredHome 19:3f82c1161fd2 971
WiredHome 44:207594dece70 972
WiredHome 19:3f82c1161fd2 973 int RA8875::rows(void)
WiredHome 19:3f82c1161fd2 974 {
WiredHome 105:4f116006ba1f 975 return screenheight / fontheight();
WiredHome 19:3f82c1161fd2 976 }
WiredHome 19:3f82c1161fd2 977
WiredHome 44:207594dece70 978
WiredHome 38:38d503b4fad6 979 dim_t RA8875::width(void)
WiredHome 19:3f82c1161fd2 980 {
WiredHome 90:d113d71ae4f0 981 if (portraitmode)
WiredHome 90:d113d71ae4f0 982 return screenheight;
WiredHome 90:d113d71ae4f0 983 else
WiredHome 90:d113d71ae4f0 984 return screenwidth;
WiredHome 19:3f82c1161fd2 985 }
WiredHome 19:3f82c1161fd2 986
WiredHome 44:207594dece70 987
WiredHome 38:38d503b4fad6 988 dim_t RA8875::height(void)
WiredHome 19:3f82c1161fd2 989 {
WiredHome 90:d113d71ae4f0 990 if (portraitmode)
WiredHome 90:d113d71ae4f0 991 return screenwidth;
WiredHome 90:d113d71ae4f0 992 else
WiredHome 90:d113d71ae4f0 993 return screenheight;
WiredHome 19:3f82c1161fd2 994 }
WiredHome 19:3f82c1161fd2 995
WiredHome 44:207594dece70 996
WiredHome 43:3becae133285 997 dim_t RA8875::color_bpp(void)
WiredHome 43:3becae133285 998 {
WiredHome 105:4f116006ba1f 999 return screenbpp;
WiredHome 43:3becae133285 1000 }
WiredHome 43:3becae133285 1001
WiredHome 103:7e0464ca6c5c 1002 RetCode_t RA8875::SetTextCursor(point_t p)
WiredHome 103:7e0464ca6c5c 1003 {
WiredHome 103:7e0464ca6c5c 1004 return SetTextCursor(p.x, p.y);
WiredHome 103:7e0464ca6c5c 1005 }
WiredHome 44:207594dece70 1006
WiredHome 37:f19b7e7449dc 1007 RetCode_t RA8875::SetTextCursor(loc_t x, loc_t y)
WiredHome 19:3f82c1161fd2 1008 {
WiredHome 98:ecebed9b80b2 1009 INFO("SetTextCursor(%d, %d)", x, y);
WiredHome 75:ca78388cfd77 1010 cursor_x = x; // set these values for non-internal fonts
WiredHome 75:ca78388cfd77 1011 cursor_y = y;
WiredHome 175:7be3a1fb7fc2 1012 WriteCommandW(RA8875_FCURXL, x);
WiredHome 175:7be3a1fb7fc2 1013 WriteCommandW(RA8875_FCURYL, y);
WiredHome 19:3f82c1161fd2 1014 return noerror;
WiredHome 19:3f82c1161fd2 1015 }
WiredHome 19:3f82c1161fd2 1016
WiredHome 103:7e0464ca6c5c 1017 point_t RA8875::GetTextCursor(void)
WiredHome 103:7e0464ca6c5c 1018 {
WiredHome 103:7e0464ca6c5c 1019 point_t p;
WiredHome 103:7e0464ca6c5c 1020
WiredHome 103:7e0464ca6c5c 1021 p.x = GetTextCursor_X();
WiredHome 103:7e0464ca6c5c 1022 p.y = GetTextCursor_Y();
WiredHome 103:7e0464ca6c5c 1023 return p;
WiredHome 103:7e0464ca6c5c 1024 }
WiredHome 44:207594dece70 1025
WiredHome 37:f19b7e7449dc 1026 loc_t RA8875::GetTextCursor_Y(void)
WiredHome 29:422616aa04bd 1027 {
WiredHome 98:ecebed9b80b2 1028 loc_t y;
WiredHome 98:ecebed9b80b2 1029
WiredHome 29:422616aa04bd 1030 if (font == NULL)
WiredHome 98:ecebed9b80b2 1031 y = ReadCommand(0x2C) | (ReadCommand(0x2D) << 8);
WiredHome 29:422616aa04bd 1032 else
WiredHome 98:ecebed9b80b2 1033 y = cursor_y;
WiredHome 98:ecebed9b80b2 1034 INFO("GetTextCursor_Y = %d", y);
WiredHome 98:ecebed9b80b2 1035 return y;
WiredHome 29:422616aa04bd 1036 }
WiredHome 29:422616aa04bd 1037
WiredHome 44:207594dece70 1038
WiredHome 37:f19b7e7449dc 1039 loc_t RA8875::GetTextCursor_X(void)
WiredHome 29:422616aa04bd 1040 {
WiredHome 98:ecebed9b80b2 1041 loc_t x;
WiredHome 98:ecebed9b80b2 1042
WiredHome 29:422616aa04bd 1043 if (font == NULL)
WiredHome 98:ecebed9b80b2 1044 x = ReadCommand(0x2A) | (ReadCommand(0x2B) << 8);
WiredHome 29:422616aa04bd 1045 else
WiredHome 98:ecebed9b80b2 1046 x = cursor_x;
WiredHome 98:ecebed9b80b2 1047 INFO("GetTextCursor_X = %d", x);
WiredHome 98:ecebed9b80b2 1048 return x;
WiredHome 29:422616aa04bd 1049 }
WiredHome 29:422616aa04bd 1050
WiredHome 44:207594dece70 1051
WiredHome 24:8ca861acf12d 1052 RetCode_t RA8875::SetTextCursorControl(cursor_t cursor, bool blink)
WiredHome 23:a50ded45dbaf 1053 {
WiredHome 23:a50ded45dbaf 1054 unsigned char mwcr0 = ReadCommand(0x40) & 0x0F; // retain direction, auto-increase
WiredHome 43:3becae133285 1055 unsigned char mwcr1 = ReadCommand(0x41) & 0x01; // retain currently selected layer
WiredHome 24:8ca861acf12d 1056 unsigned char horz = 0;
WiredHome 24:8ca861acf12d 1057 unsigned char vert = 0;
WiredHome 73:f22a18707b5e 1058
WiredHome 24:8ca861acf12d 1059 mwcr0 |= 0x80; // text mode
WiredHome 24:8ca861acf12d 1060 if (cursor != NOCURSOR)
WiredHome 24:8ca861acf12d 1061 mwcr0 |= 0x40; // visible
WiredHome 23:a50ded45dbaf 1062 if (blink)
WiredHome 24:8ca861acf12d 1063 mwcr0 |= 0x20; // blink
WiredHome 175:7be3a1fb7fc2 1064 WriteCommand(RA8875_MWCR0, mwcr0); // configure the cursor
WiredHome 175:7be3a1fb7fc2 1065 WriteCommand(RA8875_MWCR1, mwcr1); // close the graphics cursor
WiredHome 175:7be3a1fb7fc2 1066 WriteCommand(RA8875_BTCR, 0x1f); // The cursor flashing cycle
WiredHome 24:8ca861acf12d 1067 switch (cursor) {
WiredHome 24:8ca861acf12d 1068 case IBEAM:
WiredHome 24:8ca861acf12d 1069 horz = 0x01;
WiredHome 24:8ca861acf12d 1070 vert = 0x1F;
WiredHome 24:8ca861acf12d 1071 break;
WiredHome 24:8ca861acf12d 1072 case UNDER:
WiredHome 24:8ca861acf12d 1073 horz = 0x07;
WiredHome 24:8ca861acf12d 1074 vert = 0x01;
WiredHome 24:8ca861acf12d 1075 break;
WiredHome 24:8ca861acf12d 1076 case BLOCK:
WiredHome 24:8ca861acf12d 1077 horz = 0x07;
WiredHome 24:8ca861acf12d 1078 vert = 0x1F;
WiredHome 24:8ca861acf12d 1079 break;
WiredHome 24:8ca861acf12d 1080 case NOCURSOR:
WiredHome 24:8ca861acf12d 1081 default:
WiredHome 24:8ca861acf12d 1082 break;
WiredHome 24:8ca861acf12d 1083 }
WiredHome 175:7be3a1fb7fc2 1084 WriteCommand(RA8875_CURHS, horz); // The cursor size horz
WiredHome 175:7be3a1fb7fc2 1085 WriteCommand(RA8875_CURVS, vert); // The cursor size vert
WiredHome 23:a50ded45dbaf 1086 return noerror;
WiredHome 23:a50ded45dbaf 1087 }
WiredHome 23:a50ded45dbaf 1088
WiredHome 44:207594dece70 1089
WiredHome 19:3f82c1161fd2 1090 RetCode_t RA8875::SetTextFont(RA8875::font_t font)
WiredHome 19:3f82c1161fd2 1091 {
WiredHome 19:3f82c1161fd2 1092 if (/*font >= RA8875::ISO8859_1 && */ font <= RA8875::ISO8859_4) {
WiredHome 175:7be3a1fb7fc2 1093 WriteCommand(RA8875_FNCR0, (unsigned int)(font));
WiredHome 19:3f82c1161fd2 1094 return noerror;
WiredHome 19:3f82c1161fd2 1095 } else {
WiredHome 19:3f82c1161fd2 1096 return bad_parameter;
WiredHome 19:3f82c1161fd2 1097 }
WiredHome 19:3f82c1161fd2 1098 }
WiredHome 19:3f82c1161fd2 1099
WiredHome 44:207594dece70 1100
WiredHome 84:e102021864b5 1101 RetCode_t RA8875::SetOrientation(RA8875::orientation_t angle)
WiredHome 84:e102021864b5 1102 {
WiredHome 84:e102021864b5 1103 uint8_t fncr1Val = ReadCommand(0x22);
WiredHome 84:e102021864b5 1104 uint8_t dpcrVal = ReadCommand(0x20);
WiredHome 84:e102021864b5 1105
WiredHome 84:e102021864b5 1106 fncr1Val &= ~0x10; // remove the old direction bit
WiredHome 84:e102021864b5 1107 dpcrVal &= ~0x0C; // remove the old scan direction bits
WiredHome 84:e102021864b5 1108 switch (angle) {
WiredHome 84:e102021864b5 1109 case RA8875::normal:
WiredHome 84:e102021864b5 1110 //fncr1Val |= 0x10;
WiredHome 84:e102021864b5 1111 //dpcrVal |= 0x00;
WiredHome 90:d113d71ae4f0 1112 portraitmode = false;
WiredHome 84:e102021864b5 1113 break;
WiredHome 84:e102021864b5 1114 case RA8875::rotate_90:
WiredHome 84:e102021864b5 1115 fncr1Val |= 0x10;
WiredHome 84:e102021864b5 1116 dpcrVal |= 0x08;
WiredHome 90:d113d71ae4f0 1117 portraitmode = true;
WiredHome 84:e102021864b5 1118 break;
WiredHome 84:e102021864b5 1119 case RA8875::rotate_180:
WiredHome 84:e102021864b5 1120 //fncr1Val |= 0x00;
WiredHome 84:e102021864b5 1121 dpcrVal |= 0x0C;
WiredHome 90:d113d71ae4f0 1122 portraitmode = false;
WiredHome 84:e102021864b5 1123 break;
WiredHome 84:e102021864b5 1124 case RA8875::rotate_270:
WiredHome 84:e102021864b5 1125 fncr1Val |= 0x10;
WiredHome 84:e102021864b5 1126 dpcrVal |= 0x04;
WiredHome 90:d113d71ae4f0 1127 portraitmode = true;
WiredHome 84:e102021864b5 1128 break;
WiredHome 84:e102021864b5 1129 default:
WiredHome 84:e102021864b5 1130 return bad_parameter;
WiredHome 84:e102021864b5 1131 }
WiredHome 100:0b084475d5a9 1132 INFO("Orientation: %d, %d", angle, portraitmode);
WiredHome 175:7be3a1fb7fc2 1133 WriteCommand(RA8875_FNCR1, fncr1Val);
WiredHome 175:7be3a1fb7fc2 1134 return WriteCommand(RA8875_DPCR, dpcrVal);
WiredHome 84:e102021864b5 1135 }
WiredHome 84:e102021864b5 1136
WiredHome 84:e102021864b5 1137
WiredHome 19:3f82c1161fd2 1138 RetCode_t RA8875::SetTextFontControl(fill_t fillit,
WiredHome 73:f22a18707b5e 1139 RA8875::HorizontalScale hScale,
WiredHome 73:f22a18707b5e 1140 RA8875::VerticalScale vScale,
WiredHome 73:f22a18707b5e 1141 RA8875::alignment_t alignment)
WiredHome 19:3f82c1161fd2 1142 {
WiredHome 73:f22a18707b5e 1143 if (hScale >= 1 && hScale <= 4 &&
WiredHome 73:f22a18707b5e 1144 vScale >= 1 && vScale <= 4) {
WiredHome 84:e102021864b5 1145 uint8_t fncr1Val = ReadCommand(0x22);
WiredHome 84:e102021864b5 1146
WiredHome 98:ecebed9b80b2 1147 fncr1Val &= ~0x10; // do not disturb the rotate flag
WiredHome 19:3f82c1161fd2 1148 if (alignment == align_full)
WiredHome 84:e102021864b5 1149 fncr1Val |= 0x80;
WiredHome 19:3f82c1161fd2 1150 if (fillit == NOFILL)
WiredHome 84:e102021864b5 1151 fncr1Val |= 0x40;
WiredHome 84:e102021864b5 1152 fncr1Val |= ((hScale - 1) << 2);
WiredHome 84:e102021864b5 1153 fncr1Val |= ((vScale - 1) << 0);
WiredHome 175:7be3a1fb7fc2 1154 return WriteCommand(RA8875_FNCR1, fncr1Val);
WiredHome 19:3f82c1161fd2 1155 } else {
WiredHome 19:3f82c1161fd2 1156 return bad_parameter;
WiredHome 19:3f82c1161fd2 1157 }
WiredHome 19:3f82c1161fd2 1158 }
WiredHome 19:3f82c1161fd2 1159
WiredHome 44:207594dece70 1160
WiredHome 19:3f82c1161fd2 1161 RetCode_t RA8875::SetTextFontSize(RA8875::HorizontalScale hScale, RA8875::VerticalScale vScale)
WiredHome 19:3f82c1161fd2 1162 {
WiredHome 19:3f82c1161fd2 1163 unsigned char reg = ReadCommand(0x22);
WiredHome 73:f22a18707b5e 1164
WiredHome 40:04aa280dfa39 1165 if (vScale == -1)
WiredHome 40:04aa280dfa39 1166 vScale = hScale;
WiredHome 19:3f82c1161fd2 1167 if (hScale >= 1 && hScale <= 4 && vScale >= 1 && vScale <= 4) {
WiredHome 153:8a85efb3eb71 1168 fontScaleX = hScale; // save for use with a Soft Font
WiredHome 153:8a85efb3eb71 1169 fontScaleY = vScale;
WiredHome 19:3f82c1161fd2 1170 reg &= 0xF0; // keep the high nibble as is.
WiredHome 19:3f82c1161fd2 1171 reg |= ((hScale - 1) << 2);
WiredHome 19:3f82c1161fd2 1172 reg |= ((vScale - 1) << 0);
WiredHome 175:7be3a1fb7fc2 1173 WriteCommand(RA8875_FNCR1, reg);
WiredHome 19:3f82c1161fd2 1174 return noerror;
WiredHome 19:3f82c1161fd2 1175 } else {
WiredHome 19:3f82c1161fd2 1176 return bad_parameter;
WiredHome 19:3f82c1161fd2 1177 }
WiredHome 19:3f82c1161fd2 1178 }
WiredHome 19:3f82c1161fd2 1179
WiredHome 127:db7f2c704693 1180 RetCode_t RA8875::GetTextFontSize(RA8875::HorizontalScale * hScale, RA8875::VerticalScale * vScale)
WiredHome 127:db7f2c704693 1181 {
WiredHome 127:db7f2c704693 1182 unsigned char reg = ReadCommand(0x22);
WiredHome 127:db7f2c704693 1183
WiredHome 127:db7f2c704693 1184 if (hScale)
WiredHome 127:db7f2c704693 1185 *hScale = 1 + (reg >> 2) & 0x03;
WiredHome 127:db7f2c704693 1186 if (vScale)
WiredHome 127:db7f2c704693 1187 *vScale = 1 + reg & 0x03;
WiredHome 127:db7f2c704693 1188 return noerror;
WiredHome 127:db7f2c704693 1189 }
WiredHome 44:207594dece70 1190
WiredHome 19:3f82c1161fd2 1191 int RA8875::_putc(int c)
WiredHome 19:3f82c1161fd2 1192 {
WiredHome 29:422616aa04bd 1193 if (font == NULL) {
WiredHome 29:422616aa04bd 1194 return _internal_putc(c);
WiredHome 29:422616aa04bd 1195 } else {
WiredHome 29:422616aa04bd 1196 return _external_putc(c);
WiredHome 29:422616aa04bd 1197 }
WiredHome 29:422616aa04bd 1198 }
WiredHome 29:422616aa04bd 1199
WiredHome 44:207594dece70 1200
WiredHome 101:e0aad446094a 1201
WiredHome 101:e0aad446094a 1202 // Questions to ponder -
WiredHome 101:e0aad446094a 1203 // - if we choose to wrap to the next line, because the character won't fit on the current line,
WiredHome 101:e0aad446094a 1204 // should it erase the space to the width of the screen (in case there is leftover junk there)?
WiredHome 101:e0aad446094a 1205 // - it currently wraps from the bottom of the screen back to the top. I have pondered what
WiredHome 101:e0aad446094a 1206 // it might take to scroll the screen - but haven't thought hard enough about it.
WiredHome 101:e0aad446094a 1207 //
WiredHome 29:422616aa04bd 1208 int RA8875::_external_putc(int c)
WiredHome 29:422616aa04bd 1209 {
WiredHome 19:3f82c1161fd2 1210 if (c) {
WiredHome 19:3f82c1161fd2 1211 if (c == '\r') {
WiredHome 111:efe436c43aba 1212 cursor_x = windowrect.p1.x;
WiredHome 29:422616aa04bd 1213 } else if (c == '\n') {
WiredHome 98:ecebed9b80b2 1214 cursor_y += extFontHeight;
WiredHome 29:422616aa04bd 1215 } else {
WiredHome 109:7b94f06f085b 1216 dim_t charWidth, charHeight;
WiredHome 101:e0aad446094a 1217 const uint8_t * charRecord;
WiredHome 101:e0aad446094a 1218
WiredHome 101:e0aad446094a 1219 charRecord = getCharMetrics(c, &charWidth, &charHeight);
WiredHome 101:e0aad446094a 1220 //int advance = charwidth(c);
WiredHome 111:efe436c43aba 1221 INFO("(%d,%d) - (%d,%d):(%d,%d), charWidth: %d '%c", cursor_x, cursor_y,
WiredHome 111:efe436c43aba 1222 windowrect.p1.x, windowrect.p1.y, windowrect.p2.x, windowrect.p2.y,
WiredHome 111:efe436c43aba 1223 charWidth, c);
WiredHome 101:e0aad446094a 1224 if (charRecord) {
WiredHome 101:e0aad446094a 1225 //cursor_x += advance;
WiredHome 111:efe436c43aba 1226 if (cursor_x + charWidth >= windowrect.p2.x) {
WiredHome 111:efe436c43aba 1227 cursor_x = windowrect.p1.x;
WiredHome 101:e0aad446094a 1228 cursor_y += charHeight;
WiredHome 29:422616aa04bd 1229 }
WiredHome 111:efe436c43aba 1230 if (cursor_y + charHeight >= windowrect.p2.y) {
WiredHome 111:efe436c43aba 1231 cursor_y = windowrect.p1.y; // @todo Should it scroll?
WiredHome 101:e0aad446094a 1232 }
WiredHome 101:e0aad446094a 1233 (void)character(cursor_x, cursor_y, c);
WiredHome 153:8a85efb3eb71 1234 cursor_x += charWidth * fontScaleX;
WiredHome 29:422616aa04bd 1235 }
WiredHome 29:422616aa04bd 1236 }
WiredHome 29:422616aa04bd 1237 }
WiredHome 29:422616aa04bd 1238 return c;
WiredHome 29:422616aa04bd 1239 }
WiredHome 29:422616aa04bd 1240
WiredHome 44:207594dece70 1241
WiredHome 29:422616aa04bd 1242 int RA8875::_internal_putc(int c)
WiredHome 29:422616aa04bd 1243 {
WiredHome 29:422616aa04bd 1244 if (c) {
WiredHome 29:422616aa04bd 1245 unsigned char mwcr0;
WiredHome 73:f22a18707b5e 1246
WiredHome 29:422616aa04bd 1247 mwcr0 = ReadCommand(0x40);
WiredHome 29:422616aa04bd 1248 if ((mwcr0 & 0x80) == 0x00) {
WiredHome 175:7be3a1fb7fc2 1249 WriteCommand(RA8875_MWCR0, 0x80 | mwcr0); // Put in Text mode if not already
WiredHome 29:422616aa04bd 1250 }
WiredHome 29:422616aa04bd 1251 if (c == '\r') {
WiredHome 37:f19b7e7449dc 1252 loc_t x;
WiredHome 19:3f82c1161fd2 1253 x = ReadCommand(0x30) | (ReadCommand(0x31) << 8); // Left edge of active window
WiredHome 175:7be3a1fb7fc2 1254 WriteCommandW(RA8875_FCURXL, x);
WiredHome 19:3f82c1161fd2 1255 } else if (c == '\n') {
WiredHome 37:f19b7e7449dc 1256 loc_t y;
WiredHome 19:3f82c1161fd2 1257 y = ReadCommand(0x2C) | (ReadCommand(0x2D) << 8); // current y location
WiredHome 19:3f82c1161fd2 1258 y += fontheight();
WiredHome 47:d96a09269f91 1259 if (y >= height()) // @TODO after bottom of active window, then scroll window?
WiredHome 19:3f82c1161fd2 1260 y = 0;
WiredHome 175:7be3a1fb7fc2 1261 WriteCommandW(RA8875_FCURYL, y);
WiredHome 19:3f82c1161fd2 1262 } else {
WiredHome 175:7be3a1fb7fc2 1263 WriteCommand(RA8875_MRWC); // RA8875 Internal Fonts
WiredHome 79:544eb4964795 1264 _select(true);
WiredHome 29:422616aa04bd 1265 WriteData(c);
WiredHome 66:468a11f05580 1266 _WaitWhileBusy(0x80);
WiredHome 79:544eb4964795 1267 _select(false);
WiredHome 19:3f82c1161fd2 1268 }
WiredHome 19:3f82c1161fd2 1269 }
WiredHome 19:3f82c1161fd2 1270 return c;
WiredHome 19:3f82c1161fd2 1271 }
WiredHome 19:3f82c1161fd2 1272
WiredHome 44:207594dece70 1273
WiredHome 32:0e4f2ae512e2 1274 RetCode_t RA8875::_StartGraphicsStream(void)
WiredHome 32:0e4f2ae512e2 1275 {
WiredHome 175:7be3a1fb7fc2 1276 WriteCommand(RA8875_MWCR0,0x00); // Graphics write mode
WiredHome 175:7be3a1fb7fc2 1277 WriteCommand(RA8875_MRWC); // Prepare for streaming data
WiredHome 32:0e4f2ae512e2 1278 return noerror;
WiredHome 32:0e4f2ae512e2 1279 }
WiredHome 32:0e4f2ae512e2 1280
WiredHome 44:207594dece70 1281
WiredHome 32:0e4f2ae512e2 1282 RetCode_t RA8875::_EndGraphicsStream(void)
WiredHome 32:0e4f2ae512e2 1283 {
WiredHome 32:0e4f2ae512e2 1284 return noerror;
WiredHome 32:0e4f2ae512e2 1285 }
WiredHome 32:0e4f2ae512e2 1286
WiredHome 44:207594dece70 1287
WiredHome 55:dfbabef7003e 1288 RetCode_t RA8875::_putp(color_t pixel)
WiredHome 32:0e4f2ae512e2 1289 {
WiredHome 38:38d503b4fad6 1290 WriteDataW((pixel>>8) | (pixel<<8));
WiredHome 73:f22a18707b5e 1291 return noerror;
WiredHome 32:0e4f2ae512e2 1292 }
WiredHome 29:422616aa04bd 1293
WiredHome 44:207594dece70 1294
WiredHome 37:f19b7e7449dc 1295 void RA8875::puts(loc_t x, loc_t y, const char * string)
WiredHome 19:3f82c1161fd2 1296 {
WiredHome 19:3f82c1161fd2 1297 SetTextCursor(x,y);
WiredHome 19:3f82c1161fd2 1298 puts(string);
WiredHome 19:3f82c1161fd2 1299 }
WiredHome 19:3f82c1161fd2 1300
WiredHome 44:207594dece70 1301
WiredHome 19:3f82c1161fd2 1302 void RA8875::puts(const char * string)
WiredHome 19:3f82c1161fd2 1303 {
WiredHome 37:f19b7e7449dc 1304 if (font == NULL) {
WiredHome 175:7be3a1fb7fc2 1305 WriteCommand(RA8875_MWCR0,0x80); // Put in Text mode if internal font
WiredHome 37:f19b7e7449dc 1306 }
WiredHome 19:3f82c1161fd2 1307 if (*string != '\0') {
WiredHome 29:422616aa04bd 1308 while (*string) { // @TODO calling individual _putc is slower... optimizations?
WiredHome 19:3f82c1161fd2 1309 _putc(*string++);
WiredHome 19:3f82c1161fd2 1310 }
WiredHome 19:3f82c1161fd2 1311 }
WiredHome 19:3f82c1161fd2 1312 }
WiredHome 19:3f82c1161fd2 1313
WiredHome 44:207594dece70 1314
WiredHome 37:f19b7e7449dc 1315 RetCode_t RA8875::SetGraphicsCursor(loc_t x, loc_t y)
WiredHome 19:3f82c1161fd2 1316 {
WiredHome 175:7be3a1fb7fc2 1317 WriteCommandW(RA8875_CURH0, x);
WiredHome 175:7be3a1fb7fc2 1318 WriteCommandW(RA8875_CURV0, y);
WiredHome 19:3f82c1161fd2 1319 return noerror;
WiredHome 19:3f82c1161fd2 1320 }
WiredHome 19:3f82c1161fd2 1321
WiredHome 136:224e03d5c31f 1322 RetCode_t RA8875::SetGraphicsCursor(point_t p)
WiredHome 136:224e03d5c31f 1323 {
WiredHome 136:224e03d5c31f 1324 return SetGraphicsCursor(p.x, p.y);
WiredHome 136:224e03d5c31f 1325 }
WiredHome 136:224e03d5c31f 1326
WiredHome 136:224e03d5c31f 1327 point_t RA8875::GetGraphicsCursor(void)
WiredHome 136:224e03d5c31f 1328 {
WiredHome 136:224e03d5c31f 1329 point_t p;
WiredHome 136:224e03d5c31f 1330
WiredHome 136:224e03d5c31f 1331 p.x = ReadCommandW(0x46);
WiredHome 136:224e03d5c31f 1332 p.y = ReadCommandW(0x48);
WiredHome 136:224e03d5c31f 1333 return p;
WiredHome 136:224e03d5c31f 1334 }
WiredHome 44:207594dece70 1335
WiredHome 41:2956a0a221e5 1336 RetCode_t RA8875::SetGraphicsCursorRead(loc_t x, loc_t y)
WiredHome 41:2956a0a221e5 1337 {
WiredHome 175:7be3a1fb7fc2 1338 WriteCommandW(RA8875_RCURH0, x);
WiredHome 175:7be3a1fb7fc2 1339 WriteCommandW(RA8875_RCURV0, y);
WiredHome 41:2956a0a221e5 1340 return noerror;
WiredHome 41:2956a0a221e5 1341 }
WiredHome 41:2956a0a221e5 1342
WiredHome 111:efe436c43aba 1343 RetCode_t RA8875::window(rect_t r)
WiredHome 111:efe436c43aba 1344 {
WiredHome 111:efe436c43aba 1345 return window(r.p1.x, r.p1.y, r.p2.x + 1 - r.p1.x, r.p2.y + 1 - r.p1.y);
WiredHome 111:efe436c43aba 1346 }
WiredHome 44:207594dece70 1347
WiredHome 37:f19b7e7449dc 1348 RetCode_t RA8875::window(loc_t x, loc_t y, dim_t width, dim_t height)
WiredHome 19:3f82c1161fd2 1349 {
WiredHome 111:efe436c43aba 1350 INFO("window(%d,%d,%d,%d)", x, y, width, height);
WiredHome 111:efe436c43aba 1351 if (width == (dim_t)-1)
WiredHome 111:efe436c43aba 1352 width = screenwidth - x;
WiredHome 111:efe436c43aba 1353 if (height == (dim_t)-1)
WiredHome 111:efe436c43aba 1354 height = screenheight - y;
WiredHome 111:efe436c43aba 1355 windowrect.p1.x = x;
WiredHome 111:efe436c43aba 1356 windowrect.p1.y = y;
WiredHome 111:efe436c43aba 1357 windowrect.p2.x = x + width - 1;
WiredHome 111:efe436c43aba 1358 windowrect.p2.y = y + height - 1;
WiredHome 37:f19b7e7449dc 1359 GraphicsDisplay::window(x,y, width,height);
WiredHome 175:7be3a1fb7fc2 1360 WriteCommandW(RA8875_HSAW0, x);
WiredHome 175:7be3a1fb7fc2 1361 WriteCommandW(RA8875_VSAW0, y);
WiredHome 175:7be3a1fb7fc2 1362 WriteCommandW(RA8875_HEAW0, (x+width-1));
WiredHome 175:7be3a1fb7fc2 1363 WriteCommandW(RA8875_VEAW0, (y+height-1));
WiredHome 111:efe436c43aba 1364 //SetTextCursor(x,y);
WiredHome 111:efe436c43aba 1365 //SetGraphicsCursor(x,y);
WiredHome 19:3f82c1161fd2 1366 return noerror;
WiredHome 19:3f82c1161fd2 1367 }
WiredHome 19:3f82c1161fd2 1368
WiredHome 44:207594dece70 1369
WiredHome 61:8f3153bf0baa 1370 RetCode_t RA8875::cls(uint16_t layers)
WiredHome 19:3f82c1161fd2 1371 {
WiredHome 61:8f3153bf0baa 1372 RetCode_t ret;
WiredHome 73:f22a18707b5e 1373
WiredHome 19:3f82c1161fd2 1374 PERFORMANCE_RESET;
WiredHome 61:8f3153bf0baa 1375 if (layers == 0) {
WiredHome 61:8f3153bf0baa 1376 ret = clsw(FULLWINDOW);
WiredHome 61:8f3153bf0baa 1377 } else if (layers > 3) {
WiredHome 61:8f3153bf0baa 1378 ret = bad_parameter;
WiredHome 61:8f3153bf0baa 1379 } else {
WiredHome 61:8f3153bf0baa 1380 uint16_t prevLayer = GetDrawingLayer();
WiredHome 61:8f3153bf0baa 1381 if (layers & 1) {
WiredHome 61:8f3153bf0baa 1382 SelectDrawingLayer(0);
WiredHome 61:8f3153bf0baa 1383 clsw(FULLWINDOW);
WiredHome 61:8f3153bf0baa 1384 }
WiredHome 61:8f3153bf0baa 1385 if (layers & 2) {
WiredHome 61:8f3153bf0baa 1386 SelectDrawingLayer(1);
WiredHome 61:8f3153bf0baa 1387 clsw(FULLWINDOW);
WiredHome 61:8f3153bf0baa 1388 }
WiredHome 142:6e9bff59878a 1389 SelectDrawingLayer(prevLayer);
WiredHome 61:8f3153bf0baa 1390 }
WiredHome 135:af519fe4ba91 1391 ret = SetTextCursor(0,0);
WiredHome 135:af519fe4ba91 1392 ret = locate(0,0);
WiredHome 19:3f82c1161fd2 1393 REGISTERPERFORMANCE(PRF_CLS);
WiredHome 61:8f3153bf0baa 1394 return ret;
WiredHome 19:3f82c1161fd2 1395 }
WiredHome 19:3f82c1161fd2 1396
WiredHome 44:207594dece70 1397
WiredHome 19:3f82c1161fd2 1398 RetCode_t RA8875::clsw(RA8875::Region_t region)
WiredHome 19:3f82c1161fd2 1399 {
WiredHome 19:3f82c1161fd2 1400 PERFORMANCE_RESET;
WiredHome 175:7be3a1fb7fc2 1401 WriteCommand(RA8875_MCLR, (region == ACTIVEWINDOW) ? 0xC0 : 0x80);
WiredHome 131:5bd6ba2ee4a1 1402 if (!_WaitWhileReg(0x8E, 0x80)) {
WiredHome 131:5bd6ba2ee4a1 1403 REGISTERPERFORMANCE(PRF_CLS);
WiredHome 131:5bd6ba2ee4a1 1404 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1405 }
WiredHome 19:3f82c1161fd2 1406 REGISTERPERFORMANCE(PRF_CLS);
WiredHome 19:3f82c1161fd2 1407 return noerror;
WiredHome 19:3f82c1161fd2 1408 }
WiredHome 19:3f82c1161fd2 1409
WiredHome 44:207594dece70 1410
WiredHome 87:ee2240581aa7 1411 RetCode_t RA8875::pixel(point_t p, color_t color)
WiredHome 87:ee2240581aa7 1412 {
WiredHome 87:ee2240581aa7 1413 return pixel(p.x, p.y, color);
WiredHome 87:ee2240581aa7 1414 }
WiredHome 87:ee2240581aa7 1415
WiredHome 87:ee2240581aa7 1416 RetCode_t RA8875::pixel(point_t p)
WiredHome 87:ee2240581aa7 1417 {
WiredHome 87:ee2240581aa7 1418 return pixel(p.x, p.y);
WiredHome 87:ee2240581aa7 1419 }
WiredHome 87:ee2240581aa7 1420
WiredHome 37:f19b7e7449dc 1421 RetCode_t RA8875::pixel(loc_t x, loc_t y, color_t color)
WiredHome 19:3f82c1161fd2 1422 {
WiredHome 62:ba5d33438fda 1423 RetCode_t ret;
WiredHome 73:f22a18707b5e 1424
WiredHome 62:ba5d33438fda 1425 PERFORMANCE_RESET;
WiredHome 62:ba5d33438fda 1426 ret = pixelStream(&color, 1, x,y);
WiredHome 62:ba5d33438fda 1427 REGISTERPERFORMANCE(PRF_DRAWPIXEL);
WiredHome 62:ba5d33438fda 1428 return ret;
WiredHome 19:3f82c1161fd2 1429 }
WiredHome 19:3f82c1161fd2 1430
WiredHome 44:207594dece70 1431
WiredHome 37:f19b7e7449dc 1432 RetCode_t RA8875::pixel(loc_t x, loc_t y)
WiredHome 19:3f82c1161fd2 1433 {
WiredHome 19:3f82c1161fd2 1434 RetCode_t ret;
WiredHome 73:f22a18707b5e 1435
WiredHome 19:3f82c1161fd2 1436 PERFORMANCE_RESET;
WiredHome 19:3f82c1161fd2 1437 color_t color = GetForeColor();
WiredHome 62:ba5d33438fda 1438 ret = pixelStream(&color, 1, x, y);
WiredHome 41:2956a0a221e5 1439 REGISTERPERFORMANCE(PRF_DRAWPIXEL);
WiredHome 19:3f82c1161fd2 1440 return ret;
WiredHome 19:3f82c1161fd2 1441 }
WiredHome 19:3f82c1161fd2 1442
WiredHome 44:207594dece70 1443
WiredHome 41:2956a0a221e5 1444 RetCode_t RA8875::pixelStream(color_t * p, uint32_t count, loc_t x, loc_t y)
WiredHome 41:2956a0a221e5 1445 {
WiredHome 41:2956a0a221e5 1446 PERFORMANCE_RESET;
WiredHome 41:2956a0a221e5 1447 SetGraphicsCursor(x, y);
WiredHome 109:7b94f06f085b 1448 _StartGraphicsStream();
WiredHome 79:544eb4964795 1449 _select(true);
WiredHome 79:544eb4964795 1450 _spiwrite(0x00); // Cmd: write data
WiredHome 41:2956a0a221e5 1451 while (count--) {
WiredHome 105:4f116006ba1f 1452 if (screenbpp == 16) {
WiredHome 105:4f116006ba1f 1453 _spiwrite(*p >> 8);
WiredHome 105:4f116006ba1f 1454 _spiwrite(*p & 0xFF);
WiredHome 105:4f116006ba1f 1455 } else {
WiredHome 105:4f116006ba1f 1456 _spiwrite(_cvt16to8(*p));
WiredHome 105:4f116006ba1f 1457 }
WiredHome 41:2956a0a221e5 1458 p++;
WiredHome 41:2956a0a221e5 1459 }
WiredHome 79:544eb4964795 1460 _select(false);
WiredHome 109:7b94f06f085b 1461 _EndGraphicsStream();
WiredHome 41:2956a0a221e5 1462 REGISTERPERFORMANCE(PRF_PIXELSTREAM);
WiredHome 41:2956a0a221e5 1463 return(noerror);
WiredHome 41:2956a0a221e5 1464 }
WiredHome 41:2956a0a221e5 1465
WiredHome 153:8a85efb3eb71 1466 // With a font scale X = 1, a pixel stream is "abcdefg..."
WiredHome 153:8a85efb3eb71 1467 // With a font scale X = 2, a pixel stream is "aabbccddeeffgg..."
WiredHome 153:8a85efb3eb71 1468 // With a font scale Y = 2, a pixel stream is "abcdefg..."
WiredHome 153:8a85efb3eb71 1469 // "abcdefg..."
WiredHome 153:8a85efb3eb71 1470 //
WiredHome 109:7b94f06f085b 1471 RetCode_t RA8875::booleanStream(loc_t x, loc_t y, dim_t w, dim_t h, const uint8_t * boolStream)
WiredHome 109:7b94f06f085b 1472 {
WiredHome 109:7b94f06f085b 1473 PERFORMANCE_RESET;
WiredHome 153:8a85efb3eb71 1474 const uint8_t * rowStream;
WiredHome 111:efe436c43aba 1475 rect_t restore = windowrect;
WiredHome 153:8a85efb3eb71 1476 window(x, y, w * fontScaleX, h * fontScaleY); // Scale from font scale factors
WiredHome 109:7b94f06f085b 1477 SetGraphicsCursor(x, y);
WiredHome 109:7b94f06f085b 1478 _StartGraphicsStream();
WiredHome 109:7b94f06f085b 1479 _select(true);
WiredHome 109:7b94f06f085b 1480 _spiwrite(0x00); // Cmd: write data
WiredHome 109:7b94f06f085b 1481 while (h--) {
WiredHome 153:8a85efb3eb71 1482 for (int dy=0; dy<fontScaleY; dy++) { // Vertical Font Scale Factor
WiredHome 153:8a85efb3eb71 1483 uint8_t pixels = w;
WiredHome 153:8a85efb3eb71 1484 uint8_t bitmask = 0x01;
WiredHome 153:8a85efb3eb71 1485 rowStream = boolStream;
WiredHome 153:8a85efb3eb71 1486 while (pixels) {
WiredHome 153:8a85efb3eb71 1487 uint8_t byte = *rowStream;
WiredHome 153:8a85efb3eb71 1488 //INFO("byte, mask: %02X, %02X", byte, bitmask);
WiredHome 153:8a85efb3eb71 1489 color_t c = (byte & bitmask) ? _foreground : _background;
WiredHome 153:8a85efb3eb71 1490
WiredHome 153:8a85efb3eb71 1491 for (int dx=0; dx<fontScaleX; dx++) { // Horizontal Font Scale Factor
WiredHome 153:8a85efb3eb71 1492 if (screenbpp == 16) {
WiredHome 153:8a85efb3eb71 1493 _spiwrite(c >> 8);
WiredHome 153:8a85efb3eb71 1494 _spiwrite(c & 0xFF);
WiredHome 153:8a85efb3eb71 1495 } else {
WiredHome 153:8a85efb3eb71 1496 _spiwrite(_cvt16to8(c));
WiredHome 153:8a85efb3eb71 1497 }
WiredHome 109:7b94f06f085b 1498 }
WiredHome 153:8a85efb3eb71 1499 bitmask <<= 1;
WiredHome 153:8a85efb3eb71 1500 if (pixels > 1 && bitmask == 0) {
WiredHome 153:8a85efb3eb71 1501 bitmask = 0x01;
WiredHome 153:8a85efb3eb71 1502 rowStream++;
WiredHome 153:8a85efb3eb71 1503 }
WiredHome 153:8a85efb3eb71 1504 pixels--;
WiredHome 109:7b94f06f085b 1505 }
WiredHome 109:7b94f06f085b 1506 }
WiredHome 153:8a85efb3eb71 1507 boolStream += (rowStream - boolStream + 1);
WiredHome 109:7b94f06f085b 1508 }
WiredHome 109:7b94f06f085b 1509 _select(false);
WiredHome 109:7b94f06f085b 1510 _EndGraphicsStream();
WiredHome 111:efe436c43aba 1511 window(restore);
WiredHome 109:7b94f06f085b 1512 REGISTERPERFORMANCE(PRF_BOOLSTREAM);
WiredHome 109:7b94f06f085b 1513 return(noerror);
WiredHome 109:7b94f06f085b 1514 }
WiredHome 44:207594dece70 1515
WiredHome 41:2956a0a221e5 1516 color_t RA8875::getPixel(loc_t x, loc_t y)
WiredHome 41:2956a0a221e5 1517 {
WiredHome 41:2956a0a221e5 1518 color_t pixel;
WiredHome 73:f22a18707b5e 1519
WiredHome 41:2956a0a221e5 1520 PERFORMANCE_RESET;
WiredHome 175:7be3a1fb7fc2 1521 WriteCommand(RA8875_MWCR0,0x00); // Graphics write mode
WiredHome 41:2956a0a221e5 1522 SetGraphicsCursorRead(x, y);
WiredHome 175:7be3a1fb7fc2 1523 WriteCommand(RA8875_MRWC);
WiredHome 79:544eb4964795 1524 _select(true);
WiredHome 79:544eb4964795 1525 _spiwrite(0x40); // Cmd: read data
WiredHome 79:544eb4964795 1526 _spiwrite(0x00); // dummy read
WiredHome 105:4f116006ba1f 1527 if (screenbpp == 16) {
WiredHome 105:4f116006ba1f 1528 pixel = _spiread();
WiredHome 105:4f116006ba1f 1529 pixel |= (_spiread() << 8);
WiredHome 105:4f116006ba1f 1530 } else {
WiredHome 105:4f116006ba1f 1531 pixel = _cvt8to16(_spiread());
WiredHome 105:4f116006ba1f 1532 }
WiredHome 79:544eb4964795 1533 _select(false);
WiredHome 41:2956a0a221e5 1534 REGISTERPERFORMANCE(PRF_READPIXEL);
WiredHome 41:2956a0a221e5 1535 return pixel;
WiredHome 41:2956a0a221e5 1536 }
WiredHome 41:2956a0a221e5 1537
WiredHome 44:207594dece70 1538
WiredHome 41:2956a0a221e5 1539 RetCode_t RA8875::getPixelStream(color_t * p, uint32_t count, loc_t x, loc_t y)
WiredHome 41:2956a0a221e5 1540 {
WiredHome 41:2956a0a221e5 1541 color_t pixel;
WiredHome 86:e86b355940f4 1542 RetCode_t ret = noerror;
WiredHome 73:f22a18707b5e 1543
WiredHome 41:2956a0a221e5 1544 PERFORMANCE_RESET;
WiredHome 175:7be3a1fb7fc2 1545 ret = WriteCommand(RA8875_MWCR0,0x00); // Graphics write mode
WiredHome 86:e86b355940f4 1546 ret = SetGraphicsCursorRead(x, y);
WiredHome 175:7be3a1fb7fc2 1547 ret = WriteCommand(RA8875_MRWC);
WiredHome 79:544eb4964795 1548 _select(true);
WiredHome 79:544eb4964795 1549 _spiwrite(0x40); // Cmd: read data
WiredHome 79:544eb4964795 1550 _spiwrite(0x00); // dummy read
WiredHome 106:c80828f5dea4 1551 if (screenbpp == 16)
WiredHome 106:c80828f5dea4 1552 _spiwrite(0x00); // dummy read is only necessary when in 16-bit mode
WiredHome 41:2956a0a221e5 1553 while (count--) {
WiredHome 105:4f116006ba1f 1554 if (screenbpp == 16) {
WiredHome 105:4f116006ba1f 1555 pixel = _spiread();
WiredHome 105:4f116006ba1f 1556 pixel |= (_spiread() << 8);
WiredHome 105:4f116006ba1f 1557 } else {
WiredHome 105:4f116006ba1f 1558 pixel = _cvt8to16(_spiread());
WiredHome 105:4f116006ba1f 1559 }
WiredHome 41:2956a0a221e5 1560 *p++ = pixel;
WiredHome 41:2956a0a221e5 1561 }
WiredHome 79:544eb4964795 1562 _select(false);
WiredHome 41:2956a0a221e5 1563 REGISTERPERFORMANCE(PRF_READPIXELSTREAM);
WiredHome 86:e86b355940f4 1564 return ret;
WiredHome 41:2956a0a221e5 1565 }
WiredHome 41:2956a0a221e5 1566
WiredHome 44:207594dece70 1567
WiredHome 83:7bad0068cca0 1568 RetCode_t RA8875::line(point_t p1, point_t p2)
WiredHome 83:7bad0068cca0 1569 {
WiredHome 83:7bad0068cca0 1570 return line(p1.x, p1.y, p2.x, p2.y);
WiredHome 83:7bad0068cca0 1571 }
WiredHome 83:7bad0068cca0 1572
WiredHome 83:7bad0068cca0 1573
WiredHome 83:7bad0068cca0 1574 RetCode_t RA8875::line(point_t p1, point_t p2, color_t color)
WiredHome 83:7bad0068cca0 1575 {
WiredHome 83:7bad0068cca0 1576 return line(p1.x, p1.y, p2.x, p2.y, color);
WiredHome 83:7bad0068cca0 1577 }
WiredHome 83:7bad0068cca0 1578
WiredHome 83:7bad0068cca0 1579
WiredHome 37:f19b7e7449dc 1580 RetCode_t RA8875::line(loc_t x1, loc_t y1, loc_t x2, loc_t y2, color_t color)
WiredHome 19:3f82c1161fd2 1581 {
WiredHome 19:3f82c1161fd2 1582 foreground(color);
WiredHome 19:3f82c1161fd2 1583 return line(x1,y1,x2,y2);
WiredHome 19:3f82c1161fd2 1584 }
WiredHome 19:3f82c1161fd2 1585
WiredHome 44:207594dece70 1586
WiredHome 37:f19b7e7449dc 1587 RetCode_t RA8875::line(loc_t x1, loc_t y1, loc_t x2, loc_t y2)
WiredHome 19:3f82c1161fd2 1588 {
WiredHome 19:3f82c1161fd2 1589 PERFORMANCE_RESET;
WiredHome 62:ba5d33438fda 1590 if (x1 == x2 && y1 == y2) {
WiredHome 60:2dfd574f63bd 1591 pixel(x1, y1);
WiredHome 62:ba5d33438fda 1592 } else {
WiredHome 175:7be3a1fb7fc2 1593 WriteCommandW(RA8875_DLHSR0, x1);
WiredHome 175:7be3a1fb7fc2 1594 WriteCommandW(RA8875_DLVSR0, y1);
WiredHome 175:7be3a1fb7fc2 1595 WriteCommandW(RA8875_DLHER0, x2);
WiredHome 175:7be3a1fb7fc2 1596 WriteCommandW(RA8875_DLVER0, y2);
WiredHome 60:2dfd574f63bd 1597 unsigned char drawCmd = 0x00; // Line
WiredHome 175:7be3a1fb7fc2 1598 WriteCommand(RA8875_DCR, drawCmd);
WiredHome 175:7be3a1fb7fc2 1599 WriteCommand(RA8875_DCR, 0x80 + drawCmd); // Start drawing.
WiredHome 131:5bd6ba2ee4a1 1600 if (!_WaitWhileReg(0x90, 0x80)) {
WiredHome 131:5bd6ba2ee4a1 1601 REGISTERPERFORMANCE(PRF_DRAWLINE);
WiredHome 131:5bd6ba2ee4a1 1602 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1603 }
WiredHome 60:2dfd574f63bd 1604 }
WiredHome 19:3f82c1161fd2 1605 REGISTERPERFORMANCE(PRF_DRAWLINE);
WiredHome 19:3f82c1161fd2 1606 return noerror;
WiredHome 19:3f82c1161fd2 1607 }
WiredHome 19:3f82c1161fd2 1608
WiredHome 144:ba002c4b21b3 1609
WiredHome 144:ba002c4b21b3 1610 RetCode_t RA8875::ThickLine(point_t p1, point_t p2, dim_t thickness, color_t color)
WiredHome 144:ba002c4b21b3 1611 {
WiredHome 144:ba002c4b21b3 1612 if (thickness == 1) {
WiredHome 144:ba002c4b21b3 1613 line(p1,p2, color);
WiredHome 144:ba002c4b21b3 1614 } else {
WiredHome 144:ba002c4b21b3 1615 int dx = abs(p2.x-p1.x), sx = p1.x<p2.x ? 1 : -1;
WiredHome 144:ba002c4b21b3 1616 int dy = abs(p2.y-p1.y), sy = p1.y<p2.y ? 1 : -1;
WiredHome 144:ba002c4b21b3 1617 int err = (dx>dy ? dx : -dy)/2, e2;
WiredHome 144:ba002c4b21b3 1618
WiredHome 144:ba002c4b21b3 1619 for (;;) {
WiredHome 144:ba002c4b21b3 1620 fillcircle(p1.x, p1.y, thickness/2, color);
WiredHome 144:ba002c4b21b3 1621 if (p1.x==p2.x && p1.y==p2.y)
WiredHome 144:ba002c4b21b3 1622 break;
WiredHome 144:ba002c4b21b3 1623 e2 = err;
WiredHome 144:ba002c4b21b3 1624 if (e2 >-dx)
WiredHome 144:ba002c4b21b3 1625 { err -= dy; p1.x += sx; }
WiredHome 144:ba002c4b21b3 1626 if (e2 < dy)
WiredHome 144:ba002c4b21b3 1627 { err += dx; p1.y += sy; }
WiredHome 144:ba002c4b21b3 1628 }
WiredHome 144:ba002c4b21b3 1629 }
WiredHome 144:ba002c4b21b3 1630 return noerror;
WiredHome 144:ba002c4b21b3 1631 }
WiredHome 144:ba002c4b21b3 1632
WiredHome 144:ba002c4b21b3 1633
WiredHome 107:f9ccffcb84f1 1634 //
WiredHome 107:f9ccffcb84f1 1635 // Rectangle functions all mostly helpers to the basic rectangle function
WiredHome 107:f9ccffcb84f1 1636 //
WiredHome 107:f9ccffcb84f1 1637
WiredHome 81:01da2e34283d 1638 RetCode_t RA8875::fillrect(rect_t r, color_t color, fill_t fillit)
WiredHome 81:01da2e34283d 1639 {
WiredHome 81:01da2e34283d 1640 return rect(r.p1.x, r.p1.y, r.p2.x, r.p2.y, color, fillit);
WiredHome 81:01da2e34283d 1641 }
WiredHome 44:207594dece70 1642
WiredHome 73:f22a18707b5e 1643 RetCode_t RA8875::fillrect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1644 color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1645 {
WiredHome 19:3f82c1161fd2 1646 return rect(x1,y1,x2,y2,color,fillit);
WiredHome 19:3f82c1161fd2 1647 }
WiredHome 19:3f82c1161fd2 1648
WiredHome 81:01da2e34283d 1649 RetCode_t RA8875::rect(rect_t r, color_t color, fill_t fillit)
WiredHome 81:01da2e34283d 1650 {
WiredHome 81:01da2e34283d 1651 return rect(r.p1.x, r.p1.y, r.p2.x, r.p2.y, color, fillit);
WiredHome 81:01da2e34283d 1652 }
WiredHome 44:207594dece70 1653
WiredHome 73:f22a18707b5e 1654 RetCode_t RA8875::rect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1655 color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1656 {
WiredHome 19:3f82c1161fd2 1657 foreground(color);
WiredHome 19:3f82c1161fd2 1658 return rect(x1,y1,x2,y2,fillit);
WiredHome 19:3f82c1161fd2 1659 }
WiredHome 19:3f82c1161fd2 1660
WiredHome 73:f22a18707b5e 1661 RetCode_t RA8875::rect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1662 fill_t fillit)
WiredHome 19:3f82c1161fd2 1663 {
WiredHome 85:022bba13c5c4 1664 RetCode_t ret = noerror;
WiredHome 19:3f82c1161fd2 1665 PERFORMANCE_RESET;
WiredHome 85:022bba13c5c4 1666 // check for bad_parameter
WiredHome 105:4f116006ba1f 1667 if (x1 < 0 || x1 >= screenwidth || x2 < 0 || x2 >= screenwidth
WiredHome 105:4f116006ba1f 1668 || y1 < 0 || y1 >= screenheight || y2 < 0 || y2 >= screenheight) {
WiredHome 85:022bba13c5c4 1669 ret = bad_parameter;
WiredHome 100:0b084475d5a9 1670 } else {
WiredHome 85:022bba13c5c4 1671 if (x1 == x2 && y1 == y2) {
WiredHome 85:022bba13c5c4 1672 pixel(x1, y1);
WiredHome 85:022bba13c5c4 1673 } else if (x1 == x2) {
WiredHome 85:022bba13c5c4 1674 line(x1, y1, x2, y2);
WiredHome 85:022bba13c5c4 1675 } else if (y1 == y2) {
WiredHome 85:022bba13c5c4 1676 line(x1, y1, x2, y2);
WiredHome 85:022bba13c5c4 1677 } else {
WiredHome 175:7be3a1fb7fc2 1678 WriteCommandW(RA8875_DLHSR0, x1);
WiredHome 175:7be3a1fb7fc2 1679 WriteCommandW(RA8875_DLVSR0, y1);
WiredHome 175:7be3a1fb7fc2 1680 WriteCommandW(RA8875_DLHER0, x2);
WiredHome 175:7be3a1fb7fc2 1681 WriteCommandW(RA8875_DLVER0, y2);
WiredHome 85:022bba13c5c4 1682 unsigned char drawCmd = 0x10; // Rectangle
WiredHome 85:022bba13c5c4 1683 if (fillit == FILL)
WiredHome 85:022bba13c5c4 1684 drawCmd |= 0x20;
WiredHome 175:7be3a1fb7fc2 1685 WriteCommand(RA8875_DCR, drawCmd);
WiredHome 175:7be3a1fb7fc2 1686 ret = WriteCommand(RA8875_DCR, 0x80 + drawCmd); // Start drawing.
WiredHome 131:5bd6ba2ee4a1 1687 if (!_WaitWhileReg(0x90, 0x80)) {
WiredHome 131:5bd6ba2ee4a1 1688 REGISTERPERFORMANCE(PRF_DRAWRECTANGLE);
WiredHome 131:5bd6ba2ee4a1 1689 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1690 }
WiredHome 85:022bba13c5c4 1691 }
WiredHome 19:3f82c1161fd2 1692 }
WiredHome 19:3f82c1161fd2 1693 REGISTERPERFORMANCE(PRF_DRAWRECTANGLE);
WiredHome 85:022bba13c5c4 1694 return ret;
WiredHome 19:3f82c1161fd2 1695 }
WiredHome 19:3f82c1161fd2 1696
WiredHome 44:207594dece70 1697
WiredHome 107:f9ccffcb84f1 1698 //
WiredHome 107:f9ccffcb84f1 1699 // rounded rectangle functions are mostly helpers to the base round rect
WiredHome 107:f9ccffcb84f1 1700 //
WiredHome 107:f9ccffcb84f1 1701
WiredHome 107:f9ccffcb84f1 1702 RetCode_t RA8875::fillroundrect(rect_t r, dim_t radius1, dim_t radius2, color_t color, fill_t fillit)
WiredHome 107:f9ccffcb84f1 1703 {
WiredHome 107:f9ccffcb84f1 1704 return roundrect(r.p1.x, r.p1.y, r.p2.x, r.p2.y, radius1, radius2, color, fillit);
WiredHome 107:f9ccffcb84f1 1705 }
WiredHome 107:f9ccffcb84f1 1706
WiredHome 73:f22a18707b5e 1707 RetCode_t RA8875::fillroundrect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1708 dim_t radius1, dim_t radius2, color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1709 {
WiredHome 19:3f82c1161fd2 1710 foreground(color);
WiredHome 19:3f82c1161fd2 1711 return roundrect(x1,y1,x2,y2,radius1,radius2,fillit);
WiredHome 19:3f82c1161fd2 1712 }
WiredHome 19:3f82c1161fd2 1713
WiredHome 107:f9ccffcb84f1 1714 RetCode_t RA8875::roundrect(rect_t r, dim_t radius1, dim_t radius2, color_t color, fill_t fillit)
WiredHome 107:f9ccffcb84f1 1715 {
WiredHome 107:f9ccffcb84f1 1716 return roundrect(r.p1.x, r.p1.y, r.p2.x, r.p2.y, radius1, radius2, color, fillit);
WiredHome 107:f9ccffcb84f1 1717 }
WiredHome 44:207594dece70 1718
WiredHome 73:f22a18707b5e 1719 RetCode_t RA8875::roundrect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1720 dim_t radius1, dim_t radius2, color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1721 {
WiredHome 19:3f82c1161fd2 1722 foreground(color);
WiredHome 19:3f82c1161fd2 1723 return roundrect(x1,y1,x2,y2,radius1,radius2,fillit);
WiredHome 19:3f82c1161fd2 1724 }
WiredHome 19:3f82c1161fd2 1725
WiredHome 44:207594dece70 1726
WiredHome 73:f22a18707b5e 1727 RetCode_t RA8875::roundrect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1728 dim_t radius1, dim_t radius2, fill_t fillit)
WiredHome 19:3f82c1161fd2 1729 {
WiredHome 19:3f82c1161fd2 1730 RetCode_t ret = noerror;
WiredHome 73:f22a18707b5e 1731
WiredHome 19:3f82c1161fd2 1732 PERFORMANCE_RESET;
WiredHome 105:4f116006ba1f 1733 if (x1 < 0 || x1 >= screenwidth || x2 < 0 || x2 >= screenwidth
WiredHome 105:4f116006ba1f 1734 || y1 < 0 || y1 >= screenheight || y2 < 0 || y2 >= screenheight) {
WiredHome 85:022bba13c5c4 1735 ret = bad_parameter;
WiredHome 85:022bba13c5c4 1736 } else if (x1 > x2 || y1 > y2 || (radius1 > (x2-x1)/2) || (radius2 > (y2-y1)/2) ) {
WiredHome 21:3c1efb192927 1737 ret = bad_parameter;
WiredHome 21:3c1efb192927 1738 } else if (x1 == x2 && y1 == y2) {
WiredHome 19:3f82c1161fd2 1739 pixel(x1, y1);
WiredHome 19:3f82c1161fd2 1740 } else if (x1 == x2) {
WiredHome 19:3f82c1161fd2 1741 line(x1, y1, x2, y2);
WiredHome 19:3f82c1161fd2 1742 } else if (y1 == y2) {
WiredHome 19:3f82c1161fd2 1743 line(x1, y1, x2, y2);
WiredHome 19:3f82c1161fd2 1744 } else {
WiredHome 175:7be3a1fb7fc2 1745 WriteCommandW(RA8875_DLHSR0, x1);
WiredHome 175:7be3a1fb7fc2 1746 WriteCommandW(RA8875_DLVSR0, y1);
WiredHome 175:7be3a1fb7fc2 1747 WriteCommandW(RA8875_DLHER0, x2);
WiredHome 175:7be3a1fb7fc2 1748 WriteCommandW(RA8875_DLVER0, y2);
WiredHome 175:7be3a1fb7fc2 1749 WriteCommandW(RA8875_ELLA0, radius1);
WiredHome 175:7be3a1fb7fc2 1750 WriteCommandW(RA8875_ELLB0, radius2);
WiredHome 21:3c1efb192927 1751 // Should not need this...
WiredHome 175:7be3a1fb7fc2 1752 WriteCommandW(RA8875_DEHR0, 0);
WiredHome 175:7be3a1fb7fc2 1753 WriteCommandW(RA8875_DEVR0, 0);
WiredHome 19:3f82c1161fd2 1754 unsigned char drawCmd = 0x20; // Rounded Rectangle
WiredHome 19:3f82c1161fd2 1755 if (fillit == FILL)
WiredHome 19:3f82c1161fd2 1756 drawCmd |= 0x40;
WiredHome 175:7be3a1fb7fc2 1757 WriteCommand(RA8875_ELLIPSE, drawCmd);
WiredHome 175:7be3a1fb7fc2 1758 WriteCommand(RA8875_ELLIPSE, 0x80 + drawCmd); // Start drawing.
WiredHome 131:5bd6ba2ee4a1 1759 if (!_WaitWhileReg(0xA0, 0x80)) {
WiredHome 131:5bd6ba2ee4a1 1760 REGISTERPERFORMANCE(PRF_DRAWROUNDEDRECTANGLE);
WiredHome 131:5bd6ba2ee4a1 1761 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1762 }
WiredHome 19:3f82c1161fd2 1763 }
WiredHome 19:3f82c1161fd2 1764 REGISTERPERFORMANCE(PRF_DRAWROUNDEDRECTANGLE);
WiredHome 19:3f82c1161fd2 1765 return ret;
WiredHome 19:3f82c1161fd2 1766 }
WiredHome 19:3f82c1161fd2 1767
WiredHome 44:207594dece70 1768
WiredHome 107:f9ccffcb84f1 1769 //
WiredHome 107:f9ccffcb84f1 1770 // triangle functions
WiredHome 107:f9ccffcb84f1 1771 //
WiredHome 107:f9ccffcb84f1 1772
WiredHome 73:f22a18707b5e 1773 RetCode_t RA8875::triangle(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1774 loc_t x3, loc_t y3, color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1775 {
WiredHome 20:6e2e4a8372eb 1776 RetCode_t ret;
WiredHome 20:6e2e4a8372eb 1777
WiredHome 105:4f116006ba1f 1778 if (x1 < 0 || x1 >= screenwidth || x2 < 0 || x2 >= screenwidth || x3 < 0 || x3 >= screenwidth
WiredHome 105:4f116006ba1f 1779 || y1 < 0 || y1 >= screenheight || y2 < 0 || y2 >= screenheight || y3 < 0 || y3 >= screenheight)
WiredHome 85:022bba13c5c4 1780 ret = bad_parameter;
WiredHome 19:3f82c1161fd2 1781 foreground(color);
WiredHome 20:6e2e4a8372eb 1782 ret = triangle(x1,y1,x2,y2,x3,y3,fillit);
WiredHome 20:6e2e4a8372eb 1783 return ret;
WiredHome 19:3f82c1161fd2 1784 }
WiredHome 19:3f82c1161fd2 1785
WiredHome 44:207594dece70 1786
WiredHome 73:f22a18707b5e 1787 RetCode_t RA8875::filltriangle(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1788 loc_t x3, loc_t y3, color_t color, fill_t fillit)
WiredHome 73:f22a18707b5e 1789 {
WiredHome 73:f22a18707b5e 1790 RetCode_t ret;
WiredHome 73:f22a18707b5e 1791
WiredHome 73:f22a18707b5e 1792 foreground(color);
WiredHome 73:f22a18707b5e 1793 ret = triangle(x1,y1,x2,y2,x3,y3,fillit);
WiredHome 73:f22a18707b5e 1794 return ret;
WiredHome 73:f22a18707b5e 1795 }
WiredHome 73:f22a18707b5e 1796
WiredHome 73:f22a18707b5e 1797
WiredHome 73:f22a18707b5e 1798 RetCode_t RA8875::triangle(loc_t x1, loc_t y1 ,loc_t x2, loc_t y2,
WiredHome 73:f22a18707b5e 1799 loc_t x3, loc_t y3, fill_t fillit)
WiredHome 19:3f82c1161fd2 1800 {
WiredHome 19:3f82c1161fd2 1801 RetCode_t ret = noerror;
WiredHome 73:f22a18707b5e 1802
WiredHome 19:3f82c1161fd2 1803 PERFORMANCE_RESET;
WiredHome 19:3f82c1161fd2 1804 if (x1 == x2 && y1 == y2 && x1 == x3 && y1 == y3) {
WiredHome 19:3f82c1161fd2 1805 pixel(x1, y1);
WiredHome 19:3f82c1161fd2 1806 } else {
WiredHome 175:7be3a1fb7fc2 1807 WriteCommandW(RA8875_DLHSR0, x1);
WiredHome 175:7be3a1fb7fc2 1808 WriteCommandW(RA8875_DLVSR0, y1);
WiredHome 175:7be3a1fb7fc2 1809 WriteCommandW(RA8875_DLHER0, x2);
WiredHome 175:7be3a1fb7fc2 1810 WriteCommandW(RA8875_DLVER0, y2);
WiredHome 175:7be3a1fb7fc2 1811 WriteCommandW(RA8875_DTPH0, x3);
WiredHome 175:7be3a1fb7fc2 1812 WriteCommandW(RA8875_DTPV0, y3);
WiredHome 19:3f82c1161fd2 1813 unsigned char drawCmd = 0x01; // Triangle
WiredHome 19:3f82c1161fd2 1814 if (fillit == FILL)
WiredHome 19:3f82c1161fd2 1815 drawCmd |= 0x20;
WiredHome 175:7be3a1fb7fc2 1816 WriteCommand(RA8875_DCR, drawCmd);
WiredHome 175:7be3a1fb7fc2 1817 WriteCommand(RA8875_DCR, 0x80 + drawCmd); // Start drawing.
WiredHome 131:5bd6ba2ee4a1 1818 if (!_WaitWhileReg(0x90, 0x80)) {
WiredHome 131:5bd6ba2ee4a1 1819 REGISTERPERFORMANCE(PRF_DRAWTRIANGLE);
WiredHome 131:5bd6ba2ee4a1 1820 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1821 }
WiredHome 19:3f82c1161fd2 1822 }
WiredHome 19:3f82c1161fd2 1823 REGISTERPERFORMANCE(PRF_DRAWTRIANGLE);
WiredHome 19:3f82c1161fd2 1824 return ret;
WiredHome 19:3f82c1161fd2 1825 }
WiredHome 19:3f82c1161fd2 1826
WiredHome 83:7bad0068cca0 1827
WiredHome 83:7bad0068cca0 1828 RetCode_t RA8875::circle(point_t p, dim_t radius,
WiredHome 83:7bad0068cca0 1829 color_t color, fill_t fillit)
WiredHome 83:7bad0068cca0 1830 {
WiredHome 83:7bad0068cca0 1831 foreground(color);
WiredHome 83:7bad0068cca0 1832 return circle(p.x,p.y,radius,fillit);
WiredHome 83:7bad0068cca0 1833 }
WiredHome 83:7bad0068cca0 1834
WiredHome 83:7bad0068cca0 1835
WiredHome 83:7bad0068cca0 1836 RetCode_t RA8875::fillcircle(point_t p, dim_t radius,
WiredHome 83:7bad0068cca0 1837 color_t color, fill_t fillit)
WiredHome 83:7bad0068cca0 1838 {
WiredHome 83:7bad0068cca0 1839 foreground(color);
WiredHome 83:7bad0068cca0 1840 return circle(p.x,p.y,radius,fillit);
WiredHome 83:7bad0068cca0 1841 }
WiredHome 83:7bad0068cca0 1842
WiredHome 83:7bad0068cca0 1843
WiredHome 83:7bad0068cca0 1844 RetCode_t RA8875::circle(point_t p, dim_t radius, fill_t fillit)
WiredHome 83:7bad0068cca0 1845 {
WiredHome 83:7bad0068cca0 1846 return circle(p.x,p.y,radius,fillit);
WiredHome 83:7bad0068cca0 1847 }
WiredHome 83:7bad0068cca0 1848
WiredHome 83:7bad0068cca0 1849
WiredHome 73:f22a18707b5e 1850 RetCode_t RA8875::circle(loc_t x, loc_t y, dim_t radius,
WiredHome 73:f22a18707b5e 1851 color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1852 {
WiredHome 19:3f82c1161fd2 1853 foreground(color);
WiredHome 19:3f82c1161fd2 1854 return circle(x,y,radius,fillit);
WiredHome 19:3f82c1161fd2 1855 }
WiredHome 19:3f82c1161fd2 1856
WiredHome 44:207594dece70 1857
WiredHome 73:f22a18707b5e 1858 RetCode_t RA8875::fillcircle(loc_t x, loc_t y, dim_t radius,
WiredHome 73:f22a18707b5e 1859 color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1860 {
WiredHome 19:3f82c1161fd2 1861 foreground(color);
WiredHome 19:3f82c1161fd2 1862 return circle(x,y,radius,fillit);
WiredHome 19:3f82c1161fd2 1863 }
WiredHome 19:3f82c1161fd2 1864
WiredHome 44:207594dece70 1865
WiredHome 37:f19b7e7449dc 1866 RetCode_t RA8875::circle(loc_t x, loc_t y, dim_t radius, fill_t fillit)
WiredHome 19:3f82c1161fd2 1867 {
WiredHome 19:3f82c1161fd2 1868 RetCode_t ret = noerror;
WiredHome 73:f22a18707b5e 1869
WiredHome 19:3f82c1161fd2 1870 PERFORMANCE_RESET;
WiredHome 105:4f116006ba1f 1871 if (radius <= 0 || (x - radius) < 0 || (x + radius) > screenwidth
WiredHome 105:4f116006ba1f 1872 || (y - radius) < 0 || (y + radius) > screenheight) {
WiredHome 19:3f82c1161fd2 1873 ret = bad_parameter;
WiredHome 19:3f82c1161fd2 1874 } else if (radius == 1) {
WiredHome 19:3f82c1161fd2 1875 pixel(x,y);
WiredHome 19:3f82c1161fd2 1876 } else {
WiredHome 175:7be3a1fb7fc2 1877 WriteCommandW(RA8875_DCHR0, x);
WiredHome 175:7be3a1fb7fc2 1878 WriteCommandW(RA8875_DCVR0, y);
WiredHome 175:7be3a1fb7fc2 1879 WriteCommand(RA8875_DCRR, radius & 0xFF);
WiredHome 19:3f82c1161fd2 1880 unsigned char drawCmd = 0x00; // Circle
WiredHome 19:3f82c1161fd2 1881 if (fillit == FILL)
WiredHome 19:3f82c1161fd2 1882 drawCmd |= 0x20;
WiredHome 175:7be3a1fb7fc2 1883 WriteCommand(RA8875_DCR, drawCmd);
WiredHome 175:7be3a1fb7fc2 1884 WriteCommand(RA8875_DCR, 0x40 + drawCmd); // Start drawing.
WiredHome 131:5bd6ba2ee4a1 1885 if (!_WaitWhileReg(0x90, 0x40)) {
WiredHome 131:5bd6ba2ee4a1 1886 REGISTERPERFORMANCE(PRF_DRAWCIRCLE);
WiredHome 131:5bd6ba2ee4a1 1887 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1888 }
WiredHome 19:3f82c1161fd2 1889 }
WiredHome 19:3f82c1161fd2 1890 REGISTERPERFORMANCE(PRF_DRAWCIRCLE);
WiredHome 19:3f82c1161fd2 1891 return ret;
WiredHome 19:3f82c1161fd2 1892 }
WiredHome 19:3f82c1161fd2 1893
WiredHome 44:207594dece70 1894
WiredHome 37:f19b7e7449dc 1895 RetCode_t RA8875::ellipse(loc_t x, loc_t y, dim_t radius1, dim_t radius2, color_t color, fill_t fillit)
WiredHome 19:3f82c1161fd2 1896 {
WiredHome 19:3f82c1161fd2 1897 foreground(color);
WiredHome 25:9556a3a9b7cc 1898 return ellipse(x,y,radius1,radius2,fillit);
WiredHome 19:3f82c1161fd2 1899 }
WiredHome 19:3f82c1161fd2 1900
WiredHome 44:207594dece70 1901
WiredHome 37:f19b7e7449dc 1902 RetCode_t RA8875::fillellipse(loc_t x, loc_t y, dim_t radius1, dim_t radius2, color_t color, fill_t fillit)
WiredHome 25:9556a3a9b7cc 1903 {
WiredHome 25:9556a3a9b7cc 1904 foreground(color);
WiredHome 25:9556a3a9b7cc 1905 return ellipse(x,y,radius1,radius2,fillit);
WiredHome 25:9556a3a9b7cc 1906 }
WiredHome 44:207594dece70 1907
WiredHome 73:f22a18707b5e 1908
WiredHome 37:f19b7e7449dc 1909 RetCode_t RA8875::ellipse(loc_t x, loc_t y, dim_t radius1, dim_t radius2, fill_t fillit)
WiredHome 19:3f82c1161fd2 1910 {
WiredHome 19:3f82c1161fd2 1911 RetCode_t ret = noerror;
WiredHome 73:f22a18707b5e 1912
WiredHome 19:3f82c1161fd2 1913 PERFORMANCE_RESET;
WiredHome 105:4f116006ba1f 1914 if (radius1 <= 0 || radius2 <= 0 || (x - radius1) < 0 || (x + radius1) > screenwidth
WiredHome 105:4f116006ba1f 1915 || (y - radius2) < 0 || (y + radius2) > screenheight) {
WiredHome 85:022bba13c5c4 1916 ret = bad_parameter;
WiredHome 25:9556a3a9b7cc 1917 } else if (radius1 == 1 && radius2 == 1) {
WiredHome 19:3f82c1161fd2 1918 pixel(x, y);
WiredHome 19:3f82c1161fd2 1919 } else {
WiredHome 175:7be3a1fb7fc2 1920 WriteCommandW(RA8875_DEHR0, x);
WiredHome 175:7be3a1fb7fc2 1921 WriteCommandW(RA8875_DEVR0, y);
WiredHome 175:7be3a1fb7fc2 1922 WriteCommandW(RA8875_ELLA0, radius1);
WiredHome 175:7be3a1fb7fc2 1923 WriteCommandW(RA8875_ELLB0, radius2);
WiredHome 19:3f82c1161fd2 1924 unsigned char drawCmd = 0x00; // Ellipse
WiredHome 19:3f82c1161fd2 1925 if (fillit == FILL)
WiredHome 19:3f82c1161fd2 1926 drawCmd |= 0x40;
WiredHome 175:7be3a1fb7fc2 1927 WriteCommand(RA8875_ELLIPSE, drawCmd);
WiredHome 175:7be3a1fb7fc2 1928 WriteCommand(RA8875_ELLIPSE, 0x80 + drawCmd); // Start drawing.
WiredHome 131:5bd6ba2ee4a1 1929 if (!_WaitWhileReg(0xA0, 0x80)) {
WiredHome 131:5bd6ba2ee4a1 1930 REGISTERPERFORMANCE(PRF_DRAWELLIPSE);
WiredHome 131:5bd6ba2ee4a1 1931 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1932 }
WiredHome 19:3f82c1161fd2 1933 }
WiredHome 19:3f82c1161fd2 1934 REGISTERPERFORMANCE(PRF_DRAWELLIPSE);
WiredHome 19:3f82c1161fd2 1935 return ret;
WiredHome 19:3f82c1161fd2 1936 }
WiredHome 19:3f82c1161fd2 1937
WiredHome 44:207594dece70 1938
WiredHome 68:ab08efabfc88 1939 RetCode_t RA8875::frequency(unsigned long Hz, unsigned long Hz2)
WiredHome 19:3f82c1161fd2 1940 {
WiredHome 66:468a11f05580 1941 spiwritefreq = Hz;
WiredHome 68:ab08efabfc88 1942 if (Hz2 != 0)
WiredHome 68:ab08efabfc88 1943 spireadfreq = Hz2;
WiredHome 68:ab08efabfc88 1944 else
WiredHome 68:ab08efabfc88 1945 spireadfreq = Hz/2;
WiredHome 68:ab08efabfc88 1946 _setWriteSpeed(true);
WiredHome 19:3f82c1161fd2 1947 // __ ___
WiredHome 19:3f82c1161fd2 1948 // Clock ___A Rising edge latched
WiredHome 19:3f82c1161fd2 1949 // ___ ____
WiredHome 19:3f82c1161fd2 1950 // Data ___X____
WiredHome 19:3f82c1161fd2 1951 spi.format(8, 3); // 8 bits and clock to data phase 0
WiredHome 19:3f82c1161fd2 1952 return noerror;
WiredHome 19:3f82c1161fd2 1953 }
WiredHome 19:3f82c1161fd2 1954
WiredHome 68:ab08efabfc88 1955 void RA8875::_setWriteSpeed(bool writeSpeed)
WiredHome 68:ab08efabfc88 1956 {
WiredHome 68:ab08efabfc88 1957 if (writeSpeed) {
WiredHome 68:ab08efabfc88 1958 spi.frequency(spiwritefreq);
WiredHome 68:ab08efabfc88 1959 spiWriteSpeed = true;
WiredHome 68:ab08efabfc88 1960 } else {
WiredHome 68:ab08efabfc88 1961 spi.frequency(spireadfreq);
WiredHome 68:ab08efabfc88 1962 spiWriteSpeed = false;
WiredHome 68:ab08efabfc88 1963 }
WiredHome 68:ab08efabfc88 1964 }
WiredHome 44:207594dece70 1965
WiredHome 131:5bd6ba2ee4a1 1966
WiredHome 131:5bd6ba2ee4a1 1967
WiredHome 131:5bd6ba2ee4a1 1968 RetCode_t RA8875::BlockMove(uint8_t dstLayer, uint8_t dstDataSelect, point_t dstPoint,
WiredHome 131:5bd6ba2ee4a1 1969 uint8_t srcLayer, uint8_t srcDataSelect, point_t srcPoint,
WiredHome 137:9e09f6081ef1 1970 dim_t bte_width, dim_t bte_height,
WiredHome 131:5bd6ba2ee4a1 1971 uint8_t bte_op_code, uint8_t bte_rop_code)
WiredHome 131:5bd6ba2ee4a1 1972 {
WiredHome 131:5bd6ba2ee4a1 1973 uint8_t cmd;
WiredHome 131:5bd6ba2ee4a1 1974
WiredHome 131:5bd6ba2ee4a1 1975 PERFORMANCE_RESET;
WiredHome 137:9e09f6081ef1 1976 ///@todo range check and error return rather than to secretly fix
WiredHome 131:5bd6ba2ee4a1 1977 srcPoint.x &= 0x3FF; // prevent high bits from doing unexpected things
WiredHome 131:5bd6ba2ee4a1 1978 srcPoint.y &= 0x1FF;
WiredHome 131:5bd6ba2ee4a1 1979 dstPoint.x &= 0x3FF;
WiredHome 131:5bd6ba2ee4a1 1980 dstPoint.y &= 0x1FF;
WiredHome 175:7be3a1fb7fc2 1981 WriteCommandW(RA8875_HSBE0, srcPoint.x);
WiredHome 175:7be3a1fb7fc2 1982 WriteCommandW(RA8875_VSBE0, ((dim_t)(srcLayer & 1) << 15) | srcPoint.y);
WiredHome 175:7be3a1fb7fc2 1983 WriteCommandW(RA8875_HDBE0, dstPoint.x);
WiredHome 175:7be3a1fb7fc2 1984 WriteCommandW(RA8875_VDBE0, ((dim_t)(dstLayer & 1) << 15) | dstPoint.y);
WiredHome 175:7be3a1fb7fc2 1985 WriteCommandW(RA8875_BEWR0, bte_width);
WiredHome 175:7be3a1fb7fc2 1986 WriteCommandW(RA8875_BEHR0, bte_height);
WiredHome 175:7be3a1fb7fc2 1987 WriteCommand(RA8875_BECR1, ((bte_rop_code & 0x0F) << 4) | (bte_op_code & 0x0F));
WiredHome 131:5bd6ba2ee4a1 1988 cmd = ((srcDataSelect & 1) << 6) | ((dstDataSelect & 1) << 5);
WiredHome 175:7be3a1fb7fc2 1989 WriteCommand(RA8875_BECR0, 0x80 | cmd); // enable the BTE
WiredHome 131:5bd6ba2ee4a1 1990 if (!_WaitWhileBusy(0x40)) {
WiredHome 131:5bd6ba2ee4a1 1991 REGISTERPERFORMANCE(PRF_BLOCKMOVE);
WiredHome 131:5bd6ba2ee4a1 1992 return external_abort;
WiredHome 131:5bd6ba2ee4a1 1993 }
WiredHome 131:5bd6ba2ee4a1 1994 REGISTERPERFORMANCE(PRF_BLOCKMOVE);
WiredHome 131:5bd6ba2ee4a1 1995 return noerror;
WiredHome 131:5bd6ba2ee4a1 1996 }
WiredHome 131:5bd6ba2ee4a1 1997
WiredHome 131:5bd6ba2ee4a1 1998
WiredHome 19:3f82c1161fd2 1999 RetCode_t RA8875::Power(bool on)
WiredHome 19:3f82c1161fd2 2000 {
WiredHome 175:7be3a1fb7fc2 2001 WriteCommand(RA8875_PWRR, (on) ? 0x80 : 0x00);
WiredHome 19:3f82c1161fd2 2002 return noerror;
WiredHome 19:3f82c1161fd2 2003 }
WiredHome 19:3f82c1161fd2 2004
WiredHome 44:207594dece70 2005
WiredHome 131:5bd6ba2ee4a1 2006 RetCode_t RA8875::Backlight_u8(uint8_t brightness)
WiredHome 19:3f82c1161fd2 2007 {
WiredHome 19:3f82c1161fd2 2008 static bool is_enabled = false;
WiredHome 131:5bd6ba2ee4a1 2009
WiredHome 19:3f82c1161fd2 2010 if (brightness == 0) {
WiredHome 175:7be3a1fb7fc2 2011 WriteCommand(RA8875_P1CR); // Disable the PWM
WiredHome 19:3f82c1161fd2 2012 WriteData(0x00);
WiredHome 19:3f82c1161fd2 2013 is_enabled = false;
WiredHome 19:3f82c1161fd2 2014 } else if (!is_enabled) {
WiredHome 175:7be3a1fb7fc2 2015 WriteCommand(RA8875_P1CR); // Enable the PWM
WiredHome 19:3f82c1161fd2 2016 WriteData(0x80);
WiredHome 175:7be3a1fb7fc2 2017 WriteCommand(RA8875_P1CR); // Not sure why this is needed, but following the pattern
WiredHome 19:3f82c1161fd2 2018 WriteData(0x81); // open PWM (SYS_CLK / 2 as best I can tell)
WiredHome 19:3f82c1161fd2 2019 is_enabled = true;
WiredHome 19:3f82c1161fd2 2020 }
WiredHome 175:7be3a1fb7fc2 2021 WriteCommand(RA8875_P1DCR, brightness); // Brightness parameter 0xff-0x00
WiredHome 19:3f82c1161fd2 2022 return noerror;
WiredHome 19:3f82c1161fd2 2023 }
WiredHome 19:3f82c1161fd2 2024
WiredHome 86:e86b355940f4 2025 uint8_t RA8875::GetBacklight_u8(void)
WiredHome 86:e86b355940f4 2026 {
WiredHome 86:e86b355940f4 2027 return ReadCommand(0x8b);
WiredHome 86:e86b355940f4 2028 }
WiredHome 44:207594dece70 2029
WiredHome 19:3f82c1161fd2 2030 RetCode_t RA8875::Backlight(float brightness)
WiredHome 19:3f82c1161fd2 2031 {
WiredHome 19:3f82c1161fd2 2032 unsigned char b;
WiredHome 73:f22a18707b5e 2033
WiredHome 29:422616aa04bd 2034 if (brightness >= 1.0)
WiredHome 19:3f82c1161fd2 2035 b = 255;
WiredHome 29:422616aa04bd 2036 else if (brightness <= 0.0)
WiredHome 19:3f82c1161fd2 2037 b = 0;
WiredHome 19:3f82c1161fd2 2038 else
WiredHome 19:3f82c1161fd2 2039 b = (unsigned char)(brightness * 255);
WiredHome 19:3f82c1161fd2 2040 return Backlight_u8(b);
WiredHome 19:3f82c1161fd2 2041 }
WiredHome 19:3f82c1161fd2 2042
WiredHome 86:e86b355940f4 2043 float RA8875::GetBacklight(void)
WiredHome 86:e86b355940f4 2044 {
WiredHome 86:e86b355940f4 2045 return (float)(GetBacklight_u8())/255;
WiredHome 86:e86b355940f4 2046 }
WiredHome 44:207594dece70 2047
WiredHome 98:ecebed9b80b2 2048 RetCode_t RA8875::SelectUserFont(const uint8_t * _font)
WiredHome 98:ecebed9b80b2 2049 {
WiredHome 98:ecebed9b80b2 2050 INFO("Cursor(%d,%d) %p", cursor_x, cursor_y, _font);
WiredHome 98:ecebed9b80b2 2051 INFO("Text C(%d,%d)", GetTextCursor_X(), GetTextCursor_Y());
WiredHome 98:ecebed9b80b2 2052 if (_font) {
WiredHome 98:ecebed9b80b2 2053 HexDump("Font Memory", _font, 16);
WiredHome 98:ecebed9b80b2 2054 extFontHeight = _font[6];
WiredHome 98:ecebed9b80b2 2055 uint32_t totalWidth = 0;
WiredHome 98:ecebed9b80b2 2056 uint16_t firstChar = _font[3] * 256 + _font[2];
WiredHome 98:ecebed9b80b2 2057 uint16_t lastChar = _font[5] * 256 + _font[4];
WiredHome 98:ecebed9b80b2 2058 uint16_t i;
WiredHome 98:ecebed9b80b2 2059
WiredHome 98:ecebed9b80b2 2060 for (i=firstChar; i<=lastChar; i++) {
WiredHome 98:ecebed9b80b2 2061 // 8 bytes of preamble to the first level lookup table
WiredHome 98:ecebed9b80b2 2062 uint16_t offsetToCharLookup = 8 + 4 * (i - firstChar); // 4-bytes: width(pixels), 16-bit offset from table start, 0
WiredHome 98:ecebed9b80b2 2063 totalWidth += _font[offsetToCharLookup];
WiredHome 98:ecebed9b80b2 2064 }
WiredHome 98:ecebed9b80b2 2065 extFontWidth = totalWidth / (lastChar - firstChar);
WiredHome 98:ecebed9b80b2 2066 INFO("Font Metrics: Avg W: %2d, H: %2d, First:%d, Last:%d", extFontWidth, extFontHeight, firstChar, lastChar);
WiredHome 98:ecebed9b80b2 2067 }
WiredHome 98:ecebed9b80b2 2068 SetTextCursor(GetTextCursor_X(), GetTextCursor_Y()); // soft-font cursor -> hw cursor
WiredHome 98:ecebed9b80b2 2069 font = _font;
WiredHome 98:ecebed9b80b2 2070 return GraphicsDisplay::SelectUserFont(_font);
WiredHome 98:ecebed9b80b2 2071 }
WiredHome 44:207594dece70 2072
WiredHome 19:3f82c1161fd2 2073 RetCode_t RA8875::background(color_t color)
WiredHome 19:3f82c1161fd2 2074 {
WiredHome 37:f19b7e7449dc 2075 GraphicsDisplay::background(color);
WiredHome 133:e36dcfc2d756 2076 return _writeColorTrio(0x60, color);
WiredHome 19:3f82c1161fd2 2077 }
WiredHome 19:3f82c1161fd2 2078
WiredHome 44:207594dece70 2079
WiredHome 19:3f82c1161fd2 2080 RetCode_t RA8875::background(unsigned char r, unsigned char g, unsigned char b)
WiredHome 19:3f82c1161fd2 2081 {
WiredHome 37:f19b7e7449dc 2082 background(RGB(r,g,b));
WiredHome 19:3f82c1161fd2 2083 return noerror;
WiredHome 19:3f82c1161fd2 2084 }
WiredHome 19:3f82c1161fd2 2085
WiredHome 44:207594dece70 2086
WiredHome 19:3f82c1161fd2 2087 RetCode_t RA8875::foreground(color_t color)
WiredHome 19:3f82c1161fd2 2088 {
WiredHome 37:f19b7e7449dc 2089 GraphicsDisplay::foreground(color);
WiredHome 133:e36dcfc2d756 2090 return _writeColorTrio(0x63, color);
WiredHome 19:3f82c1161fd2 2091 }
WiredHome 19:3f82c1161fd2 2092
WiredHome 44:207594dece70 2093
WiredHome 37:f19b7e7449dc 2094 RetCode_t RA8875::foreground(unsigned char r, unsigned char g, unsigned char b)
WiredHome 19:3f82c1161fd2 2095 {
WiredHome 37:f19b7e7449dc 2096 foreground(RGB(r,g,b));
WiredHome 19:3f82c1161fd2 2097 return noerror;
WiredHome 19:3f82c1161fd2 2098 }
WiredHome 19:3f82c1161fd2 2099
WiredHome 44:207594dece70 2100
WiredHome 37:f19b7e7449dc 2101 color_t RA8875::GetForeColor(void)
WiredHome 19:3f82c1161fd2 2102 {
WiredHome 133:e36dcfc2d756 2103 return _readColorTrio(0x63);
WiredHome 19:3f82c1161fd2 2104 }
WiredHome 19:3f82c1161fd2 2105
WiredHome 44:207594dece70 2106
WiredHome 19:3f82c1161fd2 2107 color_t RA8875::DOSColor(int i)
WiredHome 73:f22a18707b5e 2108 {
WiredHome 73:f22a18707b5e 2109 const color_t colors[16] = {
WiredHome 19:3f82c1161fd2 2110 Black, Blue, Green, Cyan,
WiredHome 19:3f82c1161fd2 2111 Red, Magenta, Brown, Gray,
WiredHome 19:3f82c1161fd2 2112 Charcoal, BrightBlue, BrightGreen, BrightCyan,
WiredHome 19:3f82c1161fd2 2113 Orange, Pink, Yellow, White
WiredHome 73:f22a18707b5e 2114 };
WiredHome 85:022bba13c5c4 2115 if (i >= 0 && i < 16)
WiredHome 19:3f82c1161fd2 2116 return colors[i];
WiredHome 19:3f82c1161fd2 2117 else
WiredHome 19:3f82c1161fd2 2118 return 0;
WiredHome 73:f22a18707b5e 2119 }
WiredHome 19:3f82c1161fd2 2120
WiredHome 44:207594dece70 2121
WiredHome 73:f22a18707b5e 2122 const char * RA8875::DOSColorNames(int i)
WiredHome 73:f22a18707b5e 2123 {
WiredHome 73:f22a18707b5e 2124 const char * names[16] = {
WiredHome 19:3f82c1161fd2 2125 "Black", "Blue", "Green", "Cyan",
WiredHome 19:3f82c1161fd2 2126 "Red", "Magenta", "Brown", "Gray",
WiredHome 19:3f82c1161fd2 2127 "Charcoal", "BrightBlue", "BrightGreen", "BrightCyan",
WiredHome 19:3f82c1161fd2 2128 "Orange", "Pink", "Yellow", "White"
WiredHome 73:f22a18707b5e 2129 };
WiredHome 85:022bba13c5c4 2130 if (i >= 0 && i < 16)
WiredHome 19:3f82c1161fd2 2131 return names[i];
WiredHome 19:3f82c1161fd2 2132 else
WiredHome 19:3f82c1161fd2 2133 return NULL;
WiredHome 73:f22a18707b5e 2134 }
WiredHome 19:3f82c1161fd2 2135
WiredHome 19:3f82c1161fd2 2136
WiredHome 19:3f82c1161fd2 2137 ///////////////////////////////////////////////////////////////
WiredHome 19:3f82c1161fd2 2138 // Private functions
WiredHome 19:3f82c1161fd2 2139
WiredHome 79:544eb4964795 2140 unsigned char RA8875::_spiwrite(unsigned char data)
WiredHome 19:3f82c1161fd2 2141 {
WiredHome 19:3f82c1161fd2 2142 unsigned char retval;
WiredHome 73:f22a18707b5e 2143
WiredHome 68:ab08efabfc88 2144 if (!spiWriteSpeed)
WiredHome 68:ab08efabfc88 2145 _setWriteSpeed(true);
WiredHome 19:3f82c1161fd2 2146 retval = spi.write(data);
WiredHome 19:3f82c1161fd2 2147 return retval;
WiredHome 19:3f82c1161fd2 2148 }
WiredHome 19:3f82c1161fd2 2149
WiredHome 44:207594dece70 2150
WiredHome 79:544eb4964795 2151 unsigned char RA8875::_spiread(void)
WiredHome 19:3f82c1161fd2 2152 {
WiredHome 19:3f82c1161fd2 2153 unsigned char retval;
WiredHome 19:3f82c1161fd2 2154 unsigned char data = 0;
WiredHome 73:f22a18707b5e 2155
WiredHome 68:ab08efabfc88 2156 if (spiWriteSpeed)
WiredHome 68:ab08efabfc88 2157 _setWriteSpeed(false);
WiredHome 19:3f82c1161fd2 2158 retval = spi.write(data);
WiredHome 19:3f82c1161fd2 2159 return retval;
WiredHome 19:3f82c1161fd2 2160 }
WiredHome 19:3f82c1161fd2 2161
WiredHome 44:207594dece70 2162
WiredHome 79:544eb4964795 2163 RetCode_t RA8875::_select(bool chipsel)
WiredHome 19:3f82c1161fd2 2164 {
WiredHome 19:3f82c1161fd2 2165 cs = (chipsel == true) ? 0 : 1;
WiredHome 19:3f82c1161fd2 2166 return noerror;
WiredHome 19:3f82c1161fd2 2167 }
WiredHome 19:3f82c1161fd2 2168
WiredHome 44:207594dece70 2169
WiredHome 72:ecffe56af969 2170 RetCode_t RA8875::PrintScreen(uint16_t layer, loc_t x, loc_t y, dim_t w, dim_t h, const char *Name_BMP)
WiredHome 72:ecffe56af969 2171 {
WiredHome 74:686faa218914 2172 (void)layer;
WiredHome 96:40b74dd3695b 2173
WiredHome 96:40b74dd3695b 2174 // AttachPrintHandler(this, RA8875::_printCallback);
WiredHome 96:40b74dd3695b 2175 // return PrintScreen(x,y,w,h);
WiredHome 74:686faa218914 2176 return PrintScreen(x, y, w, h, Name_BMP);
WiredHome 72:ecffe56af969 2177 }
WiredHome 72:ecffe56af969 2178
WiredHome 96:40b74dd3695b 2179 RetCode_t RA8875::_printCallback(RA8875::filecmd_t cmd, uint8_t * buffer, uint16_t size)
WiredHome 96:40b74dd3695b 2180 {
WiredHome 96:40b74dd3695b 2181 HexDump("CB", buffer, size);
WiredHome 96:40b74dd3695b 2182 switch(cmd) {
WiredHome 96:40b74dd3695b 2183 case RA8875::OPEN:
WiredHome 96:40b74dd3695b 2184 //pc.printf("About to write %lu bytes\r\n", *(uint32_t *)buffer);
WiredHome 96:40b74dd3695b 2185 _printFH = fopen("file.bmp", "w+b");
WiredHome 96:40b74dd3695b 2186 if (_printFH == 0)
WiredHome 96:40b74dd3695b 2187 return file_not_found;
WiredHome 96:40b74dd3695b 2188 break;
WiredHome 96:40b74dd3695b 2189 case RA8875::WRITE:
WiredHome 96:40b74dd3695b 2190 //pc.printf(" Write %4u bytes\r\n", size);
WiredHome 96:40b74dd3695b 2191 fwrite(buffer, 1, size, _printFH);
WiredHome 96:40b74dd3695b 2192 break;
WiredHome 96:40b74dd3695b 2193 case RA8875::CLOSE:
WiredHome 96:40b74dd3695b 2194 //pc.printf(" close\r\n");
WiredHome 96:40b74dd3695b 2195 fclose(_printFH);
WiredHome 96:40b74dd3695b 2196 _printFH = 0;
WiredHome 96:40b74dd3695b 2197 break;
WiredHome 96:40b74dd3695b 2198 default:
WiredHome 96:40b74dd3695b 2199 //pc.printf("Unexpected callback %d\r\n", cmd);
WiredHome 96:40b74dd3695b 2200 return file_not_found;
WiredHome 96:40b74dd3695b 2201 //break;
WiredHome 96:40b74dd3695b 2202 }
WiredHome 96:40b74dd3695b 2203 return noerror;
WiredHome 96:40b74dd3695b 2204 }
WiredHome 96:40b74dd3695b 2205
WiredHome 162:a2d7f1988711 2206 int RA8875::RoundUp(int value, int roundTo)
WiredHome 162:a2d7f1988711 2207 {
WiredHome 162:a2d7f1988711 2208 if (roundTo == 0)
WiredHome 162:a2d7f1988711 2209 return 0;
WiredHome 162:a2d7f1988711 2210 return ((value + roundTo - 1) / roundTo) * roundTo;
WiredHome 162:a2d7f1988711 2211 }
WiredHome 162:a2d7f1988711 2212
WiredHome 164:76edd7d9cb68 2213 RetCode_t RA8875::PrintScreen(loc_t x, loc_t y, dim_t w, dim_t h, uint8_t bitsPerPixel)
WiredHome 96:40b74dd3695b 2214 {
WiredHome 96:40b74dd3695b 2215 BITMAPFILEHEADER BMP_Header;
WiredHome 96:40b74dd3695b 2216 BITMAPINFOHEADER BMP_Info;
WiredHome 96:40b74dd3695b 2217 uint8_t * lineBuffer = NULL;
WiredHome 96:40b74dd3695b 2218 color_t * pixelBuffer = NULL;
WiredHome 96:40b74dd3695b 2219 color_t * pixelBuffer2 = NULL;
WiredHome 96:40b74dd3695b 2220
WiredHome 164:76edd7d9cb68 2221 INFO("(%d,%d)-(%d,%d)x%d", x,y,w,h,bitsPerPixel);
WiredHome 105:4f116006ba1f 2222 if (x >= 0 && x < screenwidth
WiredHome 105:4f116006ba1f 2223 && y >= 0 && y < screenheight
WiredHome 105:4f116006ba1f 2224 && w > 0 && x + w <= screenwidth
WiredHome 105:4f116006ba1f 2225 && h > 0 && y + h <= screenheight) {
WiredHome 96:40b74dd3695b 2226 BMP_Header.bfType = BF_TYPE;
WiredHome 96:40b74dd3695b 2227 BMP_Header.bfReserved1 = 0;
WiredHome 96:40b74dd3695b 2228 BMP_Header.bfReserved2 = 0;
WiredHome 164:76edd7d9cb68 2229 switch (bitsPerPixel) {
WiredHome 164:76edd7d9cb68 2230 case 24:
WiredHome 164:76edd7d9cb68 2231 BMP_Header.bfOffBits = sizeof(BMP_Header) + sizeof(BMP_Info);
WiredHome 164:76edd7d9cb68 2232 BMP_Header.bfSize = (h * RoundUp(w * sizeof(RGBQUAD),4)) + BMP_Header.bfOffBits;
WiredHome 164:76edd7d9cb68 2233 break;
WiredHome 164:76edd7d9cb68 2234 case 8:
WiredHome 164:76edd7d9cb68 2235 default:
WiredHome 164:76edd7d9cb68 2236 BMP_Header.bfOffBits = sizeof(BMP_Header) + sizeof(BMP_Info) + sizeof(WebColorPalette);
WiredHome 164:76edd7d9cb68 2237 INFO("Initial Offset to Bitstream %X", BMP_Header.bfOffBits);
WiredHome 164:76edd7d9cb68 2238 //if (BMP_Header.bfOffBits & 0x03) {
WiredHome 164:76edd7d9cb68 2239 // BMP_Header.bfOffBits += (4 - (BMP_Header.bfOffBits & 0x03));
WiredHome 164:76edd7d9cb68 2240 //}
WiredHome 164:76edd7d9cb68 2241 BMP_Header.bfSize = (h * RoundUp(w * 1,4)) + BMP_Header.bfOffBits;
WiredHome 164:76edd7d9cb68 2242 break;
WiredHome 164:76edd7d9cb68 2243 }
WiredHome 164:76edd7d9cb68 2244 INFO("Offset to Bitstream %X", BMP_Header.bfOffBits);
WiredHome 162:a2d7f1988711 2245
WiredHome 162:a2d7f1988711 2246 // Bytes in the line buffer
WiredHome 164:76edd7d9cb68 2247 int lineBufSize = RoundUp(((bitsPerPixel == 24) ? 3 : 1) * w, 4);
WiredHome 162:a2d7f1988711 2248 INFO("LineBufSize: %d", lineBufSize);
WiredHome 96:40b74dd3695b 2249
WiredHome 96:40b74dd3695b 2250 BMP_Info.biSize = sizeof(BMP_Info);
WiredHome 96:40b74dd3695b 2251 BMP_Info.biWidth = w;
WiredHome 96:40b74dd3695b 2252 BMP_Info.biHeight = h;
WiredHome 96:40b74dd3695b 2253 BMP_Info.biPlanes = 1;
WiredHome 164:76edd7d9cb68 2254 BMP_Info.biBitCount = bitsPerPixel;
WiredHome 96:40b74dd3695b 2255 BMP_Info.biCompression = BI_RGB;
WiredHome 162:a2d7f1988711 2256 BMP_Info.biSizeImage = lineBufSize * h;
WiredHome 96:40b74dd3695b 2257 BMP_Info.biXPelsPerMeter = 0;
WiredHome 96:40b74dd3695b 2258 BMP_Info.biYPelsPerMeter = 0;
WiredHome 164:76edd7d9cb68 2259 // for 24-bit, there is no palette, so these are zero
WiredHome 164:76edd7d9cb68 2260 // for 8-bit, there can be up to 256 RGB values in the palette
WiredHome 164:76edd7d9cb68 2261
WiredHome 164:76edd7d9cb68 2262 BMP_Info.biClrUsed = (bitsPerPixel == 24) ? 0 : sizeof(WebColorPalette)/sizeof(WebColorPalette[0]); // for 8b/pixel
WiredHome 164:76edd7d9cb68 2263 BMP_Info.biClrImportant = BMP_Info.biClrUsed;
WiredHome 96:40b74dd3695b 2264
WiredHome 96:40b74dd3695b 2265 // Allocate the memory we need to proceed
WiredHome 145:5eb2492acdda 2266 lineBuffer = (uint8_t *)swMalloc(lineBufSize);
WiredHome 96:40b74dd3695b 2267 if (lineBuffer == NULL) {
WiredHome 96:40b74dd3695b 2268 ERR("Not enough RAM for PrintScreen lineBuffer");
WiredHome 96:40b74dd3695b 2269 return(not_enough_ram);
WiredHome 96:40b74dd3695b 2270 }
WiredHome 162:a2d7f1988711 2271 memset(lineBuffer, 0, lineBufSize); // zero-Fill
WiredHome 96:40b74dd3695b 2272
WiredHome 96:40b74dd3695b 2273 #define DOUBLEBUF /* one larger buffer instead of two */
WiredHome 96:40b74dd3695b 2274
WiredHome 96:40b74dd3695b 2275 #ifdef DOUBLEBUF
WiredHome 96:40b74dd3695b 2276 // In the "#else", pixelBuffer2 malloc returns a value,
WiredHome 96:40b74dd3695b 2277 // but is actually causing a failure later.
WiredHome 96:40b74dd3695b 2278 // This test helps determine if it is truly out of memory,
WiredHome 96:40b74dd3695b 2279 // or if malloc is broken.
WiredHome 145:5eb2492acdda 2280 pixelBuffer = (color_t *)swMalloc(2 * w * sizeof(color_t));
WiredHome 96:40b74dd3695b 2281 pixelBuffer2 = pixelBuffer + (w * sizeof(color_t));
WiredHome 96:40b74dd3695b 2282 #else
WiredHome 145:5eb2492acdda 2283 pixelBuffer = (color_t *)swMalloc(w * sizeof(color_t));
WiredHome 145:5eb2492acdda 2284 pixelBuffer2 = (color_t *)swMalloc(w * sizeof(color_t));
WiredHome 96:40b74dd3695b 2285 #endif
WiredHome 96:40b74dd3695b 2286 if (pixelBuffer == NULL || pixelBuffer2 == NULL) {
WiredHome 96:40b74dd3695b 2287 ERR("Not enough RAM for pixelBuffer");
WiredHome 96:40b74dd3695b 2288 #ifndef DOUBLEBUF
WiredHome 96:40b74dd3695b 2289 if (pixelBuffer2)
WiredHome 145:5eb2492acdda 2290 swFree(pixelBuffer2);
WiredHome 96:40b74dd3695b 2291 #endif
WiredHome 96:40b74dd3695b 2292 if (pixelBuffer)
WiredHome 145:5eb2492acdda 2293 swFree(pixelBuffer);
WiredHome 145:5eb2492acdda 2294 swFree(lineBuffer);
WiredHome 96:40b74dd3695b 2295 return(not_enough_ram);
WiredHome 96:40b74dd3695b 2296 }
WiredHome 96:40b74dd3695b 2297
WiredHome 96:40b74dd3695b 2298 // Get the file primed...
WiredHome 164:76edd7d9cb68 2299 /// @todo check return value for possibility of a fatal error
WiredHome 96:40b74dd3695b 2300 privateCallback(OPEN, (uint8_t *)&BMP_Header.bfSize, 4);
WiredHome 96:40b74dd3695b 2301
WiredHome 96:40b74dd3695b 2302 // Be optimistic - don't check for errors.
WiredHome 162:a2d7f1988711 2303 HexDump("BMP_Header", (uint8_t *)&BMP_Header, sizeof(BMP_Header));
WiredHome 96:40b74dd3695b 2304 //fwrite(&BMP_Header, sizeof(char), sizeof(BMP_Header), Image);
WiredHome 96:40b74dd3695b 2305 privateCallback(WRITE, (uint8_t *)&BMP_Header, sizeof(BMP_Header));
WiredHome 96:40b74dd3695b 2306
WiredHome 96:40b74dd3695b 2307 HexDump("BMP_Info", (uint8_t *)&BMP_Info, sizeof(BMP_Info));
WiredHome 96:40b74dd3695b 2308 //fwrite(&BMP_Info, sizeof(char), sizeof(BMP_Info), Image);
WiredHome 96:40b74dd3695b 2309 privateCallback(WRITE, (uint8_t *)&BMP_Info, sizeof(BMP_Info));
WiredHome 164:76edd7d9cb68 2310 if (bitsPerPixel != 24) {
WiredHome 164:76edd7d9cb68 2311 HexDump("Palette", (uint8_t *)&WebColorPalette, sizeof(WebColorPalette));
WiredHome 164:76edd7d9cb68 2312 //fwrite(&WebColorPalette, sizeof(char), sizeof(WebColorPalette), Image);
WiredHome 164:76edd7d9cb68 2313 privateCallback(WRITE, (uint8_t *)&WebColorPalette, sizeof(WebColorPalette));
WiredHome 164:76edd7d9cb68 2314 if (0 && sizeof(WebColorPalette) % 4) {
WiredHome 164:76edd7d9cb68 2315 const uint8_t padd[] = { 0, 0, 0 };
WiredHome 164:76edd7d9cb68 2316 //fwrite(&padd, sizeof(char), (sizeof(BMP_Header) + sizeof(BMP_Info) + sizeof(WebColorPalette)) % 4, Image);
WiredHome 164:76edd7d9cb68 2317 privateCallback(WRITE, (uint8_t *)&padd, (sizeof(BMP_Header) + sizeof(BMP_Info) + sizeof(WebColorPalette)) % 4);
WiredHome 164:76edd7d9cb68 2318 }
WiredHome 164:76edd7d9cb68 2319 }
WiredHome 96:40b74dd3695b 2320 //color_t transparency = GetBackgroundTransparencyColor();
WiredHome 96:40b74dd3695b 2321 LayerMode_T ltpr0 = GetLayerMode();
WiredHome 96:40b74dd3695b 2322
WiredHome 96:40b74dd3695b 2323 uint16_t prevLayer = GetDrawingLayer();
WiredHome 96:40b74dd3695b 2324 // If only one of the layers is visible, select that layer
WiredHome 96:40b74dd3695b 2325 switch(ltpr0) {
WiredHome 96:40b74dd3695b 2326 case ShowLayer0:
WiredHome 96:40b74dd3695b 2327 SelectDrawingLayer(0);
WiredHome 96:40b74dd3695b 2328 break;
WiredHome 96:40b74dd3695b 2329 case ShowLayer1:
WiredHome 96:40b74dd3695b 2330 SelectDrawingLayer(1);
WiredHome 96:40b74dd3695b 2331 break;
WiredHome 96:40b74dd3695b 2332 default:
WiredHome 96:40b74dd3695b 2333 break;
WiredHome 96:40b74dd3695b 2334 }
WiredHome 96:40b74dd3695b 2335
WiredHome 96:40b74dd3695b 2336 // Read the display from the last line toward the top
WiredHome 96:40b74dd3695b 2337 // so we can write the file in one pass.
WiredHome 96:40b74dd3695b 2338 for (int j = h - 1; j >= 0; j--) {
WiredHome 149:c62c4b2d6a15 2339 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 2340 (*idle_callback)(progress, (h - 1 - j) * 100 / (h - 1));
WiredHome 149:c62c4b2d6a15 2341 }
WiredHome 149:c62c4b2d6a15 2342
WiredHome 96:40b74dd3695b 2343 if (ltpr0 >= 2) // Need to combine the layers...
WiredHome 96:40b74dd3695b 2344 SelectDrawingLayer(0); // so read layer 0 first
WiredHome 96:40b74dd3695b 2345 // Read one line of pixels to a local buffer
WiredHome 96:40b74dd3695b 2346 if (getPixelStream(pixelBuffer, w, x,y+j) != noerror) {
WiredHome 96:40b74dd3695b 2347 ERR("getPixelStream error, and no recovery handler...");
WiredHome 96:40b74dd3695b 2348 }
WiredHome 96:40b74dd3695b 2349 if (ltpr0 >= 2) { // Need to combine the layers...
WiredHome 96:40b74dd3695b 2350 SelectDrawingLayer(1); // so read layer 1 next
WiredHome 96:40b74dd3695b 2351 if (getPixelStream(pixelBuffer2, w, x,y+j) != noerror) {
WiredHome 96:40b74dd3695b 2352 ERR("getPixelStream error, and no recovery handler...");
WiredHome 96:40b74dd3695b 2353 }
WiredHome 96:40b74dd3695b 2354 }
WiredHome 164:76edd7d9cb68 2355 INFO("Line: %3d", j);
WiredHome 164:76edd7d9cb68 2356 //HexDump("Raster", (uint8_t *)pixelBuffer, w * sizeof(color_t));
WiredHome 96:40b74dd3695b 2357 // Convert the local buffer to RGBQUAD format
WiredHome 96:40b74dd3695b 2358 int lb = 0;
WiredHome 96:40b74dd3695b 2359 for (int i=0; i<w; i++) {
WiredHome 162:a2d7f1988711 2360 color_t tColor = pixelBuffer[x+i];
WiredHome 162:a2d7f1988711 2361 tColor = (tColor >> 8) | (tColor << 8); // Byte Swap
WiredHome 162:a2d7f1988711 2362 RGBQUAD q0 = RGB16ToRGBQuad(tColor); // Scale to 24-bits
WiredHome 162:a2d7f1988711 2363 tColor = pixelBuffer2[x+i];
WiredHome 162:a2d7f1988711 2364 tColor = (tColor >> 8) | (tColor << 8); // Byte Swap
WiredHome 162:a2d7f1988711 2365 RGBQUAD q1 = RGB16ToRGBQuad(tColor); // Scale to 24-bits
WiredHome 96:40b74dd3695b 2366 switch (ltpr0) {
WiredHome 96:40b74dd3695b 2367 case 0:
WiredHome 96:40b74dd3695b 2368 case 1:
WiredHome 96:40b74dd3695b 2369 case 2: // lighten-overlay (@TODO Not supported yet)
WiredHome 96:40b74dd3695b 2370 case 6: // Floating Windows (@TODO not sure how to support)
WiredHome 96:40b74dd3695b 2371 default: // Reserved...
WiredHome 164:76edd7d9cb68 2372 //lineBuffer[lb++] = q0.rgbBlue;
WiredHome 164:76edd7d9cb68 2373 //lineBuffer[lb++] = q0.rgbGreen;
WiredHome 164:76edd7d9cb68 2374 //lineBuffer[lb++] = q0.rgbRed;
WiredHome 164:76edd7d9cb68 2375 break;
WiredHome 164:76edd7d9cb68 2376 case 3: // transparent mode (@TODO Read the background color register for transparent)
WiredHome 164:76edd7d9cb68 2377 case 4: // boolean or
WiredHome 164:76edd7d9cb68 2378 q0.rgbBlue = q0.rgbBlue | q1.rgbBlue;
WiredHome 164:76edd7d9cb68 2379 q0.rgbGreen = q0.rgbGreen | q1.rgbGreen;
WiredHome 164:76edd7d9cb68 2380 q0.rgbRed = q0.rgbRed | q1.rgbRed;
WiredHome 164:76edd7d9cb68 2381 break;
WiredHome 164:76edd7d9cb68 2382 case 5: // boolean AND
WiredHome 164:76edd7d9cb68 2383 q0.rgbBlue = q0.rgbBlue & q1.rgbBlue;
WiredHome 164:76edd7d9cb68 2384 q0.rgbGreen = q0.rgbGreen & q1.rgbGreen;
WiredHome 164:76edd7d9cb68 2385 q0.rgbRed = q0.rgbRed & q1.rgbRed;
WiredHome 164:76edd7d9cb68 2386 break;
WiredHome 164:76edd7d9cb68 2387 }
WiredHome 164:76edd7d9cb68 2388 switch (bitsPerPixel) {
WiredHome 164:76edd7d9cb68 2389 case 24:
WiredHome 96:40b74dd3695b 2390 lineBuffer[lb++] = q0.rgbBlue;
WiredHome 96:40b74dd3695b 2391 lineBuffer[lb++] = q0.rgbGreen;
WiredHome 96:40b74dd3695b 2392 lineBuffer[lb++] = q0.rgbRed;
WiredHome 96:40b74dd3695b 2393 break;
WiredHome 164:76edd7d9cb68 2394 case 8:
WiredHome 164:76edd7d9cb68 2395 default:
WiredHome 164:76edd7d9cb68 2396 lineBuffer[lb++] = FindNearestWebColor(q0.rgbRed,q0.rgbGreen,q0.rgbBlue);
WiredHome 96:40b74dd3695b 2397 break;
WiredHome 96:40b74dd3695b 2398 }
WiredHome 96:40b74dd3695b 2399 }
WiredHome 164:76edd7d9cb68 2400 //if (j == h - 1) {
WiredHome 164:76edd7d9cb68 2401 // HexDump("Line", lineBuffer, lineBufSize);
WiredHome 164:76edd7d9cb68 2402 //}
WiredHome 96:40b74dd3695b 2403 // Write to disk
WiredHome 162:a2d7f1988711 2404 privateCallback(WRITE, (uint8_t *)lineBuffer, lineBufSize);
WiredHome 96:40b74dd3695b 2405 }
WiredHome 96:40b74dd3695b 2406 SelectDrawingLayer(prevLayer);
WiredHome 96:40b74dd3695b 2407 privateCallback(CLOSE, NULL, 0);
WiredHome 96:40b74dd3695b 2408 #ifndef DOUBLEBUF
WiredHome 96:40b74dd3695b 2409 if (pixelBuffer2)
WiredHome 145:5eb2492acdda 2410 swFree(pixelBuffer2);
WiredHome 96:40b74dd3695b 2411 #endif
WiredHome 96:40b74dd3695b 2412 if (pixelBuffer)
WiredHome 145:5eb2492acdda 2413 swFree(pixelBuffer);
WiredHome 145:5eb2492acdda 2414 swFree(lineBuffer);
WiredHome 96:40b74dd3695b 2415 INFO("Image closed");
WiredHome 96:40b74dd3695b 2416 return noerror;
WiredHome 96:40b74dd3695b 2417 } else {
WiredHome 96:40b74dd3695b 2418 return bad_parameter;
WiredHome 96:40b74dd3695b 2419 }
WiredHome 96:40b74dd3695b 2420 }
WiredHome 79:544eb4964795 2421
WiredHome 163:17526689a3ed 2422
WiredHome 164:76edd7d9cb68 2423
WiredHome 163:17526689a3ed 2424 RetCode_t RA8875::PrintScreen(loc_t x, loc_t y, dim_t w, dim_t h, const char *Name_BMP, uint8_t bitsPerPixel)
WiredHome 72:ecffe56af969 2425 {
WiredHome 72:ecffe56af969 2426 BITMAPFILEHEADER BMP_Header;
WiredHome 72:ecffe56af969 2427 BITMAPINFOHEADER BMP_Info;
WiredHome 86:e86b355940f4 2428 uint8_t * lineBuffer = NULL;
WiredHome 86:e86b355940f4 2429 color_t * pixelBuffer = NULL;
WiredHome 86:e86b355940f4 2430 color_t * pixelBuffer2 = NULL;
WiredHome 86:e86b355940f4 2431
WiredHome 163:17526689a3ed 2432 INFO("(%d,%d)-(%d,%d)x%d %s", x,y,w,h,bitsPerPixel,Name_BMP);
WiredHome 105:4f116006ba1f 2433 if (x >= 0 && x < screenwidth
WiredHome 105:4f116006ba1f 2434 && y >= 0 && y < screenheight
WiredHome 105:4f116006ba1f 2435 && w > 0 && x + w <= screenwidth
WiredHome 105:4f116006ba1f 2436 && h > 0 && y + h <= screenheight) {
WiredHome 72:ecffe56af969 2437 BMP_Header.bfType = BF_TYPE;
WiredHome 72:ecffe56af969 2438 BMP_Header.bfReserved1 = 0;
WiredHome 72:ecffe56af969 2439 BMP_Header.bfReserved2 = 0;
WiredHome 163:17526689a3ed 2440 switch (bitsPerPixel) {
WiredHome 163:17526689a3ed 2441 case 24:
WiredHome 163:17526689a3ed 2442 BMP_Header.bfOffBits = sizeof(BMP_Header) + sizeof(BMP_Info);
WiredHome 163:17526689a3ed 2443 BMP_Header.bfSize = (h * RoundUp(w * sizeof(RGBQUAD),4)) + BMP_Header.bfOffBits;
WiredHome 163:17526689a3ed 2444 break;
WiredHome 163:17526689a3ed 2445 case 8:
WiredHome 163:17526689a3ed 2446 default:
WiredHome 163:17526689a3ed 2447 BMP_Header.bfOffBits = sizeof(BMP_Header) + sizeof(BMP_Info) + sizeof(WebColorPalette);
WiredHome 163:17526689a3ed 2448 INFO("Initial Offset to Bitstream %X", BMP_Header.bfOffBits);
WiredHome 163:17526689a3ed 2449 //if (BMP_Header.bfOffBits & 0x03) {
WiredHome 163:17526689a3ed 2450 // BMP_Header.bfOffBits += (4 - (BMP_Header.bfOffBits & 0x03));
WiredHome 163:17526689a3ed 2451 //}
WiredHome 163:17526689a3ed 2452 BMP_Header.bfSize = (h * RoundUp(w * 1,4)) + BMP_Header.bfOffBits;
WiredHome 163:17526689a3ed 2453 break;
WiredHome 163:17526689a3ed 2454 }
WiredHome 163:17526689a3ed 2455 INFO("Offset to Bitstream %X", BMP_Header.bfOffBits);
WiredHome 162:a2d7f1988711 2456
WiredHome 162:a2d7f1988711 2457 // Bytes in the line buffer
WiredHome 163:17526689a3ed 2458 int lineBufSize = RoundUp(((bitsPerPixel == 24) ? 3 : 1) * w, 4);
WiredHome 162:a2d7f1988711 2459 INFO("LineBufSize: %d", lineBufSize);
WiredHome 73:f22a18707b5e 2460
WiredHome 72:ecffe56af969 2461 BMP_Info.biSize = sizeof(BMP_Info);
WiredHome 72:ecffe56af969 2462 BMP_Info.biWidth = w;
WiredHome 72:ecffe56af969 2463 BMP_Info.biHeight = h;
WiredHome 72:ecffe56af969 2464 BMP_Info.biPlanes = 1;
WiredHome 163:17526689a3ed 2465 BMP_Info.biBitCount = bitsPerPixel;
WiredHome 72:ecffe56af969 2466 BMP_Info.biCompression = BI_RGB;
WiredHome 162:a2d7f1988711 2467 BMP_Info.biSizeImage = lineBufSize * h;
WiredHome 72:ecffe56af969 2468 BMP_Info.biXPelsPerMeter = 0;
WiredHome 72:ecffe56af969 2469 BMP_Info.biYPelsPerMeter = 0;
WiredHome 163:17526689a3ed 2470 // for 24-bit, there is no palette, so these are zero
WiredHome 163:17526689a3ed 2471 // for 8-bit, there can be up to 256 RGB values in the palette
WiredHome 163:17526689a3ed 2472
WiredHome 163:17526689a3ed 2473 BMP_Info.biClrUsed = (bitsPerPixel == 24) ? 0 : sizeof(WebColorPalette)/sizeof(WebColorPalette[0]); // for 8b/pixel
WiredHome 163:17526689a3ed 2474 BMP_Info.biClrImportant = BMP_Info.biClrUsed;
WiredHome 72:ecffe56af969 2475
WiredHome 86:e86b355940f4 2476 // Allocate the memory we need to proceed
WiredHome 145:5eb2492acdda 2477 lineBuffer = (uint8_t *)swMalloc(lineBufSize);
WiredHome 86:e86b355940f4 2478 if (lineBuffer == NULL) {
WiredHome 86:e86b355940f4 2479 ERR("Not enough RAM for PrintScreen lineBuffer");
WiredHome 86:e86b355940f4 2480 return(not_enough_ram);
WiredHome 86:e86b355940f4 2481 }
WiredHome 162:a2d7f1988711 2482 memset(lineBuffer, 0, lineBufSize); // zero-Fill
WiredHome 86:e86b355940f4 2483
WiredHome 86:e86b355940f4 2484 #define DOUBLEBUF /* one larger buffer instead of two */
WiredHome 86:e86b355940f4 2485
WiredHome 86:e86b355940f4 2486 #ifdef DOUBLEBUF
WiredHome 86:e86b355940f4 2487 // In the "#else", pixelBuffer2 malloc returns a value,
WiredHome 86:e86b355940f4 2488 // but is actually causing a failure later.
WiredHome 86:e86b355940f4 2489 // This test helps determine if it is truly out of memory,
WiredHome 86:e86b355940f4 2490 // or if malloc is broken.
WiredHome 145:5eb2492acdda 2491 pixelBuffer = (color_t *)swMalloc(2 * w * sizeof(color_t));
WiredHome 86:e86b355940f4 2492 pixelBuffer2 = pixelBuffer + (w * sizeof(color_t));
WiredHome 86:e86b355940f4 2493 #else
WiredHome 145:5eb2492acdda 2494 pixelBuffer = (color_t *)swMalloc(w * sizeof(color_t));
WiredHome 145:5eb2492acdda 2495 pixelBuffer2 = (color_t *)swMalloc(w * sizeof(color_t));
WiredHome 86:e86b355940f4 2496 #endif
WiredHome 86:e86b355940f4 2497 if (pixelBuffer == NULL || pixelBuffer2 == NULL) {
WiredHome 86:e86b355940f4 2498 ERR("Not enough RAM for pixelBuffer");
WiredHome 86:e86b355940f4 2499 #ifndef DOUBLEBUF
WiredHome 86:e86b355940f4 2500 if (pixelBuffer2)
WiredHome 145:5eb2492acdda 2501 swFree(pixelBuffer2);
WiredHome 86:e86b355940f4 2502 #endif
WiredHome 86:e86b355940f4 2503 if (pixelBuffer)
WiredHome 145:5eb2492acdda 2504 swFree(pixelBuffer);
WiredHome 145:5eb2492acdda 2505 swFree(lineBuffer);
WiredHome 86:e86b355940f4 2506 return(not_enough_ram);
WiredHome 86:e86b355940f4 2507 }
WiredHome 86:e86b355940f4 2508
WiredHome 72:ecffe56af969 2509 FILE *Image = fopen(Name_BMP, "wb");
WiredHome 72:ecffe56af969 2510 if (!Image) {
WiredHome 86:e86b355940f4 2511 ERR("Can't open file for write");
WiredHome 86:e86b355940f4 2512 #ifndef DOUBLEBUF
WiredHome 86:e86b355940f4 2513 if (pixelBuffer2)
WiredHome 145:5eb2492acdda 2514 swFree(pixelBuffer2);
WiredHome 86:e86b355940f4 2515 #endif
WiredHome 86:e86b355940f4 2516 if (pixelBuffer)
WiredHome 145:5eb2492acdda 2517 swFree(pixelBuffer);
WiredHome 145:5eb2492acdda 2518 swFree(lineBuffer);
WiredHome 72:ecffe56af969 2519 return(file_not_found);
WiredHome 72:ecffe56af969 2520 }
WiredHome 72:ecffe56af969 2521
WiredHome 72:ecffe56af969 2522 // Be optimistic - don't check for errors.
WiredHome 93:6fbc516de05e 2523 HexDump("BMP_Header", (uint8_t *)&BMP_Header, sizeof(BMP_Header));
WiredHome 95:ef538bd687c0 2524 fwrite(&BMP_Header, sizeof(char), sizeof(BMP_Header), Image);
WiredHome 73:f22a18707b5e 2525
WiredHome 93:6fbc516de05e 2526 HexDump("BMP_Info", (uint8_t *)&BMP_Info, sizeof(BMP_Info));
WiredHome 95:ef538bd687c0 2527 fwrite(&BMP_Info, sizeof(char), sizeof(BMP_Info), Image);
WiredHome 163:17526689a3ed 2528
WiredHome 163:17526689a3ed 2529 if (bitsPerPixel != 24) {
WiredHome 163:17526689a3ed 2530 HexDump("Palette", (uint8_t *)&WebColorPalette, sizeof(WebColorPalette));
WiredHome 163:17526689a3ed 2531 fwrite(&WebColorPalette, sizeof(char), sizeof(WebColorPalette), Image);
WiredHome 163:17526689a3ed 2532 if (0 && sizeof(WebColorPalette) % 4) {
WiredHome 163:17526689a3ed 2533 const uint8_t padd[] = { 0, 0, 0 };
WiredHome 163:17526689a3ed 2534 fwrite(&padd, sizeof(char),
WiredHome 163:17526689a3ed 2535 (sizeof(BMP_Header) + sizeof(BMP_Info) + sizeof(WebColorPalette)) % 4, Image);
WiredHome 163:17526689a3ed 2536 }
WiredHome 163:17526689a3ed 2537 }
WiredHome 95:ef538bd687c0 2538 //color_t transparency = GetBackgroundTransparencyColor();
WiredHome 95:ef538bd687c0 2539 LayerMode_T ltpr0 = GetLayerMode();
WiredHome 73:f22a18707b5e 2540
WiredHome 73:f22a18707b5e 2541 uint16_t prevLayer = GetDrawingLayer();
WiredHome 73:f22a18707b5e 2542 // If only one of the layers is visible, select that layer
WiredHome 73:f22a18707b5e 2543 switch(ltpr0) {
WiredHome 95:ef538bd687c0 2544 case ShowLayer0:
WiredHome 73:f22a18707b5e 2545 SelectDrawingLayer(0);
WiredHome 73:f22a18707b5e 2546 break;
WiredHome 95:ef538bd687c0 2547 case ShowLayer1:
WiredHome 73:f22a18707b5e 2548 SelectDrawingLayer(1);
WiredHome 73:f22a18707b5e 2549 break;
WiredHome 73:f22a18707b5e 2550 default:
WiredHome 73:f22a18707b5e 2551 break;
WiredHome 73:f22a18707b5e 2552 }
WiredHome 73:f22a18707b5e 2553
WiredHome 72:ecffe56af969 2554 // Read the display from the last line toward the top
WiredHome 72:ecffe56af969 2555 // so we can write the file in one pass.
WiredHome 72:ecffe56af969 2556 for (int j = h - 1; j >= 0; j--) {
WiredHome 149:c62c4b2d6a15 2557 if (idle_callback) {
WiredHome 149:c62c4b2d6a15 2558 (*idle_callback)(progress, (h - 1 - j) * 100 / (h - 1));
WiredHome 149:c62c4b2d6a15 2559 }
WiredHome 149:c62c4b2d6a15 2560
WiredHome 73:f22a18707b5e 2561 if (ltpr0 >= 2) // Need to combine the layers...
WiredHome 73:f22a18707b5e 2562 SelectDrawingLayer(0); // so read layer 0 first
WiredHome 72:ecffe56af969 2563 // Read one line of pixels to a local buffer
WiredHome 72:ecffe56af969 2564 if (getPixelStream(pixelBuffer, w, x,y+j) != noerror) {
WiredHome 72:ecffe56af969 2565 ERR("getPixelStream error, and no recovery handler...");
WiredHome 72:ecffe56af969 2566 }
WiredHome 73:f22a18707b5e 2567 if (ltpr0 >= 2) { // Need to combine the layers...
WiredHome 86:e86b355940f4 2568 SelectDrawingLayer(1); // so read layer 1 next
WiredHome 73:f22a18707b5e 2569 if (getPixelStream(pixelBuffer2, w, x,y+j) != noerror) {
WiredHome 73:f22a18707b5e 2570 ERR("getPixelStream error, and no recovery handler...");
WiredHome 73:f22a18707b5e 2571 }