A lib to handle a E-Paper display from Pervasive Displays. There is a interface board from Embedded Artists. The lib can handle graphic and text drawing and is using external fonts.

Dependents:   epaper_mbed_130411_KL25Z epaper_mbed_test epaper_KL25Z_2 test_he10 ... more

/media/uploads/dreschpe/epaper.jpg

The E-Paper display from Pervasive Displays with a interface board is available from Embedded Artists : http://www.embeddedartists.com/products/displays/lcd_27_epaper.php The 2.7 inch display have 264*176 pixel, monochrome.

Technology

You can look at the webside from Pervasive to see how the display works. http://www.pervasivedisplays.com/technology/home

This type of display have ultra low power consumption - due to its bi-stable nature. It requires only very little power to update the display and needs no power to maintain an image. You can disconnect the display - the image is still there. The viewing angle is like real paper - near 180°.

There are also some disadvantages of this technology. To change the image, we have to rewrite the full display in 4 steps. Invert, clear, invert new, new image. This process is visible and take a while -2s at room temperature. If it gets colder the display reacts slower and the interface timing has to be slow down. To compensate this, there is a LM75 temp sensor on the interface board. We also need ram to double buffer the display. 264 * 176 / 8 = 5808 Byte. To double buffer we need 11616 byte of ram. This is no problem for most mbed devices, but it will not run on the LPC11U24 or LPC800-MAX.

Interface

The graphic data is transferred to the display via spi. The maximum speed is 12Mhz. There are also some control signal and the I2C for the temperature sensor. Together we need 12 signals.

Displaymbed LPC1768mbed KL25Zsignal type
1 GNDGNDGNDGND
2 3V3VOUTP3V33.3 V power
3 SCKp7PTD1SCK
4 MOSIp5PTD2MOSI
5 MISOp6PTD3MISO
6 SSELp8PTC17GPIO
7 Busyp13PTA16GPIO
8 Borderp10PTD6GPIO
9 SCLp27PTE1SCL
10 SDAp28PTE0SDA
11 PWMp26PTD4PWM
12 Resetp12PTA17GPIO
13 Power controlp9PTD7GPIO
14 Dischargep11PTE31GPIO

Software

Fonts

How to get nice looking fonts ?

To print characters to a graphic screen we need a font. To code a font by paper is ok for a small lcd, but for a 264*176 pixel display we need bigger fonts. A 12*12 pixel font is readable, but it a lot of work to construct it by hand.

Fonts can be made with the GLCD Font Creator also from http://www.mikroe.com .

With this program you can load a window font and convert it into a c-array. To use this Font with my lib you have to add 4 parameter at the beginning of the font array. - the number of byte / char - the vertial size in pixel - the horizontal size in pixel - the number of byte per vertical line (it is vertical size / 8 ) You also have to change the type of array to char[]. After that you can switch between different fonts with set_font(unsigned char* font); The horizontal size of each character is also stored in the font. It look better if you use bigger fonts or italic. The letter M is wider than a l.

Here are some Fonts from me : http://mbed.org/users/dreschpe/code/TFT_fonts/

The small made for the mbed lab board can also be used : http://mbed.org/users/dreschpe/code/LCD_fonts/

And from Peter Holzleitner : http://mbed.org/users/pholzleitner/code/SourceCodePro31-SB/

Text commands :

You can use the claim() function to redirect the output to stdout or stderr to the display. After claim(stdout) you can simply use the printf function without the classname to print to the display. All other printf from other libs are also redirected to the display if you use this.

  • printf(...); print text and variables to the buffer with format options.
  • locate(x,y); function is used to setup the cursor position. x,y are the pixel position.

Graphics

