Changeset 36 in projects


Ignore:
Timestamp:
Mar 25, 2009, 6:27:53 PM (15 years ago)
Author:
sven
Message:

New version that feeds in punch cards automatically, with verbose
debugging output.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • punch-card/driver/nixdorf-0377.01/reader.c

    r34 r36  
    7373}
    7474
    75 void simple_reading_program() {
    76         // this will never touch the magnet...
     75void run_real_program() {
    7776        long long abs_time = mtime(0);
    78         for(;;) {
    79                 fflush(NULL);
    80                 unsigned char data;
    81                 unsigned char status;
    82                 fd_set rfds;
    83                 FD_ZERO (&rfds);
    84                 FD_SET (parport_fd, &rfds);
    85                 //printf("Waiting for interrupt...\n");
    86                 if (!select (parport_fd + 1, &rfds, NULL, NULL, NULL)) {
    87                         /* Caught a signal? */
    88                         printf("Caught a signal...");
    89                         continue;
    90                 }
    91                        
    92                 // we've got that interrupt
    93                 // fetch current register values
    94                 if(ioctl(parport_fd, PPRDATA, &data)) {
    95                         perror("Interrupt data readout");
    96                         exit(1);
    97                 }
    98                 if(ioctl(parport_fd, PPRSTATUS, &status)) {
    99                         perror("Status data readout");
    100                         exit(1);
    101                 }
    102                
    103                 // display status:
    104                 printf("%i: ",(int)mtime(abs_time));
    105                 if(status & PARPORT_STATUS_SELECT) {
    106                                        
    107                 printf("DATA: ");
    108                 int pos;
    109                 for(pos=0; pos<8; print_bit(NULL, pos++, data));
    110                 printf("| STATUS: ");
    111                 print_bit("ERROR", 3, status);
    112                 print_bit("SEL", 4, status);
    113                 print_bit("PE", 5, status);
    114                 print_bit("ACK", 6, status);
    115                 print_bit("BUSY", 7, status);
    116                 printf("\n");
    117                
    118                 } else {
    119                         printf("no card.\n");
    120                 }
    121                
    122                 // clear the interrupt
     77        for(;;) { // for cards
     78                printf("\n------- Starting for a new card. ------\n", mtime(abs_time));
     79                printf("[%i abs] Starting magnet...\n", mtime(abs_time));
     80                long long card_time = mtime(0);
     81                set_magnet(MAGNET_ON);
     82               
     83                // clear the interrupt counter:
    12384                unsigned char irqc;
    12485                ioctl(parport_fd, PPCLRIRQ, &irqc);
    125                 if(irqc > 1)
    126                         printf("MISSED %d INTTERUPT%s!!!!!\n", irqc-1, irqc == 2 ? "S" : "");
    127         }
     86                //usleep(1500*1000); // 1,5 Sec -- Magnete sind Traege!
     87
     88                magnet_status_t magnet_status = MAGNET_ON;
     89                int col_counter = 0; // counts the columns that have been read in
     90                int missed_counter=0;// how many columns=intterupts I have missed
     91                int had_card = 0;    // switch whether we have had a card
     92                int can_finish = 0;  // true if we can finish the card loop.
     93                for(; !can_finish;) {
     94                        fflush(NULL);
     95                        unsigned char data;
     96                        unsigned char status;
     97
     98                        struct timeval select_timeout;
     99                        select_timeout.tv_sec = 2;
     100                        select_timeout.tv_usec = 0;
     101                       
     102                        fd_set rfds;
     103                        FD_ZERO (&rfds);
     104                        FD_SET (parport_fd, &rfds);
     105                        //printf("Waiting for interrupt...\n");
     106                        if (!select (parport_fd + 1, &rfds, NULL, NULL, &select_timeout)) {
     107                                // didn't fetch an interrupt
     108                                printf("[%i cyc] Could not fetch any interrupt!\n", mtime(card_time));
     109                                printf("Will break scanning this card.\n");
     110                                break;
     111                        }
     112                       
     113                        // control the magnet...
     114                        if(magnet_status == MAGNET_ON && mtime(card_time) > 500) {
     115                                printf("[%i cyc] Turning magnet off\n",
     116                                        mtime(card_time));
     117                                set_magnet(MAGNET_OFF);
     118                                magnet_status = MAGNET_OFF;
     119                        }
     120                       
     121                        // we've got that interrupt
     122                        // fetch current STATUS register values
     123                        if(ioctl(parport_fd, PPRSTATUS, &status)) {
     124                                perror("Status data readout");
     125                                exit(1);
     126                        }
     127               
     128                        // display status:
     129                        printf("[%i cyc] ",(int)mtime(card_time));
     130                        if(status & PARPORT_STATUS_SELECT) {
     131                                // we've got a card, so read next column
     132                                col_counter++;
     133                                // and remind that we actually have at least read one
     134                                // column from a card
     135                                had_card = 1;
     136                       
     137                                if(ioctl(parport_fd, PPRDATA, &data)) {
     138                                        perror("Interrupt data readout");
     139                                        exit(1);
     140                                }
     141                       
     142                                printf("%0i. column: ", col_counter);
     143                                int pos;
     144                                for(pos=0; pos<8; print_bit(NULL, pos++, data));
     145                                printf("| status: ");
     146                                print_bit("ERROR", 3, status);
     147                                print_bit("SEL", 4, status);
     148                                print_bit("PE", 5, status);
     149                                print_bit("ACK", 6, status);
     150                                print_bit("BUSY", 7, status);
     151                                printf("\n");
     152                        } else if(!had_card) {
     153                                printf("No card yet.\n");
     154                                if(mtime(card_time) > 900) {
     155                                        printf("[%i cyc] Being bored. Canceling card scanning\n", mtime(card_time));
     156                                        can_finish = 1;
     157                                }
     158                        } else {
     159                                printf("Card gone! Finish successfullly\n");
     160                                can_finish = 1;
     161                        }
     162                       
     163                        if(col_counter > 90) {
     164                                printf("[%i cyc] Aborting! Too many cols!", mtime(card_time));
     165                                can_finish = 1;
     166                        }
     167               
     168                        // clear the interrupt
     169                        unsigned char irqc;
     170                        ioctl(parport_fd, PPCLRIRQ, &irqc);
     171                        if(irqc > 1) {
     172                                missed_counter += irqc-1;
     173                                printf("[%i cyc] MISSED %d INTTERUPT%s!\n",
     174                                        mtime(card_time), irqc-1, irqc == 2 ? "S" : "");
     175                        }
     176                } // for columns
     177               
     178                printf("[%i cyc] Disabling magnet...\n", mtime(card_time));
     179                set_magnet(MAGNET_OFF);
     180                printf("+------------------------------------------+\n");
     181                printf("|                CARD REPORT               |\n");
     182                printf("| Card read in time:  %i ms\n", mtime(card_time));
     183                printf("| Columns read in:    %0i\n", col_counter);
     184                printf("| Interrupts missed:  %0i\n", missed_counter);
     185                printf("| Sum:                %0i\n", col_counter+missed_counter);
     186                printf("| Diff to 80 cols:    %0i\n", 80-(col_counter+missed_counter));
     187                if(col_counter == 80)
     188                        printf("|       C O N G R A T U L A T I O N S      |\n");
     189                else if(col_counter+missed_counter == 80)
     190                        printf("|        YOU ARE SIMPLY TOO SLOW!!!        |\n");
     191                else if(col_counter+missed_counter < 80)
     192                        printf("|       THAT'S THE READER'S FAULT!         |\n");
     193                else if(col_counter > 80)
     194                        printf("|       READ IN *MORE* THAN ONE CARD!      |\n");
     195                else
     196                        printf("|           PRETTY MYSTERIOUS.             |\n");
     197                printf("+------------------------------------------+\n");
     198                sleep(2);
     199        } // for all the cards
    128200} // function
    129201
     
    189261        // now fetch interrupts and give out data at these times.
    190262        // never use the magnet.
    191         simple_reading_program();
    192        
     263        //simple_reading_program();
     264       
     265        run_real_program();
    193266
    194267        printf("Finished. Turning magnet off and quitting.\n");
Note: See TracChangeset for help on using the changeset viewer.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License