summaryrefslogtreecommitdiffstats
path: root/source/Concept/Framework/modules/input/distance_sensor.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/Concept/Framework/modules/input/distance_sensor.c')
-rwxr-xr-xsource/Concept/Framework/modules/input/distance_sensor.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/source/Concept/Framework/modules/input/distance_sensor.c b/source/Concept/Framework/modules/input/distance_sensor.c
new file mode 100755
index 0000000..977784f
--- /dev/null
+++ b/source/Concept/Framework/modules/input/distance_sensor.c
@@ -0,0 +1,32 @@
+#include "distance_sensor.h"
+
+//-----------------------------------------------------------------------------
+float Distance_Sensor::GetDistance()
+{
+ uint32 result = 0;
+
+ //Generate pulse
+ *hardwareDDR |= pin;//Set pin output
+ *hardwarePort |= pin;//Activate port
+ usleep(10);//Wait for 10µs
+ //*hardwarePort &= ~pin;//Deactivate port
+ *hardwareDDR &= ~pin;//Set pin input
+
+ (parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print("pre 1", 4, 1);
+
+ //Wait for response
+ while(!(PINC & pin)){asm volatile("nop");}
+
+ (parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print("pre 2", 4, 1);
+
+ //Calculate duration of response
+ while(*hardwarePin & pin)
+ {
+ result++;
+ asm volatile("nop");
+ }
+
+ (parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print("pre 3", 4, 1);
+
+ return (float(result) * DISTANCE_PER_VALUE);
+}