mp_obj_t mlx90620_read() { float temp[64]; float max_temp = FLT_MIN; float min_temp = FLT_MAX; image_t img = { .w=4, .h=16, .bpp=1, .pixels=xalloc(16*4) }; // get raw temperatures mlx90620_read_to(temp); // normalize temp readings for (int i=0; i<64; i++) { if (temp[i] > max_temp) { max_temp = temp[i]; } else if (temp[i] < min_temp) { min_temp = temp[i]; } } for (int i=0; i<64; i++) { img.pixels[i] = (uint8_t)(((temp[i]-min_temp)/(max_temp-min_temp))*255.0f); } return py_image_from_struct(&img); } mp_obj_t mlx90620_read_raw() { float *t = m_new(float, 64); mp_obj_t t_list = mp_obj_new_list(0, NULL); // get raw temperatures mlx90620_read_to(t); // normalize temp readings for (int i=0; i<64; i++) { mp_obj_list_append(t_list, mp_obj_new_float(t[i])); } return t_list; } mp_obj_t mlx90620_init() { uint8_t cmd_buf[5]; uint8_t EEPROM_DATA[256]; // Init I2C soft_i2c_init(); // Read EEPROM data cmd_buf[0]=REG_EEPROM_DATA; soft_i2c_write_bytes(MLX_EEPROM_ADDR, cmd_buf, 1, false); soft_i2c_read_bytes(MLX_EEPROM_ADDR, EEPROM_DATA, 256, true); // Write oscillator trimming value uint8_t trim = EEPROM_DATA[OSC_TRIM_OFFSET]; memcpy(cmd_buf, (uint8_t [5]){WRITE_OSC_TRIM, (uint8_t)(trim-0xAA), trim, 0x56, 0x00}, 5); soft_i2c_write_bytes(MLX_SLAVE_ADDR, cmd_buf, sizeof(cmd_buf), true); // Write configuration register uint8_t lsb = 0x0A; //0x09==16Hz uint8_t msb = 0x74; memcpy(cmd_buf, (uint8_t [5]){SET_CONFIG_DATA, (uint8_t)(lsb-0x55), lsb, (uint8_t)(msb-0x55), msb}, 5); soft_i2c_write_bytes(MLX_SLAVE_ADDR, cmd_buf, sizeof(cmd_buf), true); // Calculate Ta constants v_th = (256 * EEPROM_DATA[VTH_H] + EEPROM_DATA[VTH_L]); k_t1 = (256 * EEPROM_DATA[KT1_H] + EEPROM_DATA[KT1_L]) / 1024.0f; k_t2 = (256 * EEPROM_DATA[KT2_H] + EEPROM_DATA[KT2_L]) / 1048576.0f; emissivity = ((unsigned int)256 * EEPROM_DATA[CAL_EMIS_H] + EEPROM_DATA[CAL_EMIS_L]) / 32768.0f; k_t1_sq = k_t1 * k_t1; a_cp = (int8_t)EEPROM_DATA[CAL_ACP]; b_cp = (int8_t)EEPROM_DATA[CAL_BCP]; tgc = (int8_t)EEPROM_DATA[CAL_TGC]; b_i_scale = EEPROM_DATA[CAL_BI_SCALE]; // Hack for (int i=0; i<8; i++) { EEPROM_DATA[i]=EEPROM_DATA[i+8]; EEPROM_DATA[i+64]=EEPROM_DATA[i+8+64]; } for (int i=0; i<64; i++) { // Read pixel offsets a_ij[i] = (int8_t)EEPROM_DATA[i]; // Read slope coefficients b_ij[i] = (int8_t)EEPROM_DATA[i+4]; } return mp_const_true; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mlx90620_init_obj, mlx90620_init); STATIC MP_DEFINE_CONST_FUN_OBJ_0(mlx90620_read_obj, mlx90620_read); STATIC MP_DEFINE_CONST_FUN_OBJ_0(mlx90620_read_raw_obj, mlx90620_read_raw); static const mp_map_elem_t globals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_mlx) }, //{ MP_OBJ_NEW_QSTR(MP_QSTR_HZ_8), MP_OBJ_NEW_SMALL_INT(MLX_HZ_8)}, //{ MP_OBJ_NEW_QSTR(MP_QSTR_HZ_16), MP_OBJ_NEW_SMALL_INT(MLX_HZ_16)}, //{ MP_OBJ_NEW_QSTR(MP_QSTR_HZ_32), MP_OBJ_NEW_SMALL_INT(MLX_HZ_32)}, //{ MP_OBJ_NEW_QSTR(MP_QSTR_HZ_64), MP_OBJ_NEW_SMALL_INT(MLX_HZ_64)}, { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&mlx90620_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mlx90620_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read_raw), (mp_obj_t)&mlx90620_read_raw_obj }, }; STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table); static const mp_obj_module_t mlx_module = { .base = { &mp_type_module }, .name = MP_QSTR_mlx, .globals = (mp_obj_t)&globals_dict, }; const mp_obj_module_t *py_mlx90620_init() { return &mlx_module; }
STATIC mp_obj_t esp_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { // TODO support additional args beyond the first two struct addrinfo *res = NULL; _socket_getaddrinfo2(args[0], args[1], &res); mp_obj_t ret_list = mp_obj_new_list(0, NULL); for (struct addrinfo *resi = res; resi; resi = resi->ai_next) { mp_obj_t addrinfo_objs[5] = { mp_obj_new_int(resi->ai_family), mp_obj_new_int(resi->ai_socktype), mp_obj_new_int(resi->ai_protocol), mp_obj_new_str(resi->ai_canonname, strlen(resi->ai_canonname)), mp_const_none }; if (resi->ai_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)resi->ai_addr; // This looks odd, but it's really just a u32_t ip4_addr_t ip4_addr = { .addr = addr->sin_addr.s_addr }; char buf[16]; ip4addr_ntoa_r(&ip4_addr, buf, sizeof(buf)); mp_obj_t inaddr_objs[2] = { mp_obj_new_str(buf, strlen(buf)), mp_obj_new_int(ntohs(addr->sin_port)) }; addrinfo_objs[4] = mp_obj_new_tuple(2, inaddr_objs); } mp_obj_list_append(ret_list, mp_obj_new_tuple(5, addrinfo_objs)); } if (res) lwip_freeaddrinfo(res); return ret_list; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_socket_getaddrinfo_obj, 2, 6, esp_socket_getaddrinfo); STATIC mp_obj_t esp_socket_initialize() { static int initialized = 0; if (!initialized) { ESP_LOGI("modsocket", "Initializing"); tcpip_adapter_init(); initialized = 1; } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_socket_initialize_obj, esp_socket_initialize); STATIC const mp_map_elem_t mp_module_socket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) }, { MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&esp_socket_initialize_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&get_socket_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&esp_socket_getaddrinfo_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) }, { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_RAW), MP_OBJ_NEW_SMALL_INT(SOCK_RAW) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_TCP), MP_OBJ_NEW_SMALL_INT(IPPROTO_TCP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_UDP), MP_OBJ_NEW_SMALL_INT(IPPROTO_UDP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IPPROTO_IP), MP_OBJ_NEW_SMALL_INT(IPPROTO_IP) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(SOL_SOCKET) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(SO_REUSEADDR) }, { MP_OBJ_NEW_QSTR(MP_QSTR_IP_ADD_MEMBERSHIP), MP_OBJ_NEW_SMALL_INT(IP_ADD_MEMBERSHIP) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_socket_globals, mp_module_socket_globals_table); const mp_obj_module_t mp_module_usocket = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&mp_module_socket_globals, };