Fork of Silabs MemoryLCD library

Dependents:   demoUI whrmDemoUI Host_Software_MAX32664GWEB_HR_EXTENDED Host_Software_MAX32664GWEC_SpO2_HR-_EXTE ... more

C++ library for Sharp Microelectronics 1.28 inch LCD TFT, LS013B7DH03, SPI bus. Forked from Silicon Labs MemoryLCD display driver.

Committer:
stevew817
Date:
Thu Jul 30 08:54:55 2015 +0000
Revision:
8:39206d1e11f7
Parent:
7:6cf0aa7bc0fc
Child:
9:2441ef131ab8
typo.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Steven Cooreman 0:a0faa86660d4 1 /***************************************************************************//**
Steven Cooreman 0:a0faa86660d4 2 * @file BufferedDisplay.cpp
Steven Cooreman 0:a0faa86660d4 3 * @brief Buffered version of GraphicsDisplay
Steven Cooreman 0:a0faa86660d4 4 *******************************************************************************
Steven Cooreman 0:a0faa86660d4 5 * @section License
Steven Cooreman 0:a0faa86660d4 6 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
Steven Cooreman 0:a0faa86660d4 7 *******************************************************************************
Steven Cooreman 0:a0faa86660d4 8 *
Steven Cooreman 0:a0faa86660d4 9 * Permission is granted to anyone to use this software for any purpose,
Steven Cooreman 0:a0faa86660d4 10 * including commercial applications, and to alter it and redistribute it
Steven Cooreman 0:a0faa86660d4 11 * freely, subject to the following restrictions:
Steven Cooreman 0:a0faa86660d4 12 *
Steven Cooreman 0:a0faa86660d4 13 * 1. The origin of this software must not be misrepresented; you must not
Steven Cooreman 0:a0faa86660d4 14 * claim that you wrote the original software.
Steven Cooreman 0:a0faa86660d4 15 * 2. Altered source versions must be plainly marked as such, and must not be
Steven Cooreman 0:a0faa86660d4 16 * misrepresented as being the original software.
Steven Cooreman 0:a0faa86660d4 17 * 3. This notice may not be removed or altered from any source distribution.
Steven Cooreman 0:a0faa86660d4 18 *
Steven Cooreman 0:a0faa86660d4 19 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
Steven Cooreman 0:a0faa86660d4 20 * obligation to support this Software. Silicon Labs is providing the
Steven Cooreman 0:a0faa86660d4 21 * Software "AS IS", with no express or implied warranties of any kind,
Steven Cooreman 0:a0faa86660d4 22 * including, but not limited to, any implied warranties of merchantability
Steven Cooreman 0:a0faa86660d4 23 * or fitness for any particular purpose or warranties against infringement
Steven Cooreman 0:a0faa86660d4 24 * of any proprietary rights of a third party.
Steven Cooreman 0:a0faa86660d4 25 *
Steven Cooreman 0:a0faa86660d4 26 * Silicon Labs will not be liable for any consequential, incidental, or
Steven Cooreman 0:a0faa86660d4 27 * special damages, or any other relief, or for any claim by any third party,
Steven Cooreman 0:a0faa86660d4 28 * arising from your use of this Software.
Steven Cooreman 0:a0faa86660d4 29 *
Steven Cooreman 0:a0faa86660d4 30 ******************************************************************************/
Steven Cooreman 0:a0faa86660d4 31
Steven Cooreman 0:a0faa86660d4 32 #include "BufferedDisplay.h"
Steven Cooreman 0:a0faa86660d4 33
Steven Cooreman 0:a0faa86660d4 34 #define SWAP8(a) ((((a) & 0x80) >> 7) | (((a) & 0x40) >> 5) | (((a) & 0x20) >> 3) | (((a) & 0x10) >> 1) | (((a) & 0x08) << 1) | (((a) & 0x04) << 3) | (((a) & 0x02) << 5) | (((a) & 0x01) << 7))
Steven Cooreman 0:a0faa86660d4 35
Steven Cooreman 0:a0faa86660d4 36 namespace silabs {
Steven Cooreman 0:a0faa86660d4 37
Steven Cooreman 0:a0faa86660d4 38 BufferedDisplay::BufferedDisplay(const char *name) : GraphicsDisplay(name) {
Steven Cooreman 0:a0faa86660d4 39 memset((uint8_t*)_pixelBuffer, White, sizeof(_pixelBuffer)); // init full frame buffer
Steven Cooreman 0:a0faa86660d4 40 memset((uint8_t*)_dirtyRows, 0xFF, sizeof(_dirtyRows)); // init dirty status
Steven Cooreman 0:a0faa86660d4 41 }
Steven Cooreman 0:a0faa86660d4 42
Steven Cooreman 0:a0faa86660d4 43 /**
Steven Cooreman 0:a0faa86660d4 44 * Override of GraphicsDisplay's pixel()
Steven Cooreman 0:a0faa86660d4 45 */
Steven Cooreman 0:a0faa86660d4 46
Steven Cooreman 0:a0faa86660d4 47 void BufferedDisplay::pixel(int x, int y, int colour) {
stevew817 7:6cf0aa7bc0fc 48 /* Apply constraint to x and y */
stevew817 7:6cf0aa7bc0fc 49 if(x < 0 || y < 0) return;
stevew817 7:6cf0aa7bc0fc 50
stevew817 7:6cf0aa7bc0fc 51 /*****************************************************************************************************************
stevew817 7:6cf0aa7bc0fc 52 * The display expects LSB input, while the SPI is configured for 8bit MSB transfers. Therefore, we should
stevew817 7:6cf0aa7bc0fc 53 * construct the framebuffer accordingly, so that an MSB transmission will put pixel 0 first on the wire.
stevew817 7:6cf0aa7bc0fc 54 *
stevew817 7:6cf0aa7bc0fc 55 * So the actual pixel layout in framebuffer (for 128x128) is as follows:
stevew817 7:6cf0aa7bc0fc 56 * { //Framebuffer
stevew817 7:6cf0aa7bc0fc 57 * { //Line 0
stevew817 7:6cf0aa7bc0fc 58 * {p0, p1, p2, p3, p4, p5, p6, p7}, //Line 0 byte 0 (byte 0)
stevew817 7:6cf0aa7bc0fc 59 * {p8, p9,p10,p11,p12,p13,p14,p15}, //Line 0 byte 1 (byte 1)
stevew817 7:6cf0aa7bc0fc 60 * ...
stevew817 7:6cf0aa7bc0fc 61 * {p120,p121,p122,p123,p124,p125,p126,p127} //Line 0 byte 15 (byte 15)
stevew817 7:6cf0aa7bc0fc 62 * },
stevew817 7:6cf0aa7bc0fc 63 * { //Line 1
stevew817 7:6cf0aa7bc0fc 64 * {p128,p129,p130,p131,p132,p133,p134,p135}, //Line 1 byte 0 (byte 16)
stevew817 7:6cf0aa7bc0fc 65 * ...
stevew817 7:6cf0aa7bc0fc 66 * },
stevew817 7:6cf0aa7bc0fc 67 * ...
stevew817 7:6cf0aa7bc0fc 68 * { //Line 127
stevew817 7:6cf0aa7bc0fc 69 * {...}, //Line 127 byte 0 (byte 2032)
stevew817 7:6cf0aa7bc0fc 70 * ...
stevew817 7:6cf0aa7bc0fc 71 * {...} //Line 127 byte 15 (byte 2047) = 128*128 bits
stevew817 7:6cf0aa7bc0fc 72 * }
stevew817 7:6cf0aa7bc0fc 73 * }
stevew817 7:6cf0aa7bc0fc 74 *
stevew817 7:6cf0aa7bc0fc 75 * This means that to calculate the actual bit position in the framebuffer byte, we need to swap the bit
stevew817 7:6cf0aa7bc0fc 76 * order of the lower three bits. So pixel 7 becomes bit offset 0, 6 -> 1, 5 -> 2, 4->3, 3->4, 2->5, 1->6 and 0->7.
stevew817 7:6cf0aa7bc0fc 77 *****************************************************************************************************************/
Steven Cooreman 0:a0faa86660d4 78 uint8_t swapx = 7 - ((unsigned int)x & 0x07);
Steven Cooreman 0:a0faa86660d4 79 x = ((unsigned int)x & 0xFFFFFFF8) | swapx;
Steven Cooreman 0:a0faa86660d4 80
stevew817 7:6cf0aa7bc0fc 81 /* Since we are dealing with 1-bit pixels, we can avoid having to do bitshift and comparison operations twice.
stevew817 7:6cf0aa7bc0fc 82 * Basically, do the comparison with the requested state and current state, and if it changed, do an XOR on the framebuffer pixel and set the line to dirty.
stevew817 7:6cf0aa7bc0fc 83 */
Steven Cooreman 0:a0faa86660d4 84 bool change = ((_pixelBuffer[((y * DISPLAY_WIDTH) + x) / DISPLAY_BUFFER_TYPE_SIZE] & (1 << (x % DISPLAY_BUFFER_TYPE_SIZE))) != ((colour & 0x01) << (x % DISPLAY_BUFFER_TYPE_SIZE)));
Steven Cooreman 0:a0faa86660d4 85 if(change) {
stevew817 7:6cf0aa7bc0fc 86 /* Do XOR (no bitshifting required) */
Steven Cooreman 0:a0faa86660d4 87 _pixelBuffer[((y * DISPLAY_WIDTH) + x) / DISPLAY_BUFFER_TYPE_SIZE] ^= (1 << (x % DISPLAY_BUFFER_TYPE_SIZE));
Steven Cooreman 0:a0faa86660d4 88
stevew817 7:6cf0aa7bc0fc 89 /* notify dirty status of this line */
Steven Cooreman 0:a0faa86660d4 90 _dirtyRows[y / DISPLAY_BUFFER_TYPE_SIZE] |= (1 << (y % DISPLAY_BUFFER_TYPE_SIZE));
Steven Cooreman 0:a0faa86660d4 91 }
Steven Cooreman 0:a0faa86660d4 92 }
Steven Cooreman 0:a0faa86660d4 93
Steven Cooreman 0:a0faa86660d4 94 int BufferedDisplay::width() {
Steven Cooreman 0:a0faa86660d4 95 return DISPLAY_WIDTH;
Steven Cooreman 0:a0faa86660d4 96 }
Steven Cooreman 0:a0faa86660d4 97 int BufferedDisplay::height() {
Steven Cooreman 0:a0faa86660d4 98 return DISPLAY_HEIGHT;
Steven Cooreman 0:a0faa86660d4 99 }
Steven Cooreman 0:a0faa86660d4 100
Steven Cooreman 0:a0faa86660d4 101 /**
Steven Cooreman 0:a0faa86660d4 102 * Function to move bitmap into frame buffer
Steven Cooreman 0:a0faa86660d4 103 * arguments:
Steven Cooreman 0:a0faa86660d4 104 * * bitmap: pointer to uint8 array containing horizontal pixel data
stevew817 6:fe04073fe90c 105 * * bmpWidth: width of the bitmap in pixels (must be multiple of 8)
stevew817 6:fe04073fe90c 106 * * bmpHeight: height of the bitmap in pixels
stevew817 6:fe04073fe90c 107 * * startX: starting position to apply bitmap in horizontal direction (0 = leftmost) (must be multiple of 8)
stevew817 6:fe04073fe90c 108 * * startY: starting position to apply bitmap in vertical direction (0 = topmost)
Steven Cooreman 0:a0faa86660d4 109 */
Steven Cooreman 0:a0faa86660d4 110 void BufferedDisplay::showBMP(const uint8_t* bitmap, const uint32_t bmpWidth, const uint32_t bmpHeight, const uint32_t startX, const uint32_t startY) {
stevew817 8:39206d1e11f7 111 uint32_t bmpLine = 0, y = startY, bytesPerLine = ((bmpWidth >= (DISPLAY_WIDTH - startX)) ? (DISPLAY_WIDTH - startX) : bmpWidth) / 8;
Steven Cooreman 0:a0faa86660d4 112
Steven Cooreman 0:a0faa86660d4 113 /* Apply constraints */
Steven Cooreman 0:a0faa86660d4 114 if((bmpWidth & 0x07) != 0) return;
Steven Cooreman 0:a0faa86660d4 115 if((startX & 0x07) != 0) return;
stevew817 6:fe04073fe90c 116 if(startX >= DISPLAY_WIDTH) return;
stevew817 6:fe04073fe90c 117
stevew817 6:fe04073fe90c 118 //Superflouous due to for-loop check
stevew817 6:fe04073fe90c 119 //if((startY >= DISPLAY_HEIGHT) return;
Steven Cooreman 0:a0faa86660d4 120
stevew817 7:6cf0aa7bc0fc 121 /* Copy over bytes to the framebuffer, do not write outside framebuffer boundary */
stevew817 6:fe04073fe90c 122 for(; y < DISPLAY_HEIGHT; y++) {
stevew817 7:6cf0aa7bc0fc 123 /* Check that we are not writing more than the BMP height */
stevew817 8:39206d1e11f7 124 if(bmpLine >= bmpHeight) break;
stevew817 7:6cf0aa7bc0fc 125
stevew817 8:39206d1e11f7 126 /* Copy over one line (bmpLine) from the BMP file to the corresponding line (y) in the pixel buffer */
stevew817 6:fe04073fe90c 127 memcpy( (void*) &(((uint8_t*)_pixelBuffer)[((y * DISPLAY_WIDTH) + startX) / 8]),
stevew817 8:39206d1e11f7 128 (const void*) &(bitmap[bmpLine * (bmpWidth / 8)]),
stevew817 6:fe04073fe90c 129 bytesPerLine);
Steven Cooreman 0:a0faa86660d4 130
stevew817 7:6cf0aa7bc0fc 131 /* Set dirty status for the line we just overwrote */
stevew817 6:fe04073fe90c 132 _dirtyRows[y / DISPLAY_BUFFER_TYPE_SIZE] |= (1 << (y % DISPLAY_BUFFER_TYPE_SIZE));
stevew817 8:39206d1e11f7 133 bmpLine++;
Steven Cooreman 0:a0faa86660d4 134 }
Steven Cooreman 0:a0faa86660d4 135
Steven Cooreman 0:a0faa86660d4 136 return;
Steven Cooreman 0:a0faa86660d4 137 }
Steven Cooreman 0:a0faa86660d4 138 }