Page 1 of 1

global variables across all modules

Posted: Mon Mar 06, 2017 11:48 am
by devnull
I would like to have 3 or 4 read-only global variables that are set during the boot up sequence to be made available in every module without needing an additional import, i.e truly global variables.

I thought that I could do this by adding them to the __builtins__ however this does not appear to be present ??

What's the alternative to __builtins__ on micropython to achieve this ??

Re: global variables across all modules

Posted: Mon Mar 06, 2017 11:55 am
by deshipu
Perhaps you should write more about why you need that for, because the solution that you came up with is not particularly good, and there must be a better way of achieving what you need.

Re: global variables across all modules

Posted: Mon Mar 06, 2017 12:22 pm
by devnull
During bootup I read the vcc level, and also detect the boot mode, either setup or run, this mode is changed by a long / short press on a button as well as reading a .json file 'mode' variable. which is set using a mqtt subscription

During bootup I also read a json file that contains the wlan connection info as well as a variable that is used to calibrate the ADC(1) for each individual device.

These variables need to be used in the various modules that get loaded depending on whether the mode is setup or run.

I do not want to have to read the json files several times and are running on ultra-low power and up to 1 year battery life.

So I need a few global variables:

1) _mode (setup or run)
2) _vcccal (calibration value for vcc ADC)
3) _ssid (connected wlan ssid)
4) _vccmv (vcc millivolts at bootup)

There maybe on or 2 more but these are the key ones.

Re: global variables across all modules

Posted: Mon Mar 06, 2017 12:44 pm
by deshipu
I would put them all as global variables in a module somewhere, then import that module and refer to them as module.variable.
Note that importing an already imported module is a no-op -- it only makes the name available in your local namespace.

Re: global variables across all modules

Posted: Mon Mar 06, 2017 12:52 pm
by devnull
Thanks so much for helping, but wouldn't that require me to write a global module file on every boot / wakeup, I would prefer not to do that.

It would be far easier to just read it once and make it available in memory ??

Re: global variables across all modules

Posted: Mon Mar 06, 2017 2:27 pm
by Roberthh
@deshipu: That's a very good hint. and it meets exactly what I understand from @devnull's question. I have placed some symbols in a script, imported that in main.py, changed the values there, and when imported into another script, the changed values were present.

Re: global variables across all modules

Posted: Mon Mar 06, 2017 2:27 pm
by deshipu
You can modify the values of global variables after they have been imported, in memory.

Re: global variables across all modules

Posted: Mon Mar 06, 2017 3:06 pm
by Roberthh
That's what I'm saying

Re: global variables across all modules

Posted: Tue Mar 07, 2017 2:48 am
by devnull
@deshipu - Thanks, that works very well.

I did not know that if you modified variables in a imported module, that it would retain those values when imported into another.