pimoroni-pico/micropython/modules/pimoroni_i2c/pimoroni_i2c.c

69 wiersze
2.5 KiB
C
Czysty Zwykły widok Historia

#include "pimoroni_i2c.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// PimoroniI2C Class
////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Add finaliser for Pimoroni I2C This is the final piece of the puzzle. Prior to this rather considerable change, Pimoroni breakouts were not de-init'ing I2C when they failed to init() This change adds a __del__ method which cleans up the I2C instance attached to a MicroPython object. Under the hood this calls i2c_deinit() and resets the associated pins to their default state. This means that I2C is now cleaned up during a *soft* reset, so running a script with the wrong pins, seeing an error, changing the pins and running it again will not result in subsequent I2C errors. Previously a hard reset was required. To recreate on Breakout Garden run the following code: ``` from breakout_potentiometer import BreakoutPotentiometer from pimoroni_i2c import PimoroniI2C i2c = PimoroniI2C() pot = BreakoutPotentiometer(i2c) ``` This will fail correctly with "Potentiometer breakout not found when initialising." (The default pins are configured for Pico Explorer) Now change that to the following and run again without hard-resetting: ``` from breakout_potentiometer import BreakoutPotentiometer from pimoroni_i2c import PimoroniI2C i2c = PimoroniI2C(4, 5) pot = BreakoutPotentiometer(i2c) ``` This should now work, since the failed I2C instance was cleaned up. Without this change, the second attempt would result in an inexplicable failure. Since most? (many?) Pico users do not have a reset button, this trap requiring a hard-reset is pretty nasty and would likely have resulted in a support nightmare. Whew.
2021-05-18 08:48:41 +00:00
MP_DEFINE_CONST_FUN_OBJ_1(PimoroniI2C___del___obj, PimoroniI2C___del__);
static const mp_rom_map_elem_t PimoroniI2C_locals_dict_table[] = {
Add finaliser for Pimoroni I2C This is the final piece of the puzzle. Prior to this rather considerable change, Pimoroni breakouts were not de-init'ing I2C when they failed to init() This change adds a __del__ method which cleans up the I2C instance attached to a MicroPython object. Under the hood this calls i2c_deinit() and resets the associated pins to their default state. This means that I2C is now cleaned up during a *soft* reset, so running a script with the wrong pins, seeing an error, changing the pins and running it again will not result in subsequent I2C errors. Previously a hard reset was required. To recreate on Breakout Garden run the following code: ``` from breakout_potentiometer import BreakoutPotentiometer from pimoroni_i2c import PimoroniI2C i2c = PimoroniI2C() pot = BreakoutPotentiometer(i2c) ``` This will fail correctly with "Potentiometer breakout not found when initialising." (The default pins are configured for Pico Explorer) Now change that to the following and run again without hard-resetting: ``` from breakout_potentiometer import BreakoutPotentiometer from pimoroni_i2c import PimoroniI2C i2c = PimoroniI2C(4, 5) pot = BreakoutPotentiometer(i2c) ``` This should now work, since the failed I2C instance was cleaned up. Without this change, the second attempt would result in an inexplicable failure. Since most? (many?) Pico users do not have a reset button, this trap requiring a hard-reset is pretty nasty and would likely have resulted in a support nightmare. Whew.
2021-05-18 08:48:41 +00:00
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&PimoroniI2C___del___obj) },
};
static MP_DEFINE_CONST_DICT(PimoroniI2C_locals_dict, PimoroniI2C_locals_dict_table);
*/
static const mp_machine_i2c_p_t machine_i2c_p = {
.transfer = mp_machine_i2c_transfer_adaptor,
.transfer_single = machine_i2c_transfer_single,
};
/***** Class Definition *****/
#ifdef MP_DEFINE_CONST_OBJ_TYPE
MP_DEFINE_CONST_OBJ_TYPE(
PimoroniI2C_type,
MP_QSTR_pimoroni_i2c,
MP_TYPE_FLAG_NONE,
make_new, PimoroniI2C_make_new,
print, PimoroniI2C_print,
protocol, &machine_i2c_p,
locals_dict, (mp_obj_dict_t*)&mp_machine_i2c_locals_dict
);
#else
const mp_obj_type_t PimoroniI2C_type = {
{ &mp_type_type },
.name = MP_QSTR_pimoroni_i2c,
.print = PimoroniI2C_print,
.make_new = PimoroniI2C_make_new,
.protocol = &machine_i2c_p,
.locals_dict = (mp_obj_dict_t*)&mp_machine_i2c_locals_dict,
};
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
// breakout_potentiometer Module
////////////////////////////////////////////////////////////////////////////////////////////////////
/***** Globals Table *****/
static const mp_map_elem_t pimoroni_i2c_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pimoroni_i2c) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PimoroniI2C), (mp_obj_t)&PimoroniI2C_type },
};
static MP_DEFINE_CONST_DICT(mp_module_pimoroni_i2c_globals, pimoroni_i2c_globals_table);
/***** Module Definition *****/
const mp_obj_module_t pimoroni_i2c_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_pimoroni_i2c_globals,
};
////////////////////////////////////////////////////////////////////////////////////////////////////
#if MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_pimoroni_i2c, pimoroni_i2c_user_cmodule, MODULE_PIMORONI_I2C_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_pimoroni_i2c, pimoroni_i2c_user_cmodule);
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////