summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src
diff options
context:
space:
mode:
Diffstat (limited to 'digital/io/src')
-rw-r--r--digital/io/src/main.c21
-rw-r--r--digital/io/src/sharp.c60
-rw-r--r--digital/io/src/sharp.h3
3 files changed, 43 insertions, 41 deletions
diff --git a/digital/io/src/main.c b/digital/io/src/main.c
index ea200a73..76bc66b9 100644
--- a/digital/io/src/main.c
+++ b/digital/io/src/main.c
@@ -117,16 +117,6 @@ static uint8_t main_stats_asserv_, main_stats_asserv_cpt_;
static uint8_t main_stats_timer_;
/**
- * Update frequency of sharps.
- */
-#define MAIN_SHARP_UPDATE_FREQ 5
-
-/**
- * Sharps frequency counter.
- */
-uint8_t main_sharp_freq_counter_;
-
-/**
* Initialize the main and all its subsystems.
*/
static void
@@ -246,15 +236,8 @@ main_loop (void)
/* Update wait flag for move FSM */
if (main_move_wait_cycle)
main_move_wait_cycle--;
- /* Update sharp module if required and only every
- * MAIN_SHARP_UPDATE_FREQ cycles */
- if (++main_sharp_freq_counter_ == MAIN_SHARP_UPDATE_FREQ)
- {
- /* Update sharps */
- sharp_update (0xff);
- /* Reset counter */
- main_sharp_freq_counter_ = 0;
- }
+ /* Update sharps */
+ sharp_update ();
/* Update FSM timeouts. */
FSM_HANDLE_TIMEOUT (&move_fsm);
FSM_HANDLE_TIMEOUT (&top_fsm);
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. */
diff --git a/digital/io/src/sharp.h b/digital/io/src/sharp.h
index 2a4f1c5d..4b0eb1de 100644
--- a/digital/io/src/sharp.h
+++ b/digital/io/src/sharp.h
@@ -89,9 +89,8 @@ void sharp_init (void);
* Update read data from sharps.
* This function is blocking. To get the value you have to use the get
* function (@a sharp_get_raw).
- * @param sharp_mask list of sharps (using a mask) to update.
*/
-void sharp_update (uint8_t sharp_mask);
+void sharp_update (void);
/**
* Get raw cached data from sharps.