📄 illum.c
字号:
*/}/* * R e a d L i n e * * Read one complete record from the CASIO * The illuminator version */voidReadLine (){ int i, tot = 0; /* * Read the CASIO record header */ if (debugmode > 2) fprintf (dbg, "calling readheader\n");/* grab the header */ ReadHeader (); DisplayStatus ();/* based on the type read the rest of the data */ switch (ourdata.type) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: ReadGenData (); break; case 9: case 10: case 11: ReadExpData (); break; case 12: case 13: case 14: ReadSchData (); break; case 15: ReadRemData (); break; default: ReadUnknown (); break; } if (debugmode > 2) fprintf (dbg, "\nDone ReadHeader\n"); if (tflag) { fprintf (data, "\n++********************************************++*\n");/* based on the type read the rest of the data */ switch (ourdata.type) { case 0: case 1: case 2: disptel1 (&ourdata); break; case 3: case 4: case 5: dispmemo (&ourdata); break; case 6: case 7: case 8: disptodo (&ourdata); break; case 9: case 10: case 11: dispexp (&ourdata); break; case 12: case 13: case 14: dispsched (&ourdata); break; case 15: disprem (&ourdata); break; default: printf ("\n dont know how to format-print"); dispUnknown (&ourdata); break; } fprintf (data, "\n++********************************************++*\n"); }}/* * W r i t e L i n e * * Write one complete record from * the file to the CASIO */int WriteLine (FILE * infile){ int i,theEnd=0; if (fread (&MHeader, sizeof (MHeader), 1, casiofile) != 1) { Stopped=1; theEnd=1; printf("\n The end \n"); } if (!Stopped&& (debugmode > 2)) { fprintf (dbg, "\nfileread: Mheader.type=%d length=%d\n",MHeader.type,MHeader.nbytes); }if (!Stopped&&(MHeader.ourhead == 0x8a)){ourdata.type=MHeader.type;ourdata.length=MHeader.nbytes;} else {if (!theEnd)printf("\n 1: input file appears corrupt. If you are sure there is nothing wrong contact the author\n"); if (debugmode > 2) fprintf (dbg, "\nfileread: 1\n"); Stopped=1;}/* read the data next */ if (!Stopped&&fread (&ourdata.data, ourdata.length, 1, casiofile) != 1) { printf("\n 2:input file appears corrupt. If you are sure there is nothing wrong contact the author\n"); if (debugmode > 2) fprintf (dbg, "\nfileread: 2\n"); Stopped=1; }/* read the checksum next */ if (!Stopped&&fread (&ourdata.cksum, sizeof (byte), 1, casiofile) != 1) { printf("\n 3: input file appears corrupt. If you are sure there is nothing wrong contact the author\n"); if (debugmode > 2) fprintf (dbg, "\nfileread: 3\n"); Stopped=1; }/** send the record to the Casio*/ WriteBuffer (&ourdata); /* * Wait for replyprintf("\nWaiting for ACK\n"); */ i = ReadByte (28, 0, 0);/* the only thing we expect back from the illuminator isan IACK; but hey who knows? */ switch (i) { case NACK: fprintf (stderr, "\nTransmission error, stopped\n"); Stopped = 1; WriteByte (STOP); break; case ACK: case IACK: fprintf (dbg, "\nAck received\n"); return; case STOP: case XON: case XOFF: return; default: fprintf (stderr, "0x%02x\n", i); return; }}/* * W r i t e B u f f e r * * Write the data in the CASIO Write Buffer * to the CASIO */voidWriteBuffer (casiorec *ourdata){int i;int j=200;byte d;if (!firstAck){getFirstAck(ourdata);}/* send the header */d=0x8a;fprintf(dbg,"\nsending header\n");/*fprintf(stderr,"\nsending header\n");*/WriteByte (d);/* write the type */fprintf(dbg,"\nsending type\n");/*fprintf(stderr,"\nsending type\n");*/WriteByte (ourdata->type);fprintf(dbg,"\nsending data\n");/*fprintf(stderr,"\nsending data\n");*//* write the data */ for (i = 0; i < ourdata->length ; i++) { WriteByte ( ourdata->data[i]); }fprintf(dbg,"\nsending checksum\n");/*fprintf(stderr,"\nsending checksum\n");*//* write the checksum */WriteByte (ourdata->cksum);}getFirstAck (casiorec *ourdata){int i;int j=200;byte d;int Acked=0;while(!Acked){/* send the header */d=0x8a;fprintf(dbg,"\nsending header\n");/*fprintf(stderr,"\nsending header\n");*/WriteByte (d);/* write the type */fprintf(dbg,"\nsending type\n");/*fprintf(stderr,"\nsending type\n");*/WriteByte (ourdata->type);fprintf(dbg,"\nsending data\n");/*fprintf(stderr,"\nsending data\n");*//* write the data + 0xFF */ for (i = 0; i < ourdata->length ; i++) { WriteByte ( ourdata->data[i]); }fprintf(dbg,"\nsending checksum\n");/* write the checksum */WriteByte (ourdata->cksum);/* now lets wait for that first Ack */d=ReadByte (1, 0, 0);if (debugmode > 3)fprintf (dbg, "\n%d: received %x from serial port \n",j, d);if (d == IACK){firstAck=1;Acked=1;sleep(2);}fprintf(dbg,"\n******** Resending The data *************\n");}fprintf(dbg,"\n******** got the first Ack *************\n");}/* * U p p e r C a s e * * Turn a string into all uppercase characters */voidUpperCase (char *s){ while (*s) { if (*s >= 'a' && *s <= 'z') { *s = *s - 'a' + 'A'; } s++; }}/* * R e a d B y t e * * Read a byte from the COM port */byteReadByte (int secs, int usecs, int mode){/** mode =0 ==> return for errors and timeouts* mode=1 ==> terminate for errors and timeouts*/ int retval, j; byte i; char bufin[32];#ifdef jamal /* * Check if the user pressed ESC to stop communication */ if (kbhit ()) { if (getchar () == 0x1b) { fprintf (stderr, "\n\nESC pressed, stop communication\n"); Stopped = 1; return (-1); } } #endif/* wait for up to secs,usecs for a char to arrive; otherwise timeout */ retval = waitforchar (Port, secs, usecs); if (retval == 0) { if (mode == 1) { fprintf (stderr, "\n terminating due to timeout ....\n"); terminate (); } else { return -1; } } if (retval == -1) { if (mode == 1) { fprintf (stderr, "\n terminating due to read error ....\n"); terminate (); } else { return -1; } } /* read a char and store in i */ j = read (Port, &i, 1);#ifndef __i386__ NTOHL (i);#endif if (debugmode > 2) fprintf (dbg, "\n read\n "); if (j == 0) { if (mode == 1) { if (debugmode > 2) fprintf (dbg, "\nEOF encountered\n"); terminate (); } else { return -1; } } if (j > 0) { if (debugmode > 2) fprintf (dbg, "\nsuccesful READ\n"); } if (j == -1) { if (mode == 1) { if (debugmode > 2) fprintf (dbg, "\nUnsuccessfull READ\n"); terminate (); } else { return -1; } } sprintf (bufin, "received %x\n", i & 0xff); if (debugmode > 1) fprintf (dbg, "%s\n", bufin); return (i);}/* * W r i t e B y t e * * Write a byte to the COM port */voidWriteByte (byte d){ int i; long TimeOut; TimeOut = 0; if (debugmode > 1) fprintf (dbg, "\n sending %x to serial port \n", d);/* fprintf (stdout, "\n sending %x to serial port \n", d);*/#ifndef __i386__ HTONL (i);#endif /* write the character to the serial port */ if (write (Port, &d, 1) != 1) { fprintf (stderr, "\nERROR WriteByte: couldnt write the char %c to serial port \n", d); terminate (); }#ifdef jamal if (Direction == WRITE) { usleep (4000); } #endif}/* * a t o h * * Convert an ASCII number to a hexadecimal value */byteatoh (char c){ if (c >= '0' && c <= '9') { return (c - '0'); } if (c >= 'a' && c <= 'f') { return (c - 'a' + 10); } if (c >= 'A' && c <= 'F') { return (c - 'A' + 10); } return (0);}/* * h t o a * * Convert a hexadecimal value to an ASCII number */charhtoa (byte c){ c &= 0xf; if (c <= 9) { return (c + '0'); } return (c - 10 + 'A');}/* * D i s p l a y S t a t u s */voidDisplayStatus (){ if (ourdata.type > MAXTYPE - 1) { fprintf (stderr, "\n>>>>>>Unknown type: %d <<<<<<< ", ourdata.type); fprintf (data, "\n>>>>>>Unknown type: %d <<<<<<<\n", ourdata.type); } else { if (!displayed[ourdata.type]) { fprintf (data, "\n>>>>>>%s<<<<<<<\n", rectypes[ourdata.type]); fprintf (stderr, "\n>>>>>>%s<<<<<<< \b\b\b%3d", rectypes[ourdata.type], ++displayed[ourdata.type]); } else { fprintf (stderr, " \b\b\b%3d", ++displayed[ourdata.type]); fprintf (data, "\n>>>>>>%s<<<<<<<\n", rectypes[ourdata.type]); } }}/* poll the keyboard for a character *//* idea borrowed from The Linux Journal article of Sept 95 issue 17 */intkbhit (void){ struct timeval tv; fd_set read_fd;/*do not wait at all, not even a microsecond */ tv.tv_sec = 0; tv.tv_usec = 0;/* must be done first to initilize read_fd */ FD_ZERO (&read_fd);/* makes select() ask if input is ready : * 0 is the file descriptor stdin */ FD_SET (0, &read_fd);/* the first parameter is the number of the * largest file descriptor to check + 1. */ if (select (1, &read_fd, NULL, /* NO writes */ NULL, /* NO exceptions */ &tv) == -1) return 0; /* An error occured *//* read_fd now holds a bitmap of files that are * readable. We test the entry for the standard * input (file 0). */ if (FD_ISSET (0, &read_fd)) /* character pending on stdin */ return 1;/* no charcaters were pending */ return 0;}static voidsig_catch (int signo){ fprintf (stderr, "\nsignal caught %d\n", signo); fprintf (dbg, "\nsignal caught %d\n", signo); /*inform */ fprintf (stderr, "\nstopping contact with casio \n\n"); /* should also reset the serial port being used by casio and close all files */ terminate ();}inttty_reset (int fd) /* restore terminal's mode */{ if (fd == Port) { if (tcsetattr (fd, TCSAFLUSH, &oldterm) < 0) fprintf (stderr, "\nFailed to reset serial port\n"); return (-1); } else if (fd == STDIN_FILENO) { if (tcsetattr (fd, TCSAFLUSH, &save_stdin) < 0) fprintf (stderr, "\nFailed to reset stdin\n"); return (-1); } return (0);}/* the terminator */voidterminate (){ blkflg |= O_NONBLOCK; if (fcntl (Port, F_SETFL, blkflg) < 0) { fprintf (stderr, "\nexiting ..\n"); exit (-1); } if (debugmode > 1) fprintf (dbg, "\nterminate: writting stop to port\n"); Stopped = 1;printf("\n\n*** Data transfer over: Please press ESC on the Casio\n");sleep(30); /* reset terminals */ if (debugmode > 1) fprintf (dbg, "\n reseting stdin\n"); tty_reset (STDIN_FILENO); if (debugmode > 1) fprintf (dbg, "\n reseting port\n"); tty_reset (Port); /* close all open files */ if (debugmode > 1) fprintf (dbg, "\n closing casiofile\n"); fclose (casiofile); if (debugmode > 1) fprintf (dbg, "\n closing Port\n"); close (Port); if (debugmode > 1) fprintf (dbg, "\n closing data file\n"); fclose (data); if (debugmode > 1) fprintf (dbg, "\n closing debug file\n"); fclose (dbg); exit (0);}/* borrowed from W. Richard Stevens book Advanced Programming in the Unix Environment */inttty_cbreak (int fd) /* put terminal into a cbreak mode */{ struct termios buf; if (fd == STDIN_FILENO) { if (tcgetattr (fd, &save_stdin) < 0) return (-1); } buf = save_stdin; /* structure copy */ buf.c_lflag &= ~(ECHO | ICANON); /* echo off, canonical mode off */ buf.c_cc[VMIN] = 1; /* Case B: 1 byte at a time, no timer */ buf.c_cc[VTIME] = 0; if (tcsetattr (fd, TCSAFLUSH, &buf) < 0) return (-1); return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -