Display driver for Sharp's range of SPI-driven memory LCD's, leveraging the power of asynchronous transfers. Currently supports LS013B7DH03, but easily extendable to other models as well.

Dependents:   memLCD-Demo memLCD-Demo memLCD-Demo MemLCD-Temperature-Humidity-Demo ... more

Information

All examples in this repo are considered EXPERIMENTAL QUALITY, meaning this code has been created as one-off proof-of-concept and is suitable as a demonstration for experimental purposes only. This code will not be regularly maintained by Silicon Labs and there is no guarantee that these projects will work across all environments, SDK versions and hardware.

Memory LCD extension board

Caution

This library builds upon the asynchronous SPI interface provided by mbed, but not all platforms currently support this. So, make sure your platform is capable of asynchronous SPI (all Silicon Labs platforms are), otherwise you will get compiler errors!

Usage

The library is purposefully quite simple to use. To set it up, you initialize an SPI object and the required I/O pins, and call the library constructor with those. Make sure the display is powered on, enabled, and the inversion mode is set to external (EXTMODE is high).

setup

#define SCK     PD2
#define MOSI 	PD0

DigitalOut CS(PD3);
DigitalOut EXTCOM(PC4);

SPI displaySPI(MOSI, NC, SCK);
silabs::LS013B7DH03 display(&displaySPI, &CS, &EXTCOM);

You should also swap out the pin names for the relevant names on your platform.

After setup, you usually want to clear any static information left on the screen. To do that, you would call clearImmediate, and optionally provide a callback. The callback will get called when the clearing operation is complete and the display has been cleared. In this example, refreshCallback sets a global boolean 'refreshed' to true when called.

clearing the display

refreshed = false;
int result = display.clearImmediate(refreshCallback);
if(result == LS013B7DH03_OK) {
    while(refreshed == false) sleep();
} else {
    printf("Display error: %d", result);
}

Of course, instead of sleeping while the display is clearing, you could also just continue with the program, and check back whether the callback happened or not.

Then comes the fun part, actually writing stuff on the display! Since the display only supports one-way communication (i.e. you cannot read from it), it uses an internal frame buffer in RAM. So, to actually display something on the LCD, two steps need to happen: writing to the pixel buffer, and at the very end writing the pixel buffer to the LCD.

Pixelbuffer operations

The MemoryLCD library builds upon Simon Ford's TextDisplay library, which means you can use all of that functionality. Even printf is supported!

most important display operations

// Set one pixel at x=10, y=40 to the color White
display.pixel(10,40,White); 

// print "I love mbed" at the character position x=4, y=5
// i.e. starting on row 5, 4 characters from the left border
display.locate(4,5);
display.printf("I love mbed!");

// show a bitmap at a given location
// Constraints: bitmap is 8-bit, MSB first, 1 bit per pixel. Width, height and starting coordinates must be divisible by 8.
// This example: show a bitmap which is 128 pixels wide and high, and start at location 0,0.
display.showBMP(&mbed_logo, 128, 128, 0, 0);

//Draw a line (really a 1px wide rectangle) from (4,10) until (4,15)
display.fill(4, 10, 1, 5, White);

Updating the LCD

Updating the LCD (i.e. moving the framebuffer to the display) happens much the same way as clearing it: asynchronously. To start the update, you call update() on the display, optionally providing a callback to be called after the whole update has happened. In the example below, refreshCallback sets a global boolean 'refreshed' to true when called. Signature: void refreshCallback(void);

updating the display

refreshed = false;
int result = display.update(refreshCallback);
if(result == LS013B7DH03_OK) {
    while(refreshed == false) sleep();
} else {
    printf("Display error: %d", result);
}

Full example

example program

#include "LS013B7DH03.h"
#include "mbed_logo.h"
/******************** Define I/O *****************************/
DigitalOut myled(LED1);

#define SCK     PD2
#define MOSI 	PD0

DigitalOut CS(PD3);
DigitalOut EXTCOM(PC4);
DigitalOut EXTMODE(PD4);
DigitalOut DISP(PD5);

SPI displaySPI(MOSI, NC, SCK);
silabs::LS013B7DH03 display(&displaySPI, &CS, &EXTCOM);

/******************** Define Timers *****************************/

LowPowerTicker timeKeeping;

/***************** Define global variables **********************/
#define INIT_SECONDS		17600

volatile uint32_t prevSeconds = INIT_SECONDS, seconds = INIT_SECONDS;
volatile bool refreshed = false;

/***************** Define callback handlers *********************/
void secondsCallback(void);
void refreshCallback(void);

void secondsCallback(void) {
	seconds++;
}

/**
 * Callback for refresh completion
 */
void refreshCallback(void) {
	refreshed = true;
}

