summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/sharp.c
diff options
context:
space:
mode:
authorJérémy Dufour2009-05-16 01:23:16 +0200
committerJérémy Dufour2009-05-16 01:23:16 +0200
commitbda06a1cc21008d119ef3ca95913113ee062fbc2 (patch)
treeeeae5f24d01f51169948a50567fb028a6cc3ae5d /digital/io/src/sharp.c
parent7d8758cc6834db74eea2a86c8272eb2793b26239 (diff)
* digital/io/src:
- always update sharp module every cycle, - sharp update now read only one value every cycle, filter it SHARP_RAW_FILTER times and only keep the smallest one.
Diffstat (limited to 'digital/io/src/sharp.c')
-rw-r--r--digital/io/src/sharp.c60
1 files changed, 40 insertions, 20 deletions
diff --git a/digital/io/src/sharp.c b/digital/io/src/sharp.c
index 2283937c..78c0d371 100644
--- a/digital/io/src/sharp.c
+++ b/digital/io/src/sharp.c
@@ -27,6 +27,7 @@
#include "sharp.h"
#include "modules/adc/adc.h" /* ADC functions */
+#include "modules/utils/utils.h"
#include "io.h"
/** Sharp conversion table.
@@ -41,6 +42,12 @@ uint16_t sharp_conv_table[SHARP_NB_ELEMENT_TABLE_CONV][2] =
/**
+ * Raw filter iteration.
+ * How many values should we get when reading a sharp?
+ */
+#define SHARP_RAW_FILTER 2
+
+/**
* Cached array of raw sharp values.
*/
uint16_t sharp_values_[SHARP_NUMBER];
@@ -130,27 +137,40 @@ sharp_init (void)
/* Update read data from sharps. */
void
-sharp_update (uint8_t sharp_mask)
+sharp_update (void)
{
- uint8_t compt;
-
- /* Go through the bit mask */
- for (compt = 0; compt < SHARP_NUMBER; compt++)
- {
- /* Check if the bit/sharp id is set */
- if (bit_is_set (sharp_mask, compt))
- {
- /* Start the capture */
- adc_start (compt);
- /* Wait until ADC mesure is finished */
- while (!adc_checkf ())
- ;
- /* Store the value */
- sharp_values_[compt] = adc_read ();
- /* Update interpreted cached value */
- sharp_cache_interpreted_[compt] = sharp_update_interpreted (compt);
- }
- }
+ static uint8_t sharp_filter_count = SHARP_RAW_FILTER;
+ static uint8_t sharp_id = 0;
+ static uint16_t min_value = 0xFFFF;
+ uint16_t current;
+
+ /* Start the capture */
+ adc_start (sharp_id);
+ /* Wait until ADC mesure is finished */
+ while (!adc_checkf ())
+ ;
+ /* Get the value */
+ current = adc_read ();
+ /* Only keep the minimum. */
+ min_value = UTILS_MIN (min_value, current);
+ /* Filter decrement. */
+ sharp_filter_count--;
+
+ /* Enough reading? */
+ if (!sharp_filter_count)
+ {
+ /* Store value. */
+ sharp_values_[sharp_id] = min_value;
+ /* Update interpreted cached value */
+ sharp_cache_interpreted_[sharp_id] = sharp_update_interpreted
+ (sharp_id);
+ /* Next sharp. */
+ sharp_id++;
+ sharp_id %= SHARP_NUMBER;
+ /* Reset. */
+ min_value = 0xFFFF;
+ sharp_filter_count = SHARP_RAW_FILTER;
+ }
}
/* Get raw cached data from sharps. */