
#include "libOLED/displayXY.H"

const unsigned MARGIN = 1;
const unsigned BOTTOM_MARGIN = 17;

displayXY_t ::displayXY_t(display_t &disp,
                          int xmin, int xmax, int ymin, int ymax) : display(disp), x_min(xmin), x_max(xmax), y_min(ymin), y_max(ymax),
                                                                    left_margin(MARGIN), right_margin(display.width() - MARGIN), plot_width(right_margin - left_margin),
                                                                    top_margin(MARGIN), bottom_margin(display.height() - BOTTOM_MARGIN), plot_height(bottom_margin - top_margin), next_persist(0)
{
    for (uint16_t i = 0; i < PERSIST;  i++)
    {
        persist_x[i] = left_margin;
        persist_y[i] = bottom_margin;
    }
};

void displayXY_t ::drawAxes()
{

    display.drawLine(left_margin, bottom_margin, right_margin, bottom_margin,
                     WHITE, 0b10);
    display.drawLine(left_margin, bottom_margin, left_margin, top_margin, WHITE, 0b10);
}

void displayXY_t ::plotPoint(int x, int y, char *message,char * message_lower, char * message_upper)
{
    int y_coord = (plot_height * (y - y_min)) / (y_max - y_min);
    int x_coord = (plot_width * (x - x_min)) / (x_max - x_min);
    display.drawLine(left_margin, y_coord, right_margin, y_coord, WHITE, 0b111000);
    display.drawLine(x_coord, top_margin, x_coord, bottom_margin, WHITE, 0b111000);
    persist_x[next_persist] = x_coord;
    persist_y[next_persist] = y_coord;
    next_persist++;
    if (next_persist > PERSIST)
        next_persist = 0;
    for (uint16_t i = 0; i < PERSIST; i++)
    {
        display.drawPixel(persist_x[i], persist_y[i], WHITE);
    }

    if(message)
    {
    display.gotoxy(0, 48);
    display.printString(large_font, message, 0, OVERLAY);
    }
    if (message_upper)
    {
    display.gotoxy(56,48);
    display.printString(small_font, message_upper, 0, OVERLAY);
    }
    if (message_lower)
    {
    display.gotoxy(56,56);
    display.printString(small_font, message_lower, 0, OVERLAY);
    }
    
}
