#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

#include <vector>

class point {
public:
    point() : m_x(0), m_y(0) {}
    point(int x, int y) : m_x(x), m_y(y) {}

    int get_x() const { return m_x; }
    int get_y() const { return m_y; }

private:
    int m_x;
    int m_y;
};

std::vector<point> Graph;

int ECG[] = {-4, -3, -3, -2, 0, 3, 8, 15, 22, 27, 29, 26, 20, 13, 7, 3, 1, 0, -4, -14, -15, 42, 130, 142, 36, -20, -22, -5, 2, 4, 5, 7, 9, 13, 18, 24, 30, 37, 44, 48, 50, 50, 47, 41, 34, 27, 20, 14, 10, 7, 4, 3, 3, 2, 2, 3, 3, 3, 3, 2, 1, 0, -2, -3, -4, -4, -3, -3, -2, 0, 3, 8, 15, 22, 27, 29, 26, 20, 13, 7, 3, 1, 0, -4, -14, -15, 42, 130, 142, 36, -20, -22, -5, 2, 4, 5, 7, 9, 13, 18, 24, 30, 37, 44, 48, 50, 50, 47, 41, 34, 27, 20, 14, 10, 7, 4, 3, 3, 2, 2, 3, 3, 3, 3, 2, 1, 0, -2, -3, -4, -4, -3, -3, -2, 0, 3, 8, 15, 22, 27, 29, 26, 20, 13, 7, 3, 1, 0, -4, -14, -15, 42, 130, 142, 36, -20, -22, -5, 2, 4, 5, 7, 9, 13, 18, 24, 30, 37, 44, 48, 50, 50, 47, 41, 34, 27, 20, 14, 10, 7, 4, 3, 3, 2, 2, 3, 3, 3, 3, 2, 1, 0, -2, -3, -4};

#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

void setup() {
  Serial.begin(9600);
  tft.begin();
  tft.setRotation(1);

  int origin_x = 0;
  int origin_y = tft.height() / 2;

  for (int loop_index = origin_x; loop_index < sizeof(ECG) / sizeof(int); loop_index++)
    Graph.emplace_back(loop_index, ECG[loop_index] + 50);
}

void loop() {
  tft.fillScreen(ILI9341_BLACK);

  for (int index = 0; index < Graph.size() - 1; index++) {
    tft.drawLine(Graph[index].get_x(), Graph[index].get_y(), Graph[index + 1].get_x(), Graph[index + 1].get_y(), ILI9341_GREEN);
    delay(15);
  }
}