in4lio / ev3dev-c

LEGO MINDSTORMS EV3 Debian C library + Python, Ruby and Perl wrappers
MIT License
71 stars 30 forks source link

Propose new functions to support configuration of manually specified sensors #20

Open tcwan opened 6 years ago

tcwan commented 6 years ago

I'm trying to write some support routines to allow someone to specify the sensor type (from ev3_sensor.h) and the port (INPUT_1/2/3/4) that it should be configured in.

The API would look like the following:

/** Configure the specific sensor type fpr a given port and find the sequence number
 *
 * @param type_inx Sensor type. [From ev3_sensor.h]
 * @param port EV3 port.
 * @param extport Extended port (used by Motor Multiplexers).
 * @param[out] sn Buffer for the sequence number.
 * @return Flag - the sensor is found.
 *
 */
bool dvcs_config_sensor_type_for_port(INX_T type_inx, U8 port, U8 extport, U8 *sn );

From the examples, I'd need to call set_port_mode_inx() and then set_port_set_device() to configure the device/sensor correctly.

Currently, there is no API to determine the correct port mode for a given sensor type. I've attempted to come up with a routine to do so but I'm not sure if it is all correct since I'm still quite confused regarding how configuration of the port for specific sensors are supposed to be done:

/* Convert a Sensor Type into the appropriate Port Mode for configuring set_port_mode_inx() */
INX_T ev3_sensor_port_mode_inx(INX_T type_inx ) {

    switch ( type_inx ) {
    case EV3_ANALOG_XX:
        return INPUT_EV3_ANALOG;
    case NXT_ANALOG:
        return INPUT_NXT_ANALOG;
    case PIXY_LEGO:
        return INPUT_NXT_I2C;
    case DI_DFLEX:
        return NXT_ANALOG;
    case FCL_9DOF:
    case FCL_ADC:
    case FCL_ALTITUDE:
    case FCL_GESTURE:
    case FCL_HUMIDITY:
    case FCL_IR:
    case FCL_LIGHT:
        return INPUT_EV3_UART;
    case HT_NXT_COLOR:
    case HT_NXT_ANGLE:
    case HT_NXT_ACCEL:
    case HT_NXT_BAROMETRIC:
    case HT_NXT_COLOR_V2:
        return INPUT_NXT_I2C;
    case HT_NXT_EOPD:
    case HT_NXT_FORCE:
    case HT_NXT_GYRO:
        return NXT_ANALOG;
    case HT_NXT_IR_LINK:
    case HT_NXT_IR_RECEIVER:
    case HT_NXT_PIR:
    case HT_NXT_COMPASS:
        return INPUT_NXT_I2C;
    case HT_NXT_MAG:
        return NXT_ANALOG;
    case HT_NXT_IR_SEEK_V2:
    case HT_NXT_SMUX:
    case HT_SUPER_PRO:
        return INPUT_NXT_I2C;
    case LEGO_EV3_US:
    case LEGO_EV3_GYRO:
    case LEGO_EV3_COLOR:
        return INPUT_EV3_UART;
    case LEGO_EV3_TOUCH:
        return INPUT_EV3_ANALOG;
    case LEGO_EV3_IR:
        return INPUT_EV3_UART;
    case WEDO_HUB:
        return PORT_MODE__NONE_;                    // FIXME: USB Port (?)
    case WEDO_MOTION:
    case WEDO_TILT:
        return WEDO_AUTO;
    case LEGO_POWER_STORAGE:
    case LEGO_NXT_TEMP:
        return INPUT_NXT_I2C;
    case LEGO_NXT_TOUCH:
    case LEGO_NXT_LIGHT:
    case LEGO_NXT_SOUND:
        return INPUT_NXT_ANALOG;
    case LEGO_NXT_US:
        return INPUT_NXT_I2C;
    case MI_XG1300L:
    case MS_ABSOLUTE_IMU:
    case MS_ANGLE:
    case MS_LIGHT_ARRAY:
    case MS_LINE_LEADER:
    case MS_NXTCAM:
    case MS_NXTCAM5:
    case MS_8CH_SERVO:
    case MS_PPS58_NX:
    case MS_PIXY_ADAPTER:
        return INPUT_NXT_I2C;
    case MS_EV3_SMUX:
        return MS_EV3_SMUX_ANALOG;              // FIXME: There are two settings, MS_EV3_SMUX_ANALOG and MS_EV3_SMUX_UART
    case MS_NXTMMX:
        return MS_NXTMMX_TACHO_MOTOR;
    case MS_NXT_TOUCH_MUX:
        return INPUT_NXT_ANALOG;
    default:
        return PORT_MODE__NONE_;
}

In the servo snippet, you have:

sn_port = ev3_search_port( INPUT_1, EXT_PORT__NONE_ );
set_port_mode_inx( sn_port, INPUT_NXT_ANALOG );
set_port_set_device( sn_port, ( char *) ev3_sensor_type( MS_8CH_SERVO ));

I think INPUT_NXT_ANALOG should be INPUT_NXT_I2C instead based on this. But then there are also specific cases such as MS_NXTMMX_TACHO_MOTOR which I presume is the correct port mode for MS_NXTMMX.

I'd like to propose that ev3_sensor_port_mode_inx() (or whichever naming is better) is added to the ev3dev-c library.

tcwan commented 6 years ago

You can see the support routines here.