#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
//#include <system.h>

//#define LCDFCN
//#define PTRFCN
#define SPEAKER "/dev/squarewave0"
#define BEEP_LENGTH 2
#define BEEP_PITCH 10
#define CARDREADER "/dev/isaseat0"

int main()
{
  FILE *card; //card reader file descriptor
  FILE *wave; //sound control file descriptor
  int endprg = 0;
  int endcurrent = 0;
  char item_input[100];
  char item_p_string[100];
  char buff[100];
  char cardbuff[1024];
  double item_price;
  double item_total;

  //Open Card Reader
  card = fopen(CARDREADER, "ro");
  if(card ==  0)
  {
    fprintf(stderr, "Cannot open LCD driver, aborting... \n");
    exit(1);
  }
 
  //Open SquareWave

  wave = fopen(SPEAKER, "ro");
  if(wave ==  0)
  {
    fprintf(stderr, "Cannot open SquareWave driver, aborting... \n");
    exit(1);
  }
 
  //Initializes the LED
  //init_port();
  //init_display();

  while(endprg != 1)
  {
    item_total = 0; //reset subtotal for new purchase
    endcurrent = 0;
    while(endcurrent != 1)
    {
      system("./userlcd \"Item #: \"");
      scanf("%s", item_input);
      system("echo 200000 550 > /dev/squarewave0");
      if(strcmp(item_input, "TOTAL") == 0) //Calculate Total
      {
      	endcurrent = 1;
      	sprintf(buff, "./preset \"Total: %.2f\n\"", item_total);
        system(buff);
      	sprintf(buff, "./userlcd \"Sum:%.2f\"", item_total);
        system(buff);
        sleep(2);
      	fprintf(wave, "100 10");
      	sprintf(buff, "./userlcd \"Swipe CD\"");
        system(buff);
      	//fscanf(card, "%s", credit_output);
      	//LCDFCN("Card#: %s", credit_output);
      	//printf("Card#: %s", credit_output); 
        fgets(cardbuff, 1024, card);
        fgets(cardbuff + strlen(cardbuff) - 1, 1024, card);
        fgets(cardbuff + strlen(cardbuff) - 1, 1024, card);
        printf("%s", cardbuff); 
//        
// Credit card validation sequence goes here!
//  
  	sleep(1);
        system("./userlcd \"Approved\"");
        system("echo 200000 1000 > /dev/squarewave0");
	sleep(1);
        system("./userlcd \"Thank U!\"");
        system("./preset \"Approved\"");
	system("./preset \"Thank you!\"");
        system("./preset \" \"");
        system("./preset \" \"");
	system("./preset \" \"");
      }
      else if(strcmp(item_input, "QUIT") == 0) //Exit Program
      {
        endcurrent = 1;
        endprg = 1;
      }
      else //Normal Item & Price (Sales) process
      {
      	sprintf(buff, "./userlcd \"Price:\"");
        system(buff);
      	scanf("%s", item_p_string);
        system("echo 200000 1000 > /dev/squarewave0");
        item_price = atof(item_p_string);
      	//sprintf(buff, "./userlcd \"%s\n\"", item_p_string);
        sprintf(buff, "./userlcd \'%s $%.2f\'", item_input, item_price);
        system(buff);
        sleep(1);
      	//item_price = atof(item_p_string);
      	sprintf(buff, "./preset \"Item #: %s\n\"", item_input);
        system(buff);
      	sprintf(buff, "./preset \"   Price: %.2f\n\"", item_price);
        system(buff);
      	item_total = item_total + item_price;
      }
    }
  }     
  //Close Card Reader
  if(fclose(card) < 0)
  {
    fprintf(stderr, "Cannot close device (Card Reader) driver, aborting... \n");
    exit(1);
  }
  
  //Close SquareWave
  if(fclose(wave) < 0)
  {
    fprintf(stderr, "Cannot close device (SquareWave) driver, aborting... \n");
    exit(1);
  }
  return 0;
}
