Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I access the display buffer? #10

Open
NonRidiculousAdjective opened this issue Apr 7, 2023 · 4 comments
Open

How do I access the display buffer? #10

NonRidiculousAdjective opened this issue Apr 7, 2023 · 4 comments

Comments

@NonRidiculousAdjective
Copy link

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
Copy link
Owner

daschr commented Apr 8, 2023

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;
}

@NonRidiculousAdjective
Copy link
Author

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?

@NonRidiculousAdjective
Copy link
Author

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

@daschr
Copy link
Owner

daschr commented May 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants