mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 23:24:20 +03:00 
			
		
		
		
	Removed commented code
Ran clang-format on modified files Signed-off-by: Serban Waltter <serban.waltter@rinftech.com>
This commit is contained in:
		| @@ -5,27 +5,24 @@ | |||||||
| #include "bh1750.hpp" | #include "bh1750.hpp" | ||||||
| #include "max44009.hpp" | #include "max44009.hpp" | ||||||
|  |  | ||||||
| // using namespace std; |  | ||||||
| // using namespace upm; |  | ||||||
|  |  | ||||||
| int | int | ||||||
| main() | main() | ||||||
| { | { | ||||||
|   std::list<upm::iLight*> lightSensors; |     std::list<upm::iLight*> lightSensors; | ||||||
|  |  | ||||||
|   // Populate list of light sensors |     // Populate list of light sensors | ||||||
|   lightSensors.push_back(new upm::APDS9002(0)); |     lightSensors.push_back(new upm::APDS9002(0)); | ||||||
|   lightSensors.push_back(new upm::BH1750()); |     lightSensors.push_back(new upm::BH1750()); | ||||||
|   lightSensors.push_back(new upm::MAX44009(1)); |     lightSensors.push_back(new upm::MAX44009(1)); | ||||||
|  |  | ||||||
|   // Measure luminance level from all 3 individual sensors |     // Measure luminance level from all 3 individual sensors | ||||||
|   for (auto& sensor : lightSensors) { |     for (auto& sensor : lightSensors) { | ||||||
|     sensor->getLuminance(); |         sensor->getLuminance(); | ||||||
|   } |     } | ||||||
|  |  | ||||||
|   for (auto& sensor : lightSensors) { |     for (auto& sensor : lightSensors) { | ||||||
|     delete sensor; |         delete sensor; | ||||||
|   } |     } | ||||||
|  |  | ||||||
|   return 0; |     return 0; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,26 +1,26 @@ | |||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <vector> | #include <vector> | ||||||
|  |  | ||||||
| #include "lm35.hpp" |  | ||||||
| #include "abp.hpp" | #include "abp.hpp" | ||||||
|  | #include "lm35.hpp" | ||||||
| // using namespace std; |  | ||||||
| // using namespace upm; |  | ||||||
|  |  | ||||||
| int | int | ||||||
| main() | main() | ||||||
| { | { | ||||||
|   std::vector<upm::iTemperature*> tempSensors {new upm::LM35(0), new upm::ABP(0, ABP_DEFAULT_ADDRESS)}; |     std::vector<upm::iTemperature*> tempSensors{ new upm::LM35(0), | ||||||
|  |                                                  new upm::ABP(0, ABP_DEFAULT_ADDRESS) }; | ||||||
|  |  | ||||||
|   for (auto& sensor : tempSensors) { |     for (auto& sensor : tempSensors) { | ||||||
|     float celsiusTemp = sensor->getTemperature(); |         float celsiusTemp = sensor->getTemperature(); | ||||||
|     std::cout << "Temperature in Celsius degrees: " << celsiusTemp << std::endl; |         std::cout << "Temperature in Celsius degrees: " << celsiusTemp << std::endl; | ||||||
|     std::cout << "Temperature in Kelvin: " << upm::iTemperature::convertCelsiusTo(celsiusTemp, upm::TemperatureUnit::KELVIN) << std::endl; |         std::cout << "Temperature in Kelvin: " | ||||||
|   } |                   << upm::iTemperature::convertCelsiusTo(celsiusTemp, upm::TemperatureUnit::KELVIN) | ||||||
|  |                   << std::endl; | ||||||
|  |     } | ||||||
|  |  | ||||||
|   for (auto& sensor : tempSensors) { |     for (auto& sensor : tempSensors) { | ||||||
|     delete sensor; |         delete sensor; | ||||||
|   } |     } | ||||||
|  |  | ||||||
|   return 0; |     return 0; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -23,8 +23,8 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <string> |  | ||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
| #include "emg.hpp" | #include "emg.hpp" | ||||||
|  |  | ||||||
| @@ -33,55 +33,56 @@ using namespace std; | |||||||
|  |  | ||||||
| EMG::EMG(int pin) | EMG::EMG(int pin) | ||||||
| { | { | ||||||
|     if ( !(m_aio = mraa_aio_init(pin)) ) |     if (!(m_aio = mraa_aio_init(pin))) { | ||||||
|     { |         throw std::invalid_argument(std::string(__FUNCTION__) + | ||||||
|       throw std::invalid_argument(std::string(__FUNCTION__) + |                                     ": mraa_aio_init() failed, invalid pin?"); | ||||||
|                                   ": mraa_aio_init() failed, invalid pin?"); |         return; | ||||||
|       return; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| EMG::~EMG() | EMG::~EMG() | ||||||
| { | { | ||||||
|   mraa_aio_close(m_aio); |     mraa_aio_close(m_aio); | ||||||
| } | } | ||||||
|  |  | ||||||
| void EMG::calibrate() | void | ||||||
|  | EMG::calibrate() | ||||||
| { | { | ||||||
| 	int val, sum = 0; |     int val, sum = 0; | ||||||
|  |  | ||||||
| 	for (int i=0; i<1100; i++) |     for (int i = 0; i < 1100; i++) { | ||||||
| 	{ |         val = mraa_aio_read(m_aio); | ||||||
| 		val = mraa_aio_read(m_aio); |         if (val != -1) | ||||||
|                 if (val != -1) throw std::runtime_error(std::string(__FUNCTION__) + |             throw std::runtime_error(std::string(__FUNCTION__) + ": Failed to do an aio read."); | ||||||
|                                                         ": Failed to do an aio read."); |         sum += val; | ||||||
| 		sum += val; |         usleep(1000); | ||||||
| 		usleep(1000); |     } | ||||||
| 	} |     sum /= 1100; | ||||||
| 	sum /= 1100; |     cout << "Static analog data = " << sum << endl; | ||||||
| 	cout << "Static analog data = " << sum << endl; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| int EMG::value() | int | ||||||
|  | EMG::value() | ||||||
| { | { | ||||||
| 	int val = mraa_aio_read(m_aio); |     int val = mraa_aio_read(m_aio); | ||||||
| 	return val; |     return val; | ||||||
| } | } | ||||||
|  |  | ||||||
| float EMG::getVolts() | float | ||||||
|  | EMG::getVolts() | ||||||
| { | { | ||||||
| 	float val = mraa_aio_read_float(m_aio); |     float val = mraa_aio_read_float(m_aio); | ||||||
| 	if (val < 0) |     if (val < 0) | ||||||
| 		return val; |         return val; | ||||||
|  |  | ||||||
| 	 /* Apply raw scale */ |     /* Apply raw scale */ | ||||||
|     val *= this->m_scale; |     val *= this->m_scale; | ||||||
|  |  | ||||||
|      /* Scale to aRef */ |     /* Scale to aRef */ | ||||||
|     val *= this->m_aRef; |     val *= this->m_aRef; | ||||||
|  |  | ||||||
|     /* Apply the offset in volts */ |     /* Apply the offset in volts */ | ||||||
|     val += this->m_offset; |     val += this->m_offset; | ||||||
|  |  | ||||||
| 	return val; |     return val; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -23,8 +23,8 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <string> |  | ||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
| #include "groveemg.hpp" | #include "groveemg.hpp" | ||||||
|  |  | ||||||
| @@ -33,55 +33,56 @@ using namespace std; | |||||||
|  |  | ||||||
| GroveEMG::GroveEMG(int pin) | GroveEMG::GroveEMG(int pin) | ||||||
| { | { | ||||||
|     if ( !(m_aio = mraa_aio_init(pin)) ) |     if (!(m_aio = mraa_aio_init(pin))) { | ||||||
|     { |         throw std::invalid_argument(std::string(__FUNCTION__) + | ||||||
|       throw std::invalid_argument(std::string(__FUNCTION__) + |                                     ": mraa_aio_init() failed, invalid pin?"); | ||||||
|                                   ": mraa_aio_init() failed, invalid pin?"); |         return; | ||||||
|       return; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| GroveEMG::~GroveEMG() | GroveEMG::~GroveEMG() | ||||||
| { | { | ||||||
|   mraa_aio_close(m_aio); |     mraa_aio_close(m_aio); | ||||||
| } | } | ||||||
|  |  | ||||||
| void GroveEMG::calibrate() | void | ||||||
|  | GroveEMG::calibrate() | ||||||
| { | { | ||||||
| 	int val, sum = 0; |     int val, sum = 0; | ||||||
|  |  | ||||||
| 	for (int i=0; i<1100; i++) |     for (int i = 0; i < 1100; i++) { | ||||||
| 	{ |         val = mraa_aio_read(m_aio); | ||||||
| 		val = mraa_aio_read(m_aio); |         if (val != -1) | ||||||
|                 if (val != -1) throw std::runtime_error(std::string(__FUNCTION__) + |             throw std::runtime_error(std::string(__FUNCTION__) + ": Failed to do an aio read."); | ||||||
|                                                         ": Failed to do an aio read."); |         sum += val; | ||||||
| 		sum += val; |         usleep(1000); | ||||||
| 		usleep(1000); |     } | ||||||
| 	} |     sum /= 1100; | ||||||
| 	sum /= 1100; |     cout << "Static analog data = " << sum << endl; | ||||||
| 	cout << "Static analog data = " << sum << endl; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| int GroveEMG::value() | int | ||||||
|  | GroveEMG::value() | ||||||
| { | { | ||||||
| 	int val = mraa_aio_read(m_aio); |     int val = mraa_aio_read(m_aio); | ||||||
| 	return val; |     return val; | ||||||
| } | } | ||||||
|  |  | ||||||
| float GroveEMG::getVolts() | float | ||||||
|  | GroveEMG::getVolts() | ||||||
| { | { | ||||||
| 	float val = mraa_aio_read_float(m_aio); |     float val = mraa_aio_read_float(m_aio); | ||||||
| 	if (val < 0) |     if (val < 0) | ||||||
| 		return val; |         return val; | ||||||
|  |  | ||||||
| 	 /* Apply raw scale */ |     /* Apply raw scale */ | ||||||
|     val *= this->m_scale; |     val *= this->m_scale; | ||||||
|  |  | ||||||
|      /* Scale to aRef */ |     /* Scale to aRef */ | ||||||
|     val *= this->m_aRef; |     val *= this->m_aRef; | ||||||
|  |  | ||||||
|     /* Apply the offset in volts */ |     /* Apply the offset in volts */ | ||||||
|     val += this->m_offset; |     val += this->m_offset; | ||||||
|  |  | ||||||
| 	return val; |     return val; | ||||||
| } | } | ||||||
| @@ -24,16 +24,17 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <iostream> |  | ||||||
| #include <unistd.h> |  | ||||||
| #include <stdlib.h> |  | ||||||
| #include <functional> | #include <functional> | ||||||
|  | #include <iostream> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <unistd.h> | ||||||
|  |  | ||||||
| #include "groveultrasonic.hpp" | #include "groveultrasonic.hpp" | ||||||
|  |  | ||||||
| using namespace upm; | using namespace upm; | ||||||
|  |  | ||||||
| GroveUltraSonic::GroveUltraSonic (int pin) { | GroveUltraSonic::GroveUltraSonic(int pin) | ||||||
|  | { | ||||||
|     m_name = "GroveUltraSonic"; |     m_name = "GroveUltraSonic"; | ||||||
|  |  | ||||||
|     mraa_init(); |     mraa_init(); | ||||||
| @@ -41,23 +42,23 @@ GroveUltraSonic::GroveUltraSonic (int pin) { | |||||||
|     // setup pin |     // setup pin | ||||||
|     m_pinCtx = mraa_gpio_init(pin); |     m_pinCtx = mraa_gpio_init(pin); | ||||||
|     if (m_pinCtx == NULL) { |     if (m_pinCtx == NULL) { | ||||||
|         fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", pin); |         fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", pin); | ||||||
|         exit (1); |         exit(1); | ||||||
|     } |     } | ||||||
|     mraa_gpio_isr (m_pinCtx, MRAA_GPIO_EDGE_BOTH, |     mraa_gpio_isr (m_pinCtx, MRAA_GPIO_EDGE_BOTH, | ||||||
|                    &signalISR, this); |                    &signalISR, this); | ||||||
| } | } | ||||||
|  |  | ||||||
| GroveUltraSonic::~GroveUltraSonic () { | GroveUltraSonic::~GroveUltraSonic() | ||||||
|  | { | ||||||
|     // close pin |     // close pin | ||||||
|     mraa_gpio_isr_exit(m_pinCtx); |     mraa_gpio_isr_exit(m_pinCtx); | ||||||
|     mraa_gpio_close (m_pinCtx); |     mraa_gpio_close(m_pinCtx); | ||||||
| } | } | ||||||
|  |  | ||||||
| float | float | ||||||
| GroveUltraSonic::getDistance () { | GroveUltraSonic::getDistance() | ||||||
|  | { | ||||||
|     // output trigger signal |     // output trigger signal | ||||||
|     mraa_gpio_dir(m_pinCtx, MRAA_GPIO_OUT); |     mraa_gpio_dir(m_pinCtx, MRAA_GPIO_OUT); | ||||||
|     mraa_gpio_write(m_pinCtx, LOW); |     mraa_gpio_write(m_pinCtx, LOW); | ||||||
| @@ -86,13 +87,15 @@ GroveUltraSonic::getDistance () { | |||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
| GroveUltraSonic::signalISR(void *ctx) { | GroveUltraSonic::signalISR(void* ctx) | ||||||
|     upm::GroveUltraSonic *This = (upm::GroveUltraSonic *)ctx; | { | ||||||
|  |     upm::GroveUltraSonic* This = (upm::GroveUltraSonic*) ctx; | ||||||
|     This->ackEdgeDetected(); |     This->ackEdgeDetected(); | ||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
| GroveUltraSonic::ackEdgeDetected () { | GroveUltraSonic::ackEdgeDetected() | ||||||
|  | { | ||||||
|     if (++m_InterruptCounter % 2 == 0) { |     if (++m_InterruptCounter % 2 == 0) { | ||||||
|         gettimeofday(&m_FallingTimeStamp, NULL); |         gettimeofday(&m_FallingTimeStamp, NULL); | ||||||
|         m_doWork = false; |         m_doWork = false; | ||||||
|   | |||||||
| @@ -24,22 +24,20 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <string> |  | ||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
| #include "hcsr04.hpp" | #include "hcsr04.hpp" | ||||||
|  |  | ||||||
| using namespace upm; | using namespace upm; | ||||||
|  |  | ||||||
| HCSR04::HCSR04 (int triggerPin, int echoPin) : | HCSR04::HCSR04(int triggerPin, int echoPin) : m_hcsr04(hcsr04_init(triggerPin, echoPin)) | ||||||
|     m_hcsr04(hcsr04_init(triggerPin, echoPin)) |  | ||||||
| { | { | ||||||
|     if(!m_hcsr04) |     if (!m_hcsr04) | ||||||
|         throw std::runtime_error(std::string(__FUNCTION__) + |         throw std::runtime_error(std::string(__FUNCTION__) + ": hcsr04_init failed"); | ||||||
|                                 ": hcsr04_init failed"); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| HCSR04::~HCSR04 () | HCSR04::~HCSR04() | ||||||
| { | { | ||||||
|     hcsr04_close(m_hcsr04); |     hcsr04_close(m_hcsr04); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -24,16 +24,17 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <string> |  | ||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
| #include <unistd.h> |  | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
|  | #include <string> | ||||||
|  | #include <unistd.h> | ||||||
|  |  | ||||||
| #include "lidarlitev3.hpp" | #include "lidarlitev3.hpp" | ||||||
|  |  | ||||||
| using namespace upm; | using namespace upm; | ||||||
|  |  | ||||||
| LIDARLITEV3::LIDARLITEV3 (int bus, int devAddr) : m_i2ControlCtx(bus) { | LIDARLITEV3::LIDARLITEV3(int bus, int devAddr) : m_i2ControlCtx(bus) | ||||||
|  | { | ||||||
|     m_name = "LIDARLITEV3"; |     m_name = "LIDARLITEV3"; | ||||||
|  |  | ||||||
|     m_controlAddr = devAddr; |     m_controlAddr = devAddr; | ||||||
| @@ -41,40 +42,40 @@ LIDARLITEV3::LIDARLITEV3 (int bus, int devAddr) : m_i2ControlCtx(bus) { | |||||||
|  |  | ||||||
|     mraa::Result ret = m_i2ControlCtx.address(m_controlAddr); |     mraa::Result ret = m_i2ControlCtx.address(m_controlAddr); | ||||||
|     if (ret != mraa::SUCCESS) { |     if (ret != mraa::SUCCESS) { | ||||||
|         throw std::invalid_argument(std::string(__FUNCTION__) + |         throw std::invalid_argument(std::string(__FUNCTION__) + ": mraa_i2c_address() failed"); | ||||||
|                                     ": mraa_i2c_address() failed"); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| float | float | ||||||
| LIDARLITEV3::getDistance () { | LIDARLITEV3::getDistance() | ||||||
|  | { | ||||||
|     if(i2cWriteReg(ACQ_COMMAND, 0x04) < 0) |     if (i2cWriteReg(ACQ_COMMAND, 0x04) < 0) | ||||||
|         return -1; |         return -1; | ||||||
|  |  | ||||||
|     return read(0x8f,true); |     return read(0x8f, true); | ||||||
| } | } | ||||||
|  |  | ||||||
| uint16_t | uint16_t | ||||||
| LIDARLITEV3::read(int reg, bool monitorBusyFlag) { | LIDARLITEV3::read(int reg, bool monitorBusyFlag) | ||||||
|     int busyFlag = 0; // busyFlag monitors when the device is done with a measurement | { | ||||||
|  |     int busyFlag = 0;    // busyFlag monitors when the device is done with a measurement | ||||||
|     int busyCounter = 0; // busyCounter counts number of times busy flag is checked, for timeout |     int busyCounter = 0; // busyCounter counts number of times busy flag is checked, for timeout | ||||||
|     uint8_t data; |     uint8_t data; | ||||||
|     uint16_t distance; |     uint16_t distance; | ||||||
|  |  | ||||||
|     if(monitorBusyFlag) { |     if (monitorBusyFlag) { | ||||||
|         busyFlag = 1; // Begin read immediately if not monitoring busy flag |         busyFlag = 1; // Begin read immediately if not monitoring busy flag | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     while(busyFlag != 0) { // Loop until device is not busy |     while (busyFlag != 0) { // Loop until device is not busy | ||||||
|         // Read status register to check busy flag |         // Read status register to check busy flag | ||||||
|         data = i2cReadReg_8 (0x01); // Read register 0x01 |         data = i2cReadReg_8(0x01); // Read register 0x01 | ||||||
|         busyFlag = data & 1; // Assign the LSB of the status register to busyFlag |         busyFlag = data & 1;       // Assign the LSB of the status register to busyFlag | ||||||
|  |  | ||||||
|         busyCounter++; // Increment busyCounter for timeout |         busyCounter++; // Increment busyCounter for timeout | ||||||
|  |  | ||||||
|         // Handle timeout condition, exit while loop and goto bailout |         // Handle timeout condition, exit while loop and goto bailout | ||||||
|         if(busyCounter > 9999) { |         if (busyCounter > 9999) { | ||||||
|             goto timeout; |             goto timeout; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -85,22 +86,22 @@ LIDARLITEV3::read(int reg, bool monitorBusyFlag) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // timeout reports error |     // timeout reports error | ||||||
|     if(busyCounter > 9999) { |     if (busyCounter > 9999) { | ||||||
|         timeout: |     timeout: | ||||||
|         busyCounter = 0; |         busyCounter = 0; | ||||||
|         throw std::invalid_argument(std::string(__FUNCTION__) + |         throw std::invalid_argument(std::string(__FUNCTION__) + ": Read timeout"); | ||||||
|                                     ": Read timeout"); |  | ||||||
|     } |     } | ||||||
|     return distance; |     return distance; | ||||||
| } | } | ||||||
|  |  | ||||||
| uint16_t | uint16_t | ||||||
| LIDARLITEV3::i2cReadReg_16 (int reg) { | LIDARLITEV3::i2cReadReg_16(int reg) | ||||||
|  | { | ||||||
|     uint16_t data; |     uint16_t data; | ||||||
|  |  | ||||||
|     m_i2ControlCtx.writeByte(reg); |     m_i2ControlCtx.writeByte(reg); | ||||||
|  |  | ||||||
|     m_i2ControlCtx.read((uint8_t *)&data, 0x2); |     m_i2ControlCtx.read((uint8_t*) &data, 0x2); | ||||||
|  |  | ||||||
|     uint8_t high = (data & 0xFF00) >> 8; |     uint8_t high = (data & 0xFF00) >> 8; | ||||||
|     data = (data << 8) & 0xFF00; |     data = (data << 8) & 0xFF00; | ||||||
| @@ -110,7 +111,8 @@ LIDARLITEV3::i2cReadReg_16 (int reg) { | |||||||
| } | } | ||||||
|  |  | ||||||
| uint8_t | uint8_t | ||||||
| LIDARLITEV3::i2cReadReg_8 (int reg) { | LIDARLITEV3::i2cReadReg_8(int reg) | ||||||
|  | { | ||||||
|     uint8_t data; |     uint8_t data; | ||||||
|  |  | ||||||
|     m_i2ControlCtx.writeByte(reg); |     m_i2ControlCtx.writeByte(reg); | ||||||
| @@ -121,14 +123,14 @@ LIDARLITEV3::i2cReadReg_8 (int reg) { | |||||||
| } | } | ||||||
|  |  | ||||||
| mraa::Result | mraa::Result | ||||||
| LIDARLITEV3::i2cWriteReg (uint8_t reg, uint8_t value) { | LIDARLITEV3::i2cWriteReg(uint8_t reg, uint8_t value) | ||||||
|  | { | ||||||
|     mraa::Result error = mraa::SUCCESS; |     mraa::Result error = mraa::SUCCESS; | ||||||
|  |  | ||||||
|     uint8_t data[2] = { reg, value }; |     uint8_t data[2] = { reg, value }; | ||||||
|     error = m_i2ControlCtx.write (data, 2); |     error = m_i2ControlCtx.write(data, 2); | ||||||
|     if ( error != mraa::SUCCESS) |     if (error != mraa::SUCCESS) | ||||||
|       throw std::invalid_argument(std::string(__FUNCTION__) + |         throw std::invalid_argument(std::string(__FUNCTION__) + ": mraa_i2c_write() failed"); | ||||||
|                                   ": mraa_i2c_write() failed"); |  | ||||||
|  |  | ||||||
|     return error; |     return error; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -23,8 +23,8 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <string> |  | ||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
| #include "maxsonarez.hpp" | #include "maxsonarez.hpp" | ||||||
|  |  | ||||||
| @@ -33,37 +33,38 @@ using namespace upm; | |||||||
|  |  | ||||||
| MAXSONAREZ::MAXSONAREZ(int pin, float aref) | MAXSONAREZ::MAXSONAREZ(int pin, float aref) | ||||||
| { | { | ||||||
|   if (!(m_aio = mraa_aio_init(pin))) |     if (!(m_aio = mraa_aio_init(pin))) { | ||||||
|     { |         throw std::invalid_argument(std::string(__FUNCTION__) + | ||||||
|       throw std::invalid_argument(std::string(__FUNCTION__) + |                                     ": mraa_aio_init() failed, invalid pin?"); | ||||||
|                                   ": mraa_aio_init() failed, invalid pin?"); |         return; | ||||||
|       return; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   m_aRes = (1 << mraa_aio_get_bit(m_aio)); |     m_aRes = (1 << mraa_aio_get_bit(m_aio)); | ||||||
|   m_aref = aref; |     m_aref = aref; | ||||||
|  |  | ||||||
|   // volt's per inch of this sensor |     // volt's per inch of this sensor | ||||||
|   m_vI = (m_aref / MAXSONAREZ_RES); |     m_vI = (m_aref / MAXSONAREZ_RES); | ||||||
| } | } | ||||||
|  |  | ||||||
| MAXSONAREZ::~MAXSONAREZ() | MAXSONAREZ::~MAXSONAREZ() | ||||||
| { | { | ||||||
|   mraa_aio_close(m_aio); |     mraa_aio_close(m_aio); | ||||||
| } | } | ||||||
|  |  | ||||||
| int MAXSONAREZ::inches() | int | ||||||
|  | MAXSONAREZ::inches() | ||||||
| { | { | ||||||
|   int val = mraa_aio_read(m_aio); |     int val = mraa_aio_read(m_aio); | ||||||
|   if (val == -1) { |     if (val == -1) { | ||||||
|     return -1; |         return -1; | ||||||
|   } |     } | ||||||
|   float volts = float(val) * (m_aref / m_aRes); |     float volts = float(val) * (m_aref / m_aRes); | ||||||
|  |  | ||||||
|   return int(volts / m_vI); |     return int(volts / m_vI); | ||||||
| } | } | ||||||
|  |  | ||||||
| float MAXSONAREZ::getDistance() | float | ||||||
|  | MAXSONAREZ::getDistance() | ||||||
| { | { | ||||||
|    return inches() * 2.54; |     return inches() * 2.54; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -24,16 +24,17 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include <iostream> |  | ||||||
| #include <unistd.h> |  | ||||||
| #include <stdlib.h> |  | ||||||
| #include <functional> | #include <functional> | ||||||
|  | #include <iostream> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <unistd.h> | ||||||
|  |  | ||||||
| #include "ultrasonic.hpp" | #include "ultrasonic.hpp" | ||||||
|  |  | ||||||
| using namespace upm; | using namespace upm; | ||||||
|  |  | ||||||
| UltraSonic::UltraSonic (int pin) { | UltraSonic::UltraSonic(int pin) | ||||||
|  | { | ||||||
|     m_name = "UltraSonic"; |     m_name = "UltraSonic"; | ||||||
|  |  | ||||||
|     mraa_init(); |     mraa_init(); | ||||||
| @@ -41,23 +42,23 @@ UltraSonic::UltraSonic (int pin) { | |||||||
|     // setup pin |     // setup pin | ||||||
|     m_pinCtx = mraa_gpio_init(pin); |     m_pinCtx = mraa_gpio_init(pin); | ||||||
|     if (m_pinCtx == NULL) { |     if (m_pinCtx == NULL) { | ||||||
|         fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", pin); |         fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", pin); | ||||||
|         exit (1); |         exit(1); | ||||||
|     } |     } | ||||||
|     mraa_gpio_isr (m_pinCtx, MRAA_GPIO_EDGE_BOTH, |     mraa_gpio_isr (m_pinCtx, MRAA_GPIO_EDGE_BOTH, | ||||||
|                    &signalISR, this); |                    &signalISR, this); | ||||||
| } | } | ||||||
|  |  | ||||||
| UltraSonic::~UltraSonic () { | UltraSonic::~UltraSonic() | ||||||
|  | { | ||||||
|     // close pin |     // close pin | ||||||
|     mraa_gpio_isr_exit(m_pinCtx); |     mraa_gpio_isr_exit(m_pinCtx); | ||||||
|     mraa_gpio_close (m_pinCtx); |     mraa_gpio_close(m_pinCtx); | ||||||
| } | } | ||||||
|  |  | ||||||
| float | float | ||||||
| UltraSonic::getDistance () { | UltraSonic::getDistance() | ||||||
|  | { | ||||||
|     // output trigger signal |     // output trigger signal | ||||||
|     mraa_gpio_dir(m_pinCtx, MRAA_GPIO_OUT); |     mraa_gpio_dir(m_pinCtx, MRAA_GPIO_OUT); | ||||||
|     mraa_gpio_write(m_pinCtx, LOW); |     mraa_gpio_write(m_pinCtx, LOW); | ||||||
| @@ -86,13 +87,15 @@ UltraSonic::getDistance () { | |||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
| UltraSonic::signalISR(void *ctx) { | UltraSonic::signalISR(void* ctx) | ||||||
|     upm::UltraSonic *This = (upm::UltraSonic *)ctx; | { | ||||||
|  |     upm::UltraSonic* This = (upm::UltraSonic*) ctx; | ||||||
|     This->ackEdgeDetected(); |     This->ackEdgeDetected(); | ||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
| UltraSonic::ackEdgeDetected () { | UltraSonic::ackEdgeDetected() | ||||||
|  | { | ||||||
|     if (++m_InterruptCounter % 2 == 0) { |     if (++m_InterruptCounter % 2 == 0) { | ||||||
|         gettimeofday(&m_FallingTimeStamp, NULL); |         gettimeofday(&m_FallingTimeStamp, NULL); | ||||||
|         m_doWork = false; |         m_doWork = false; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Serban Waltter
					Serban Waltter