• Main Page
  • Related Pages
  • Files
  • File List
  • Globals

lcd.c

Go to the documentation of this file.
00001 
00016 #include <avr/io.h>
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <util/delay.h>
00021 #include "lcd.h"
00022 
00024 char display_buffer[LCD_ROWS][LCD_COLS+1];
00025 
00027 char input_buffer[LCD_ROWS][INPUT_BUFFER_SIZE+1]= { 
00028         "********************\0",
00029         "**  UART-Display  **\0",
00030         "**by Ernst Abrazzo**\0",
00031         "********************\0"
00032         };
00033 
00035 uint8_t buffer_index=0;
00036 
00038 uint8_t char_index=0;
00039 
00041 uint8_t formfeed=0;
00042 
00044 uint8_t escape=0;
00045 
00047 uint8_t firstchar=0;
00048 
00050 uint8_t scroll=0;
00051 
00053 char remap[256] = {
00054           0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
00055          16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
00056          32,  33,  34,  35, 162,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
00057          48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
00058         160,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
00059          80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90, 250, 251, 252,  29, 196,
00060          96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
00061         112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 253, 254, 255, 206, 127,
00062         128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
00063         144, 145, 146, 147, 148, 149,  45,  45, 152, 153, 154, 155, 156, 157, 158, 159,
00064         160,  40, 162,  64, 164, 163, 254,  95, 168, 169, 170, 171, 172, 173, 174, 175,
00065         128, 140, 130, 131, 180, 143, 182, 183, 184, 185, 186, 187, 139, 138, 190,  96,
00066         192, 193, 194, 195,  91, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
00067         208, 209, 210, 211, 212, 213,  92, 215, 216, 217, 218, 219,  94, 221, 222, 190,
00068         127, 225, 226, 227, 123, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
00069         240, 125, 242, 243, 244, 245, 124, 247, 248, 249, 250, 251, 126, 253, 254, 255
00070 };
00071 
00072 void print_buffer(void) {
00073         uint8_t i;
00074         i=buffer_index;
00075 
00076         lcd_clear();
00077 
00078         if(scroll) {
00079                 #ifdef LCD_LINE1
00080                 lcd_setadr(LCD_LINE1);
00081                 lcd_puts(display_buffer[i]);
00082                 i=( (i+1) & 3);
00083                 #endif
00084                 
00085                 #ifdef LCD_LINE2
00086                 lcd_setadr(LCD_LINE2);
00087                 lcd_puts(display_buffer[i]);
00088                 i=( (i+1) & 3);
00089                 #endif
00090                 
00091                 #ifdef LCD_LINE3
00092                 lcd_setadr(LCD_LINE3);
00093                 lcd_puts(display_buffer[i]);
00094                 i=( (i+1) & 3);
00095                 #endif
00096                 
00097                 #ifdef LCD_LINE4
00098                 lcd_setadr(LCD_LINE4);
00099                 lcd_puts(display_buffer[i]);
00100                 #endif
00101 
00102         } else {        
00103                 #ifdef LCD_LINE1
00104                 lcd_setadr(LCD_LINE1);
00105                 lcd_puts(input_buffer[i]);
00106                 i=( (i+1) & 3);
00107                 #endif
00108                 
00109                 #ifdef LCD_LINE2
00110                 lcd_setadr(LCD_LINE2);
00111                 lcd_puts(input_buffer[i]);
00112                 i=( (i+1) & 3);
00113                 #endif
00114                 
00115                 #ifdef LCD_LINE3
00116                 lcd_setadr(LCD_LINE3);
00117                 lcd_puts(input_buffer[i]);
00118                 i=( (i+1) & 3);
00119                 #endif
00120                 
00121                 #ifdef LCD_LINE4
00122                 lcd_setadr(LCD_LINE4);
00123                 lcd_puts(input_buffer[i]);
00124                 #endif
00125         }
00126 }
00127 
00128 void lcd_scroll(void) {
00129         static uint8_t line[4]={0,0,0,0};
00130         
00131         uint8_t i=0;
00132         uint8_t tmp,len;
00133         
00134         for(i=0; i<4; i++) {
00135                 
00136                 len = strlen(input_buffer[i]);
00137                 
00138                 if(len<LCD_COLS) {
00139                         line[i]=0;
00140                 }
00141                 
00142                 tmp = strlcpy(display_buffer[i], input_buffer[i]+line[i], LCD_COLS+1);
00143                 
00144                 if(len>LCD_COLS) {
00145                         line[i]++;
00146                         line[i] = line[i]%len;
00147                         if(tmp<LCD_COLS) {
00148                                 strlcpy(display_buffer[i]+tmp,input_buffer[i], LCD_COLS-tmp+1);
00149                         }
00150                 }
00151         }
00152         print_buffer();
00153 }
00154 
00163 void push_char(uint8_t data) {
00164         if(scroll) {
00165                 if(char_index<INPUT_BUFFER_SIZE) {
00166                         input_buffer[buffer_index][char_index] = data;
00167                         char_index++;
00168                 }
00169         } else {
00170                 if(char_index<LCD_COLS) {
00171                         input_buffer[buffer_index][char_index] = data;
00172                         char_index++;
00173                 } else {
00174                         input_buffer[buffer_index][char_index-1] = 0x15;
00175                 }
00176         }
00177         formfeed=0;
00178         firstchar=0;
00179 }
00180 
00181 int lcd_putchar(char data, FILE *stream) {
00182         if(formfeed) {                                                  // Formfeed introduces special sequences consisting of \f (formfeed)
00183                                                                                         // and two following ASCI-Symbols, in order to dipslay symbols
00184                                                                                         // which are not available on keyboard
00185                 switch(firstchar) {                             
00186                         case 0:                                                 // 
00187                                 firstchar = data;
00188                                 break;
00189                         
00190                         case '<':
00191                                 switch(data) {
00192                                         case '-':                               // \f<- : Pfeil nach links
00193                                                 push_char(0xE1);        
00194                                                 break;
00195                                         case '|':                               // \f<| : Return-Zeichen
00196                                                 push_char(0x1C);
00197                                                 break;
00198                                         case 'b':                               // \f<b : Dicker Pfeil nach links
00199                                                 push_char(0x11);
00200                                                 break;
00201                                         case '<':                               // \f<< : Doppelpfeil nach links
00202                                                 push_char(0x14);
00203                                                 break;
00204                                                 
00205                                         default:        
00206                                                 push_char('A'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00207                                                 break;
00208                                 }
00209                                 break;
00210                                 
00211                         case '>':
00212                                 switch(data) {
00213                                         case 'b':                               // \f>b : Dicker Pfeil nach rechts
00214                                                 push_char(0x10);
00215                                                 break;
00216                                         case '>':                               // \f>> : Doppelpfeil nach rechts
00217                                                 push_char(0x15);
00218                                                 break;
00219                                                 
00220                                         default:        
00221                                                 push_char('B'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00222                                                 break;
00223                                 }
00224                                 break;
00225                         
00226                         case '^':
00227                                 switch(data) {
00228                                         case '|':                               // \f^| : Pfeil nach oben
00229                                                 push_char(0xDE);
00230                                                 break;
00231                                         case '\\':                              // \f^\\ : Pfeil nach links-oben
00232                                                 push_char(0x16);
00233                                                 break;
00234                                         case '^':                               // \f^^ : Doppelfpeil nach oben
00235                                                 push_char(0x12);
00236                                                 break;  
00237                                         case 'b':                               // \f^b : Dicker Pfeil nach oben
00238                                                 push_char(0x1A);
00239                                                 break;
00240                                         case '0':                               // \f^* : Hochgestellte Ziffer
00241                                         case '1':                               
00242                                         case '2':
00243                                         case '3':
00244                                         case '4':
00245                                         case '5':
00246                                         case '6':
00247                                         case '7':
00248                                         case '8':
00249                                         case '9':
00250                                                 push_char(0x80-'0'+data);
00251                                                 break;
00252                                                 
00253                                                 
00254                                         default:
00255                                                 push_char('C'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00256                                                 break;
00257                                 }
00258                                 break;
00259                         
00260                         case 'v':
00261                                 switch(data) {
00262                                         case '|':                               // \fv| : Pfeil nach unten
00263                                                 push_char(0xE0);
00264                                                 break;
00265                                         case '/':                               // \fv/ : Pfeil nach links-unten
00266                                                 push_char(0x18);
00267                                                 break;
00268                                         case 'v':                               // \fvv : Doppelpfeil nach unten
00269                                                 push_char(0x13);
00270                                                 break;
00271                                         case 'b':                               // \fvb : Dicker Pfeil nach unten
00272                                                 push_char(0x1B);
00273                                                 break;
00274                                         
00275                                         default: 
00276                                                 push_char('D'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00277                                                 break;
00278                                 }
00279                                 break;
00280                         
00281                         case '/':
00282                                 switch(data) {
00283                                         case '^':                               // \f/^ : Pfeil nach rechts-oben
00284                                                 push_char(0x17);
00285                                                 break;
00286                                         
00287                                         default: 
00288                                                 push_char('E'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00289                                                 break;
00290                                 }
00291                                 break;
00292                         
00293                         case '\\':                                              
00294                                 switch(data) {
00295                                         case 'v':                               // \f\\v : Pfeil nach rechts-unten
00296                                                 push_char(0x19);
00297                                                 break;
00298                                         
00299                                         default: 
00300                                                 push_char('F'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00301                                                 break;
00302                                 }
00303                                 break;
00304                         
00305                         case '-':                                               
00306                                 switch(data) {
00307                                         case '>':                               // \f-> : Pfeil nach rechts
00308                                                 push_char(0xDF);
00309                                                 break;
00310                                         
00311                                         default: 
00312                                                 push_char('G'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00313                                                 break;
00314                                 }
00315                                 break;
00316                                 
00317                         case '|':                                               
00318                                 switch(data) {
00319                                         case 'v':                               // \fv| : Pfeil nach unten
00320                                                 push_char(0xE0);
00321                                                 break;
00322                                         case '^':                               // \f^| : Pfeil nach oben
00323                                                 push_char(0xDE);
00324                                                 break;
00325                                         case '1':                               // \f|* : Progressbar
00326                                                 push_char(0xD4);        
00327                                                 break;
00328                                         case '2':
00329                                                 push_char(0xD3);
00330                                                 break;
00331                                         case '3':
00332                                                 push_char(0xD2);
00333                                                 break;
00334                                         case '4':
00335                                                 push_char(0xD1);
00336                                                 break;
00337                                         case '5':
00338                                                 push_char(0xD0);
00339                                                 break;
00340                                         
00341                                         default: 
00342                                                 push_char('H'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00343                                                 break;
00344                                 }
00345                                 break;
00346                                 
00347                         
00348                         default:        
00349                                 push_char('I'); // Müllzeichen aufs display schreiben, um zu signalisieren dass die Sequenz ungültig war
00350                                 break;
00351                 }
00352                                 
00353                 return 0;
00354         }
00355         
00356         if(escape) {                                                    // Escape-sequence (\e) introduces specialsequences consisting of \e and 
00357                                                                                         // one following ASCI-symbol, on order to control some display-properties.
00358                 switch(data) {
00359                         case 'S':                                               // /eS enables scrolling-mode for line-length above 20 (displayed) symbols
00360                                 scroll=1;
00361                                 escape=0;
00362                                 break;
00363                         case 's':                                               // /es enables scrolling-mode for line-length above 20 (displayed) symbols
00364                                 scroll=0;
00365                                 escape=0;
00366                                 break;                          
00367                         case 'i':                                               // /ei re-initilizes the display-IC
00368                                 lcd_init();
00369                                 _delay_ms(5);
00370                                 escape=0;
00371                                 break;
00372                                 
00373                         default:                                                //  Handle non-valid escape-sequences
00374                                 escape=0;
00375                                 break;
00376                 }
00377                 return 0;
00378         }
00379         
00380         switch(data) {                                                  // Handle all cases which can occure, if there has no special case happened bevore and a character is received. 
00381                 case '\r':                                                      // \r carriage return detected. Shift lines one lines to the top an insert received string into bottom line in the buffer.
00382                 case '\n':                                                      // \n so do,if newline detected. 
00383                         input_buffer[buffer_index][char_index] = 0;
00384                         buffer_index = ( (buffer_index +1) % LCD_ROWS );
00385                         char_index=0;
00386                         if(data=='\r') print_buffer();  // in addition to the commands above, print buffer to display in case of \r
00387                         break;
00388                         
00389                 case '\e':                                                      // \e escape-sequence detected
00390                         escape=1;
00391                         break;
00392                 case '\f':                                                      // \f formfeed-sequence detected
00393                         formfeed=1;
00394                         break;
00395                         
00396                         
00397                 default:
00398                         push_char(remap[(uint8_t)data]);  // If none of the above mentioned exceptiones came true, remap received character. 
00399                         break;
00400         }
00401         
00402         return 0;
00403 }

Generated on Tue Feb 15 2011 18:32:45 for UartDisplay by  doxygen 1.7.2