0

I'm trying to rewrite C code to python, but I'm not sure how to express this part in python

#include <EEPROM.h>
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}

I think I should use I2C object from machine, but I'm not sure what is going on in the C version

1 Answer 1

0

Let's dissect the macro line:

#define

This is the preprocessor command to define a macro.

EEPROM_write(address, p)

The macro is named "EEPROM_write" and it takes two arguments, "address" and "p". Since the preprocessor is mostly working here as a search-and-replace mechanism, there are no types. They depend on the site where the macro is used.

{
    int i = 0;
    byte *pp = (byte*)&(p);
    for (; i < sizeof(p); i++)
        EEPROM.write(address+i, pp[i]);
}

This is the formatted C code of the replacement. It consist of a single block of statements, and the preprocessor will replace each occurence of address and p with the arguments given at the usage of the macro.

The code block takes the address of p as a byte pointer. Then it loops through all bytes of p (including padding) and calls EEPROM.write() with consecutive addresses (starting at address) and the respective byte of p.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.