Including a macro/constant in a micropython module

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
gshekhar52
Posts: 8
Joined: Wed Apr 12, 2017 6:23 am

Including a macro/constant in a micropython module

Post by gshekhar52 » Wed Apr 12, 2017 9:26 am

Hi,

I am using an ARM Cortex M4 processor to run micropython codes sent from a cloud platform. I made a micropython module which includes some basic read and write functions. Now, I want to include some macros/constants in the module so that my code from the cloud becomes more interactive and user-friendly. Can anyone suggest me how to add a macro in a module which gets replaced by its value in the interpreter?

Suppose, my module is 'abc', my code goes like this:

import abc
abc.read(1)
abc.read(2)
abc.write(12, "Hello")

Here, 1, 2 are the IDs of some hardwares, but it doesn't give that feel. So, I want to write a code as following for doing the same operation.

import abc
abc.read(TEMPERATURE)
abc.read(HUMIDITY)
abc.write(DISPLAY, "Hello")

These macros looks more relevant than numbers. So, I am just wondering if there is any way so that code with macros, when reaches the interpreter, replaces the macros with their corresponding ID values.

Thank You,
Gaurav.

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

Re: Including a macro/constant in a micropython module

Post by deshipu » Wed Apr 12, 2017 10:36 am

Code: Select all

from micropython import const

TEMPERATURE = const(1)
HUMIDITY = const(2)
in addition, if the const names start with _, they won't be visible from the outside of the module and won't use any memory.

gshekhar52
Posts: 8
Joined: Wed Apr 12, 2017 6:23 am

Re: Including a macro/constant in a micropython module

Post by gshekhar52 » Wed Apr 12, 2017 12:18 pm

Where shall I include these declarations? Is it a change on the device side or the cloud side?

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

Re: Including a macro/constant in a micropython module

Post by deshipu » Wed Apr 12, 2017 3:05 pm

No idea what you mean by "device side" or "cloud side". You put it in your code, in the same module where you use it.

gshekhar52
Posts: 8
Joined: Wed Apr 12, 2017 6:23 am

Re: Including a macro/constant in a micropython module

Post by gshekhar52 » Thu Apr 13, 2017 5:10 am

Do you mean to say my code should be as following?

import abc
TEMPERATURE = const(1)
HUMIDITY = const(2)

abc.read(TEMPERATURE)
abc.read(HUMIDITY)

If yes, then this is not what I am seeking for. I do not want to define constants every time I write a new code, rather I want define these constants in the 'abc' module, so that it gets directly linked as it happens for read/write methods.

Thanks.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Including a macro/constant in a micropython module

Post by pythoncoder » Thu Apr 13, 2017 5:57 am

If the module abc has

Code: Select all

TEMPERATURE = 1
HUMIDITY = 2
def read():
	# code here
your main module can have

Code: Select all

from abc import read, TEMPERATURE, HUMIDITY
read(TEMPERATURE)
read(HUMIDITY)
You can pass module names around in the same way as the names of other objects, so if you have multiple modules supporting the names read, TEMPERATURE and HUMIDITY you can do things like

Code: Select all

import device
def foo(module):  # Works with any module supporting these names
	module.read(module.TEMPERATURE)
	module.read(module.HUMIDITY)

foo(device)
I'm not entirely sure if that's what you're looking for, but I hope it helps.
Peter Hinch
Index to my micropython libraries.

gshekhar52
Posts: 8
Joined: Wed Apr 12, 2017 6:23 am

Re: Including a macro/constant in a micropython module

Post by gshekhar52 » Thu Apr 13, 2017 7:01 am

Image

Hi,

I have tried to describe my use case using the above diagram (https://ibb.co/dS9bGQ). Hope this is clear.

I am concerned about adding these constants in my module files which are all in C language (modabc.c, etc.). Where shall I put these constants?

Gaurav.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Including a macro/constant in a micropython module

Post by dhylands » Thu Apr 13, 2017 4:04 pm

An example of C constants, typically placed at the module level is here:
https://github.com/micropython/micropyt ... #L572-L576

gshekhar52
Posts: 8
Joined: Wed Apr 12, 2017 6:23 am

Re: Including a macro/constant in a micropython module

Post by gshekhar52 » Fri Apr 14, 2017 1:51 pm

Hi Dave,

Thanks for a helpful reply. I think I am almost there except a compilation error that popped up after adding these lines to my module.
I declared the macros with #define, and added them to globals_table[] as follows:

Code: Select all

{ MP_OBJ_NEW_QSTR(MP_QSTR_TEMP_MACRO),         MP_OBJ_NEW_SMALL_INT(TEMPERATURE) },
	    { MP_OBJ_NEW_QSTR(MP_QSTR_HUM_MACRO),          MP_OBJ_NEW_SMALL_INT(HUMIDITY) },
But, I am getting compilation errors which says: error: 'MP_QSTR_TEMP_MACRO' undeclared here (not in a function).
I tried adding the line Q(TEMP_MACRO) to qstrdefsport.h also but errors still persists. Can you please help me out of this?

Thanks,
Gaurav.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Including a macro/constant in a micropython module

Post by dhylands » Fri Apr 14, 2017 6:11 pm

There is currently an issue where QSTR constants aren't always being generated at the right time. If you run into that error, then you can do a make clean followed by a make and that should then do the right thing.

Also note, that if you call the qstr MP_QSTR_HUM_MACRO then what python sees will be everything after the MP_QSTR_ or for this example it will be called "HUM_MACRO" and its value will be the C constant HUMIDITY.

Post Reply