/*************************** MAIN *******************************/
int main() {
    // Enable the LCD
	EXTMODE = 1;
	DISP = 1;

	// Start generating the 1Hz call for keeping time
	timeKeeping.attach(&secondsCallback, 1.0f);

	// Reset the LCD to a blank state. (All white)
	refreshed = false;
	display.clearImmediate(refreshCallback);
	while(refreshed == false) sleep();

	printf("Initialization done! \n");

	// Apply mbed logo bitmap to the pixel buffer
	display.showBMP((uint8_t*)mbed_enabled_logo, 128, 128, 0, 0);
	display.printf("I like MBED!");

	// Push update to the display
	refreshed = false;
	display.update(refreshCallback);

	// Sleep while doing the transmit
	while(refreshed == false) sleep();

	// Go into clock mode
	while(1) {
		sleep();

		// In clock mode, only update once per second
		if(prevSeconds != seconds) {
			display.locate(4,15);
			display.printf("%02d:%02d:%02d", (seconds / 1200) % 24, (seconds / 60) % 60, seconds % 60);
			if(refreshed == true) {
				prevSeconds = seconds;
				refreshed = false;
				display.update(refreshCallback);
			}
		}
	}
}

Datasheet

Datasheet for the LCD (hosted by Mouser)

Committer:
Steven Cooreman
Date:
Wed Mar 18 10:57:16 2015 -0500
Revision:
0:a0faa86660d4
Child:
1:6332f3383fb6
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Steven Cooreman 0:a0faa86660d4 1 /***************************************************************************//**
Steven Cooreman 0:a0faa86660d4 2 * @file LS013B7DH03.h
Steven Cooreman 0:a0faa86660d4 3 * @brief Driver class for the Sharp LS013B7DH03 memory LCD on some kits.
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 #ifndef SILABS_LS013B7DH03_H
Steven Cooreman 0:a0faa86660d4 33 #define SILABS_LS013B7DH03_H
Steven Cooreman 0:a0faa86660d4 34
Steven Cooreman 0:a0faa86660d4 35 #include "platform.h"
Steven Cooreman 0:a0faa86660d4 36 #include <mbed.h>
Steven Cooreman 0:a0faa86660d4 37 #include "CallbackPointer.h"
Steven Cooreman 0:a0faa86660d4 38 #include "LCDSettings.h"
Steven Cooreman 0:a0faa86660d4 39 #include "BufferedDisplay.h"
Steven Cooreman 0:a0faa86660d4 40
Steven Cooreman 0:a0faa86660d4 41 typedef void (*cbptr_t)(void);
Steven Cooreman 0:a0faa86660d4 42
Steven Cooreman 0:a0faa86660d4 43 #define LS013B7DH03_ERROR_BUSY -1
Steven Cooreman 0:a0faa86660d4 44 #define LS013B7DH03_ERROR_SPI_BUSY -2
Steven Cooreman 0:a0faa86660d4 45 #define LS013B7DH03_NO_ACTION -3
Steven Cooreman 0:a0faa86660d4 46 #define LS013B7DH03_ERROR_ARGUMENT -4
Steven Cooreman 0:a0faa86660d4 47 #define LS013B7DH03_OK 0
Steven Cooreman 0:a0faa86660d4 48
Steven Cooreman 0:a0faa86660d4 49 typedef enum {
Steven Cooreman 0:a0faa86660d4 50 IDLE, // No operation currently ongoing
Steven Cooreman 0:a0faa86660d4 51 CLEARING, // In the process of clearing the display
Steven Cooreman 0:a0faa86660d4 52 WRITING, // In the process of sending a display update
Steven Cooreman 0:a0faa86660d4 53 WAIT_CLEAR, // Going to clear after CS pin timeout
Steven Cooreman 0:a0faa86660d4 54 WAIT_WRITE, // Going to write after CS pin timeout
Steven Cooreman 0:a0faa86660d4 55 TRANSFERS_DONE, // Last transfer in progress
Steven Cooreman 0:a0faa86660d4 56 DONE // Done with transmission, waiting for CS pin to become high
Steven Cooreman 0:a0faa86660d4 57 } LS013B7DH03_state_t;
Steven Cooreman 0:a0faa86660d4 58
Steven Cooreman 0:a0faa86660d4 59 namespace silabs {
Steven Cooreman 0:a0faa86660d4 60 class LS013B7DH03 : public BufferedDisplay {
Steven Cooreman 0:a0faa86660d4 61 /** A driver for the Sharp LS013B7DH03 Memory LCD, present on some EFM32 Starter Kits.
Steven Cooreman 0:a0faa86660d4 62 *
Steven Cooreman 0:a0faa86660d4 63 * Currently supports LS013B7DH03 only, but should be easily modifiable.
Steven Cooreman 0:a0faa86660d4 64 *
Steven Cooreman 0:a0faa86660d4 65 * @code
Steven Cooreman 0:a0faa86660d4 66 * #include "mbed.h"
Steven Cooreman 0:a0faa86660d4 67 * #include "LS013B7DH03.h"
Steven Cooreman 0:a0faa86660d4 68 * #include "mbed_logo.h"
Steven Cooreman 0:a0faa86660d4 69 *
Steven Cooreman 0:a0faa86660d4 70 * #define SCK PE12
Steven Cooreman 0:a0faa86660d4 71 * #define MOSI PE10
Steven Cooreman 0:a0faa86660d4 72 *
Steven Cooreman 0:a0faa86660d4 73 * DigitalOut CS(PA10);
Steven Cooreman 0:a0faa86660d4 74 * DigitalOut EXTCOM(PF3);
Steven Cooreman 0:a0faa86660d4 75 * DigitalOut DISP(PA8);
Steven Cooreman 0:a0faa86660d4 76 *
Steven Cooreman 0:a0faa86660d4 77 * SPI displaySPI(MOSI, NC, SCK);
Steven Cooreman 0:a0faa86660d4 78 * silabs::LS013B7DH03 display(&displaySPI, &CS, &EXTCOM);
Steven Cooreman 0:a0faa86660d4 79 *
Steven Cooreman 0:a0faa86660d4 80 * volatile bool completed = false;
Steven Cooreman 0:a0faa86660d4 81 *
Steven Cooreman 0:a0faa86660d4 82 * void completionCallback( void ) {
Steven Cooreman 0:a0faa86660d4 83 * completed = true;
Steven Cooreman 0:a0faa86660d4 84 * }
Steven Cooreman 0:a0faa86660d4 85 *
Steven Cooreman 0:a0faa86660d4 86 * int main() {
Steven Cooreman 0:a0faa86660d4 87 * //Enable the LCD
Steven Cooreman 0:a0faa86660d4 88 * DISP = 1;
Steven Cooreman 0:a0faa86660d4 89 *
Steven Cooreman 0:a0faa86660d4 90 * // Reset the LCD to a blank state. (All white)
Steven Cooreman 0:a0faa86660d4 91 * completed = false;
Steven Cooreman 0:a0faa86660d4 92 * display.clearImmediate(completionCallback);
Steven Cooreman 0:a0faa86660d4 93 * while(completed == false) sleep();
Steven Cooreman 0:a0faa86660d4 94 *
Steven Cooreman 0:a0faa86660d4 95 * printf("Initialization done! \n");
Steven Cooreman 0:a0faa86660d4 96 *
Steven Cooreman 0:a0faa86660d4 97 * // Push update to the display
Steven Cooreman 0:a0faa86660d4 98 * uint32_t refreshCount = display.getRefreshTicks();
Steven Cooreman 0:a0faa86660d4 99 * completed = false;
Steven Cooreman 0:a0faa86660d4 100 * display.update(completionCallback);
Steven Cooreman 0:a0faa86660d4 101 *
Steven Cooreman 0:a0faa86660d4 102 * // Sleep while doing the transmit
Steven Cooreman 0:a0faa86660d4 103 * while(refreshed == false) sleep();
Steven Cooreman 0:a0faa86660d4 104 *
Steven Cooreman 0:a0faa86660d4 105 * // Calculate and print refresh duration
Steven Cooreman 0:a0faa86660d4 106 * refreshCount = display.getRefreshTicks() - refreshCount;
Steven Cooreman 0:a0faa86660d4 107 * printf("Refresh duration: %d cycles @ 125Hz \n", (int)refreshCount);
Steven Cooreman 0:a0faa86660d4 108 *
Steven Cooreman 0:a0faa86660d4 109 * // Sleep forever.
Steven Cooreman 0:a0faa86660d4 110 * while(1) sleep();
Steven Cooreman 0:a0faa86660d4 111 * }
Steven Cooreman 0:a0faa86660d4 112 * @endcode
Steven Cooreman 0:a0faa86660d4 113 */
Steven Cooreman 0:a0faa86660d4 114 public:
Steven Cooreman 0:a0faa86660d4 115
Steven Cooreman 0:a0faa86660d4 116 LS013B7DH03(SPI * spi, DigitalOut * CS, DigitalOut * ExtCom, const char *name=NULL);
Steven Cooreman 0:a0faa86660d4 117
Steven Cooreman 0:a0faa86660d4 118 /**
Steven Cooreman 0:a0faa86660d4 119 * Call this function to push all changes to the display
Steven Cooreman 0:a0faa86660d4 120 *
Steven Cooreman 0:a0faa86660d4 121 * @param callback void (void) type callback pointer to call on display update completion.
Steven Cooreman 0:a0faa86660d4 122 * @return Status code
Steven Cooreman 0:a0faa86660d4 123 */
Steven Cooreman 0:a0faa86660d4 124 int update( cbptr_t callback = NULL );
Steven Cooreman 0:a0faa86660d4 125
Steven Cooreman 0:a0faa86660d4 126 /**
Steven Cooreman 0:a0faa86660d4 127 * Immediately clear the display: set pixel buffer to zero and clear out the display.
Steven Cooreman 0:a0faa86660d4 128 *
Steven Cooreman 0:a0faa86660d4 129 * @param callback void (void) type callback pointer to call on display clear completion.
Steven Cooreman 0:a0faa86660d4 130 * @return Status code
Steven Cooreman 0:a0faa86660d4 131 */
Steven Cooreman 0:a0faa86660d4 132 int clearImmediate( cbptr_t callback = NULL );
Steven Cooreman 0:a0faa86660d4 133
Steven Cooreman 0:a0faa86660d4 134 /**
Steven Cooreman 0:a0faa86660d4 135 * Function to test display buffer
Steven Cooreman 0:a0faa86660d4 136 *
Steven Cooreman 0:a0faa86660d4 137 * @return Status code
Steven Cooreman 0:a0faa86660d4 138 */
Steven Cooreman 0:a0faa86660d4 139 int showDemo();
Steven Cooreman 0:a0faa86660d4 140
Steven Cooreman 0:a0faa86660d4 141 /**
Steven Cooreman 0:a0faa86660d4 142 * Function to get internal 128Hz refresh counter. Can be useful for performance measurement.
Steven Cooreman 0:a0faa86660d4 143 *
Steven Cooreman 0:a0faa86660d4 144 * @return Number of display toggle ticks since init
Steven Cooreman 0:a0faa86660d4 145 */
Steven Cooreman 0:a0faa86660d4 146 uint32_t getRefreshTicks();
Steven Cooreman 0:a0faa86660d4 147
Steven Cooreman 0:a0faa86660d4 148 protected:
Steven Cooreman 0:a0faa86660d4 149 mbed::SPI *_spi;
Steven Cooreman 0:a0faa86660d4 150 mbed::DigitalOut *_EXTCOM;
Steven Cooreman 0:a0faa86660d4 151 mbed::DigitalOut *_CS;
Steven Cooreman 0:a0faa86660d4 152
Steven Cooreman 0:a0faa86660d4 153 mbed::LowPowerTicker _displayToggler;
Steven Cooreman 0:a0faa86660d4 154 mbed::LowPowerTimeout _csTimeout;
Steven Cooreman 0:a0faa86660d4 155
Steven Cooreman 0:a0faa86660d4 156 mbed::CallbackPointer _internalCallback;
Steven Cooreman 0:a0faa86660d4 157 volatile uint32_t _refreshCount;
Steven Cooreman 0:a0faa86660d4 158 uint8_t _lcdPolarity;
Steven Cooreman 0:a0faa86660d4 159 LS013B7DH03_state_t _state;
Steven Cooreman 0:a0faa86660d4 160 cbptr_t _completionCallbackPtr;
Steven Cooreman 0:a0faa86660d4 161 volatile uint32_t _rowCount;
Steven Cooreman 0:a0faa86660d4 162 uint8_t _cmd[2 + (DISPLAY_WIDTH / DISPLAY_BUFFER_TYPE_SIZE * sizeof(DISPLAY_BUFFER_TYPE))];
Steven Cooreman 0:a0faa86660d4 163
Steven Cooreman 0:a0faa86660d4 164 /**
Steven Cooreman 0:a0faa86660d4 165 * Callback handler for internal SPI transfers.
Steven Cooreman 0:a0faa86660d4 166 */
Steven Cooreman 0:a0faa86660d4 167 void _cbHandler( int event );
Steven Cooreman 0:a0faa86660d4 168
Steven Cooreman 0:a0faa86660d4 169 /**
Steven Cooreman 0:a0faa86660d4 170 * Callback handler for internal SPI transfers triggered by timeout.
Steven Cooreman 0:a0faa86660d4 171 */
Steven Cooreman 0:a0faa86660d4 172 void _cbHandlerTimeout( void );
Steven Cooreman 0:a0faa86660d4 173
Steven Cooreman 0:a0faa86660d4 174 /**
Steven Cooreman 0:a0faa86660d4 175 * Call this function at 128 Hz to keep the display from losing contrast.
Steven Cooreman 0:a0faa86660d4 176 */
Steven Cooreman 0:a0faa86660d4 177 void toggle();
Steven Cooreman 0:a0faa86660d4 178 };
Steven Cooreman 0:a0faa86660d4 179
Steven Cooreman 0:a0faa86660d4 180 } // namespace silabs
Steven Cooreman 0:a0faa86660d4 181
Steven Cooreman 0:a0faa86660d4 182 #endif //SILABS_LS013B7DH03_H