I have a for loop that decide not to stop when I have a certain typecast process that is assigned to a variable.
below is the code:
RegisterPatch _patchingSetting;
uint8_t regAddress1 = 0;
uint8_t regAddress2 = 0;
uint8_t regAddress3 = 0;
uint8_t regAddress4 = 0;
uint8_t value1 = 0;
uint8_t value2 = 0;
uint8_t value3 = 0;
uint8_t value4 = 0;
int rowsize = sizeof(patchingSetting) / sizeof(RegisterPatch);
int columnsize = sizeof(_patchingSetting) / 4;
for (int row = 0; row < rowsize; row++)
{
PROGMEM_readAnything(&patchingSetting[row], _patchingSetting);
if (row != rowsize - 1)
{
int col = 0;
for (byte j = 0; j < columnsize; j++)
{
#ifdef PATCH_DEBUG_LAST_ROW
Serial.print("Row: ");
Serial.print(row);
Serial.print(" Column: ");
Serial.println(j);
#endif
regAddress = _patchingSetting[0] + (col * 4);
regAddress1 = (uint8_t)((regAddress >> 24) & 0x000000FF);
regAddress2 = (uint8_t)((regAddress >> 16) & 0x000000FF);
regAddress3 = (uint8_t)((regAddress >> 8) & 0x000000FF);
regAddress4 = (uint8_t)(regAddress & 0x000000FF);
column = col + 1;
(uint8_t)_patchingSetting[column]; // <<< This is OK
value1 = (uint8_t)_patchingSetting[0]; // <<< This is also OK
value1 = (uint8_t)_patchingSetting[column]; // <<< This is NOK
// value1 = ((_patchingSetting[column] & 0xFF000000) >> 24);
// value2 = ((_patchingSetting[column] & 0x00FF0000) >> 16);
// value3 = ((_patchingSetting[column] & 0x0000FF00) >> 8);
// value4 = (_patchingSetting[column] & 0x000000FF);
Wire.beginTransmission(VCM_Special_Driver_Addr);
Wire.write(regAddress1);
Wire.write(regAddress2);
Wire.write(regAddress3);
Wire.write(regAddress4);
Wire.write(value1);
Wire.write(value2);
Wire.write(value3);
Wire.write(value4);
Wire.endTransmission();
col += 1;
}
}
else
{
for (int j = 0; j < 29; j++)
{
regAddress = _patchingSetting[0] + (j * 4);
Wire.beginTransmission(VCM_Special_Driver_Addr);
Wire.write((uint8_t)((regAddress >> 24) & 0x000000FF));
Wire.write((uint8_t)((regAddress >> 16) & 0x000000FF));
Wire.write((uint8_t)((regAddress >> 8) & 0x000000FF));
Wire.write((uint8_t)(regAddress & 0x000000FF));
Wire.write((uint8_t)((_patchingSetting[j + 1] >> 24) & 0x000000FF));
Wire.write((uint8_t)((_patchingSetting[j + 1] >> 16) & 0x000000FF));
Wire.write((uint8_t)((_patchingSetting[j + 1] >> 8) & 0x000000FF));
Wire.write((uint8_t)(_patchingSetting[j + 1] & 0x000000FF));
Wire.endTransmission();
}
}
}
There are 2 for loops in the 'if then else' structure that essentially do the same thing.
For the first for loop, when I performed the typecast from unsigned long to uint8_t, it is ok as long as I do not assign it to another variable (Marked with 'This is OK').
But whenever I assign that typecast result into another variable, the loop just doesn't stop, it goes on until the variable ends and return back to the first value of that loop indicator variable (in this case it is byte j).
Funny thing is that it didn't happen on the second loop, that also performs the same typecast and assign it into a variable or put it into another process (in this case an I2C 'Wire.write' function.
I have ensured that there is no other part in the code that can change the for loop condition. And narrowed down to the 'typecasting of an array component that is assigned to another variable' but only in the first loop.
Please help, what else could I check?
Thank you