DatanoiseTV / PicoADK-Firmware-Template

🎵 🎹 Firmware boilerplate for the RP2040 / RP2350 powered PicoADK Audio Development Boards. Build your own stand alone synthesizers! Includes all nuts and bolts (FreeRTOS, USB MIDI, Vult DSP, Hardware Plumbing, DMA, ..). https://github.com/DatanoiseTV/PicoADK-Hardware
MIT License
149 stars 17 forks source link

small bug in ssd1306_draw_line #22

Open Karijn opened 4 months ago

Karijn commented 4 months ago

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 commented 4 months ago

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