Lightweight proportional text library for C12832 LCD. Easy to modify, fast, robust and compact. Nice font, good for text driven menus, messages, etc. Fell free to use and modify in any projects.

Dependents:   app-board-lcd128

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lcd128lib.h Source File

lcd128lib.h

00001 //
00002 // MBED Application Board
00003 // Lightweight C12832 LCD library
00004 // 2014, Alexander Medvedev, @medvdv
00005 //
00006 
00007 // 
00008 // BC12832 LCD
00009 // Base geometry: 128 x 32 px
00010 // X - [0..127] pixels
00011 // Y - [0..3]   8 bit rows  
00012 // 512 byte total buffer size
00013 //
00014  
00015 #define LCD_X       128     // In pixels
00016 #define LCD_Y       4       // In rows by 8 pixels each
00017 #define LCD_SIZE    512     // In bytes, = LCD_X*LCD_Y
00018     
00019 //
00020 // Proportional font  
00021 //
00022 
00023 typedef struct {
00024     unsigned char first_code, glyphs_total;   
00025     const unsigned char*  widths;
00026     const unsigned char** glyphs;    
00027 } lcd128font; 
00028  
00029 //
00030 // Progress bar shape 
00031 // Drawed as BFFF...FFUU...UUUE
00032 // B - Begin  one
00033 // F - Filled zone
00034 // U - Empty zone
00035 // E - End one
00036 //
00037 
00038 #define LCD_BAR_B   0x1c
00039 #define LCD_BAR_F   0x3e 
00040 #define LCD_BAR_U   0x22
00041 #define LCD_BAR_E   0x1c
00042 
00043 //
00044 // Special Glyphs for menu / interface
00045 //
00046 
00047 #define LCD_RADIO_OFF       128
00048 #define LCD_RADIO_ON        129
00049 #define LCD_CHECK_OFF       130
00050 #define LCD_CHECK_ON        131
00051 #define LCD_SHORT_SPACE     132
00052 #define LCD_PLAY            133
00053 #define LCD_PAUSE           134
00054 #define LCD_FORWARD         135
00055 #define LCD_REVERSE         136
00056 #define LCD_BEGIN           137
00057 #define LCD_END             138
00058 #define LCD_PLAY_PAUSE      139
00059 #define LCD_CLOCK0          140
00060 #define LCD_CLOCK1          141
00061 #define LCD_CLOCK2          142
00062 #define LCD_CLOCK3          143
00063 #define LCD_CLOCK4          144
00064 #define LCD_CLOCK5          145
00065 #define LCD_CLOCK6          146
00066 #define LCD_CLOCK7          147
00067 
00068 //
00069 // Row text align
00070 //
00071 
00072 #define LCD_ALIGN_LEFT      0
00073 #define LCD_ALIGN_CENTER    1
00074 #define LCD_ALIGN_RIGHT     2
00075 
00076 //
00077 // lcd128 class
00078 //
00079 
00080 class lcd128 {
00081     
00082     // Interface with lcd
00083     SPI spi;
00084     DigitalOut rst, cs, a0;
00085      
00086     // Current font    
00087     lcd128font font;
00088     
00089     // Write options
00090     bool invert;
00091     bool bold;
00092     bool underline;
00093             
00094     // buffer 
00095     unsigned char buffer[LCD_SIZE];
00096          
00097     // Cursor position
00098     int X, Y;
00099          
00100     // SPI writer with data / command       
00101     void write(unsigned char byte, bool cmd = false);
00102 
00103     public:
00104     
00105     // Supply LCD connected pin's here for your design   
00106     lcd128(PinName mosi = p5, PinName sclk = p7, PinName a0 = p8, PinName cs = p11, PinName rst = p6);
00107 
00108     // LCD control
00109     void Reset();                           // Reset LCD, configure defaults
00110     void Power(bool power = true);          // Power off / on
00111     void InverseMode(bool inverse = true);  // Invert LCD off / on
00112 
00113     // Buffer -> LCD
00114     void Update();                          // Update LCD from buffer, actually shows drawed
00115  
00116     // All buffer operations
00117     void Clear(int row = -1);               // Clear all buffer or one concrete row
00118     void InverseRow(int row = -1, unsigned char mask = 255);    // Inverse all buffer or one concrete row 
00119     
00120     // Write mode
00121     void Invert(bool invert = true);        // Switch inverting of chars
00122     void Bold(bool bold = true);            // Switch bold mode (repeat each char row twice)
00123     void Underline(bool underline = true);  // Switch underline mode (last pixel of row always on)
00124         
00125     // Cursor    
00126     void XY(int x = 0, int y = 0);          // Change write position X in pixels, Y in rows
00127     
00128     // Vertical 8bit pixel row
00129     void Write(unsigned char byte);                  // One 8bit row
00130     void Write(unsigned char byte, int count);       // One 8bit row * 'count' times
00131     void Write(unsigned char * data, int size);       // 'size' 8bit rows
00132     void Write2(unsigned char * data, int size);      // Bold: 'size' 8bit rows * 2   
00133     
00134     // Character drawing  
00135     void Character(char chr);               // Draw one font character (with invert and bold opt-s)
00136                                             // ! automaticaly does next line if char is wider then space left 
00137     int CharacterWidth(char chr);           // Calculate one character width (with bold opt-n)
00138 
00139     // String drawing
00140     void String(char* str);                 // Draw string proportionally
00141     int StringWidth(char* str);             // Calculate string width in px 
00142     
00143     // Row drawer
00144     void Row(int Y, char* str = "", bool inverse = false, int align = 0); // Clear one text row and draw string on it
00145     
00146     // Progress bar
00147     void Bar(int width, float fill = 0.0);  // Progress bar - one row, width pixels, fill - 0.0..1.0 
00148 };