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

Re: Including a macro/constant in a micropython module

Post by gshekhar52 » Mon Apr 17, 2017 7:38 am

Hello Dave,


Thanks. It worked for me. Now, when I pass my application code to do_str(), it fails returning name error. Here is my code for do_str():

Code: Select all

uint32_t do_str(const char *src, mp_parse_input_kind_t input_kind) {

	mp_obj_t err_obj;
	mp_obj_type_t *type;

    mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);

    if (lex == NULL) {

        printf("MemoryError: lexer could not allocate memory\n");
        //return;
        return MEMORY_ERROR;
    }

    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        qstr source_name = lex->source_name;
        mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
        mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
        mp_call_function_0(module_fun);
        nlr_pop();
        return 0;
    }
    else																		 // uncaught exception
    {
    	err_obj =   (mp_obj_t)nlr.ret_val;
    	type = mp_obj_get_type(err_obj);
    }
    }
In this, line "if (nlr_push(&nlr) == 0)" fails which takes it to else case where I am looking for the 'type'. It says 'mp_type_NameError'.
Can you please help me in solving this?

Thanks,
Gaurav.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Including a macro/constant in a micropython module

Post by stijn » Mon Apr 17, 2017 12:00 pm

Looks like an error in the Python code, print the full exception using mp_obj_print_exception (search the source on examples on how to 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 » Wed Apr 19, 2017 5:47 am

Hi,

I don't have my prints implemented, but I guess this error is occurring because of the term TEMPERATURE. Because, when I replace this term with 1 it's working fine. How can this be solved?

Thanks,
Gaurav.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Including a macro/constant in a micropython module

Post by stijn » Wed Apr 19, 2017 9:19 am

Hard to tell without seeing the full code. Based on your last post where you show the module's globals, there is no such thing as TEMPERATURE but you have to use something like mymodule.TEMP_MACRO in the python code to refere to that constant..
In any case, without any printing you are going to have a hard time debugging.

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 19, 2017 10:30 am

Hi,

Yes, it worked. I was using 'abc.read(TEMPERATURE)' instead of 'abc.read(abc.TEMPERATURE)'. It's working fine now.

Thanks to all of you,
Gaurav.

Post Reply