Skip to content

Commit 8bc696e

Browse files
authored
Merge pull request #21 from wollewald/master
Functions to write and read string objects
2 parents 2c290fb + 0049b85 commit 8bc696e

File tree

4 files changed

+108
-74
lines changed

4 files changed

+108
-74
lines changed
Lines changed: 81 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,81 @@
1-
/*
2-
Read and write settings and calibration data to an external I2C EEPROM
3-
By: Nathan Seidle
4-
SparkFun Electronics
5-
Date: December 11th, 2019
6-
License: This code is public domain but you buy me a beer if you use this
7-
and we meet someday (Beerware license).
8-
Feel like supporting our work? Buy a board from SparkFun!
9-
https://www.sparkfun.com/products/14764
10-
11-
This example demonstrates how to read and write various variables to memory.
12-
13-
The I2C EEPROM should have all its ADR pins set to GND (0). This is default
14-
on the Qwiic board.
15-
16-
Hardware Connections:
17-
Plug the SparkFun Qwiic EEPROM to an Uno, Artemis, or other Qwiic equipped board
18-
Load this sketch
19-
Open output window at 115200bps
20-
*/
21-
22-
#include <Wire.h>
23-
24-
#include "SparkFun_External_EEPROM.h" // Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM
25-
ExternalEEPROM myMem;
26-
27-
void setup()
28-
{
29-
Serial.begin(115200);
30-
Serial.println("Qwiic EEPROM example");
31-
32-
Wire.begin();
33-
34-
if (myMem.begin() == false)
35-
{
36-
Serial.println("No memory detected. Freezing.");
37-
while (1)
38-
;
39-
}
40-
Serial.println("Memory detected!");
41-
42-
Serial.print("Mem size in bytes: ");
43-
Serial.println(myMem.length());
44-
45-
//Yes you can read and write bytes, but you shouldn't!
46-
byte myValue1 = 200;
47-
myMem.write(0, myValue1); //(location, data)
48-
49-
byte myRead1 = myMem.read(0);
50-
Serial.print("I read: ");
51-
Serial.println(myRead1);
52-
53-
//You should use gets and puts. This will automatically and correctly arrange
54-
//the bytes for larger variable types.
55-
int myValue2 = -366;
56-
myMem.put(10, myValue2); //(location, data)
57-
int myRead2;
58-
myMem.get(10, myRead2); //location to read, thing to put data into
59-
Serial.print("I read: ");
60-
Serial.println(myRead2);
61-
62-
float myValue3 = -7.35;
63-
myMem.put(20, myValue3); //(location, data)
64-
float myRead3;
65-
myMem.get(20, myRead3); //location to read, thing to put data into
66-
Serial.print("I read: ");
67-
Serial.println(myRead3);
68-
}
69-
70-
void loop()
71-
{
72-
}
1+
/*
2+
Read and write settings and calibration data to an external I2C EEPROM
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: December 11th, 2019
6+
License: This code is public domain but you buy me a beer if you use this
7+
and we meet someday (Beerware license).
8+
Feel like supporting our work? Buy a board from SparkFun!
9+
https://www.sparkfun.com/products/14764
10+
11+
This example demonstrates how to read and write various variables to memory.
12+
13+
The I2C EEPROM should have all its ADR pins set to GND (0). This is default
14+
on the Qwiic board.
15+
16+
Hardware Connections:
17+
Plug the SparkFun Qwiic EEPROM to an Uno, Artemis, or other Qwiic equipped board
18+
Load this sketch
19+
Open output window at 115200bps
20+
*/
21+
22+
#include <Wire.h>
23+
24+
#include "SparkFun_External_EEPROM.h" // Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM
25+
ExternalEEPROM myMem;
26+
27+
void setup()
28+
{
29+
Serial.begin(115200);
30+
Serial.println("Qwiic EEPROM example");
31+
32+
Wire.begin();
33+
34+
if (myMem.begin() == false)
35+
{
36+
Serial.println("No memory detected. Freezing.");
37+
while (1)
38+
;
39+
}
40+
Serial.println("Memory detected!");
41+
42+
Serial.print("Mem size in bytes: ");
43+
Serial.println(myMem.length());
44+
45+
//Yes you can read and write bytes, but you shouldn't!
46+
byte myValue1 = 200;
47+
myMem.write(0, myValue1); //(location, data)
48+
49+
byte myRead1 = myMem.read(0);
50+
Serial.print("I read: ");
51+
Serial.println(myRead1);
52+
53+
//You should use gets and puts. This will automatically and correctly arrange
54+
//the bytes for larger variable types.
55+
int myValue2 = -366;
56+
myMem.put(10, myValue2); //(location, data)
57+
int myRead2;
58+
myMem.get(10, myRead2); //location to read, thing to put data into
59+
Serial.print("I read: ");
60+
Serial.println(myRead2);
61+
62+
float myValue3 = -7.35;
63+
myMem.put(20, myValue3); //(location, data)
64+
float myRead3;
65+
myMem.get(20, myRead3); //location to read, thing to put data into
66+
Serial.print("I read: ");
67+
Serial.println(myRead3);
68+
69+
String myString = "Hi, I am just a simple test string";
70+
unsigned long nextEEPROMLocation = myMem.putString(30, myString);
71+
String myRead4 = "";
72+
myMem.getString(30, myRead4);
73+
Serial.print("I read: ");
74+
Serial.println(myRead4);
75+
Serial.print("Next available EEPROM location: ");
76+
Serial.println(nextEEPROMLocation);
77+
}
78+
79+
void loop()
80+
{
81+
}

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ get KEYWORD2
3131
put KEYWORD2
3232
setI2CBufferSize KEYWORD2
3333
getI2CBufferSize KEYWORD2
34+
putString KEYWORD2
35+
getString KEYWORD2
3436

