Esempio n. 1
0
/// \classmethod \constructor(channel)
/// Create an ADC object associated with the given channel.
/// This allows you to then read analog values on that pin.
STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
    // check number of arguments
    mp_arg_check_num(n_args, n_kw, 1, 1, false);

    // the first argument is the channel number
    int32_t idx = mp_obj_get_int(args[0]) - 1;
    const pin_obj_t *pin;
    uint channel;
    switch (idx) {
    case 0:
        channel = ADC_CH_0;
        pin = &pin_GP2;
        break;
    case 1:
        channel = ADC_CH_1;
        pin = &pin_GP3;
        break;
    case 2:
        channel = ADC_CH_2;
        pin = &pin_GP4;
        break;
    case 3:
        channel = ADC_CH_3;
        pin = &pin_GP5;
        break;
    default:
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
        break;
    }

    // disable the callback before re-configuring
    pyb_adc_obj_t *self = &pyb_adc_obj[idx];
    self->base.type = &pyb_adc_type;
    self->channel = channel;
    self->idx = idx;

    // configure the pin in analog mode
    pin_config ((pin_obj_t *)pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PYBPIN_ANALOG_TYPE, PIN_STRENGTH_2MA);

    // initialize it
    pybadc_init (self);

    // register it with the sleep module
    pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pybadc_init);

    return self;
}
Esempio n. 2
0
/// \classmethod \constructor(pin)
/// Create an ADC object associated with the given pin.
/// This allows you to then read analog values on that pin.
STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
    // check number of arguments
    mp_arg_check_num(n_args, n_kw, 1, 1, false);

    // the argument passed is the pin
    const pin_obj_t *pin = (pin_obj_t *)pin_find(args[0]);
    for (int32_t idx = 0; idx < PYB_ADC_NUM_CHANNELS; idx++) {
        if (pin == pyb_adc_obj[idx].pin) {
            pyb_adc_obj_t *self = &pyb_adc_obj[idx];
            self->base.type = &pyb_adc_type;
            pybadc_init (self);
            // register it with the sleep module
            pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pybadc_init);
            return self;
        }
    }
    nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
Esempio n. 3
0
/// \method enable()
/// Enable the adc channel
STATIC mp_obj_t adc_enable(mp_obj_t self_in) {
    pyb_adc_obj_t *self = self_in;

    pybadc_init(self);
    return mp_const_none;
}