mbed support for LPC4088 Display Module

Dependencies:   DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos mbed-src

Dependents:   emptyProgram

Fork of DMSupport by Embedded Artists

Display/Display.h

Committer:
embeddedartists
Date:
2014-12-11
Revision:
9:a33326afd686
Parent:
0:6b68dac0d986
Child:
10:1ac4b213f0f7

File content as of revision 9:a33326afd686:

/*
 *  Copyright 2014 Embedded Artists AB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef DISPLAY_H
#define DISPLAY_H

#include "mbed.h"

/**
 * Display example
 *
 * @code
 * #include "mbed.h"
 * #include "DMBoard.h"
 *
 * int main(void) {
 *    // initialize the display
 *    DMBoard::instance().init();
 *
 *    // allocate one framebuffer
 *    Display* disp = DMBoard::instance().display();
 *    uint16_t* fb = (uint16_t*)disp->allocateFramebuffer();
 *    if (fb == NULL) {
 *      DMBoard::instance().logger()->printf("Failed to allocate memory for framebuffer\r\n");
 *      mbed_die();
 *    }
 *
 *    // draw something on the framebuffer
 *    ...
 *
 *    // turn on the display
 *    disperr = disp->powerUp(fb);
 *    if (disperr != Display::Ok) {
 *      DMBoard::instance().logger()->printf("Failed to initialize the display, got error %d\r\n", disperr);
 *      mbed_die();
 *    }
 *
 *    ...
 * }
 * @endcode
 */
class Display {
public:
    enum Constants {
        NumLEDs       =  4,
    };
  
    enum DisplayError {
        Ok            =       0,
        ConfigError   =       1,
        WrongBPP      =       2,
        InvalidParam  =       3,
        NoInit        =       4,
    };
    
    enum Resolution {
        Resolution_16bit_rgb565 = 16,
        Resolution_18bit_rgb666 = 18,
        Resolution_24bit_rgb888 = 24,
    };
    
    /** Get the only instance of the Display
     *
     *  @returns The display
     */
    static Display& instance()
    {
        static Display singleton;
        return singleton;
    }
  

    /** Initializes the display but does not turn it on
     *
     *  @returns
     *       Ok on success
     *       An error code on failure
     */
    DisplayError init();
  
    /** Turns the display on with the specified framebuffer showing
     *
     *  @returns
     *       Ok on success
     *       An error code on failure
     */
    DisplayError powerUp(void* framebuffer, Resolution wanted = Resolution_16bit_rgb565);
  
    /** Turns the display off
     *
     *  @returns
     *       Ok on success
     *       An error code on failure
     */
    DisplayError powerDown();
  
    /** Sets the backlight level. 0% is off and 100% is fully on
     *
     *  @returns
     *       Ok on success
     *       An error code on failure
     */
    DisplayError backlight(int percent);
  
    uint16_t width() { return _width; }
    uint16_t height() { return _height; }
    uint16_t bpp() { return _bpp; }
    uint32_t fbSize() { return _fbSize; }
    bool landscape() { return _landscape; }
    bool isSupported(Resolution res);
    
    /** Replaces the current framebuffer.
     *
     * Note that this requires the caller or someone else to have a
     * reference to the existing framebuffer, otherwise that memory
     * is lost.
     *
     *  @param buff the new framebuffer
     */
    void setFramebuffer(void* buff);
    
    /** Replaces the current framebuffer with the specified one.
     *
     * This function as opposed to the setFramebuffer() one does return
     * the old framebuffer. This way the caller can save the old one and
     * then swap it back when done.
     *
     *  @param buff the new framebuffer
     *  @returns the old framebuffer
     */
    void* swapFramebuffer(void* buff);
    
    /** Allocate enough memory for one framebuffer
     *
     * This function is a to make it easier to allocate memory for framebuffers
     * as the number of bytes needed depends on width, height and bytes per pixel.
     *
     * Free the allocated memory when done using the free() function.
     *
     *  @returns a new framebuffer or NULL if out of memory 
     */
    void* allocateFramebuffer(Resolution res=Resolution_16bit_rgb565);

private:

    bool _initialized;
    bool _poweredOn;
    DigitalOut _pinWP;
    DigitalOut _pin3v3;
    DigitalOut _pin5v;
    DigitalOut _pinDE;
    DigitalOut _pinColDepth;
    PwmOut _pinBacklight;

    uint16_t _width;
    uint16_t _height;
    uint16_t _bpp;
    uint32_t _fbSize;
    bool _landscape;

    explicit Display();
    // hide copy constructor
    Display(const Display&);
    // hide assign operator
    Display& operator=(const Display&);
    ~Display();
    
    void pinInit(bool powerOn, Resolution res=Resolution_16bit_rgb565);
    DisplayError regInit(void* info, Resolution res);
    uint32_t getClockDivisor(uint32_t clock);
    void set3v3(bool on);
    void set5v(bool on);
    void setDisplayEnable(bool enable);    
};

#endif