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

small bug in ssd1306_draw_line #22

Open
Karijn opened this issue Jul 13, 2024 · 1 comment
Open

small bug in ssd1306_draw_line #22

Karijn opened this issue Jul 13, 2024 · 1 comment

Comments

@Karijn
Copy link
Contributor

Karijn commented Jul 13, 2024

in the sample

        for(int y=0, i=1; y>=0; y+=i) {
            ssd1306_draw_line(&disp, 0, 31-y, 127, 31+y);
            ssd1306_draw_line(&disp, 0, 31+y, 127, 31-y);
            ssd1306_show(&disp);
            sleep_ms(SLEEPTIME);
            ssd1306_clear(&disp);
            if(y==32) i=-1;
        }

only one line (and a dot) is drawn!

The function should read:

void ssd1306_draw_line(ssd1306_t *p, uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
{
    if (x1 > x2)
    {
        swap(&x1, &x2);
        swap(&y1, &y2);
    }

    if (x1 == x2)
    {
        if (y1 > y2)
            swap(&y1, &y2);
        for (int32_t i = y1; i <= y2; ++i)
            ssd1306_draw_pixel(p, x1, i);
        return;
    }
    // NOTE The cast to int32_t before subtracting y2 - y1
    float m = (float)((int32_t)y2 - (int32_t)y1) / (float)(x2 - x1);

    for (int32_t i = x1; i <= x2; ++i)
    {
        float y = m * (float)(i - x1) + (float)y1;
        ssd1306_draw_pixel(p, i, (uint32_t)y);
    }
}
@DatanoiseTV
Copy link
Owner

Thank you for your contribution. Could you submit a PR?

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