Library for Adafruit SHARP Memory Displays. This is a port of the original Arduino library available at: https://github.com/adafruit/Adafruit_SHARP_Memory_Display .
Dependents: Adafruit_SHARP_Memory_Display_demo Analog_Clock
Library for Adafruit Sharp Memory Displays. A demo is available here: https://developer.mbed.org/users/marcpl/code/Adafruit_SHARP_Memory_Display_demo/
Adafruit_SharpMem.cpp@0:070c76a8d782, 2015-06-23 (annotated)
- Committer:
- marcpl
- Date:
- Tue Jun 23 16:49:02 2015 +0000
- Revision:
- 0:070c76a8d782
- Child:
- 3:99cc0e72e165
Port the "Adafruit_SHARP_Memory_Display" library that was originally written for Arduino. For more information, see https://github.com/adafruit/Adafruit_SHARP_Memory_Display.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcpl | 0:070c76a8d782 | 1 | /********************************************************************* |
marcpl | 0:070c76a8d782 | 2 | This is an Arduino library for our Monochrome SHARP Memory Displays |
marcpl | 0:070c76a8d782 | 3 | |
marcpl | 0:070c76a8d782 | 4 | Pick one up today in the adafruit shop! |
marcpl | 0:070c76a8d782 | 5 | ------> http://www.adafruit.com/products/1393 |
marcpl | 0:070c76a8d782 | 6 | |
marcpl | 0:070c76a8d782 | 7 | These displays use SPI to communicate, 3 pins are required to |
marcpl | 0:070c76a8d782 | 8 | interface |
marcpl | 0:070c76a8d782 | 9 | |
marcpl | 0:070c76a8d782 | 10 | Adafruit invests time and resources providing this open source code, |
marcpl | 0:070c76a8d782 | 11 | please support Adafruit and open-source hardware by purchasing |
marcpl | 0:070c76a8d782 | 12 | products from Adafruit! |
marcpl | 0:070c76a8d782 | 13 | |
marcpl | 0:070c76a8d782 | 14 | Written by Limor Fried/Ladyada for Adafruit Industries. |
marcpl | 0:070c76a8d782 | 15 | BSD license, check license.txt for more information |
marcpl | 0:070c76a8d782 | 16 | All text above, and the splash screen must be included in any redistribution |
marcpl | 0:070c76a8d782 | 17 | *********************************************************************/ |
marcpl | 0:070c76a8d782 | 18 | |
marcpl | 0:070c76a8d782 | 19 | /* |
marcpl | 0:070c76a8d782 | 20 | * Modified by Marc PLOUHINEC 23/06/2015 for use in mbed |
marcpl | 0:070c76a8d782 | 21 | */ |
marcpl | 0:070c76a8d782 | 22 | |
marcpl | 0:070c76a8d782 | 23 | #include "Adafruit_SharpMem.h" |
marcpl | 0:070c76a8d782 | 24 | |
marcpl | 0:070c76a8d782 | 25 | /************************************************************************** |
marcpl | 0:070c76a8d782 | 26 | Sharp Memory Display Connector |
marcpl | 0:070c76a8d782 | 27 | ----------------------------------------------------------------------- |
marcpl | 0:070c76a8d782 | 28 | Pin Function Notes |
marcpl | 0:070c76a8d782 | 29 | === ============== =============================== |
marcpl | 0:070c76a8d782 | 30 | 1 VIN 3.3-5.0V (into LDO supply) |
marcpl | 0:070c76a8d782 | 31 | 2 3V3 3.3V out |
marcpl | 0:070c76a8d782 | 32 | 3 GND |
marcpl | 0:070c76a8d782 | 33 | 4 SCLK Serial Clock |
marcpl | 0:070c76a8d782 | 34 | 5 MOSI Serial Data Input |
marcpl | 0:070c76a8d782 | 35 | 6 CS Serial Chip Select |
marcpl | 0:070c76a8d782 | 36 | 9 EXTMODE COM Inversion Select (Low = SW clock/serial) |
marcpl | 0:070c76a8d782 | 37 | 7 EXTCOMIN External COM Inversion Signal |
marcpl | 0:070c76a8d782 | 38 | 8 DISP Display On(High)/Off(Low) |
marcpl | 0:070c76a8d782 | 39 | |
marcpl | 0:070c76a8d782 | 40 | **************************************************************************/ |
marcpl | 0:070c76a8d782 | 41 | |
marcpl | 0:070c76a8d782 | 42 | #define SHARPMEM_BIT_WRITECMD (0x80) |
marcpl | 0:070c76a8d782 | 43 | #define SHARPMEM_BIT_VCOM (0x40) |
marcpl | 0:070c76a8d782 | 44 | #define SHARPMEM_BIT_CLEAR (0x20) |
marcpl | 0:070c76a8d782 | 45 | #define TOGGLE_VCOM do { _sharpmem_vcom = _sharpmem_vcom ? 0x00 : SHARPMEM_BIT_VCOM; } while(0); |
marcpl | 0:070c76a8d782 | 46 | |
marcpl | 0:070c76a8d782 | 47 | uint8_t sharpmem_buffer[(SHARPMEM_LCDWIDTH * SHARPMEM_LCDHEIGHT) / 8]; |
marcpl | 0:070c76a8d782 | 48 | |
marcpl | 0:070c76a8d782 | 49 | /* ************* */ |
marcpl | 0:070c76a8d782 | 50 | /* CONSTRUCTORS */ |
marcpl | 0:070c76a8d782 | 51 | /* ************* */ |
marcpl | 0:070c76a8d782 | 52 | Adafruit_SharpMem::Adafruit_SharpMem(PinName enable, PinName cs, PinName mosi, PinName miso_unused, PinName sclk, PinName _unused) : |
marcpl | 0:070c76a8d782 | 53 | Adafruit_GFX(SHARPMEM_LCDWIDTH, SHARPMEM_LCDHEIGHT), spi(mosi, miso_unused, sclk, _unused), displayEnable(enable, 0), chipSelect(cs, 0), _sharpmem_vcom(SHARPMEM_BIT_VCOM) { |
marcpl | 0:070c76a8d782 | 54 | |
marcpl | 0:070c76a8d782 | 55 | spi.frequency(1000000); |
marcpl | 0:070c76a8d782 | 56 | spi.format(8, 0); |
marcpl | 0:070c76a8d782 | 57 | } |
marcpl | 0:070c76a8d782 | 58 | |
marcpl | 0:070c76a8d782 | 59 | void Adafruit_SharpMem::begin() { |
marcpl | 0:070c76a8d782 | 60 | setRotation(2); |
marcpl | 0:070c76a8d782 | 61 | } |
marcpl | 0:070c76a8d782 | 62 | |
marcpl | 0:070c76a8d782 | 63 | /* ************** */ |
marcpl | 0:070c76a8d782 | 64 | /* PUBLIC METHODS */ |
marcpl | 0:070c76a8d782 | 65 | /* ************** */ |
marcpl | 0:070c76a8d782 | 66 | |
marcpl | 0:070c76a8d782 | 67 | // 1<<n is a costly operation on AVR -- table usu. smaller & faster |
marcpl | 0:070c76a8d782 | 68 | static const uint8_t |
marcpl | 0:070c76a8d782 | 69 | set[] = { 1, 2, 4, 8, 16, 32, 64, 128 }, |
marcpl | 0:070c76a8d782 | 70 | clr[] = { ~1, ~2, ~4, ~8, ~16, ~32, ~64, ~128 }; |
marcpl | 0:070c76a8d782 | 71 | |
marcpl | 0:070c76a8d782 | 72 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 73 | /*! |
marcpl | 0:070c76a8d782 | 74 | @brief Draws a single pixel in image buffer |
marcpl | 0:070c76a8d782 | 75 | |
marcpl | 0:070c76a8d782 | 76 | @param[in] x |
marcpl | 0:070c76a8d782 | 77 | The x position (0 based) |
marcpl | 0:070c76a8d782 | 78 | @param[in] y |
marcpl | 0:070c76a8d782 | 79 | The y position (0 based) |
marcpl | 0:070c76a8d782 | 80 | */ |
marcpl | 0:070c76a8d782 | 81 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 82 | void Adafruit_SharpMem::drawPixel(int16_t x, int16_t y, uint16_t color) |
marcpl | 0:070c76a8d782 | 83 | { |
marcpl | 0:070c76a8d782 | 84 | if((x < 0) || (x >= _width) || (y < 0) || (y >= _height)) return; |
marcpl | 0:070c76a8d782 | 85 | |
marcpl | 0:070c76a8d782 | 86 | switch(rotation) { |
marcpl | 0:070c76a8d782 | 87 | case 1: |
marcpl | 0:070c76a8d782 | 88 | swap(x, y); |
marcpl | 0:070c76a8d782 | 89 | x = _rawWidth - 1 - x; |
marcpl | 0:070c76a8d782 | 90 | break; |
marcpl | 0:070c76a8d782 | 91 | case 2: |
marcpl | 0:070c76a8d782 | 92 | x = _rawWidth - 1 - x; |
marcpl | 0:070c76a8d782 | 93 | y = _rawHeight - 1 - y; |
marcpl | 0:070c76a8d782 | 94 | break; |
marcpl | 0:070c76a8d782 | 95 | case 3: |
marcpl | 0:070c76a8d782 | 96 | swap(x, y); |
marcpl | 0:070c76a8d782 | 97 | y = _rawHeight - 1 - y; |
marcpl | 0:070c76a8d782 | 98 | break; |
marcpl | 0:070c76a8d782 | 99 | } |
marcpl | 0:070c76a8d782 | 100 | |
marcpl | 0:070c76a8d782 | 101 | if(color) { |
marcpl | 0:070c76a8d782 | 102 | sharpmem_buffer[(y*SHARPMEM_LCDWIDTH + x) / 8] |= set[x & 7]; |
marcpl | 0:070c76a8d782 | 103 | } else { |
marcpl | 0:070c76a8d782 | 104 | sharpmem_buffer[(y*SHARPMEM_LCDWIDTH + x) / 8] &= clr[x & 7]; |
marcpl | 0:070c76a8d782 | 105 | } |
marcpl | 0:070c76a8d782 | 106 | } |
marcpl | 0:070c76a8d782 | 107 | |
marcpl | 0:070c76a8d782 | 108 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 109 | /*! |
marcpl | 0:070c76a8d782 | 110 | @brief Gets the value (1 or 0) of the specified pixel from the buffer |
marcpl | 0:070c76a8d782 | 111 | |
marcpl | 0:070c76a8d782 | 112 | @param[in] x |
marcpl | 0:070c76a8d782 | 113 | The x position (0 based) |
marcpl | 0:070c76a8d782 | 114 | @param[in] y |
marcpl | 0:070c76a8d782 | 115 | The y position (0 based) |
marcpl | 0:070c76a8d782 | 116 | |
marcpl | 0:070c76a8d782 | 117 | @return 1 if the pixel is enabled, 0 if disabled |
marcpl | 0:070c76a8d782 | 118 | */ |
marcpl | 0:070c76a8d782 | 119 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 120 | uint8_t Adafruit_SharpMem::getPixel(uint16_t x, uint16_t y) |
marcpl | 0:070c76a8d782 | 121 | { |
marcpl | 0:070c76a8d782 | 122 | if((x >= _width) || (y >= _height)) return 0; // <0 test not needed, unsigned |
marcpl | 0:070c76a8d782 | 123 | |
marcpl | 0:070c76a8d782 | 124 | switch(rotation) { |
marcpl | 0:070c76a8d782 | 125 | case 1: |
marcpl | 0:070c76a8d782 | 126 | swap(x, y); |
marcpl | 0:070c76a8d782 | 127 | x = _rawWidth - 1 - x; |
marcpl | 0:070c76a8d782 | 128 | break; |
marcpl | 0:070c76a8d782 | 129 | case 2: |
marcpl | 0:070c76a8d782 | 130 | x = _rawWidth - 1 - x; |
marcpl | 0:070c76a8d782 | 131 | y = _rawHeight - 1 - y; |
marcpl | 0:070c76a8d782 | 132 | break; |
marcpl | 0:070c76a8d782 | 133 | case 3: |
marcpl | 0:070c76a8d782 | 134 | swap(x, y); |
marcpl | 0:070c76a8d782 | 135 | y = _rawHeight - 1 - y; |
marcpl | 0:070c76a8d782 | 136 | break; |
marcpl | 0:070c76a8d782 | 137 | } |
marcpl | 0:070c76a8d782 | 138 | |
marcpl | 0:070c76a8d782 | 139 | return sharpmem_buffer[(y*SHARPMEM_LCDWIDTH + x) / 8] & set[x & 7] ? 1 : 0; |
marcpl | 0:070c76a8d782 | 140 | } |
marcpl | 0:070c76a8d782 | 141 | |
marcpl | 0:070c76a8d782 | 142 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 143 | /*! |
marcpl | 0:070c76a8d782 | 144 | @brief Clears the screen |
marcpl | 0:070c76a8d782 | 145 | */ |
marcpl | 0:070c76a8d782 | 146 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 147 | void Adafruit_SharpMem::clearDisplay() |
marcpl | 0:070c76a8d782 | 148 | { |
marcpl | 0:070c76a8d782 | 149 | memset(sharpmem_buffer, 0xff, (SHARPMEM_LCDWIDTH * SHARPMEM_LCDHEIGHT) / 8); |
marcpl | 0:070c76a8d782 | 150 | // Send the clear screen command rather than doing a HW refresh (quicker) |
marcpl | 0:070c76a8d782 | 151 | chipSelect = 1; |
marcpl | 0:070c76a8d782 | 152 | spi.write(_sharpmem_vcom | SHARPMEM_BIT_CLEAR); |
marcpl | 0:070c76a8d782 | 153 | spi.write(0x00); |
marcpl | 0:070c76a8d782 | 154 | TOGGLE_VCOM; |
marcpl | 0:070c76a8d782 | 155 | chipSelect = 0; |
marcpl | 0:070c76a8d782 | 156 | } |
marcpl | 0:070c76a8d782 | 157 | |
marcpl | 0:070c76a8d782 | 158 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 159 | /*! |
marcpl | 0:070c76a8d782 | 160 | @brief Renders the contents of the pixel buffer on the LCD |
marcpl | 0:070c76a8d782 | 161 | */ |
marcpl | 0:070c76a8d782 | 162 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 163 | void Adafruit_SharpMem::refresh(void) |
marcpl | 0:070c76a8d782 | 164 | { |
marcpl | 0:070c76a8d782 | 165 | uint16_t i, totalbytes, currentline, oldline; |
marcpl | 0:070c76a8d782 | 166 | totalbytes = (SHARPMEM_LCDWIDTH * SHARPMEM_LCDHEIGHT) / 8; |
marcpl | 0:070c76a8d782 | 167 | |
marcpl | 0:070c76a8d782 | 168 | // Send the write command |
marcpl | 0:070c76a8d782 | 169 | chipSelect = 1; |
marcpl | 0:070c76a8d782 | 170 | spi.write(SHARPMEM_BIT_WRITECMD | _sharpmem_vcom); |
marcpl | 0:070c76a8d782 | 171 | TOGGLE_VCOM; |
marcpl | 0:070c76a8d782 | 172 | |
marcpl | 0:070c76a8d782 | 173 | // Send the address for line 1 |
marcpl | 0:070c76a8d782 | 174 | oldline = currentline = 1; |
marcpl | 0:070c76a8d782 | 175 | spi.write(bitReverse8(currentline)); |
marcpl | 0:070c76a8d782 | 176 | |
marcpl | 0:070c76a8d782 | 177 | // Send image buffer |
marcpl | 0:070c76a8d782 | 178 | for (i=0; i<totalbytes; i++) |
marcpl | 0:070c76a8d782 | 179 | { |
marcpl | 0:070c76a8d782 | 180 | spi.write(bitReverse8(sharpmem_buffer[i])); |
marcpl | 0:070c76a8d782 | 181 | currentline = ((i+1)/(SHARPMEM_LCDWIDTH/8)) + 1; |
marcpl | 0:070c76a8d782 | 182 | if(currentline != oldline) |
marcpl | 0:070c76a8d782 | 183 | { |
marcpl | 0:070c76a8d782 | 184 | // Send end of line and address bytes |
marcpl | 0:070c76a8d782 | 185 | spi.write(0x00); |
marcpl | 0:070c76a8d782 | 186 | if (currentline <= SHARPMEM_LCDHEIGHT) |
marcpl | 0:070c76a8d782 | 187 | { |
marcpl | 0:070c76a8d782 | 188 | spi.write(bitReverse8(currentline)); |
marcpl | 0:070c76a8d782 | 189 | } |
marcpl | 0:070c76a8d782 | 190 | oldline = currentline; |
marcpl | 0:070c76a8d782 | 191 | } |
marcpl | 0:070c76a8d782 | 192 | } |
marcpl | 0:070c76a8d782 | 193 | |
marcpl | 0:070c76a8d782 | 194 | // Send another trailing 8 bits for the last line |
marcpl | 0:070c76a8d782 | 195 | spi.write(0x00); |
marcpl | 0:070c76a8d782 | 196 | chipSelect = 0; |
marcpl | 0:070c76a8d782 | 197 | } |
marcpl | 0:070c76a8d782 | 198 | |
marcpl | 0:070c76a8d782 | 199 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 200 | /*! |
marcpl | 0:070c76a8d782 | 201 | @brief Turn on the screen |
marcpl | 0:070c76a8d782 | 202 | */ |
marcpl | 0:070c76a8d782 | 203 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 204 | void Adafruit_SharpMem::enableDisplay() { |
marcpl | 0:070c76a8d782 | 205 | displayEnable = 1; |
marcpl | 0:070c76a8d782 | 206 | } |
marcpl | 0:070c76a8d782 | 207 | |
marcpl | 0:070c76a8d782 | 208 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 209 | /*! |
marcpl | 0:070c76a8d782 | 210 | @brief Turn off the screen |
marcpl | 0:070c76a8d782 | 211 | */ |
marcpl | 0:070c76a8d782 | 212 | /**************************************************************************/ |
marcpl | 0:070c76a8d782 | 213 | void Adafruit_SharpMem::disableDisplay() { |
marcpl | 0:070c76a8d782 | 214 | displayEnable = 0; |
marcpl | 0:070c76a8d782 | 215 | } |