#include "scope.h"

#include <QPainter>
#include <QString>

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

using namespace std;

void UI(void);

Scope::Scope(QWidget *parent) { // CONSTRUCTOR, graphic stuff
  dataAquired=0;

//  data=new int[LEN*sizeof(int)]; ;

  int i;
  for(i=0;i<LEN;i++) // buffer init
    data[i]=0;

  scale=DEF_SCALE;  // default coords: data[i] = (data[i] + shift)/scale;
  shift=DEF_SHIFT;
        
  scaleSlider = new QSlider(Qt::Horizontal,this);
  scaleSlider->setRange(1, 256);
  scaleSlider->setValue(DEF_SCALE);
  connect(scaleSlider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int)));
        
  shiftSlider = new QSlider(Qt::Horizontal,this);
  shiftSlider->setRange(0, +60000);
  shiftSlider->setValue(DEF_SHIFT);
  connect(shiftSlider, SIGNAL(valueChanged(int)), this, SLOT(setShift(int)));

  updateTimer = new QTimer(this);
  updateTimer->start(TICK);
  connect(updateTimer, SIGNAL(timeout()), this, SLOT(getData()));
        
  labels[1] = new QLabel(this);
        /*QString tempStr;
        tempStr.setNum(shift);
  labels[1]->setText("Zero level: " + tempStr);
  labels[1]->setGeometry (20, 550, 200, 20);
  labels[1]->show();
  shiftSlider->setGeometry(QRect(20,580,400,60));
  shiftSlider->show();*/


  labels[2] = new QLabel(this);
  this->UI();/*
        tempStr.setNum(scale);
  labels[2]->setText("Divisor: " + tempStr);
  labels[2]->setGeometry (20, 630, 400, 20);
  labels[2]->show();
  scaleSlider->setGeometry(QRect(20,660,400,60));
  scaleSlider->show();*/
        
}

void Scope::UI(void) {
        QString tempStr;
        tempStr.setNum(shift);
  labels[1]->setText("Zero level: " + tempStr);
  labels[1]->setGeometry (20, 550, 200, 20);
  labels[1]->show();
  shiftSlider->setGeometry(QRect(20,580,400,60));
  shiftSlider->show();

        tempStr.setNum(scale);
  labels[2]->setText("Divisor: " + tempStr);
  labels[2]->setGeometry (20, 630, 400, 20);
  labels[2]->show();
  scaleSlider->setGeometry(QRect(20,660,400,60));
  scaleSlider->show();  
}






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


void Scope::paintEvent(QPaintEvent* event) {

  QPainter painter(this);

  if (dataAquired) {
    painter.drawLine(LEFT-1,TOP,LEFT-1,TOP+HEIGHT);             //vertical bound box lines
    painter.drawLine(LEFT+WIDTH+1,TOP,LEFT+WIDTH+1,TOP+HEIGHT);

    painter.drawLine(LEFT-1,TOP,LEFT+WIDTH+1,TOP);              //horizontals bound box lines
    painter.drawLine(LEFT-1,TOP+HEIGHT,LEFT+WIDTH+1,TOP+HEIGHT);
    painter.drawLine(LEFT-1,TOP+HEIGHT/2,LEFT+WIDTH+1,TOP+HEIGHT/2);

    int i;
  
    for(i=0;i<LEN;i++) {
      data[i] = (data[i] - shift)/scale;
    }

    for(i=0;i<LEN-1;i++) {
      painter.drawLine(PIXPT_X*i+LEFT,TOP+HEIGHT/2-PIXPT_Y*data[i],PIXPT_X*(i+1)+LEFT,TOP+HEIGHT/2-data[i+1]*PIXPT_Y);
    }
  //cout << "Screen updated"<<endl;
  }
}


void Scope::getData() {
  char c='m';

  cout << "Serial transfer start."<<endl;
  FILE *serial=fopen("/dev/ttyUSB0","r+");

  if (serial==NULL) {
    cout << "Serial port error."<<endl;
  } else {
    //cout << "Serial port open."<<endl;
    
    fwrite(&c,sizeof(char),1,serial); // poprosime o data
    fread(buffer,sizeof(uint8_t),2*LEN,serial); // a berem je
                
   // cout << "Serial transfer completed."<<endl;
    fclose(serial);
                
    int i;
    for(i=0;i<LEN;i++) {
      data[i]=buffer[2*i]*256+buffer[2*i+1];
//    cout << 1*data[i] <<endl;
    }
   // cout << "Screen updating...."<<endl;
   
    dataAquired=1;
    update();
  }
}


void Scope::setScale(int val) {
        scale=val;
  UI();
}

void Scope::setShift(int val) {
        shift=val;
  UI();
}