@@ -330,119 +330,116 @@ pbio_error_t pbio_port_dcm_thread(pbio_os_state_t *state, pbio_os_timer_t *timer
330330
331331 PBIO_OS_ASYNC_BEGIN (state );
332332
333- for (;;) {
334-
335- debug_pr ("Start device scan\n" );
336- dcm -> category = DCM_CATEGORY_NONE ;
337- dcm -> connected = false;
338-
339- // Wait for any device to be connected.
340- for (dcm -> count = 0 ; dcm -> count < DCM_LOOP_STEADY_STATE_COUNT ; dcm -> count ++ ) {
341- pbio_port_dcm_pin_state_t pin_state = pbio_port_dcm_get_state (pins );
342- pbio_port_dcm_category_t category = pbio_port_dcm_get_category (pin_state );
343- if (category != dcm -> category || category == DCM_CATEGORY_NONE ) {
344- dcm -> count = 0 ;
345- dcm -> category = category ;
346- }
347- PBIO_OS_AWAIT_MS (state , timer , DCM_LOOP_TIME_MS );
333+ debug_pr ("Start device scan\n" );
334+ dcm -> category = DCM_CATEGORY_NONE ;
335+ dcm -> connected = false;
336+
337+ // Wait for any device to be connected.
338+ for (dcm -> count = 0 ; dcm -> count < DCM_LOOP_STEADY_STATE_COUNT ; dcm -> count ++ ) {
339+ pbio_port_dcm_pin_state_t pin_state = pbio_port_dcm_get_state (pins );
340+ pbio_port_dcm_category_t category = pbio_port_dcm_get_category (pin_state );
341+ if (category != dcm -> category || category == DCM_CATEGORY_NONE ) {
342+ dcm -> count = 0 ;
343+ dcm -> category = category ;
348344 }
349- debug_pr ("Device kind detected: %d\n" , dcm -> category );
350- dcm -> connected = true;
345+ PBIO_OS_AWAIT_MS (state , timer , DCM_LOOP_TIME_MS );
346+ }
347+ debug_pr ("Device kind detected: %d\n" , dcm -> category );
348+ dcm -> connected = true;
351349
352- // Now run processes for devices that require a process, and otherwise
353- // wait for disconnection.
350+ // Now run processes for devices that require a process, and otherwise
351+ // wait for disconnection.
354352
355- if (dcm -> category == DCM_CATEGORY_LUMP ) {
356- debug_pr ("Continue as LUMP process\n" );
357- // Exit EV3 device manager, letting LUMP manager take over.
358- // That process runs until the device no longer reports data.
359- return PBIO_SUCCESS ;
360- }
353+ if (dcm -> category == DCM_CATEGORY_LUMP ) {
354+ debug_pr ("Continue as LUMP process\n" );
355+ // Exit EV3 device manager, letting LUMP manager take over.
356+ // That process runs until the device no longer reports data.
357+ return PBIO_SUCCESS ;
358+ }
361359
362- if (dcm -> category == DCM_CATEGORY_NXT_TEMPERATURE ) {
363- // This device has no way to passively detect disconnection, so
364- // we need to monitor I2C transactions to see if it is still there.
365- debug_pr ("Starting NXT temperature sensor process.\n" );
366- // TODO.
367- debug_pr ("Stopped NXT temperature sensor process.\n" );
368- continue ;
369- }
360+ if (dcm -> category == DCM_CATEGORY_NXT_TEMPERATURE ) {
361+ // This device has no way to passively detect disconnection, so
362+ // we need to monitor I2C transactions to see if it is still there.
363+ debug_pr ("Starting NXT temperature sensor process.\n" );
364+ // TODO.
365+ debug_pr ("Stopped NXT temperature sensor process.\n" );
366+ return PBIO_SUCCESS ;
367+ }
370368
371- if (dcm -> category == DCM_CATEGORY_NXT_LIGHT ) {
372- debug_pr ("Reading NXT Light Sensor until disconnected.\n" );
373- // While plugged in, get reflected and ambient light intensity.
374- while (!pbdrv_gpio_input (& pins -> p2 )) {
375- // Reflected intensity.
376- pbdrv_gpio_out_high (& pins -> p5 );
377- PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
378- dcm -> nxt_rgba .r = pbio_port_dcm_get_mv (pins , 1 );
379-
380- // Ambient intensity.
381- pbdrv_gpio_out_low (& pins -> p5 );
382- PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
383- dcm -> nxt_rgba .a = pbio_port_dcm_get_mv (pins , 1 );
384- }
385- continue ;
369+ if (dcm -> category == DCM_CATEGORY_NXT_LIGHT ) {
370+ debug_pr ("Reading NXT Light Sensor until disconnected.\n" );
371+ // While plugged in, get reflected and ambient light intensity.
372+ while (!pbdrv_gpio_input (& pins -> p2 )) {
373+ // Reflected intensity.
374+ pbdrv_gpio_out_high (& pins -> p5 );
375+ PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
376+ dcm -> nxt_rgba .r = pbio_port_dcm_get_mv (pins , 1 );
377+
378+ // Ambient intensity.
379+ pbdrv_gpio_out_low (& pins -> p5 );
380+ PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
381+ dcm -> nxt_rgba .a = pbio_port_dcm_get_mv (pins , 1 );
386382 }
383+ return PBIO_SUCCESS ;
384+ }
387385
388- if (dcm -> category == DCM_CATEGORY_NXT_COLOR ) {
389- debug_pr ("Initializing NXT Color Sensor.\n" );
390-
391- // The original firmware has a reset sequence where p6 is high and
392- // then p5 is toggled twice. It also works with 8 toggles, we can
393- // just use the send function with 0xff to achieve the same effect.
394- PBIO_OS_AWAIT (state , & dcm -> child , pbio_port_dcm_nxt_color_tx_msg (& dcm -> child , & dcm -> nxt_color_state , pins , timer , 0xff ));
395- PBIO_OS_AWAIT_MS (state , timer , 100 );
386+ if (dcm -> category == DCM_CATEGORY_NXT_COLOR ) {
387+ debug_pr ("Initializing NXT Color Sensor.\n" );
396388
397- // Set to full color mode.
398- PBIO_OS_AWAIT (state , & dcm -> child , pbio_port_dcm_nxt_color_tx_msg (& dcm -> child , & dcm -> nxt_color_state , pins , timer , 13 ));
389+ // The original firmware has a reset sequence where p6 is high and
390+ // then p5 is toggled twice. It also works with 8 toggles, we can
391+ // just use the send function with 0xff to achieve the same effect.
392+ PBIO_OS_AWAIT (state , & dcm -> child , pbio_port_dcm_nxt_color_tx_msg (& dcm -> child , & dcm -> nxt_color_state , pins , timer , 0xff ));
393+ PBIO_OS_AWAIT_MS (state , timer , 100 );
399394
400- // Receive all calibration info.
401- for (dcm -> count = 0 ; dcm -> count < sizeof (pbio_port_dcm_nxt_color_sensor_data_t ); dcm -> count ++ ) {
402- PBIO_OS_AWAIT (state , & dcm -> child ,
403- pbio_port_dcm_nxt_color_rx_msg (& dcm -> child , & dcm -> nxt_color_state , pins , timer , (uint8_t * )& dcm -> nxt_color_state .data + dcm -> count ));
404- }
395+ // Set to full color mode.
396+ PBIO_OS_AWAIT (state , & dcm -> child , pbio_port_dcm_nxt_color_tx_msg (& dcm -> child , & dcm -> nxt_color_state , pins , timer , 13 ));
405397
406- // REVISIT: Test checksum and exit on failure.
407- debug_pr ("Finished initializing NXT Color Sensor.\n" );
398+ // Receive all calibration info.
399+ for (dcm -> count = 0 ; dcm -> count < sizeof (pbio_port_dcm_nxt_color_sensor_data_t ); dcm -> count ++ ) {
400+ PBIO_OS_AWAIT (state , & dcm -> child ,
401+ pbio_port_dcm_nxt_color_rx_msg (& dcm -> child , & dcm -> nxt_color_state , pins , timer , (uint8_t * )& dcm -> nxt_color_state .data + dcm -> count ));
402+ }
408403
409- // While plugged in, toggle through available colors and measure intensity .
410- while (! pbdrv_gpio_input ( & pins -> p2 )) {
404+ // REVISIT: Test checksum and exit on failure .
405+ debug_pr ( "Finished initializing NXT Color Sensor.\n" );
411406
412- pbdrv_gpio_out_low (& pins -> p5 );
413- PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
414- dcm -> nxt_rgba .a = pbio_port_dcm_get_mv (pins , 6 );
407+ // While plugged in, toggle through available colors and measure intensity.
408+ while (!pbdrv_gpio_input (& pins -> p2 )) {
415409
416- pbdrv_gpio_out_high (& pins -> p5 );
417- PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
418- dcm -> nxt_rgba .r = pbio_port_dcm_get_mv (pins , 6 );
410+ pbdrv_gpio_out_low (& pins -> p5 );
411+ PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
412+ dcm -> nxt_rgba .a = pbio_port_dcm_get_mv (pins , 6 );
419413
420- pbdrv_gpio_out_low (& pins -> p5 );
421- PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 2000 ));
422- dcm -> nxt_rgba .g = pbio_port_dcm_get_mv (pins , 6 );
414+ pbdrv_gpio_out_high (& pins -> p5 );
415+ PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
416+ dcm -> nxt_rgba .r = pbio_port_dcm_get_mv (pins , 6 );
423417
424- pbdrv_gpio_out_high (& pins -> p5 );
425- PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
426- dcm -> nxt_rgba .b = pbio_port_dcm_get_mv (pins , 6 );
427- }
428418 pbdrv_gpio_out_low (& pins -> p5 );
429- continue ;
419+ PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 2000 ));
420+ dcm -> nxt_rgba .g = pbio_port_dcm_get_mv (pins , 6 );
421+
422+ pbdrv_gpio_out_high (& pins -> p5 );
423+ PBIO_OS_AWAIT (state , & dcm -> child , pbdrv_adc_await_new_samples (& dcm -> child , & timer -> start , 200 ));
424+ dcm -> nxt_rgba .b = pbio_port_dcm_get_mv (pins , 6 );
430425 }
426+ pbdrv_gpio_out_low (& pins -> p5 );
427+ return PBIO_SUCCESS ;
428+ }
431429
432- // For everything else, disconnection is detected by just one pin going
433- // high rather than all pins going back to the none state. This is
434- // because other pins are used for data transfer and may vary between
435- // high/low during normal operation.
436- for (dcm -> count = 0 ; dcm -> count < DCM_LOOP_DISCONNECT_COUNT ; dcm -> count ++ ) {
437- // Monitor P5 for EV3 analog, P2 for all NXT sensors.
438- const pbdrv_gpio_t * gpio = dcm -> category == DCM_CATEGORY_EV3_ANALOG ? & pins -> p5 : & pins -> p2 ;
439- if (!pbdrv_gpio_input (gpio )) {
440- dcm -> count = 0 ;
441- }
442- PBIO_OS_AWAIT_MS (state , timer , DCM_LOOP_TIME_MS );
430+ // For everything else, disconnection is detected by just one pin going
431+ // high rather than all pins going back to the none state. This is
432+ // because other pins are used for data transfer and may vary between
433+ // high/low during normal operation.
434+ for (dcm -> count = 0 ; dcm -> count < DCM_LOOP_DISCONNECT_COUNT ; dcm -> count ++ ) {
435+ // Monitor P5 for EV3 analog, P2 for all NXT sensors.
436+ const pbdrv_gpio_t * gpio = dcm -> category == DCM_CATEGORY_EV3_ANALOG ? & pins -> p5 : & pins -> p2 ;
437+ if (!pbdrv_gpio_input (gpio )) {
438+ dcm -> count = 0 ;
443439 }
444- debug_pr ( "Device disconnected\n" );
440+ PBIO_OS_AWAIT_MS ( state , timer , DCM_LOOP_TIME_MS );
445441 }
442+ debug_pr ("Device disconnected\n" );
446443
447444 PBIO_OS_ASYNC_END (PBIO_SUCCESS );
448445}
0 commit comments