3537
#######################################
3638
# Constants (LITERAL1)

src/SparkFun_External_EEPROM.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ constexpr uint16_t ExternalEEPROM::getI2CBufferSize()
119119
{
120120
return I2C_BUFFER_LENGTH_TX;
121121
}
122+
uint32_t ExternalEEPROM::putString(uint32_t eepromLocation, String &strToWrite)
123+
{
124+
uint16_t strLen = strToWrite.length() + 1;
125+
write(eepromLocation, (uint8_t*)strToWrite.c_str(), strLen);
126+
return eepromLocation + strLen;
127+
}
128+
void ExternalEEPROM::getString(uint32_t eepromLocation, String &strToRead)
129+
{
130+
if(strToRead.length()){
131+
strToRead.remove(0,strToRead.length());
132+
}
133+
uint8_t tmp = 65; // dummy
134+
while(tmp != 0){
135+
tmp = read(eepromLocation);
136+
if(tmp != 0){
137+
strToRead += static_cast<char>(tmp);
138+
}
139+
eepromLocation++;
140+
}
141+
}
122142
//Read a byte from a given location
123143
uint8_t ExternalEEPROM::read(uint32_t eepromLocation)
124144
{

src/SparkFun_External_EEPROM.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,16 @@ class ExternalEEPROM
147147
write(idx, ptr, sizeof(T)); //Address, data, sizeOfData
148148
return t;
149149
}
150+
151+
uint32_t putString(uint32_t eepromLocation, String &strToWrite);
152+
void getString(uint32_t eepromLocation, String &strToRead);
150153

151154
private:
152155
// Default settings are for onsemi CAT24C51 512Kbit I2C EEPROM used on SparkFun Qwiic EEPROM Breakout
153156
struct_memorySettings settings = {
154157
.i2cPort = &Wire,
155-
.deviceAddress = 0b1010000, // 0x50; format is 0b1010 + (A2 A1 A0) or 0b1010 + (B0 A1 A0) for larger (>512kbit) EEPROMs
156-
.memorySize_bytes = (uint32_t)512 * 1024 / 8, // equals 64 KB
158+
.deviceAddress = 0b1010000, // 0x50; format is 0b1010 + (A2 A1 A0) or 0b1010 + (B0 A1 A0) for larger (>512kbit) EEPROMs
159+
.memorySize_bytes = (uint32_t)512 * 1024 / 8, // equals 64 KB
157160
.pageSize_bytes = 64,
158161
.pageWriteTime_ms = 5,
159162
.pollForWriteComplete = true

0 commit comments

Comments
 (0)