Skip to content

Functions to write and read string objects #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 81 additions & 72 deletions examples/Example1_BasicReadWrite/Example1_BasicReadWrite.ino
Original file line number Diff line number Diff line change
@@ -1,72 +1,81 @@
/*
Read and write settings and calibration data to an external I2C EEPROM
By: Nathan Seidle
SparkFun Electronics
Date: December 11th, 2019
License: This code is public domain but you buy me a beer if you use this
and we meet someday (Beerware license).
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/14764

This example demonstrates how to read and write various variables to memory.

The I2C EEPROM should have all its ADR pins set to GND (0). This is default
on the Qwiic board.

Hardware Connections:
Plug the SparkFun Qwiic EEPROM to an Uno, Artemis, or other Qwiic equipped board
Load this sketch
Open output window at 115200bps
*/

#include <Wire.h>

#include "SparkFun_External_EEPROM.h" // Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM
ExternalEEPROM myMem;

void setup()
{
Serial.begin(115200);
Serial.println("Qwiic EEPROM example");

Wire.begin();

if (myMem.begin() == false)
{
Serial.println("No memory detected. Freezing.");
while (1)
;
}
Serial.println("Memory detected!");

Serial.print("Mem size in bytes: ");
Serial.println(myMem.length());

//Yes you can read and write bytes, but you shouldn't!
byte myValue1 = 200;
myMem.write(0, myValue1); //(location, data)

byte myRead1 = myMem.read(0);
Serial.print("I read: ");
Serial.println(myRead1);

//You should use gets and puts. This will automatically and correctly arrange
//the bytes for larger variable types.
int myValue2 = -366;
myMem.put(10, myValue2); //(location, data)
int myRead2;
myMem.get(10, myRead2); //location to read, thing to put data into
Serial.print("I read: ");
Serial.println(myRead2);

float myValue3 = -7.35;
myMem.put(20, myValue3); //(location, data)
float myRead3;
myMem.get(20, myRead3); //location to read, thing to put data into
Serial.print("I read: ");
Serial.println(myRead3);
}

void loop()
{
}
/*
Read and write settings and calibration data to an external I2C EEPROM
By: Nathan Seidle
SparkFun Electronics
Date: December 11th, 2019
License: This code is public domain but you buy me a beer if you use this
and we meet someday (Beerware license).
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/14764

This example demonstrates how to read and write various variables to memory.

The I2C EEPROM should have all its ADR pins set to GND (0). This is default
on the Qwiic board.

Hardware Connections:
Plug the SparkFun Qwiic EEPROM to an Uno, Artemis, or other Qwiic equipped board
Load this sketch
Open output window at 115200bps
*/

#include <Wire.h>

#include "SparkFun_External_EEPROM.h" // Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM
ExternalEEPROM myMem;

void setup()
{
Serial.begin(115200);
Serial.println("Qwiic EEPROM example");

Wire.begin();

if (myMem.begin() == false)
{
Serial.println("No memory detected. Freezing.");
while (1)
;
}
Serial.println("Memory detected!");

Serial.print("Mem size in bytes: ");
Serial.println(myMem.length());

//Yes you can read and write bytes, but you shouldn't!
byte myValue1 = 200;
myMem.write(0, myValue1); //(location, data)

byte myRead1 = myMem.read(0);
Serial.print("I read: ");
Serial.println(myRead1);

//You should use gets and puts. This will automatically and correctly arrange
//the bytes for larger variable types.
int myValue2 = -366;
myMem.put(10, myValue2); //(location, data)
int myRead2;
myMem.get(10, myRead2); //location to read, thing to put data into
Serial.print("I read: ");
Serial.println(myRead2);

float myValue3 = -7.35;
myMem.put(20, myValue3); //(location, data)
float myRead3;
myMem.get(20, myRead3); //location to read, thing to put data into
Serial.print("I read: ");
Serial.println(myRead3);

String myString = "Hi, I am just a simple test string";
unsigned long nextEEPROMLocation = myMem.putString(30, myString);
String myRead4 = "";
myMem.getString(30, myRead4);
Serial.print("I read: ");
Serial.println(myRead4);
Serial.print("Next available EEPROM location: ");
Serial.println(nextEEPROMLocation);
}

void loop()
{
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ get KEYWORD2
put KEYWORD2
setI2CBufferSize KEYWORD2
getI2CBufferSize KEYWORD2
putString KEYWORD2
getString KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
20 changes: 20 additions & 0 deletions src/SparkFun_External_EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ constexpr uint16_t ExternalEEPROM::getI2CBufferSize()
{
return I2C_BUFFER_LENGTH_TX;
}
uint32_t ExternalEEPROM::putString(uint32_t eepromLocation, String &strToWrite)
{
uint16_t strLen = strToWrite.length() + 1;
write(eepromLocation, (uint8_t*)strToWrite.c_str(), strLen);
return eepromLocation + strLen;
}
void ExternalEEPROM::getString(uint32_t eepromLocation, String &strToRead)
{
if(strToRead.length()){
strToRead.remove(0,strToRead.length());
}
uint8_t tmp = 65; // dummy
while(tmp != 0){
tmp = read(eepromLocation);
if(tmp != 0){
strToRead += static_cast<char>(tmp);
}
eepromLocation++;
}
}
//Read a byte from a given location
uint8_t ExternalEEPROM::read(uint32_t eepromLocation)
{
Expand Down
7 changes: 5 additions & 2 deletions src/SparkFun_External_EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,16 @@ class ExternalEEPROM
write(idx, ptr, sizeof(T)); //Address, data, sizeOfData
return t;
}

uint32_t putString(uint32_t eepromLocation, String &strToWrite);
void getString(uint32_t eepromLocation, String &strToRead);

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