This program opens a socket and wait connection through Wi-Fi. When the socket is connected, print out received characters to LCD.

Dependencies:   TextLCD mbed

LcdScreen.h

Committer:
nakata
Date:
2012-04-23
Revision:
0:ac3682c7c208
Child:
1:e87727c8979d

File content as of revision 0:ac3682c7c208:

#include "TextLCD.h"

#define SCREEN_WIDTH    16
#define SCREEN_HEIGHT   2
#define CR  13
#define LF  10

TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7

class LcdScreen {
protected:
    unsigned char cram[SCREEN_HEIGHT][SCREEN_WIDTH];
    int cursor_x;
    int cursor_y;
public:
    LcdScreen()
    {
        cursor_x = 0;
        cursor_y = 0;
        
        int x, y;
        for ( y = 0; y < SCREEN_HEIGHT; y++ ) {
            for ( x = 0; x < SCREEN_WIDTH; x++ ) {
                cram[y][x] = ' ';
            }
        }
        
        syncScreen();
    }
    
    void syncScreen()
    {
        int x, y;
        
        for ( y = 0; y < SCREEN_HEIGHT; y++ ) {
            lcd.locate(0,y);
            for ( x = 0; x < SCREEN_WIDTH; x++ ) {
                lcd.putc(cram[y][x]);
            }
        }
    }
    
    void scroll()
    {
        int x;
        for ( x = 0; x < SCREEN_WIDTH; x++ ) {
            cram[0][x] = cram[1][x];
            cram[1][x] = ' ';
        }
    }
    
    void print(const unsigned char *data)
    {
        const unsigned char *p;
        for ( p = data; *p != '\0'; p++ ) {
            if ( *p == CR ) {
                cursor_x = 0;
            } else if ( *p == LF ) {
                cursor_y++;
            } else if ( 0x20 <= *p && *p <= 0x7f ) {
                if ( cursor_x >= SCREEN_WIDTH ) {
                    cursor_x = 0;
                    cursor_y++;
                }
                if ( cursor_y >= SCREEN_HEIGHT ) {
                    scroll();
                    cursor_y = SCREEN_HEIGHT - 1;
                }
                cram[cursor_y][cursor_x++] = *p;
            }
        }
        if ( cursor_x >= SCREEN_WIDTH ) {
            cursor_x = 0;
            ++cursor_y;
        }
        syncScreen();
    }
};