Graphic commands :

  • cls(); Fill the screen with background color
  • pixel(x,y,color); set a single pixel at x,y with 1 : black or 0 : white
  • line(x0,y0,x1,y1,color); draw a line from x0,y0 to x1,y1 with color
  • rect(x0,y0,x1,y1,color); draw a rectangle x0,y0 to x1,y1 with color
  • fillrect(x0,y0,x1,y1,color); draw a filled rectangle
  • circle( x0,y0,radius ,color); draw a circle around x0,y0 with radius
  • fillcircle(x0,y0,radius ,color); draw a filled circle around x0,y0 with radius
  • setmode(mode); Set the drawing mode for all functions. mode can be NORMAL -> 0 is white and 1 is black or XOR -> the new pixel is a xor between the old display and the new. This mode will invert if a black pixel is draw over a black pixel.
  • print_bm(Bitmap ,x0,x0); Print a monochrome bitmap array. This graphic is defined by a Bitmap struct :

The pixel date array :

static char arm_logo[]={
0x00,0x00...
};

and a Bitmap struct:

Bitmap bitmARM = {
  48, // XSize
  48, // YSize 
  6,  // Bytes in Line
  arm_logo // Pointer to picture data 
};

To convert a graphic into a byte array we can use the tool Picture Converter 1bpp from http://www.embedded-tools.de.vu/ With this tool we load a image, press the convert button and save it as C-Header file. We have to save with horizontal orientation, so we have to press "No". Inside this file we find the data array which we can copy into a header file.

All this commands are writing to the frame buffer only ! To change the active display we have to call

  • write_disp(); This will refresh the display.

Sample code

test code for the LPC1768: http://mbed.org/users/dreschpe/code/epaper_mbed_test/

test code for KL25Z: http://mbed.org/users/dreschpe/code/epaper_mbed_130411_KL25Z/

#include "mbed.h"
#include "EaEpaper.h"
#include "Arial28x28.h"
#include "Arial12x12.h"
#include "font_big.h"
#include "graphics.h"

EaEpaper epaper(
                PTD7,            // PWR_CTRL
                PTD6,            // BORDER
                PTE31,           // DISCHARGE
                PTA17,           // RESET_DISP
                PTA16,           // BUSY
                PTC17,           // SSEL
                PTD4,            // PWM
                PTD2,PTD3,PTD1,  // MOSI,MISO,SCLK
                PTE0,PTE1);      // SDA,SDL 
 
int main() {

    epaper.cls();                                      // clear screen
    epaper.set_font((unsigned char*) Arial28x28);  // select the font
    epaper.locate(5,20);                           // set cursor
    epaper.printf("Hello Mbed");                  // print  text
    epaper.rect(3,15,150,50,1);                  // print a frame 
     
    epaper.set_font((unsigned char*) Arial12x12);  // change font
    epaper.locate(5,60);                               // set cursor
    epaper.printf("small Font");                    // print text
    epaper.set_font((unsigned char*) Neu42x35);  // change font
    epaper.locate(5,70);                               //set cursor
    epaper.printf("big Font");                        // change font
    
    epaper.write_disp(); // update screen       // update display
    
    wait(5);                                           // wait 5 s
    epaper.fillcircle(180,30,22,1);              // paint filled circle
    epaper.circle(160,150,20,1);               // paint circle
    epaper.write_disp(); // update screen      // update display
    
}
  

EaEpaper.cpp

Committer:
dreschpe
Date:
2014-06-25
Revision:
3:1371614703cd
Parent:
0:fedcef5319f5

File content as of revision 3:1371614703cd:


#include "EaEpaper.h"

#define EA_IMG_BUF_SZ (5808) // 264 x 176 / 8 = 5808

#define LM75A_ADDRESS (0x92) // 0x49 << 1
#define LM75A_CMD_TEMP 0x00

static uint8_t _oldImage[EA_IMG_BUF_SZ];
static uint8_t _newImage[EA_IMG_BUF_SZ];

EaEpaper::EaEpaper(PinName p_on, PinName border, PinName discharge,  PinName reset, PinName busy, PinName cs,
                   PinName pwm, 
                   PinName mosi, PinName miso, PinName sck, 
                   PinName sda, PinName scl, 
                   const char* name):
    epd_(EPD_2_7,
         p_on,  // panel_on_pin
         border, // border_pin
         discharge, // discharge_pin
         pwm, // pwm_pin
         reset, // reset_pin
         busy, // busy_pin
         cs,  // chip_select_pin
         mosi,  // mosi
         miso,  // miso
         sck),  // sck
    i2c_(sda, scl),
    GraphicsDisplay(name)
{
    draw_mode = NORMAL;
    memset(_oldImage, 0, EA_IMG_BUF_SZ);
    memset(_newImage, 0, EA_IMG_BUF_SZ);
}


