Skip to content

Conversation

@safocl
Copy link
Contributor

@safocl safocl commented Sep 21, 2025

cleaner code using Arduino functionality.
#4

@safocl
Copy link
Contributor Author

safocl commented Sep 21, 2025

potentially, this code will be more efficient than the previous one

@latchdevel
Copy link
Owner

This looks great!

I'm going to test it on actual hardware just to be fully confident about its correct behavior, and then I'll go ahead and merge the changes.

Thanks a lot!

@latchdevel latchdevel self-assigned this Sep 28, 2025
@latchdevel latchdevel linked an issue Sep 28, 2025 that may be closed by this pull request
@latchdevel latchdevel removed a link to an issue Sep 28, 2025
@safocl safocl force-pushed the arduinization branch 2 times, most recently from 74ab1d9 to 7045151 Compare September 28, 2025 19:02
@latchdevel
Copy link
Owner

The <cstring> library is not required nor included in the C++ AVR toolchain.

And I insist, previously using flag.length() meant that 8 characters were read, written, and compared. However, in the updated version, sizeof(flag) returns 9 because char flag[] = ESR_METER_FLAG; includes the null terminator (\0) at the end.

This change breaks compatibility for users upgrading from older firmware versions, since their EEPROM data contains only the original 8 characters and lacks the null terminator at the ninth byte.

Fortunately, the fix is simple: subtract one byte from the comparison length to ensure only the original 8 characters are checked.

if (strncmp(flag, flag_read, sizeof(flag)-1) == 0)

Thanks.

@safocl
Copy link
Contributor Author

safocl commented Sep 29, 2025

The <cstring> library is not required nor included in the C++ AVR toolchain.

which header contains strncmp?

@latchdevel
Copy link
Owner

latchdevel commented Sep 29, 2025

cleaner code using Arduino functionality.
latchdevel#4
@safocl
Copy link
Contributor Author

safocl commented Sep 29, 2025

Using these functions from EEPROM (get and put), it is not possible to use the rest normally (the same lines) - it will clearly not look the best - for this reason, I am now not at all sure that any moderately normal type of code is possible with the Arduino API.

@safocl safocl marked this pull request as draft September 29, 2025 16:07
@latchdevel
Copy link
Owner

I think this should work because the sizeof(T) in the template of put method will resolve the correct size, just like the get method does for the char flag_read[sizeof(ESR_METER_FLAG)];

https://github.com/arduino/ArduinoCore-avr/blob/855ea0100ba0f6b9036a19a6e71517125473b014/libraries/EEPROM/src/EEPROM.h#L137-L142

template<typename T>
const T &put(int idx, const T &t) {
    EEPtr e = idx;
    const uint8_t *ptr = (const uint8_t*) &t;
    for (int count = sizeof(T); count; --count, ++e) {
        (*e).update(*ptr++);
    }
    return t;
}

I'll test it on actual hardware and let you know.
Thanks a lot!

@latchdevel latchdevel marked this pull request as ready for review September 29, 2025 18:32
@safocl
Copy link
Contributor Author

safocl commented Sep 29, 2025

I'll test it on actual hardware and let you know.
Thanks a lot!

Perhaps you yourself will suggest a more elegant option, or of course you can refuse to accept such changes at all)))
I initially wanted to make the code simpler, but as I now understand, using the Arduino API, I have difficulty reducing it to such a simple form.

@safocl safocl marked this pull request as draft October 2, 2025 05:35
@latchdevel
Copy link
Owner

Hi @safocl,

I've tested the changes — they work perfectly, are elegantly implemented, and save over 1KB of flash. The only thing I'd suggest is removing the unnecessary #include <string.h>. Once that's gone, I'd be happy to merge your contribution.

Thanks a lot!

@safocl safocl marked this pull request as ready for review October 5, 2025 11:12
@latchdevel latchdevel linked an issue Oct 5, 2025 that may be closed by this pull request
@latchdevel latchdevel merged commit e8d24cf into latchdevel:main Oct 5, 2025
1 check passed
@safocl safocl deleted the arduinization branch October 6, 2025 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Arduinization of code

2 participants