/* This is a program for stock market which operates following these operations:
1.Register company: In this case it will be register three companies and store these informations inside a file.
2.Show Company: In this case it will show the details of those three companies which are already stored inside a file.
Company Details:
1. Name of company
2. Location of Company
3.Annual Income of Company
Note: Name and location of company will be display on CLCD screen where as Annual income of company will be display on 7 Segment.
*/
#define SIZE 3
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#define NODE_EXP "/dev/fpga_char_lcd" /* Device name */
#define LCD_ON 1
#define LCD_CLEAR 2
#define LCD_SET_HOME 3
#define LCD_CURSOR_BLINK 4
#define LCD_DATA_WRITE 5
#define LCD_OFF 6
#define NODE_EXP1 "/dev/7seg_led" /* Device name */
#define SEG_7_LED_ON 1
#define SEG_7_LED_WRITE 2
#define SEG_7_LED_CLEAR 3
#define SEG_7_LED_OFF 4
typedef struct
{
int turn_over;
char name[20];
char address[20];
//char address[20];
} company;
int main (int argc,char *argv[])
{
int fd= 0;
int ctr,n;
int exp_dev= 0; /* exp_dev is file descriptor */
int ret,res= 0; /* return value of system call */
int exp_dev1= 0; /* exp_dev1 is file descriptor */
int ret1,res1= 0; /* return value of system call */
size_t sw,sp,sj,choice;
char dummy[40];
company comp[SIZE], *sptr;
sptr = comp; // Eroor may be possible at this point //
//char p[]="This is my first programme of file handling in mtlc";
while(1)
{
printf("Press 1 for Register new company Details:\n");
printf("Press 2 to show company Details:\n");
printf("Enter your choice :\n");
scanf("%d",&choice);
if(argc!=2)
{
printf("\n ERROR: Usage:: File_w <filename> \n");
return 1;
}
/* ******************************Open Device************************** */
exp_dev = open(NODE_EXP, O_RDWR);
if (exp_dev < 0)
{
fprintf(stderr, "Open error: %s\n", NODE_EXP);
return 1;
}
/* ****************************Open Device************************** */
exp_dev1 = open(NODE_EXP1, O_RDWR);
if (exp_dev1 < 0){
fprintf(stderr, "Open error: %s\n", NODE_EXP1); // may be fault //
return 1;
}
/* open as blocking mode */
switch(choice)
{
/* ****************Application : Register Company ********************** */
case 1:
fd = open(argv[1], O_WRONLY | O_CREAT);
if (fd < 0){
printf("error opening");
return 1;
}
printf("File is ready to open");
for(ctr=0; ctr<SIZE; ctr++)
{
printf("Welcome !Register your company details:\n ");
printf("Enter name of company :\n");
scanf("%s",&comp[ctr].name);
printf("Enter Address Details of company :\n");
scanf("%s",&comp[ctr].address);
printf("Annual turn_over of company :\n");
scanf("%x",&comp[ctr].turn_over);
}
sw=write(fd,sptr,sizeof(comp)*3);
if(sw<=0)
{
printf("\n Zero bytes has been written ....");
return 1;
}
printf("file name is :%s\n",argv[1]);
printf("Registration has been completed \n thank u!\n Have a nice day\n");
/*for(ctr=0; ctr<SIZE; ctr++)
{
printf("Registration has been completed \n thank u!\n Have a nice day\n");
}*/
getchar();
close(fd);
break;
/* ****************Application : Register Company ********************** */
/* ******************Application : Read Company Detail******************** */
case 2:
if (res=ioctl(exp_dev,LCD_ON, NULL) < 0 ) /* Ioctl */
{
printf("%d---> Error in switching OFF the LCD \r\n",res);
getchar();
close(exp_dev);
return 1;
}
printf("\n LCD_ON: Hit A Key To Continue...");
getchar();
if (res=ioctl(exp_dev,LCD_CLEAR, NULL) < 0 )
{
printf("%d---> Error in Clearing the LCD \r\n",res);
getchar();
close(exp_dev);
return 1;
}
if (res=ioctl(exp_dev,LCD_SET_HOME, NULL) < 0 )
{
printf("Error in Set home the LCD \r\n");
getchar();
close(exp_dev);
return 1;
}
if (ioctl(exp_dev,LCD_CURSOR_BLINK , NULL) < 0 )
{
printf("Error inCursor blink in LCD \r\n");
getchar();
close(exp_dev);
return 1;
}
/* Open file for read purpose */
/* ****************************Function for 7 Segment ********************** */
if (res=ioctl(exp_dev,SEG_7_LED_ON, NULL) < 0 ) /* Ioctl */
{
printf("%d---> Error in switching OFF the LED \r\n",res);
getchar();
close(exp_dev);
return 1;
}
/* ****************************Function for 7 Segment ********************** */
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
printf("error opening");
return 1;
}
printf("File is ready to open");
sw = read(fd,sptr,sizeof(comp)*3);
if(sw<=0)
{
printf("\n Zero bytes has been written ....");
return 1;
}
printf("file name is :%s\n",argv[1]);
for(ctr=0; ctr<SIZE; ctr++)
{
printf("\n SPTR = %u \n", sptr);
sptr++;
}
sptr=comp;
for(ctr=0; ctr<SIZE; ctr++)
{
strcpy(dummy, sptr->name);
strcat(dummy, ", ");
strcat(dummy, sptr->address);
printf("\n SPTR = %u, Dummy = %s \n", sptr, dummy);
printf("\n%d.: Details: Name: %s,\tAddress: %s,\tTurn Over: %d, %x", ctr+1, sptr->name, sptr->address, sptr->turn_over, sptr->turn_over);
if (ioctl(exp_dev,LCD_DATA_WRITE, dummy) < 0 )
{
printf("Error in switching OFF the LCD \r\n");
getchar();
close(exp_dev);
return 1;
}
if (res1=ioctl(exp_dev1,SEG_7_LED_WRITE,sptr->turn_over) < 0 )
{
printf("%d---> Error in writing to the 7-Seg LED \r\n",res1);
getchar();
close(exp_dev1);
return 1;
}
printf("\n Hit A Key for next record...");
getchar();
if(ctr<2)
{
sptr++;
}
}
getchar();
if (ioctl(exp_dev,LCD_OFF, NULL) < 0 )
{
printf("Error in switching OFF the LCD \r\n");
getchar();
close(exp_dev);
return 1;
}
if (res1=ioctl(exp_dev1,SEG_7_LED_CLEAR, NULL) < 0 )
{
printf("%d---> Error in Clearing the LED \r\n",res);
getchar();
close(exp_dev1);
return 1;
}
if (ioctl(exp_dev1,SEG_7_LED_OFF, NULL) < 0 )
{
printf("Error in switching OFF the LED \r\n");
getchar();
close(exp_dev1);
return 1;
}
close(exp_dev1);
close(exp_dev);
printf("\n Hit A Key To Stop...");
getchar();
close(fd);
break;
default :
printf("! sorry invalid request ..");
break;
printf("Press 1 for continue and press 0 for exit :...");
scanf("%d",&n);
if(n==0)
{
exit(0);
}
}
}
return 0;
}