Alexander Medvedev / lcd128lib

Dependents:   app-board-lcd128

lcd128lib.h

Committer:
medvdv
Date:
2014-02-01
Revision:
0:6ac8f630d9a0
Child:
1:17443d6d2740

File content as of revision 0:6ac8f630d9a0:

//
// MBED Application Board
// Lightweight C12832 LCD library
// 2014, Alexander Medvedev, @medvdv
//

// 
// BC12832 LCD
// Base geometry: 128 x 32 px
// X - [0..127] pixels
// Y - [0..3]   8 bit rows  
// 512 byte total buffer size
//
 
#define LCD_X 128
#define LCD_Y 4
#define LCD_SIZE 512
  
//
// Proportional font  
//

typedef struct {
    char first_code, glyphs_total;   
    const char* widths;
    const char** glyphs;    
} lcd128font; 
 
// 
// lcd128 class
//

class lcd128 {
    
    // Interface with lcd
    SPI spi;
    DigitalOut rst, cs, a0;
     
    // Current font    
    lcd128font font;
    
    bool invert;
    bool bold;
            
    // buffer 
    char buffer[LCD_SIZE];
         
    // Cursor position
    
    int X, Y;
         
    // Generic SPI writer       
    void write(char byte, bool cmd = false);

    public:
    
    // Supply LCD connected pin's here for your design   
    lcd128(PinName mosi= p5, PinName sclk = p7, PinName a0 = p8, PinName cs = p11, PinName rst = p6);

    // LCD control
    void Reset();                           // Reset LCD, configure defaults
    void Power(bool power = true);          // Power off / on
    void InverseMode(bool inverse = true);  // Invert LCD off / on

    // Buffer -> LCD
    void Update();                          // Update LCD from buffer, shows all actually 
    void Clear(int row = -1);               // Clear all buffer or one row
    
    // Write mode
    void Invert(bool invert = true);        // Switch inverting of chars
    void Bold(bool bold = true);            // Switch bold mode (repeat each char row twice)
        
    // Cursor    
    void XY(int x = 0, int y = 0);          // Change write position X in pixels, Y in rows
    
    // Row write
    void Write(char byte);                  // One 8bit row
    void Write(char byte, int count);       // One 8bit row * 'count' times
    void Write(char* data, int size);       // 'size' 8bit rows
    void Write2(char* data, int size);      // Bold: 'size' 8bit rows * 2   
    
    // Character drawing  
    void Character(char chr);               // Draw one font character (with invert and bold opt-s)
                                            // ! automaticaly does next line if char is wider then space left 
    int CharacterWidth(char chr);           // Calculate one character width (with bold opt-n)

    // String drawing
    void String(char* str);                 // Draw string proportionally
    int StringWidth(char* str);             // Calculate string width in px 
    
    void Row(int Y, char* str = "");        // Clear one text row and draw string on it
};