vince.cimo
Well-known member
Hi guys, I've been using the i2c_t3 library for a while with no issue, but recently had to switch to Wire because of memory limitations. For some reason, upon switching libraries, the last two bytes of a page write/read operation return incorrectly.
I'm not sure why exactly. Here's my write code:
I'm not sure why exactly. Here's my write code:
Code:
void DataLooper::writeCommands(bool shouldWrite){
int daddr = 0x50;
//tracks page
int numBytesWritten = 0;
for(int buttonNumber = 0; buttonNumber < NUM_BUTTONS; buttonNumber++){
for(int commandNum = 0; commandNum < NUMBER_USER_COMMANDS; commandNum++){
//startbyte = number of bytes for all previous buttons commands, the number of bytes of all previous commands on current button, the number of bytes for all the commands in previous presets, a 32 byte offset for global config commands
int startByte = (buttonNumber * NUMBER_USER_COMMANDS * BYTES_PER_COMMAND) + commandNum * BYTES_PER_COMMAND + (State::presetsWritten * NUM_BUTTONS * NUMBER_USER_COMMANDS * BYTES_PER_COMMAND ) + GLOBAL_CONFIG_PAGE_OFFSET ;
if(State::presetsWritten != 0){
//each preset occupys 144 bytes of memory (12 buttons, 2 commands per button, 6 bytes per command), leaving 16 bytes left over after every preset. This advances the start byte to the next memory page.
//while this is inefficient, it allows for faster operation and still 49 presets.
startByte += (16 * State::presetsWritten);
}
for(int cmdByte = 0; cmdByte < BYTES_PER_COMMAND; cmdByte++){
//grab the byte that we're going to write
int newByte = buttons[buttonNumber].commands[commandNum].ee_storage.asBytes[cmdByte];
//append the address
int eeaddress = cmdByte + startByte;
if(!shouldWrite){
int checkByte = readByte(eeaddress);
//Serial.print("stored byte: ");
//Serial.print(checkByte);
//Serial.print(" command byte: ");
//Serial.println(newByte);
if(checkByte != newByte){
writeCommands(true);
return;
}
}else{
//if this is the start of a page
if(numBytesWritten == 0){
Serial.println("new page");
Wire.beginTransmission(daddr);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
}
Serial.print("writing byte:");
Serial.print(newByte);
Serial.print(" at: ");
Serial.println(eeaddress);
Wire.write(newByte);
numBytesWritten+=1;
if(numBytesWritten == 32){
numBytesWritten = 0;
Wire.endTransmission();
Serial.println("page written");
delay(100);
}
}
}
}
}
//ends partial page writes at the end of preset
if(shouldWrite && numBytesWritten != 0){
for(int x = 0; x < 3; x++){
Wire.write(State::presetColor.asBytes[x]);
delay(100);
numBytesWritten += 1;
Serial.print("writing color byte: ");
Serial.println(State::presetColor.asBytes[x]);
Serial.print(" at: ");
Serial.println(numBytesWritten);
}
Wire.endTransmission();
delay(100);
} else{
int colorStartByte = (160 * State::presetsWritten) + GLOBAL_CONFIG_PAGE_OFFSET + 144;
for(int x = 0 ; x < 3 ; x++){
if(readByte(colorStartByte + x) != State::presetColor.asBytes[x]){
Wire.beginTransmission(daddr);
Wire.write((int)(colorStartByte + x >> 8)); // MSB
Wire.write((int)(colorStartByte + x & 0xFF)); // LSB
Wire.write(State::presetColor.asBytes[x]);
delay(100);
Wire.endTransmission();
}
}
}
}