Esempio n. 1
0
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
    o->base.type = &fun_native_type;
    o->n_args_min = n_args_min;
    o->n_args_max = ~((machine_uint_t)0);
    o->fun = fun;
    return o;
}
Esempio n. 2
0
STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
    mp_obj_array_t *array = array_in;
    mp_obj_array_it_t *o = m_new_obj(mp_obj_array_it_t);
    o->base.type = &array_it_type;
    o->array = array;
    o->cur = 0;
    return o;
}
Esempio n. 3
0
// args are those returned from rt_load_method_maybe (ie either an attribute or a method)
mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args) {
    mp_obj_getitem_iter_t *o = m_new_obj(mp_obj_getitem_iter_t);
    o->base.type = &it_type;
    o->args[0] = args[0];
    o->args[1] = args[1];
    o->args[2] = MP_OBJ_NEW_SMALL_INT(0);
    return o;
}
Esempio n. 4
0
mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
    o->base.type = &fun_native_type;
    o->n_args_min = 3;
    o->n_args_max = 3;
    o->fun = fun;
    return o;
}
Esempio n. 5
0
/// \function poll()
STATIC mp_obj_t select_poll(void) {
    mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t);
    poll->base.type = &mp_type_poll;
    mp_map_init(&poll->poll_map, 0);
    poll->iter_cnt = 0;
    poll->ret_tuple = MP_OBJ_NULL;
    return poll;
}
Esempio n. 6
0
emit_inline_asm_t *emit_inline_xtensa_new(mp_uint_t max_num_labels) {
    emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t);
    memset(&emit->as, 0, sizeof(emit->as));
    mp_asm_base_init(&emit->as.base, max_num_labels);
    emit->max_num_labels = max_num_labels;
    emit->label_lookup = m_new(qstr, max_num_labels);
    return emit;
}
Esempio n. 7
0
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, uint len, uint free_len) {
    mp_lexer_str_buf_t *sb = m_new_obj(mp_lexer_str_buf_t);
    sb->free_len = free_len;
    sb->src_beg = str;
    sb->src_cur = str;
    sb->src_end = str + len;
    return mp_lexer_new(src_name, sb, (mp_lexer_stream_next_char_t)str_buf_next_char, (mp_lexer_stream_close_t)str_buf_free);
}
Esempio n. 8
0
STATIC mp_obj_list_t *list_new(uint n) {
    mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
    o->base.type = &mp_type_list;
    o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
    o->len = n;
    o->items = m_new(mp_obj_t, o->alloc);
    return o;
}
Esempio n. 9
0
STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) {
    mp_obj_t o_out = m_new_obj(mp_obj_dict_view_t);
    mp_obj_dict_view_t *o = MP_OBJ_CAST(o_out);
    o->base.type = &dict_view_type;
    o->dict = dict;
    o->kind = kind;
    return o_out;
}
Esempio n. 10
0
mp_obj_t mp_obj_new_module(qstr module_name) {
    mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
    o->base.type = &module_type;
    o->name = module_name;
    o->globals = mp_map_new(1);
    mp_map_lookup(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = mp_obj_new_str(module_name);
    return o;
}
Esempio n. 11
0
STATIC mp_obj_t dict_getiter(mp_obj_t self_in) {
    mp_obj_t o_out = m_new_obj(mp_obj_dict_it_t);
    mp_obj_dict_it_t *o = MP_OBJ_CAST(o_out);
    o->base.type = &mp_type_dict_it;
    o->dict = self_in;
    o->cur = 0;
    return o_out;
}
Esempio n. 12
0
STATIC mp_obj_t dict_getiter(mp_obj_t self_in) {
    mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
    o->base.type = &mp_type_polymorph_iter;
    o->iternext = dict_it_iternext;
    o->dict = self_in;
    o->cur = 0;
    return MP_OBJ_FROM_PTR(o);
}
Esempio n. 13
0
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) {
    mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
    o->base.type = &gen_wrap_type;
    // we have at least 3 locals so the bc can write back fast[0,1,2] safely; should improve how this is done
    o->n_state = (n_locals < 3 ? 3 : n_locals) + n_stack;
    o->fun = fun;
    return o;
}
Esempio n. 14
0
mp_obj_t mp_obj_new_fun_native(mp_uint_t n_args, void *fun_data) {
    assert(0 <= n_args && n_args <= 3);
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
    o->base.type = &mp_type_fun_native;
    o->n_args = n_args;
    o->fun_data = fun_data;
    return o;
}
Esempio n. 15
0
STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step) {
    mp_obj_range_it_t *o = m_new_obj(mp_obj_range_it_t);
    o->base.type = &range_it_type;
    o->cur = cur;
    o->stop = stop;
    o->step = step;
    return o;
}
Esempio n. 16
0
emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels) {
    emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t);
    emit->max_num_labels = max_num_labels;
    emit->label_lookup = m_new(qstr, max_num_labels);
    memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr));
    emit->as = asm_thumb_new(max_num_labels);
    return emit;
}
Esempio n. 17
0
// min and max are inclusive
mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
    o->base.type = &fun_native_type;
    o->n_args_min = n_args_min;
    o->n_args_max = n_args_max;
    o->fun = fun;
    return o;
}
Esempio n. 18
0
mp_obj_t pyb_ADC(mp_obj_t pin_name_obj) {

    pyb_obj_adc_t *o = m_new_obj(pyb_obj_adc_t);
    o->base.type = &adc_type;
    o->pin_name = pin_name_obj;

    // work out the channel from the pin name
    const char *pin_name = mp_obj_str_get_str(pin_name_obj);
    GPIO_TypeDef *port;
    switch (pin_name[0]) {
    case 'A':
    case 'a':
        port = GPIOA;
        break;
    case 'B':
    case 'b':
        port = GPIOB;
        break;
    case 'C':
    case 'c':
        port = GPIOC;
        break;
    default:
        goto pin_error;
    }
    uint pin_num = 0;
    for (const char *s = pin_name + 1; *s; s++) {
        if (!('0' <= *s && *s <= '9')) {
            goto pin_error;
        }
        pin_num = 10 * pin_num + *s - '0';
    }
    if (!(0 <= pin_num && pin_num <= 15)) {
        goto pin_error;
    }

    int i;
    for (i = 0; i < ADC_NUM_CHANNELS; i++) {
        if (adc_gpio[i].port == port && adc_gpio[i].pin == (1 << pin_num)) {
            o->channel = i;
            break;
        }
    }

    if (i == ADC_NUM_CHANNELS) {
        nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ValueError, "pin %s does not have ADC capabilities", pin_name));
    }

    // init ADC just for this channel
    adc_init_single(o->channel);

    o->is_enabled = true;

    return o;

