global variables across all modules

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
devnull
Posts: 77
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall

global variables across all modules

Postby devnull » Mon Mar 06, 2017 11:48 am

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 ??

User avatar
deshipu
Posts: 977
Joined: Thu May 28, 2015 5:54 pm

Re: global variables across all modules

Postby deshipu » Mon Mar 06, 2017 11:55 am

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.

devnull
Posts: 77
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall

Re: global variables across all modules

Postby devnull » Mon Mar 06, 2017 12:22 pm

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.

User avatar
deshipu
Posts: 977
Joined: Thu May 28, 2015 5:54 pm

Re: global variables across all modules

Postby deshipu » Mon Mar 06, 2017 12:44 pm

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.

devnull
Posts: 77
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall

Re: global variables across all modules

Postby devnull » Mon Mar 06, 2017 12:52 pm

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 ??

User avatar
Roberthh
Posts: 583
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: global variables across all modules

Postby Roberthh » Mon Mar 06, 2017 2:27 pm

@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.

User avatar
deshipu
Posts: 977
Joined: Thu May 28, 2015 5:54 pm

Re: global variables across all modules

Postby deshipu » Mon Mar 06, 2017 2:27 pm

You can modify the values of global variables after they have been imported, in memory.

User avatar
Roberthh
Posts: 583
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: global variables across all modules

Postby Roberthh » Mon Mar 06, 2017 3:06 pm

That's what I'm saying

devnull
Posts: 77
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall

Re: global variables across all modules

Postby devnull » Tue Mar 07, 2017 2:48 am

@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.


Return to “General Discussion and Questions”

Who is online

Users browsing this forum: No registered users and 2 guests