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.

LS013B7DH03.h

Committer:
Steven Cooreman
Date:
2015-03-18
Revision:
1:6332f3383fb6
Parent:
0:a0faa86660d4
Child:
2:2f10f00fe56c

File content as of revision 1:6332f3383fb6:

/***************************************************************************//**
 * @file LS013B7DH03.h
 * @brief Driver class for the Sharp LS013B7DH03 memory LCD on some kits.
 *******************************************************************************
 * @section License
 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
 *******************************************************************************
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
 * obligation to support this Software. Silicon Labs is providing the
 * Software "AS IS", with no express or implied warranties of any kind,
 * including, but not limited to, any implied warranties of merchantability
 * or fitness for any particular purpose or warranties against infringement
 * of any proprietary rights of a third party.
 *
 * Silicon Labs will not be liable for any consequential, incidental, or
 * special damages, or any other relief, or for any claim by any third party,
 * arising from your use of this Software.
 *
 ******************************************************************************/

#ifndef SILABS_LS013B7DH03_H
#define SILABS_LS013B7DH03_H

#include "platform.h"
#include <mbed.h>
#include "CallbackPointer.h"
#include "LCDSettings.h"
#include "BufferedDisplay.h"

typedef void (*cbptr_t)(void);

#define LS013B7DH03_ERROR_BUSY		-1
#define LS013B7DH03_ERROR_SPI_BUSY	-2
#define LS013B7DH03_NO_ACTION		-3
#define LS013B7DH03_ERROR_ARGUMENT	-4
#define	LS013B7DH03_OK				0

typedef enum {
	IDLE,			// No operation currently ongoing
	CLEARING,		// In the process of clearing the display
	WRITING,		// In the process of sending a display update
	WAIT_CLEAR,		// Going to clear after CS pin timeout
	WAIT_WRITE,		// Going to write after CS pin timeout
	TRANSFERS_DONE, // Last transfer in progress
	DONE			// Done with transmission, waiting for CS pin to become high
} LS013B7DH03_state_t;

namespace silabs {
/**  A driver for the Sharp LS013B7DH03 Memory LCD, present on some EFM32 Starter Kits.
 *
 * Currently supports LS013B7DH03 only, but should be easily modifiable.
 *
 * @code
 * #include "mbed.h"
 * #include "LS013B7DH03.h"
 * #include "mbed_logo.h"
 * 
 * #define SCK     PE12
 * #define MOSI 	PE10
 *
 * DigitalOut CS(PA10);
 * DigitalOut EXTCOM(PF3);
 * DigitalOut DISP(PA8);
 * 
 * SPI displaySPI(MOSI, NC, SCK);
 * silabs::LS013B7DH03 display(&displaySPI, &CS, &EXTCOM);
 *
 * volatile bool completed = false;
 *
 * void completionCallback( void ) {
 *     completed = true;
 * }
 * 
 * int main() {
 *     //Enable the LCD
 *     DISP = 1;
 *
 *     // Reset the LCD to a blank state. (All white)
 *     completed = false;
 *     display.clearImmediate(completionCallback);
 *     while(completed == false) sleep();
 *
 *     printf("Initialization done! \n");
 *
 *     // Push update to the display
 *     uint32_t refreshCount = display.getRefreshTicks();
 *     completed = false;
 *     display.update(completionCallback);
 *
 *     // Sleep while doing the transmit
 *     while(refreshed == false) sleep();
 *
 *     // Calculate and print refresh duration
 *     refreshCount = display.getRefreshTicks() - refreshCount;
 *     printf("Refresh duration: %d cycles @ 125Hz \n", (int)refreshCount);
 *
 *     // Sleep forever.
 *     while(1) sleep();
 * }
 * @endcode
 */
class LS013B7DH03 : public BufferedDisplay {
public:

	LS013B7DH03(SPI * spi, DigitalOut * CS, DigitalOut * ExtCom, const char *name=NULL);

	/**
	 * Call this function to push all changes to the display
     *
     * @param callback        void (void) type callback pointer to call on display update completion.
     * @return Status code
	 */
	int update( cbptr_t callback = NULL );

	/**
	 * Immediately clear the display: set pixel buffer to zero and clear out the display.
     *
     * @param callback        void (void) type callback pointer to call on display clear completion.
     * @return Status code
	 */
	int clearImmediate( cbptr_t callback = NULL );

	/**
	 * Function to test display buffer
     *
     * @return Status code
	 */
	int showDemo();

	/**
	 * Function to get internal 128Hz refresh counter. Can be useful for performance measurement.
     *
     * @return Number of display toggle ticks since init
	 */
	uint32_t getRefreshTicks();

protected:
	mbed::SPI *_spi;
	mbed::DigitalOut *_EXTCOM;
	mbed::DigitalOut *_CS;

	mbed::LowPowerTicker _displayToggler;
	mbed::LowPowerTimeout _csTimeout;

	mbed::CallbackPointer _internalCallback;
	volatile uint32_t _refreshCount;
	uint8_t _lcdPolarity;
	LS013B7DH03_state_t _state;
	cbptr_t _completionCallbackPtr;
	volatile uint32_t _rowCount;
	uint8_t _cmd[2 + (DISPLAY_WIDTH / DISPLAY_BUFFER_TYPE_SIZE * sizeof(DISPLAY_BUFFER_TYPE))];

	/**
	 * Callback handler for internal SPI transfers.
	 */
	void _cbHandler( int event );

	/**
	 * Callback handler for internal SPI transfers triggered by timeout.
	 */
	void _cbHandlerTimeout( void );

	/**
	 * Call this function at 128 Hz to keep the display from losing contrast.
	 */
	void toggle();
};

} // namespace silabs

#endif //SILABS_LS013B7DH03_H