Utility library for HSP SPo2 HR demo including user interface, board support adn accelerometer.

Committer:
gmehmet
Date:
Mon Dec 17 13:58:56 2018 +0300
Revision:
0:a12d6976d64c
create and put source to HSP demo utility repo

Who changed what in which revision?

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