Sylaina / oled-display

Library for OLED-displays runs on AVR
GNU General Public License v3.0
121 stars 29 forks source link

Incorrect drawing when display's height is different than 64 #3

Closed heikete closed 5 years ago

heikete commented 5 years ago

When display has only 32 pixels high the primitives are torn apart. The reason for this is calculation in this function:

void lcd_drawPixel(uint8_t x, uint8_t y, uint8_t color){
    if( x > DISPLAY_WIDTH-1 || y > (DISPLAY_HEIGHT-1)) return; // out of Display
    if( color == WHITE){
        displayBuffer[(y / (DISPLAY_HEIGHT/8))][x] |= (1 << (y % (DISPLAY_HEIGHT/8)));
    } else {
        displayBuffer[(y / (DISPLAY_HEIGHT/8))][x] &= ~(1 << (y % (DISPLAY_HEIGHT/8)));
    }
}

The y offset has nothing to do with DISPLAY_HEIGHT it has to be divided by 8, because the memory is organized this way even in infinitely large displays of this kind.

So the correct function looks like this:

void lcd_drawPixel(uint8_t x, uint8_t y, uint8_t color){
    if( x > DISPLAY_WIDTH-1 || y > (DISPLAY_HEIGHT-1)) return; // out of Display
    if( color == WHITE){
        displayBuffer[(y / 8)][x] |= (1 << (y % 8));
    } else {
        displayBuffer[(y / 8)][x] &= ~(1 << (y % 8));
    }
}

// Tested with 128x32 oled display.

Sylaina commented 5 years ago

fixed, thanks for info. This calculation was from an old display-size check and it was obsolet by adding if-statement at the begin of drawPixel