1

I have EXTREMELY limited programming experience but am working on a custom Micropython build. A feature is enabled/disabled using a #define statement in a C header file e.g. #define MICROPY_HW_USB_MSC (1); I would like to make this dynamic as part of the code build.

This defines whether the MCU supports USB Mass Storage at runtime. What I would like to do is to set this variable (?) from a script (or some other recommended means) that reads the state of a GPI pin on the MCU at boot.

So if the pin is low, it effectively creates #define MICROPY_HW_USB_MSC (0), and conversely if the pin is high, #define MICROPY_HW_USB_MSC (1).

I hope this makes sense and thanks in advance for any assistance.

I have tried searching for a solution online but am not experienced enough to search for the correct terminology - hence turning to experts for assistance.

John Peters
  • 1,177
  • 14
  • 24
DoubleVee
  • 11
  • 2
  • Note: Please properly format your code by using one back-tick (\`) at the start and one at the end. This could look like `this`. If you want to write full blocks of code, use three of them (3 at start, 3 at end). – Notiq Mar 11 '23 at 17:33
  • Its not possible, sorry! What you could TRY to do is to use a global variable in your header file with ```extern int microscopy_hw_usb_msc;```, then in your main function you can set its value depending on the pin value. Basically, ```#define``` statements only have meaning at compile time. – squidwardsface Mar 11 '23 at 17:36
  • That's not how macros work. Macros are compile-time only things, it's basically a simple text-replacement. – Some programmer dude Mar 11 '23 at 17:54
  • https://stackoverflow.com/questions/7572872/changing-a-macro-at-runtime-in-c – Jeff Mar 12 '23 at 22:00
  • _"So if the pin is low, it effectively creates #define MICROPY_HW_USB_MSC (0), and conversely if the pin is high, #define MICROPY_HW_USB_MSC (1)."_ This will not work. Defines are evaluated at compile time. This means you have to specify a value **when compiling**. You cannot (easily) change a "macro" after the fact, if at all. – Marco Mar 12 '23 at 22:01

1 Answers1

1

Anything specified in #define's is done by the preprocessor that runs at compile time. This means it cannot know what the state of a pin is at runtime.

The only way to make a feature dynamic is to write actual C code that checks the state of the pin and acts on it.

John Peters
  • 1,177
  • 14
  • 24
  • Thank you so much. Now I have read all these great answers, I understand the process. Macros are essentially text ‘lookups’ used by the preprocessing - thanks again – DoubleVee Mar 12 '23 at 23:53