int EaEpaper::width()
{
    return 264;
}

int EaEpaper::height()
{
    return 176;
}


// erase pixel after power up
void EaEpaper::clear()
{
    epd_.begin();
    epd_.setFactor(readTemperature()/100);
    epd_.clear();
    epd_.end();

    memset(_oldImage, 0, EA_IMG_BUF_SZ);
    memset(_newImage, 0, EA_IMG_BUF_SZ);
}

// update screen  _newImage -> _oldImage
void EaEpaper::write_disp(void)
{
    epd_.begin();
    epd_.setFactor(readTemperature()/100);
    epd_.image(_oldImage, _newImage);
    epd_.end();

    memcpy(_oldImage, _newImage, EA_IMG_BUF_SZ);
}

// read LM75A sensor on board to calculate display speed
int32_t EaEpaper::readTemperature()
{
    char buf[2];
    int32_t t = 0;
    char reg = LM75A_CMD_TEMP;

    i2c_.write(LM75A_ADDRESS, &reg, 1);
    i2c_.read(LM75A_ADDRESS, buf, 2);

    t = ((buf[0] << 8) | (buf[1]));

    return ((t * 100) >> 8);
}

// set one pixel in buffer _newImage

void EaEpaper::pixel(int x, int y, int color)
{
    // first check parameter
    if(x > 263 || y > 175 || x < 0 || y < 0) return;

    if(draw_mode == NORMAL) {
        if(color == 0)
            _newImage[(x / 8) + ((y-1) * 33)] &= ~(1 << (x%8));  // erase pixel
        else
            _newImage[(x / 8) + ((y-1) * 33)] |= (1 << (x%8));   // set pixel
    } else { // XOR mode
        if(color == 1)
            _newImage[(x / 8) + ((y-1) * 33)] ^= (1 << (x%8));   // xor pixel
    }
}

// clear screen
void EaEpaper::cls(void)
{
    memset(_newImage, 0, EA_IMG_BUF_SZ);  // clear display buffer
}

// print line
void EaEpaper::line(int x0, int y0, int x1, int y1, int color)
{
    int   dx = 0, dy = 0;
    int   dx_sym = 0, dy_sym = 0;
    int   dx_x2 = 0, dy_x2 = 0;
    int   di = 0;

    dx = x1-x0;
    dy = y1-y0;

    if (dx > 0) {
        dx_sym = 1;
    } else {
        dx_sym = -1;
    }

    if (dy > 0) {
        dy_sym = 1;
    } else {
        dy_sym = -1;
    }

    dx = dx_sym*dx;
    dy = dy_sym*dy;

    dx_x2 = dx*2;
    dy_x2 = dy*2;

    if (dx >= dy) {
        di = dy_x2 - dx;
        while (x0 != x1) {

            pixel(x0, y0, color);
            x0 += dx_sym;
            if (di<0) {
                di += dy_x2;
            } else {
                di += dy_x2 - dx_x2;
                y0 += dy_sym;
            }
        }
        pixel(x0, y0, color);
    } else {
        di = dx_x2 - dy;
        while (y0 != y1) {
            pixel(x0, y0, color);
            y0 += dy_sym;
            if (di < 0) {
                di += dx_x2;
            } else {
                di += dx_x2 - dy_x2;
                x0 += dx_sym;
            }
        }
        pixel(x0, y0, color);
    }
}

// print rect
void EaEpaper::rect(int x0, int y0, int x1, int y1, int color)
{

    if (x1 > x0) line(x0,y0,x1,y0,color);
    else  line(x1,y0,x0,y0,color);

    if (y1 > y0) line(x0,y0,x0,y1,color);
    else line(x0,y1,x0,y0,color);

    if (x1 > x0) line(x0,y1,x1,y1,color);
    else  line(x1,y1,x0,y1,color);

    if (y1 > y0) line(x1,y0,x1,y1,color);
    else line(x1,y1,x1,y0,color);
}