pin_error:
    nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ValueError, "pin %s does not exist", pin_name));
}
Esempio n. 19
0
mp_obj_t mp_obj_new_memoryview(byte typecode, mp_uint_t nitems, void *items) {
    mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
    self->base.type = &mp_type_memoryview;
    self->typecode = typecode;
    self->free = 0;
    self->len = nitems;
    self->items = items;
    return self;
}
Esempio n. 20
0
mp_obj_t mp_obj_new_fun_bc(int n_args, uint n_state, const byte *code) {
    mp_obj_fun_bc_t *o = m_new_obj(mp_obj_fun_bc_t);
    o->base.type = &fun_bc_type;
    o->globals = rt_globals_get();
    o->n_args = n_args;
    o->n_state = n_state;
    o->bytecode = code;
    return o;
}
Esempio n. 21
0
/// \classmethod \constructor(port)
/// Construct a new DAC object.
///
/// `port` can be a pin object, or an integer (1 or 2).
/// DAC(1) is on pin X5 and DAC(2) is on pin X6.
STATIC mp_obj_t pyb_dac_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
    // check arguments
    mp_arg_check_num(n_args, n_kw, 1, 1, false);

    // get pin/channel to output on
    mp_int_t dac_id;
    if (MP_OBJ_IS_INT(args[0])) {
        dac_id = mp_obj_get_int(args[0]);
    } else {
        const pin_obj_t *pin = pin_find(args[0]);
        if (pin == &pin_A4) {
            dac_id = 1;
        } else if (pin == &pin_A5) {
            dac_id = 2;
        } else {
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %q does not have DAC capabilities", pin->name));
        }
    }

    pyb_dac_obj_t *dac = m_new_obj(pyb_dac_obj_t);
    dac->base.type = &pyb_dac_type;

    uint32_t pin;
    if (dac_id == 1) {
        pin = GPIO_PIN_4;
        dac->dac_channel = DAC_CHANNEL_1;
        dac->dma_stream = DMA1_Stream5;
    } else if (dac_id == 2) {
        pin = GPIO_PIN_5;
        dac->dac_channel = DAC_CHANNEL_2;
        dac->dma_stream = DMA1_Stream6;
    } else {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "DAC %d does not exist", dac_id));
    }

    // GPIO configuration
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = pin;
    GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

    // DAC peripheral clock
    __DAC_CLK_ENABLE();

    // stop anything already going on
    HAL_DAC_Stop(&DAC_Handle, dac->dac_channel);
    if ((dac->dac_channel == DAC_CHANNEL_1 && DAC_Handle.DMA_Handle1 != NULL)
            || (dac->dac_channel == DAC_CHANNEL_2 && DAC_Handle.DMA_Handle2 != NULL)) {
        HAL_DAC_Stop_DMA(&DAC_Handle, dac->dac_channel);
    }

    dac->state = 0;

    // return object
    return dac;
}
Esempio n. 22
0
// fun must have the correct signature for n_args fixed arguments
mp_obj_t mp_make_function_n(int n_args, void *fun) {
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
    o->base.type = &mp_type_fun_native;
    o->is_kw = false;
    o->n_args_min = n_args;
    o->n_args_max = n_args;
    o->fun = fun;
    return o;
}
Esempio n. 23
0
mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun) {
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
    o->base.type = &mp_type_fun_native;
    o->is_kw = false;
    o->n_args_min = n_args_min;
    o->n_args_max = MP_OBJ_FUN_ARGS_MAX;
    o->fun = fun;
    return o;
}
Esempio n. 24
0
mp_obj_t pyb_ADC_all(mp_obj_t resolution) {
    /* init ADC */
    adc_init_all(mp_obj_get_int(resolution));

    pyb_obj_adc_all_t *o = m_new_obj(pyb_obj_adc_all_t);
    o->base.type = &adc_all_type;
    o->is_enabled = true;
    return o;
}
Esempio n. 25
0
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
    mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
    o->base.type = &mp_type_set;
    mp_set_init(&o->set, n_args);
    for (int i = 0; i < n_args; i++) {
        mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
    }
    return o;
}
Esempio n. 26
0
STATIC mp_obj_array_t *array_new(char typecode, uint n) {
    mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
    o->base.type = &array_type;
    o->typecode = typecode;
    o->free = 0;
    o->len = n;
    o->items = m_malloc(mp_binary_get_size(typecode) * o->len);
    return o;
}
Esempio n. 27
0
void pyb_sleep_add (const mp_obj_t obj, WakeUpCB_t wakeup) {
    pyb_sleep_obj_t *sleep_obj = m_new_obj(pyb_sleep_obj_t);
    sleep_obj->base.type = &pyb_sleep_type;
    sleep_obj->obj = obj;
    sleep_obj->wakeup = wakeup;
    // remove it in case it was already registered
    pyb_sleep_remove (obj);
    mp_obj_list_append(&MP_STATE_PORT(pyb_sleep_obj_list), sleep_obj);
}
Esempio n. 28
0
// Create bytearray which references specified memory area
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
    mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
    o->base.type = &mp_type_bytearray;
    o->typecode = BYTEARRAY_TYPECODE;
    o->free = 0;
    o->len = n;
    o->items = items;
    return o;
}
Esempio n. 29
0
static mp_obj_t dict_view_getiter(mp_obj_t view_in) {
    assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
    mp_obj_dict_view_t *view = view_in;
    mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
    o->base.type = &dict_view_it_type;
    o->kind = view->kind;
    o->iter = mp_obj_new_dict_iterator(view->dict, 0);
    return o;
}
Esempio n. 30
0
STATIC mp_obj_btree_t *btree_new(DB *db) {
    mp_obj_btree_t *o = m_new_obj(mp_obj_btree_t);
    o->base.type = &btree_type;
    o->db = db;
    o->start_key = mp_const_none;
    o->end_key = mp_const_none;
    o->next_flags = 0;
    return o;
}