|
| 1 | +#include <stdint.h> |
| 2 | +#include <lego/device.h> |
| 3 | + |
| 4 | +/** |
| 5 | + * Gets the minimum time needed before stale data is discarded. |
| 6 | + * |
| 7 | + * This is empirically determined based on sensor experiments. |
| 8 | + * |
| 9 | + * @param [in] id The device type ID. |
| 10 | + * @param [in] mode The device mode. |
| 11 | + * @return Required delay in milliseconds. |
| 12 | + */ |
| 13 | +uint32_t lego_device_stale_data_delay(lego_device_type_id_t id, uint8_t mode) { |
| 14 | + switch (id) { |
| 15 | + case LEGO_DEVICE_TYPE_ID_COLOR_DIST_SENSOR: |
| 16 | + return mode == LEGO_DEVICE_MODE_PUP_COLOR_DISTANCE_SENSOR__IR_TX ? 0 : 30; |
| 17 | + case LEGO_DEVICE_TYPE_ID_SPIKE_COLOR_SENSOR: |
| 18 | + return mode == LEGO_DEVICE_MODE_PUP_COLOR_SENSOR__LIGHT ? 0 : 30; |
| 19 | + case LEGO_DEVICE_TYPE_ID_SPIKE_ULTRASONIC_SENSOR: |
| 20 | + return mode == LEGO_DEVICE_MODE_PUP_ULTRASONIC_SENSOR__LIGHT ? 0 : 50; |
| 21 | + case LEGO_DEVICE_TYPE_ID_EV3_COLOR_SENSOR: |
| 22 | + return 30; |
| 23 | + case LEGO_DEVICE_TYPE_ID_EV3_IR_SENSOR: |
| 24 | + return 1100; |
| 25 | + case LEGO_DEVICE_TYPE_ID_NXT_LIGHT_SENSOR: |
| 26 | + return 20; |
| 27 | + case LEGO_DEVICE_TYPE_ID_NXT_SOUND_SENSOR: |
| 28 | + return 300; |
| 29 | + case LEGO_DEVICE_TYPE_ID_NXT_ENERGY_METER: |
| 30 | + return 200; |
| 31 | + default: |
| 32 | + // Default delay for other sensors and modes. |
| 33 | + return 0; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets the minimum time needed for the device to handle written data. |
| 39 | + * |
| 40 | + * This is empirically determined based on sensor experiments. |
| 41 | + * |
| 42 | + * @param [in] id The device type ID. |
| 43 | + * @param [in] mode The device mode. |
| 44 | + * @return Required delay in milliseconds. |
| 45 | + */ |
| 46 | +uint32_t lego_device_data_set_delay(lego_device_type_id_t id, uint8_t mode) { |
| 47 | + // The Boost Color Distance Sensor requires a long delay or successive |
| 48 | + // writes are ignored. |
| 49 | + if (id == LEGO_DEVICE_TYPE_ID_COLOR_DIST_SENSOR && mode == LEGO_DEVICE_MODE_PUP_COLOR_DISTANCE_SENSOR__IR_TX) { |
| 50 | + return 250; |
| 51 | + } |
| 52 | + |
| 53 | + // Default delay for setting data. In practice, this is the delay for setting |
| 54 | + // the light on the color sensor and ultrasonic sensor. |
| 55 | + return 10; |
| 56 | +} |
0 commit comments