]> git.piffa.net Git - arduino/blob - books/beginning_c_code/Chapter10/Listings10_4thru10/Listings10_4thru10.ino
first commit
[arduino] / books / beginning_c_code / Chapter10 / Listings10_4thru10 / Listings10_4thru10.ino
1 /*
2  Purpose: To write data to EEPROM memory.
3   Dr. Purdum, Aug. 27, 2012
4  */
5 #include <EEPROM.h>
6 #define DEBUG 1                         // We want to see debug print statements
7                                         // Comment out these lines to avoid seeing print statements
8 const int MAXPEOPLE = 10;
9 struct servicePeople {                  // Structure definition for servicePeople
10   int ID;
11   char Name[20];
12   char PW[10];
13   long Phone;
14 };
15
16 union servicePeopleUnion {                      // A union definition for myUnion
17   int testID;
18   struct servicePeople testServicePeople;
19 } myUnion;
20 servicePeople myPeople[MAXPEOPLE] = {   // company data for testing
21   {0x3737, "This is a dummy","admin",5555555},  // ID of dummy needs to be non-zero!
22   {101,"Kack Lawn Service","Clowder",2345678},
23   {222,"Jane's Plants","Noah",4202513},
24   {333,"Terrys Pool Service","Billings",4301832}
25 };
26 // function declarations:
27 void DataDump(struct servicePeople temp);
28 int FindEepromTop();
29 int ReadIntFlag();
30 void ReadOneRecord(int index);
31 void WriteFirstRecord();
32
33 int loopCounter = 0;            // Number of passes to make through loop
34 int initFlag = 0;               // Has the EEPROM been initialized?
35 struct servicePeople temp;              // A temporary structure 
36
37 void setup()
38 {
39   int eepromMax;
40   int i;
41   
42   Serial.begin(9600);
43   eepromMax = FindEepromTop();          // How much EEPROM?
44 #ifdef DEBUG
45   Serial.print("EepromMax = ");
46   Serial.println(eepromMax);
47 #endif
48   initFlag = ReadIntFlag();  // Initialized?
49 #ifdef DEBUG
50   Serial.print("  flag = ");
51   Serial.println(initFlag);
52 #endif
53   for (i = 0; i < MAXPEOPLE; i++) {
54       WriteOneRecord(i);
55   }
56 }
57 /*****
58   Purpose: Find out how much EEPROM this board has.
59   Parameter list:
60     void
61   Return value:
62     int          the EEPROM size       
63     Free to use: econjack 
64 *****/
65 int FindEepromTop()
66 {
67   return E2END + 1;
68 }
69 /*****
70   Purpose: This function reads the first two bytes of EEPROM and
71           returns the integer found there.
72   Parameter list:
73     void
74   Return value:
75     int          0 if no records in EEPROM, 1 if there are
76 *****/
77 int ReadIntFlag()
78 {
79   int *ptr = &myUnion.testID;
80   *ptr = EEPROM.read(0);
81   return myUnion.testID;
82 }
83 /*****
84   Purpose: This function writes one record from the myPeople[] array
85         to EEPROM
86   Parameter list:
87     int index      The element of the myPeople[] array to write
88   Return value:
89     void
90 *****/
91 void WriteOneRecord(int index)
92 {
93   byte *b;
94   int i;
95   int offset = index * sizeof(servicePeople);
96   
97   b = (byte *) &myPeople[index];    // Going to write this record
98   for (i = 0; i < sizeof(servicePeople); i++)
99     EEPROM.write(i + offset, *b++);
100 }
101 void loop()
102 {
103   static int eepromIndex = 1;  // Assume there are records
104   loopCounter++;
105   if (initFlag > 0) {  // There are records to read
106     ReadOneRecord(eepromIndex++);
107     if (myUnion.testServicePeople.ID != 0) { // Read some real data
108        DataDump(myUnion.testServicePeople);
109     }
110   } else {
111     eepromIndex++;     // Make sure loop can end with no records
112   }
113   #ifdef DEBUG
114     Serial.println("==========");
115   #endif
116   if (eepromIndex == MAXPEOPLE) {
117        Serial.flush();
118        exit(0);
119   }
120 }
121 /*****
122   Purpose: This function reads one servicePerson record from
123         EEPROM
124   Parameter list:
125     int index    The element of the myPerson[] array to read
126                 from EEPROM
127
128   Return value:
129     void        
130 *****/
131 void ReadOneRecord(int index)
132 {
133   byte *bPtr;
134   int i;
135   int offset;
136   
137   offset = index * sizeof(servicePeople);  // must offset from 0 in EEPROM
138   bPtr = (byte *) &myUnion.testServicePeople;          // where to place the data read
139   for (i = 0; i < sizeof(temp); i++) {  // Loop through the bytes...
140     *bPtr = EEPROM.read(offset + i);
141     bPtr++;
142   }
143 }
144 /*****
145   Purpose: Sends the data stored in parameter to the serial monitor
146   Parameter list:
147     struct servicePeople temp    // The data to be displayed
148   Return value:
149     void        
150 *****/
151 void DataDump(struct servicePeople temp)
152
153   
154     Serial.println();
155     Serial.print("ID = ");
156     Serial.print(temp.ID);
157     Serial.print("  Name = ");
158     Serial.println(temp.Name);
159     Serial.print("  PW = ");
160     Serial.print(temp.PW);
161     Serial.print("  Phone = ");
162     Serial.println(temp.Phone);
163 }
164
165
166 \r