ME11B Sample Code in Maxim Integrated Team

Dependencies:   BMI160 max32630hsp3 MemoryLCD USBDevice

Fork of Host_Software_MAX32664GWEB_HR_EXTENDED by Seyhmus Cacina

Committer:
seyhmus.cacina
Date:
Mon Mar 18 10:21:53 2019 +0300
Revision:
0:ac4dea3e2894
ME11B Sample Code First Commit

Who changed what in which revision?

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