#include "scope.h"

#include <QPainter>
#include <iostream>
#include <stdio.h>

using namespace std;

#define LEN 128


Scope::Scope(QWidget *parent) {
  dataAquired=0;

  updateTimer = new QTimer(this);
  updateTimer->start(250);
  connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData()));
}


Scope::~Scope() {
  delete [] data;
}


void Scope::paintEvent(QPaintEvent* event) {
  if (dataAquired) {
    QPainter painter(this);

    painter.drawLine(9,45,9,300);
    painter.drawLine(522,45,522,300);
    painter.drawLine(9,45,522,45);
    painter.drawLine(9,300,522,300);
    int i;
    for(i=0;i<LEN-1;i++) {
       painter.drawLine(4*i+10,300-(unsigned char)data[i],4*i+4+10,300-(unsigned char)data[i+1]);
    }
  }
}

void Scope::getData() {
  char c='a';
  data=new char[LEN*sizeof(char)]; 

  FILE *serial=fopen("/dev/ttyUSB0","r+");
  fwrite(&c,sizeof(char),1,serial); // ask for data
  fread(data,sizeof(char),LEN,serial); // and take them
  fclose(serial);

  dataAquired=1;
  update();
}