daschr/pico-ssd1306

How do I access the display buffer?

NonRidiculousAdjective opened this issue · 4 comments

Hey,
is there a way to directly access the data stored in the framebuffer that is created in the ssd1306_init()?
I tried printf("%d", *oled.buffer);, but it prints zeros for me.

daschr commented

Well you can access the buffer directly. See https://github.com/daschr/pico-ssd1306/blob/main/ssd1306.c#L143 to get an idea how to access a pixel. The buffer is horizontally line by line encoded where (0,0) is the upper left corner.
Also, one byte encodes 8 pixels where the most significant bit represents the bigger y.
F.e: pixel (8,56) is encoded at byte=buffer[8 + (56/8)] and then (byte>>(y%8))&1.

You could use something like this:

uint8_t ssd1306_get_pixel(ssd1306_t *p, uint32_t x, uint32_t y){
    const uint8_t p = (p->buffer[x+p->width*(y>>3)]>>(y&0x7))&1;
    return p;
}

Hey, sorry for the late reply, so if I understand it correctly, you store the pixels in an array of uint8_t, where each 1 or 0 is a pixel except the most significant digit, which stores the y value?

Do you like reserve the most significant nibble for all 64 lines of y ?

daschr commented

I think my explanation was a bad.
The RAM stores the display data in pages of 128 x 1byte. Each bytes encodes the 8 vertically neighbouring pixels.
See https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf page 25.
So if you want to access the pixel at (x,y), you first calculate the page (y/8, since each byte encodes 8 vertically pixels) and multiplicate it by 128 (size of a page). Then you add x to for getting the position of the byte in the array. Now, you have to access the y%8 bit of the byte, which encodes the pixel.