// print filled rect
void EaEpaper::fillrect(int x0, int y0, int x1, int y1, int color)
{
    int l,c,i;
    if(x0 > x1) {
        i = x0;
        x0 = x1;
        x1 = i;
    }

    if(y0 > y1) {
        i = y0;
        y0 = y1;
        y1 = i;
    }

    for(l = x0; l<= x1; l ++) {
        for(c = y0; c<= y1; c++) {
            pixel(l,c,color);
        }
    }
}

// print circle
void EaEpaper::circle(int x0, int y0, int r, int color)
{
    int x = -r, y = 0, err = 2-2*r, e2;
    do {
        pixel(x0-x, y0+y,color);
        pixel(x0+x, y0+y,color);
        pixel(x0+x, y0-y,color);
        pixel(x0-x, y0-y,color);
        e2 = err;
        if (e2 <= y) {
            err += ++y*2+1;
            if (-x == y && e2 <= x) e2 = 0;
        }
        if (e2 > x) err += ++x*2+1;
    } while (x <= 0);

}

// print filled circle
void EaEpaper::fillcircle(int x0, int y0, int r, int color)
{
    int x = -r, y = 0, err = 2-2*r, e2;
    do {
        line(x0-x, y0-y, x0-x, y0+y, color);
        line(x0+x, y0-y, x0+x, y0+y, color);
        e2 = err;
        if (e2 <= y) {
            err += ++y*2+1;
            if (-x == y && e2 <= x) e2 = 0;
        }
        if (e2 > x) err += ++x*2+1;
    } while (x <= 0);
}

// set drawing mode
void EaEpaper::setmode(int mode)
{
    draw_mode = mode;
}

// set cursor position
void EaEpaper::locate(int x, int y)
{
    char_x = x;
    char_y = y;
}

// calc char columns
int EaEpaper::columns()
{
    return width() / font[1];
}

// calc char rows
int EaEpaper::rows()
{
    return height() / font[2];
}

// print char
int EaEpaper::_putc(int value)
{
    if (value == '\n') {    // new line
        char_x = 0;
        char_y = char_y + font[2];
        if (char_y >= height() - font[2]) {
            char_y = 0;
        }
    } else {
        character(char_x, char_y, value);
    }
    return value;
}

// paint char out of font
void EaEpaper::character(int x, int y, int c)
{
    unsigned int hor,vert,offset,bpl,j,i,b;
    unsigned char* zeichen;
    unsigned char z,w;

    if ((c < 31) || (c > 127)) return;   // test char range

    // read font parameter from start of array
    offset = font[0];                    // bytes / char
    hor = font[1];                       // get hor size of font
    vert = font[2];                      // get vert size of font
    bpl = font[3];                       // bytes per line

    if (char_x + hor > width()) {
        char_x = 0;
        char_y = char_y + vert;
        if (char_y >= height() - font[2]) {
            char_y = 0;
        }
    }

    zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
    w = zeichen[0];                          // width of actual char
    // construct the char into the buffer
    for (j=0; j<vert; j++) {  //  vert line
        for (i=0; i<hor; i++) {   //  horz line
            z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
            b = 1 << (j & 0x07);
            if (( z & b ) == 0x00) {
                pixel(x+i,y+j,0);
            } else {
                pixel(x+i,y+j,1);
            }

        }
    }

    char_x += w;
}

// set actual font
void EaEpaper::set_font(unsigned char* f)
{
    font = f;
}

void EaEpaper::print_bm(Bitmap bm, int x, int y)
{
    int h,v,b;
    char d;

    for(v=0; v < bm.ySize; v++) {   // lines
        for(h=0; h < bm.xSize; h++) { // pixel
            if(h + x > width()) break;
            if(v + y > height()) break;
            d = bm.data[bm.Byte_in_Line * v + ((h & 0xF8) >> 3)];
            b = 0x80 >> (h & 0x07);
            if((d & b) == 0) {
                pixel(x+h,y+v,0);
            } else {
                pixel(x+h,y+v,1);
            }
        }
    }

}