@@ -46,7 +46,11 @@ typedef enum {
4646 /**
4747 * Pin 2 GPIO is high.
4848 */
49+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
4950 PIN_STATE_P2_HIGH = 1 << 4 ,
51+ #else
52+ PIN_STATE_P2_HIGH = 0 ,
53+ #endif
5054 /**
5155 * Pin 5 GPIO is high.
5256 */
@@ -70,6 +74,7 @@ typedef enum {
7074 * only one known device.
7175 */
7276typedef enum {
77+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
7378 /**
7479 * Device category is EV3 UART sensor. P6 and ADC6 are arbitrary (can be set by sensor TX).
7580 */
@@ -78,6 +83,7 @@ typedef enum {
7883 * Device category is EV3 analog sensor.
7984 */
8085 DCM_CATEGORY_EV3_ANALOG = PIN_STATE_ADC1_100_to_3100 | PIN_STATE_P2_HIGH ,
86+ #endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
8187 /**
8288 * No device is connected.
8389 */
@@ -148,22 +154,30 @@ static uint32_t pbio_port_dcm_get_mv(const pbdrv_ioport_pins_t *pins, uint8_t pi
148154 */
149155static pbio_port_dcm_category_t pbio_port_dcm_get_category (pbio_port_dcm_pin_state_t state ) {
150156
157+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
151158 if ((state | PIN_STATE_MASK_P6 ) == DCM_CATEGORY_LUMP ) {
152159 return DCM_CATEGORY_LUMP ;
153160 }
161+ #endif
154162
155163 if ((state | PIN_STATE_MASK_P6 ) == DCM_CATEGORY_NXT_COLOR ) {
156164 return DCM_CATEGORY_NXT_COLOR ;
157165 }
158166
167+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
159168 if ((state | PIN_STATE_MASK_P1 ) == DCM_CATEGORY_NXT_ANALOG_OTHER ) {
160169 return DCM_CATEGORY_NXT_ANALOG_OTHER ;
161170 }
171+ #endif
162172
163173 if ((state | PIN_STATE_MASK_P1 ) == DCM_CATEGORY_NXT_LIGHT ) {
164174 return DCM_CATEGORY_NXT_LIGHT ;
165175 }
166176
177+ #if !PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
178+ return DCM_CATEGORY_NONE ;
179+ #endif
180+
167181 // All other can be tested for equality.
168182 return (pbio_port_dcm_category_t )state ;
169183}
@@ -191,17 +205,19 @@ static pbio_port_dcm_pin_state_t pbio_port_dcm_get_state(const pbdrv_ioport_pins
191205 }
192206
193207 // Get the GPIO state for pins 2, 5, and 6.
208+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
194209 if (pbdrv_gpio_input (& pins -> p2 )) {
195210 state |= PIN_STATE_P2_HIGH ;
196211 }
212+ #endif
197213 if (pbdrv_gpio_input (& pins -> p5 )) {
198214 state |= PIN_STATE_P5_HIGH ;
199215 }
200216 if (pbdrv_gpio_input (& pins -> p6 )) {
201217 state |= PIN_STATE_P6_HIGH ;
202218 }
203219
204- #if DEBUG == 2
220+ #if DEBUG == 2 && PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
205221 debug_pr ("%d::: p1: %dmv p2:%d p5:%d p6:%d (%d mv)\n" ,
206222 pbio_port_dcm_get_category (state ),
207223 adc1 ,
@@ -350,6 +366,7 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
350366 // Now run processes for devices that require a process, and otherwise
351367 // wait for disconnection.
352368
369+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
353370 if (dcm -> category == DCM_CATEGORY_LUMP ) {
354371 debug_pr ("Continue as LUMP process\n" );
355372 // Exit EV3 device manager, letting LUMP manager take over.
@@ -365,6 +382,7 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
365382 debug_pr ("Stopped NXT temperature sensor process.\n" );
366383 return PBIO_SUCCESS ;
367384 }
385+ #endif
368386
369387 if (dcm -> category == DCM_CATEGORY_NXT_LIGHT ) {
370388 debug_pr ("Reading NXT Light Sensor until disconnected.\n" );
@@ -427,9 +445,11 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
427445 dcm -> nxt_rgba .b = pbio_port_dcm_get_mv (pins , 6 );
428446 }
429447 pbdrv_gpio_out_low (& pins -> p5 );
448+ debug_pr ("Color Sensor disconnected.\n" );
430449 return PBIO_SUCCESS ;
431450 }
432451
452+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
433453 // For everything else, disconnection is detected by just one pin going
434454 // high rather than all pins going back to the none state. This is
435455 // because other pins are used for data transfer and may vary between
@@ -443,6 +463,7 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
443463 PBIO_OS_AWAIT_MS (state , timer , DCM_LOOP_TIME_MS );
444464 }
445465 debug_pr ("Device disconnected\n" );
466+ #endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
446467
447468 PBIO_OS_ASYNC_END (PBIO_SUCCESS );
448469}
@@ -480,6 +501,7 @@ pbio_error_t pbio_port_dcm_assert_type_id(pbio_port_dcm_t *dcm, lego_device_type
480501 }
481502
482503 switch (* expected_type_id ) {
504+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
483505 case LEGO_DEVICE_TYPE_ID_ANY_LUMP_UART :
484506 return matches_category (dcm , DCM_CATEGORY_LUMP );
485507 case LEGO_DEVICE_TYPE_ID_EV3_TOUCH_SENSOR :
@@ -503,15 +525,29 @@ pbio_error_t pbio_port_dcm_assert_type_id(pbio_port_dcm_t *dcm, lego_device_type
503525 PBIO_SUCCESS : PBIO_ERROR_NO_DEV ;
504526 default :
505527 return PBIO_ERROR_NO_DEV ;
528+ #else
529+ // On NXT without GPIO2, we can only definitively assert the color
530+ // sensor and the light sensor.
531+ case LEGO_DEVICE_TYPE_ID_ANY_LUMP_UART :
532+ return PBIO_ERROR_NO_DEV ;
533+ case LEGO_DEVICE_TYPE_ID_NXT_COLOR_SENSOR :
534+ return matches_category (dcm , DCM_CATEGORY_NXT_COLOR );
535+ case LEGO_DEVICE_TYPE_ID_NXT_LIGHT_SENSOR :
536+ return matches_category (dcm , DCM_CATEGORY_NXT_LIGHT );
537+ default :
538+ return PBIO_SUCCESS ;
539+ #endif // PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
506540 }
507541}
508542
509543uint32_t pbio_port_dcm_get_analog_value (pbio_port_dcm_t * dcm , const pbdrv_ioport_pins_t * pins , bool active ) {
510544
545+ #if PBDRV_CONFIG_IOPORT_HAS_GPIO_P2
511546 // This category measures analog on pin 6.
512547 if (dcm -> category == DCM_CATEGORY_EV3_ANALOG ) {
513548 return pbio_port_dcm_get_mv (pins , 6 );
514549 }
550+ #endif
515551
516552 // Some NXT sensors have an active mode by setting P5 high.
517553 if (active ) {
0 commit comments