Skip to content

Commit ef904f5

Browse files
committed
pybricks.iodevices.I2CDevice: Implement for embedded Pybricks.
Also supports async API. This replaces the smbus version for ev3dev. Some missing methods will be reinstated in future commits.
1 parent 20930e5 commit ef904f5

File tree

6 files changed

+177
-117
lines changed

6 files changed

+177
-117
lines changed

bricks/ev3/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define PYBRICKS_PY_EXPERIMENTAL (1)
3131
#define PYBRICKS_PY_HUBS (1)
3232
#define PYBRICKS_PY_IODEVICES (1)
33+
#define PYBRICKS_PY_IODEVICES_I2CDEVICE (1)
3334
#define PYBRICKS_PY_IODEVICES_XBOX_CONTROLLER (0)
3435
#define PYBRICKS_PY_MEDIA (1)
3536
#define PYBRICKS_PY_NXTDEVICES (1)

lib/pbio/platform/ev3/platform.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
322322
#if PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
323323
.supported_modes = PBIO_PORT_MODE_UART,
324324
#else // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
325-
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_LEGO_DCM,
325+
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_I2C | PBIO_PORT_MODE_LEGO_DCM,
326326
#endif
327327
},
328328
{
@@ -345,7 +345,7 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
345345
.adc_p1 = 8,
346346
.adc_p6 = 7,
347347
},
348-
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_LEGO_DCM,
348+
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_I2C | PBIO_PORT_MODE_LEGO_DCM,
349349
},
350350
{
351351
.port_id = PBIO_PORT_ID_3,
@@ -367,7 +367,7 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
367367
.adc_p1 = 10,
368368
.adc_p6 = 9,
369369
},
370-
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_LEGO_DCM,
370+
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_I2C | PBIO_PORT_MODE_LEGO_DCM,
371371
},
372372
{
373373
.port_id = PBIO_PORT_ID_4,
@@ -389,7 +389,7 @@ const pbdrv_ioport_platform_data_t pbdrv_ioport_platform_data[PBDRV_CONFIG_IOPOR
389389
.adc_p1 = 12,
390390
.adc_p6 = 11,
391391
},
392-
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_LEGO_DCM,
392+
.supported_modes = PBIO_PORT_MODE_UART | PBIO_PORT_MODE_I2C | PBIO_PORT_MODE_LEGO_DCM,
393393
},
394394
};
395395

lib/pbio/src/port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ pbio_error_t pbio_port_set_mode(pbio_port_t *port, pbio_port_mode_t mode) {
562562
// access UART from their own event loop.
563563
pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_UART);
564564
return PBIO_SUCCESS;
565+
case PBIO_PORT_MODE_I2C:
566+
// Enable I2C on the port. User controlled; no process needed here.
567+
pbdrv_ioport_p5p6_set_mode(port->pdata->pins, PBDRV_IOPORT_P5P6_MODE_I2C);
568+
return PBIO_SUCCESS;
565569
case PBIO_PORT_MODE_QUADRATURE:
566570
return PBIO_SUCCESS;
567571
default:

pybricks/iodevices/iodevices.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
extern const mp_obj_type_t pb_type_iodevices_PUPDevice;
1414
extern const mp_obj_type_t pb_type_uart_device;
1515

16+
#if PYBRICKS_PY_IODEVICES_I2CDEVICE
17+
extern const mp_obj_type_t pb_type_iodevices_I2CDevice;
18+
#endif
19+
1620
#if PYBRICKS_PY_PUPDEVICES
1721

1822
extern const mp_obj_type_t pb_type_iodevices_LWP3Device;
@@ -24,7 +28,6 @@ extern const mp_obj_type_t pb_type_iodevices_XboxController;
2428

2529
extern const mp_obj_type_t pb_type_iodevices_AnalogSensor;
2630
extern const mp_obj_type_t pb_type_iodevices_Ev3devSensor;
27-
extern const mp_obj_type_t pb_type_iodevices_I2CDevice;
2831

2932
#endif // PYBRICKS_PY_EV3DEVICES
3033

pybricks/iodevices/pb_module_iodevices.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ static const mp_rom_map_elem_t iodevices_globals_table[] = {
1212
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_iodevices) },
1313
{ MP_ROM_QSTR(MP_QSTR_PUPDevice), MP_ROM_PTR(&pb_type_iodevices_PUPDevice) },
1414
{ MP_ROM_QSTR(MP_QSTR_UARTDevice), MP_ROM_PTR(&pb_type_uart_device) },
15+
#if PYBRICKS_PY_IODEVICES_I2CDEVICE
16+
{ MP_ROM_QSTR(MP_QSTR_I2CDevice), MP_ROM_PTR(&pb_type_iodevices_I2CDevice) },
17+
#endif
1518
#if PYBRICKS_PY_PUPDEVICES && PYBRICKS_PY_PUPDEVICES_REMOTE
1619
{ MP_ROM_QSTR(MP_QSTR_LWP3Device), MP_ROM_PTR(&pb_type_iodevices_LWP3Device) },
1720
#endif
@@ -22,7 +25,6 @@ static const mp_rom_map_elem_t iodevices_globals_table[] = {
2225
{ MP_ROM_QSTR(MP_QSTR_LUMPDevice), MP_ROM_PTR(&pb_type_iodevices_PUPDevice) },
2326
{ MP_ROM_QSTR(MP_QSTR_AnalogSensor), MP_ROM_PTR(&pb_type_iodevices_AnalogSensor) },
2427
{ MP_ROM_QSTR(MP_QSTR_Ev3devSensor), MP_ROM_PTR(&pb_type_iodevices_Ev3devSensor) },
25-
{ MP_ROM_QSTR(MP_QSTR_I2CDevice), MP_ROM_PTR(&pb_type_iodevices_I2CDevice) },
2628
#if PYBRICKS_PY_COMMON_MOTORS
2729
{ MP_ROM_QSTR(MP_QSTR_DCMotor), MP_ROM_PTR(&pb_type_DCMotor) },
2830
#endif

0 commit comments

Comments
 (0)