/*
 * Userspace program that communicates with the led_vga device driver
 * primarily through ioctls
 *
 * Stephen A. Edwards
 * Columbia University
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "vga_led.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

int vga_led_fd;

/* Read and print the segment values */
void print_segment_info() {
  vga_led_arg_t vla;
  int i;

  for (i = 0 ; i < VGA_LED_DIGITS ; i++) {
    vla.digit = i;
    if (ioctl(vga_led_fd, VGA_LED_READ_DIGIT, &vla)) {
      perror("ioctl(VGA_LED_READ_DIGIT) failed");
      return;
    }
    printf("%02x ", vla.segments);
  }
  printf("\n");
}

/* Write the contents of the array to the display */
void write_segments(const unsigned char segs[8])
{
  vga_led_arg_t vla;
  int i;
  for (i = 0 ; i < VGA_LED_DIGITS ; i++) {
    vla.digit = i;
    vla.segments = segs[i];
    if (ioctl(vga_led_fd, VGA_LED_WRITE_DIGIT, &vla)) {
      perror("ioctl(VGA_LED_WRITE_DIGIT) failed");
      return;
    }
  }
}




struct coordinate
{
     //float a[140];
     int a[140];
}y;

struct coordinate getY(int *CtrlP)
{
     int rad=24;  //Max Radius
     int conP=17;   //Number of control points
     int b=conP-3;   //number of b splines
     int scale=5;
     //int angle=20;    //the angle to high light
     float y_control[17]={1};
     int x_control[17]={10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90};
     //int seed=7;
     //float theta=0.8;  //  0.8 pi to high light
     //float ecc=0.1;
     //float x[140]={0};
     float y0[140]={0};
    // float base[4]={0};     //base function coefficients

     //float x_elli[140][97]={0};
     //float y_elli[140][97]={0};

     //ecc=0.1*ceil(100/rad);
     //printf("ecc is %f\n", ecc);

     int i=0;
     int j=0;
     //int z=0;
     //srand(seed);
     for(i;i<17;i++){
         y_control[i]=CtrlP[i];
         printf("this is %d",i);
         printf(" of y control value %f\n",y_control[i]);
     }

     //float u=0;
     int index;
     int ytemi;
     float ytemf;
    //float t0=3.14*0.5;
    // float delta_t0=3.14/48;

     float base0[10]={0.1667,0.1171,0.0785,0.0495,0.0286,0.0147,0.0062,0.0018,0.0002,0.0000};  //base function coefficient
     float base1[10]={0.6667,0.6550,0.6229,0.5742,0.5133,0.4441,0.3708,0.2975,0.2282,0.1672};  //base function coefficient
     float base2[10]={0.1667,0.2276,0.2968,0.3701,0.4435,0.5127,0.5737,0.6225,0.6548,0.6667};
     float base3[10]={0.0000,0.0002,0.0018,0.0062,0.0146,0.0285,0.0492,0.0782,0.1167,0.1662};







     for(i=0; i<14; i++){
        // u=0;
         for(j=0;j<10;j++){
             //base[0]=((1-u)*(1-u)*(1-u))/6;
             //base[1]=(3*u*u*u-6*u*u+4)/6;
             //base[2]=(-3*u*u*u+3*u*u+3*u+1)/6;
             //base[3]=(u*u*u)/6;
             index=10*i+j;
             //x[index]=base[0]*x_control[i]+base[1]*x_control[i+1]+base[2]*x_control[i+2]+base[3]*x_control[i+3];
             y0[index]=base0[j]*y_control[i]+base1[j]*y_control[i+1]+base2[j]*y_control[i+2]+base3[j]*y_control[i+3];
             ytemf=scale*y0[index];
             ytemi=scale*y0[index];
             if (ytemf-ytemi>=0.5){
                 y.a[index]=ytemi+1;
            }else{
                 y.a[index]=ytemi;
            }
             //printf("*****************x %d ",index);
             //printf("is %f    ",x[index]);
             printf("y %d ",index);
             printf("is %f*****************  ",y0[index]);
             printf("y_int is %d*****************\n",y.a[index]);

             //t0=3.14*0.5;
             //for(z=0;z<97;z++){
               //  x_elli[index][z]=ecc*y[index]*cos(t0)+x[index];
                 //y_elli[index][z]=y[index]*sin(t0);
                 //t0=t0+delta_t0;

             //printf("elli number %d ",index);
             //printf("the %d point     ",z);
             //printf("x is %f    ",x_elli[index][z]);
             //printf("y is %f\n",y_elli[index][z]);
             //}
             //u=u+0.111;
         }
     }
    //y.a[]=;
    return y;
}




int main()
{
  vga_led_arg_t vla;
  //int i;
  int j;
  int k;
  int mes_1;
  int mes_2;
  int index;
  static const char filename[] = "/dev/vga_led";

  int ctrlP[17]={1};
  int seed=0;
  int rad=24;
  int i=0;
 // int forever=0;

  for(;;){

      seed=(seed+1)%10;

     srand(seed);
     for (i=0;i<17;i++){
         ctrlP[i]=rand()%rad+1;
     }

     y=getY(&ctrlP);

  //static unsigned char message[8] = { 0x39, 0x6D, 0x79, 0x79,
//				      0x66, 0x7F, 0x66, 0x3F };
  unsigned char message0[20][8];
  unsigned char message[8];

  index=0;


  for (mes_1=0;mes_1<20;mes_1++){
    for (mes_2=0;mes_2<8;mes_2++){
        if(mes_2==0){
            message0[mes_1][mes_2]=mes_1;
            //printf("this is message offset %d ",mes_1);
            //printf("**********************************************\n");
        }else{
            message0[mes_1][mes_2]=y.a[index];
            index=index+1;
            //printf("this is message  %d ",mes_2);
            //printf("the content is %d \n",message[mes_1][mes_2]);
        }
    }
  }

  printf("VGA LED Userspace program started\n");

  if ( (vga_led_fd = open(filename, O_RDWR)) == -1) {
    fprintf(stderr, "could not open %s\n", filename);
    return -1;
  }





  for(j=0;j<20;j++){

      for(k=0;k<8;k++){
          message[k]=message0[j][k];
      }

  write_segments(message);




  }
  }

  printf("VGA LED Userspace program terminating\n");
  return 0;
}
