Page 393 - Programming Microcontrollers in C
P. 393

378    Chapter 7  Advanced Topics

                       for(j=0;j<nl;i++,j++) /* at NEXT_OPEN  */
                        epro->data[i]=number[j];
                       epro-
                   >header[LIST_ENTRIES++].dataindex=NEXT_OPEN;
                       NEXT_OPEN+=sl+nl;


                       if(LIST_ENTRIES>=1) /* no entries in list never 0 */
                        epro->header[LIST_ENTRIES-1].next=LIST_ENTRIES;
                   }

                              Listing 7-13: The saveit() Function
                              If there is enough room in the EEPROM array to store the new
                          data, the encoded name and the encoded number are both written
                          into the array at the appropriate location. Recall that the encode
                          routines return the lengths of the encoded data. NEXT_OPEN is the
                          next unused entry in the array. After the data are written to the array,
                          the new value for NEXT_OPEN is calculated by adding the length
                          of the two encoded arrays to the old value.
                              Finally, if there is more than one entry in the array the index for
                          the new entry, LIST_ENTRIES, will be put in the next location of
                          the previous entry,  epro->header[LIST_ENTRIES-1].next.


            The printout() and the printafter() Functions
                              These two functions are almost the same. Therefore, they will be
                          discussed together. In both cases, control is returned to the calling
                          program if there are no data to be printed out. The printout()
                          routine starts at the beginning of the list and prints the entire contents
                          of the EEPROM. Prior to the printout, the contents are decoded.
                          Both decode routines return a new line character at the end of the
                          data stream along with a null character to indicate the end of the line.
                          Notice in the printout() function, the data starts at the start of
                          the list and cycles through all of the contents of the list.
                   #include “phone.h”


                   void printout(Epro *epro)
   388   389   390   391   392   393   394   395   396   397   398