Page 392 - Programming Microcontrollers in C
P. 392

The SAVEIT() Routine     377

                   which then sends the information to this function,
                   saveit(). The data that arrives is in the form of
                   two binary strings. The first string, the name, is
                   Huffman encoded and the second string, the phone
                   number, is bcd encoded. These data are to be
                   stored in the EEPROM. A linked list that uses
                   indicies rather than pointers is used. The EEPROM
                   is broken into two fields. The first field is an
                   array of the type Entry. This array is DLEN long.
                   The next field is an array that contains the re­
                   mainder of the EEPROM. This array is an array of
                   type unsigned. It will mostly contain data that
                   has been encoded. The first three entries in the
                   array are special. The first field contains the
                   index to the next unused entry in the array. The
                   second field is the index to the start of the list
                   and the final field contains the number of entries
                   in the list.
                   ************************************************************************/
                   #include “phone.h”


                   void saveit(char *s,char *n,Epro *epro)
                   {
                       int i,j,sl,nl;
                       unsigned name[ALEN];
                       unsigned number[NLEN];


                       sl=encode(s,name,ALEN);/* encode both the name and */
                       nl=numbdup(n,number,NLEN);/* the number */
                       /* store encoded data on the end of the array */
                       if(NEXT_OPEN+sl+nl>DATAPROM)/* do not overwrite the array */
                       {
                        puts(“***buffer full***\n”);
                        return;
                       }
                       for(i=NEXT_OPEN,j=0;j<sl;i++,j++)
                        epro->data[i]=name[j]; /* save the name then the number */
   387   388   389   390   391   392   393   394   395   396   397