groveo2.cxx revision 765adb95dc941c32690d6c43bc08b9d07d197fcb
186c93d9c46415cf7746351c502a3513f637e77e4root/* 286c93d9c46415cf7746351c502a3513f637e77e4root * Author: Zion Orent <zorent@ics.com> 386c93d9c46415cf7746351c502a3513f637e77e4root * Copyright (c) 2015 Intel Corporation. 486c93d9c46415cf7746351c502a3513f637e77e4root * 586c93d9c46415cf7746351c502a3513f637e77e4root * Permission is hereby granted, free of charge, to any person obtaining 686c93d9c46415cf7746351c502a3513f637e77e4root * a copy of this software and associated documentation files (the 786c93d9c46415cf7746351c502a3513f637e77e4root * "Software"), to deal in the Software without restriction, including 886c93d9c46415cf7746351c502a3513f637e77e4root * without limitation the rights to use, copy, modify, merge, publish, 986c93d9c46415cf7746351c502a3513f637e77e4root * distribute, sublicense, and/or sell copies of the Software, and to 1086c93d9c46415cf7746351c502a3513f637e77e4root * permit persons to whom the Software is furnished to do so, subject to 1186c93d9c46415cf7746351c502a3513f637e77e4root * the following conditions: 1286c93d9c46415cf7746351c502a3513f637e77e4root * 1386c93d9c46415cf7746351c502a3513f637e77e4root * The above copyright notice and this permission notice shall be 1486c93d9c46415cf7746351c502a3513f637e77e4root * included in all copies or substantial portions of the Software. 1586c93d9c46415cf7746351c502a3513f637e77e4root * 1686c93d9c46415cf7746351c502a3513f637e77e4root * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1786c93d9c46415cf7746351c502a3513f637e77e4root * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1886c93d9c46415cf7746351c502a3513f637e77e4root * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1986c93d9c46415cf7746351c502a3513f637e77e4root * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 2086c93d9c46415cf7746351c502a3513f637e77e4root * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 2186c93d9c46415cf7746351c502a3513f637e77e4root * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 2286c93d9c46415cf7746351c502a3513f637e77e4root * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2386c93d9c46415cf7746351c502a3513f637e77e4root */ 2486c93d9c46415cf7746351c502a3513f637e77e4root 2586c93d9c46415cf7746351c502a3513f637e77e4root#include <iostream> 2686c93d9c46415cf7746351c502a3513f637e77e4root#include <string> 27409de6acb4473c973ed2532e340831dc582e5e0eAustin Yuan#include <stdexcept> 2886c93d9c46415cf7746351c502a3513f637e77e4root 2986c93d9c46415cf7746351c502a3513f637e77e4root#include "groveo2.h" 3086c93d9c46415cf7746351c502a3513f637e77e4root 3186c93d9c46415cf7746351c502a3513f637e77e4rootusing namespace upm; 3286c93d9c46415cf7746351c502a3513f637e77e4rootusing namespace std; 3386c93d9c46415cf7746351c502a3513f637e77e4root 3486c93d9c46415cf7746351c502a3513f637e77e4rootGroveO2::GroveO2(int pin) 3586c93d9c46415cf7746351c502a3513f637e77e4root{ 3686c93d9c46415cf7746351c502a3513f637e77e4root if ( !(m_aio = mraa_aio_init(pin)) ) 3786c93d9c46415cf7746351c502a3513f637e77e4root { 3886c93d9c46415cf7746351c502a3513f637e77e4root throw std::invalid_argument(std::string(__FUNCTION__) + 3986c93d9c46415cf7746351c502a3513f637e77e4root ": mraa_aio_init() failed, invalid pin?"); 4086c93d9c46415cf7746351c502a3513f637e77e4root return; 4186c93d9c46415cf7746351c502a3513f637e77e4root } 4286c93d9c46415cf7746351c502a3513f637e77e4root} 4386c93d9c46415cf7746351c502a3513f637e77e4root 4486c93d9c46415cf7746351c502a3513f637e77e4rootGroveO2::~GroveO2() 4586c93d9c46415cf7746351c502a3513f637e77e4root{ 4686c93d9c46415cf7746351c502a3513f637e77e4root mraa_aio_close(m_aio); 4786c93d9c46415cf7746351c502a3513f637e77e4root} 4886c93d9c46415cf7746351c502a3513f637e77e4root 4986c93d9c46415cf7746351c502a3513f637e77e4rootfloat GroveO2::voltageValue() 5086c93d9c46415cf7746351c502a3513f637e77e4root{ 5186c93d9c46415cf7746351c502a3513f637e77e4root int val = mraa_aio_read(m_aio); 5286c93d9c46415cf7746351c502a3513f637e77e4root float sensorVoltage = (val/1024.0) * 5.0; 5386c93d9c46415cf7746351c502a3513f637e77e4root sensorVoltage = (sensorVoltage/201.0) * 1000.0; 5486c93d9c46415cf7746351c502a3513f637e77e4root return sensorVoltage; 5586c93d9c46415cf7746351c502a3513f637e77e4root} 5686c93d9c46415cf7746351c502a3513f637e77e4root