Marco Oehler / Mbed 2 deprecated Lab2

Dependencies:   mbed

Revision:
0:1a972ed770da
diff -r 000000000000 -r 1a972ed770da IRSensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IRSensor.cpp	Mon Mar 09 16:23:04 2020 +0000
@@ -0,0 +1,52 @@
+/*
+ * IRSensor.h
+ * Copyright (c) 2020, ZHAW
+ * All rights reserved.
+ */
+
+#include <cmath>
+#include "IRSensor.h"
+
+using namespace std;
+
+/**
+ * Creates and initialises the driver to read the distance sensors.
+ * @param distance the analog input to read a distance value from.
+ * @param bit0 a digital output to control the multiplexer.
+ * @param bit1 a digital output to control the multiplexer.
+ * @param bit2 a digital output to control the multiplexer.
+ * @param number the number of the sensor. This value must be between 0 and 5.
+ */
+IRSensor::IRSensor(AnalogIn& distance, DigitalOut& bit0, DigitalOut& bit1, DigitalOut& bit2, int number) : distance(distance), bit0(bit0), bit1(bit1), bit2(bit2) {
+    
+    this->number = number;
+}
+
+/**
+ * Deletes this IRSensor object and releases all allocated resources.
+ */
+IRSensor::~IRSensor() {}
+
+/**
+ * This method reads from the distance sensor.
+ * @return a distance value, given in [m].
+ */
+float IRSensor::read() {
+
+    bit0 = (number >> 0) & 1;
+    bit1 = (number >> 1) & 1;
+    bit2 = (number >> 2) & 1;
+    
+    float d = -0.58f*sqrt(distance)+0.58f; // calculate the distance in [m]
+    
+    return d;
+}
+
+/**
+ * The empty operator is a shorthand notation of the <code>read()</code> method.
+ */
+IRSensor::operator float() {
+    
+    return read();
+}
+