/// \classmethod \constructor(id) /// Create an LED object associated with the given LED: /// /// - `id` is the LED number, 1-4. STATIC mp_obj_t led_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { // check arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); // get led number mp_int_t led_id = mp_obj_get_int(args[0]); // check led number if (!(1 <= led_id && led_id <= NUM_LEDS)) { mp_raise_ValueError("LED doesn't exist"); } // return static led object return (mp_obj_t)&board_led_obj[led_id - 1]; }
STATIC int get_fd(mp_obj_t fdlike) { int fd; // Shortcut for fdfile compatible types if (MP_OBJ_IS_TYPE(fdlike, &mp_type_fileio) #if MICROPY_PY_SOCKET || MP_OBJ_IS_TYPE(fdlike, &mp_type_socket) #endif ) { mp_obj_fdfile_t *fdfile = MP_OBJ_TO_PTR(fdlike); fd = fdfile->fd; } else { fd = mp_obj_get_int(fdlike); } return fd; }
mp_obj_t i2c_obj_write(mp_obj_t self_in, mp_obj_t data_in) { pyb_i2c_obj_t *self = self_in; if (self->i2c_state != I2C_STATE_WRITE) { if (_i2c_restart(self->i2c_port, self->i2c_addr, 1) == false) { _i2c_stop(self->i2c_port); self->i2c_state = I2C_STATE_IDLE; return mp_const_false; } self->i2c_state = I2C_STATE_WRITE; } uint8_t data = mp_obj_get_int(data_in); if (_i2c_send_byte(self->i2c_port, data) == false) return mp_const_false; return mp_const_true; }
STATIC mp_obj_t gc_threshold(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { if (MP_STATE_MEM(gc_alloc_threshold) == (size_t)-1) { return MP_OBJ_NEW_SMALL_INT(-1); } return mp_obj_new_int(MP_STATE_MEM(gc_alloc_threshold) * MICROPY_BYTES_PER_GC_BLOCK); } mp_int_t val = mp_obj_get_int(args[0]); if (val < 0) { MP_STATE_MEM(gc_alloc_threshold) = (size_t)-1; } else { MP_STATE_MEM(gc_alloc_threshold) = val / MICROPY_BYTES_PER_GC_BLOCK; } return mp_const_none; }
// method socket.listen([backlog]) STATIC mp_obj_t socket_listen(mp_uint_t n_args, const mp_obj_t *args) { mod_network_socket_obj_t *self = args[0]; int32_t backlog = 0; if (n_args > 1) { backlog = mp_obj_get_int(args[1]); backlog = (backlog < 0) ? 0 : backlog; } int _errno; if (wlan_socket_listen(self, backlog, &_errno) != 0) { mp_raise_OSError(-_errno); } return mp_const_none; }
STATIC mp_obj_t get_socket(size_t n_args, const mp_obj_t *args) { socket_obj_t *sock = m_new_obj_with_finaliser(socket_obj_t); sock->base.type = &socket_type; sock->domain = AF_INET; sock->type = SOCK_STREAM; sock->proto = 0; if (n_args > 0) { sock->domain = mp_obj_get_int(args[0]); if (n_args > 1) { sock->type = mp_obj_get_int(args[1]); if (n_args > 2) { sock->proto = mp_obj_get_int(args[2]); } } } sock->fd = lwip_socket(sock->domain, sock->type, sock->proto); if (sock->fd < 0) { exception_from_errno(errno); } _socket_settimeout(sock, UINT64_MAX); return MP_OBJ_FROM_PTR(sock); }
/** * def write(self, mask) */ static mp_obj_t class_event_write(mp_obj_t self_in, mp_obj_t mask_in) { struct class_event_t *self_p; uint32_t mask; self_p = MP_OBJ_TO_PTR(self_in); mask = mp_obj_get_int(mask_in); if (event_write(&self_p->event, &mask, sizeof(mask)) != sizeof(mask)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "failed to write event mask")); } return (mp_const_none); }
// method socket.listen([backlog]) STATIC mp_obj_t socket_listen(mp_uint_t n_args, const mp_obj_t *args) { mod_network_socket_obj_t *self = args[0]; int32_t backlog = 0; if (n_args > 1) { backlog = mp_obj_get_int(args[1]); backlog = (backlog < 0) ? 0 : backlog; } int _errno; if (wlan_socket_listen(self, backlog, &_errno) != 0) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno))); } return mp_const_none; }
STATIC mp_obj_t led_obj_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { // check arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); // get led number machine_int_t led_id = mp_obj_get_int(args[0]); // check led number if (!(1 <= led_id && led_id <= NUM_LEDS)) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "LED %d does not exist", led_id)); } // return static led object return (mp_obj_t)&pyb_led_obj[led_id - 1]; }
mp_obj_t pyb_gpio_input(uint n_args, mp_obj_t *args) { const pin_obj_t *pin = pin_map_user_obj(args[0]); uint32_t pull = GPIO_NOPULL; if (n_args > 1) { pull = mp_obj_get_int(args[1]); } GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = pin->pin_mask; GPIO_InitStructure.Mode = GPIO_MODE_INPUT; GPIO_InitStructure.Pull = pull; HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure); return mp_const_none; }
STATIC mp_obj_t sd_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t data) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); if (bufinfo.len % SDCARD_BLOCK_SIZE != 0) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "writes must be a multiple of %d bytes", SDCARD_BLOCK_SIZE)); } mp_uint_t ret = sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE); if (ret != 0) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "sdcard_write_blocks failed [%u]", ret)); } return mp_const_none; }
// This dispatcher function is expected to be independent of the implementation of long int STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { (void)type_in; mp_arg_check_num(n_args, n_kw, 0, 2, false); switch (n_args) { case 0: return MP_OBJ_NEW_SMALL_INT(0); case 1: if (MP_OBJ_IS_INT(args[0])) { // already an int (small or long), just return it return args[0]; } else if (MP_OBJ_IS_STR_OR_BYTES(args[0])) { // a string, parse it mp_uint_t l; const char *s = mp_obj_str_get_data(args[0], &l); return mp_parse_num_integer(s, l, 0, NULL); #if MICROPY_PY_BUILTINS_FLOAT } else if (mp_obj_is_float(args[0])) { return mp_obj_new_int_from_float(mp_obj_float_get(args[0])); #endif } else { // try to convert to small int (eg from bool) return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0])); } case 2: default: { // should be a string, parse it // TODO proper error checking of argument types mp_uint_t l; const char *s = mp_obj_str_get_data(args[0], &l); return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]), NULL); } } }
/// \method unregister(obj) STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { mp_obj_poll_t *self = self_in; struct pollfd *entries = self->entries; int fd = mp_obj_get_int(obj_in); for (int i = self->len - 1; i >= 0; i--) { if (entries->fd == fd) { entries->fd = -1; break; } entries++; } // TODO raise KeyError if obj didn't exist in map return mp_const_none; }
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) { switch (typecode) { #if MICROPY_PY_BUILTINS_FLOAT case 'f': ((float*)p)[index] = mp_obj_float_get(val_in); break; case 'd': ((double*)p)[index] = mp_obj_float_get(val_in); break; #endif default: mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in)); break; } }
STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) { struct mp_stream_seek_t seek_s; // TODO: Could be uint64 seek_s.offset = mp_obj_get_int(args[1]); seek_s.whence = SEEK_SET; if (n_args == 3) { seek_s.whence = mp_obj_get_int(args[2]); } // In POSIX, it's error to seek before end of stream, we enforce it here. if (seek_s.whence == SEEK_SET && seek_s.offset < 0) { mp_raise_OSError(MP_EINVAL); } const mp_stream_p_t *stream_p = mp_get_stream(args[0]); int error; mp_uint_t res = stream_p->ioctl(args[0], MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error); if (res == MP_STREAM_ERROR) { mp_raise_OSError(error); } // TODO: Could be uint64 return mp_obj_new_int_from_uint(seek_s.offset); }
STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) { if (n_args == 0) { // get return mp_obj_new_int(system_get_cpu_freq() * 1000000); } else { // set mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; if (freq != 80 && freq != 160) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "frequency can only be either 80Mhz or 160MHz")); } system_update_cpu_freq(freq); return mp_const_none; } }
/** * def read(self, mask) */ static mp_obj_t class_event_read(mp_obj_t self_in, mp_obj_t mask_in) { struct class_event_t *self_p; uint32_t mask; self_p = MP_OBJ_TO_PTR(self_in); mask = mp_obj_get_int(mask_in); if (event_read(&self_p->event, &mask, sizeof(mask)) != sizeof(mask)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "failed to read event mask")); } return (MP_OBJ_NEW_SMALL_INT(mask)); }
STATIC mp_obj_t lwip_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) { lwip_socket_obj_t *socket = self_in; mp_uint_t timeout; if (timeout_in == mp_const_none) { timeout = -1; } else { #if MICROPY_PY_BUILTIN_FLOAT timeout = 1000 * mp_obj_get_float(timeout_in); #else timeout = 1000 * mp_obj_get_int(timeout_in); #endif } socket->timeout = timeout; return mp_const_none; }
// method socket.listen(backlog) STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog) { mod_network_socket_obj_t *self = self_in; if (self->nic == MP_OBJ_NULL) { // not connected nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOTCONN))); } int _errno; if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); } return mp_const_none; }
STATIC mp_obj_t mod_termios_setraw(mp_obj_t fd_in) { struct termios term; int fd = mp_obj_get_int(fd_in); int res = tcgetattr(fd, &term); RAISE_ERRNO(res, errno); term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); term.c_oflag = 0; term.c_cflag = (term.c_cflag & ~(CSIZE | PARENB)) | CS8; term.c_lflag = 0; term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 0; res = tcsetattr(fd, TCSAFLUSH, &term); RAISE_ERRNO(res, errno); return mp_const_none; }
/// \method write(value) /// Direct access to the DAC output (8 bit only at the moment). STATIC mp_obj_t pyb_dac_write(mp_obj_t self_in, mp_obj_t val) { pyb_dac_obj_t *self = self_in; if (self->state != 1) { DAC_ChannelConfTypeDef config; config.DAC_Trigger = DAC_TRIGGER_NONE; config.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; HAL_DAC_ConfigChannel(&DAC_Handle, &config, self->dac_channel); self->state = 1; } HAL_DAC_SetValue(&DAC_Handle, self->dac_channel, DAC_ALIGN_8B_R, mp_obj_get_int(val)); HAL_DAC_Start(&DAC_Handle, self->dac_channel); return mp_const_none; }
STATIC mp_obj_t pyb_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, 1, false); mp_int_t chn = mp_obj_get_int(args[0]); switch (chn) { case 0: return &pyb_adc_adc; case 1: return &pyb_adc_vdd3; default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "not a valid ADC Channel: %d", chn)); } }
// method socket.listen(backlog) STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog) { mod_network_socket_obj_t *self = self_in; if (self->nic == MP_OBJ_NULL) { // not connected // TODO I think we can listen even if not bound... mp_raise_OSError(MP_ENOTCONN); } int _errno; if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) { mp_raise_OSError(_errno); } return mp_const_none; }
STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // get return mp_obj_new_int(ets_get_cpu_frequency() * 1000000); } else { // set mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; if (freq != 80 && freq != 160 && freq != 240) { mp_raise_ValueError("frequency can only be either 80Mhz, 160MHz or 240MHz"); } /* system_update_cpu_freq(freq); */ return mp_const_none; } }
mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) { rtc_init_finalise(); if (n_args == 1) { // get date and time // note: need to call get time then get date to correctly access the registers RTC_DateTypeDef date; RTC_TimeTypeDef time; HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN); mp_obj_t tuple[8] = { mp_obj_new_int(2000 + date.Year), mp_obj_new_int(date.Month), mp_obj_new_int(date.Date), mp_obj_new_int(date.WeekDay), mp_obj_new_int(time.Hours), mp_obj_new_int(time.Minutes), mp_obj_new_int(time.Seconds), mp_obj_new_int(rtc_subsec_to_us(time.SubSeconds)), }; return mp_obj_new_tuple(8, tuple); } else { // set date and time mp_obj_t *items; mp_obj_get_array_fixed_n(args[1], 8, &items); RTC_DateTypeDef date; date.Year = mp_obj_get_int(items[0]) - 2000; date.Month = mp_obj_get_int(items[1]); date.Date = mp_obj_get_int(items[2]); date.WeekDay = mp_obj_get_int(items[3]); HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BIN); RTC_TimeTypeDef time; time.Hours = mp_obj_get_int(items[4]); time.Minutes = mp_obj_get_int(items[5]); time.Seconds = mp_obj_get_int(items[6]); time.SubSeconds = rtc_us_to_subsec(mp_obj_get_int(items[7])); time.TimeFormat = RTC_HOURFORMAT12_AM; time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; time.StoreOperation = RTC_STOREOPERATION_SET; HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BIN); return mp_const_none; } }
// calibration(None) // calibration(cal) // When an integer argument is provided, check that it falls in the range [-511 to 512] // and set the calibration value; otherwise return calibration value mp_obj_t pyb_rtc_calibration(mp_uint_t n_args, const mp_obj_t *args) { rtc_init_finalise(); mp_int_t cal; if (n_args == 2) { cal = mp_obj_get_int(args[1]); mp_uint_t cal_p, cal_m; if (cal < -511 || cal > 512) { #if defined(MICROPY_HW_RTC_USE_CALOUT) && MICROPY_HW_RTC_USE_CALOUT if ((cal & 0xfffe) == 0x0ffe) { // turn on/off X18 (PC13) 512Hz output // Note: // Output will stay active even in VBAT mode (and inrease current) if (cal & 1) { HAL_RTCEx_SetCalibrationOutPut(&RTCHandle, RTC_CALIBOUTPUT_512HZ); } else { HAL_RTCEx_DeactivateCalibrationOutPut(&RTCHandle); } return mp_obj_new_int(cal & 1); } else { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "calibration value out of range")); } #else nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "calibration value out of range")); #endif } if (cal > 0) { cal_p = RTC_SMOOTHCALIB_PLUSPULSES_SET; cal_m = 512 - cal; } else { cal_p = RTC_SMOOTHCALIB_PLUSPULSES_RESET; cal_m = -cal; } HAL_RTCEx_SetSmoothCalib(&RTCHandle, RTC_SMOOTHCALIB_PERIOD_32SEC, cal_p, cal_m); return mp_const_none; } else { // printf("CALR = 0x%x\n", (mp_uint_t) RTCHandle.Instance->CALR); // DEBUG // Test if CALP bit is set in CALR: if (RTCHandle.Instance->CALR & 0x8000) { cal = 512 - (RTCHandle.Instance->CALR & 0x1ff); } else { cal = -(RTCHandle.Instance->CALR & 0x1ff); } return mp_obj_new_int(cal); } }
/// \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; }
STATIC mp_obj_t mod_termios_tcsetattr(mp_obj_t fd_in, mp_obj_t when_in, mp_obj_t attrs_in) { struct termios term; int fd = mp_obj_get_int(fd_in); int when = mp_obj_get_int(when_in); if (when == 0) { // We don't export TCSANOW and friends to save on code space. Then // common lazy sense says that passing 0 should be godo enough, and // it is e.g. for glibc. But for other libc's it's not, so set just // treat 0 as defauling to TCSANOW. when = TCSANOW; } assert(MP_OBJ_IS_TYPE(attrs_in, &mp_type_list)); mp_obj_list_t *attrs = MP_OBJ_TO_PTR(attrs_in); term.c_iflag = mp_obj_get_int(attrs->items[0]); term.c_oflag = mp_obj_get_int(attrs->items[1]); term.c_cflag = mp_obj_get_int(attrs->items[2]); term.c_lflag = mp_obj_get_int(attrs->items[3]); mp_obj_list_t *cc = MP_OBJ_TO_PTR(attrs->items[6]); for (int i = 0; i < NCCS; i++) { if (i == VMIN || i == VTIME) { term.c_cc[i] = mp_obj_get_int(cc->items[i]); } else { mp_uint_t len; term.c_cc[i] = *mp_obj_str_get_data(cc->items[i], &len); } } int res = cfsetispeed(&term, mp_obj_get_int(attrs->items[4])); RAISE_ERRNO(res, errno); res = cfsetispeed(&term, mp_obj_get_int(attrs->items[5])); RAISE_ERRNO(res, errno); res = tcsetattr(fd, when, &term); RAISE_ERRNO(res, errno); return mp_const_none; }
STATIC mp_obj_t servo_obj_angle(mp_obj_t self_in, mp_obj_t angle) { pyb_servo_obj_t *self = self_in; #if MICROPY_ENABLE_FLOAT machine_int_t v = 152 + 85.0 * mp_obj_get_float(angle) / 90.0; #else machine_int_t v = 152 + 85 * mp_obj_get_int(angle) / 90; #endif if (v < 65) { v = 65; } if (v > 210) { v = 210; } switch (self->servo_id) { case 1: TIM2->CCR1 = v; break; case 2: TIM2->CCR2 = v; break; case 3: TIM2->CCR3 = v; break; case 4: TIM2->CCR4 = v; break; } return mp_const_none; }
static mp_obj_t py_image_binary(mp_obj_t image_obj, mp_obj_t threshold) { image_t *image; /* sanity checks */ PY_ASSERT_TRUE(sensor.framesize <= FRAMESIZE_QCIF); PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_GRAYSCALE); /* read arguments */ image = py_image_cobj(image_obj); int thresh = mp_obj_get_int(threshold); /* Threshold image */ imlib_binary(image, thresh); return mp_const_none; }