gp2y0a: renamed from gp2y0a21yk to indicate support for the gp2y0a family

Additionally, add some error checking in the constructor, and determine
the ADC resolution and use that in the voltage calculation rather than
a hardcoded 1024.0.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson
2015-04-02 11:03:29 -06:00
committed by Mihai Tudor Panu
parent 828d3b928e
commit 77be6adf6d
12 changed files with 154 additions and 67 deletions

View File

@ -59,7 +59,7 @@ add_executable (enc03r-example enc03r.cxx)
add_executable (adc121c021-example adc121c021.cxx)
add_executable (ds1307-example ds1307.cxx)
add_executable (a110x-example a110x.cxx)
add_executable (gp2y0a21yk-example gp2y0a21yk.cxx)
add_executable (gp2y0a-example gp2y0a.cxx)
add_executable (grovemoisture-example grovemoisture.cxx)
add_executable (groveehr-example groveehr.cxx)
add_executable (ta12200-example ta12200.cxx)
@ -151,7 +151,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/enc03r)
include_directories (${PROJECT_SOURCE_DIR}/src/adc121c021)
include_directories (${PROJECT_SOURCE_DIR}/src/ds1307)
include_directories (${PROJECT_SOURCE_DIR}/src/a110x)
include_directories (${PROJECT_SOURCE_DIR}/src/gp2y0a21yk)
include_directories (${PROJECT_SOURCE_DIR}/src/gp2y0a)
include_directories (${PROJECT_SOURCE_DIR}/src/grovemoisture)
include_directories (${PROJECT_SOURCE_DIR}/src/groveehr)
include_directories (${PROJECT_SOURCE_DIR}/src/ta12200)
@ -257,7 +257,7 @@ target_link_libraries (enc03r-example enc03r ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (adc121c021-example adc121c021 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (ds1307-example ds1307 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (a110x-example a110x ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (gp2y0a21yk-example gp2y0a21yk ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (gp2y0a-example gp2y0a ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (grovemoisture-example grovemoisture ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveehr-example groveehr ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (ta12200-example ta12200 ${CMAKE_THREAD_LIBS_INIT})

View File

@ -25,14 +25,14 @@
#include <unistd.h>
#include <iostream>
#include <signal.h>
#include "gp2y0a21yk.h"
#include "gp2y0a.h"
using namespace std;
bool shouldRun = true;
// analog voltage, usually 3.3 or 5.0
#define GP2Y0A21YK_AREF 5.0
#define GP2Y0A_AREF 5.0
#define SAMPLES_PER_QUERY 20
void sig_handler(int signo)
@ -46,21 +46,21 @@ int main()
signal(SIGINT, sig_handler);
//! [Interesting]
// Note, for the Grove version of this sensor, due to the way it is wired,
// you need to plug this into the A0 port, where it will use the available
// A1 pin for data.
// Note, for the Grove 80cm version of this sensor, due to the way
// it is wired, you need to plug this into the A0 port, where it
// will use the available A1 pin for data.
// Instantiate a GP2Y0A21YK on analog pin A1
upm::GP2Y0A21YK *volts = new upm::GP2Y0A21YK(1);
// Instantiate a GP2Y0A on analog pin A1
upm::GP2Y0A *volts = new upm::GP2Y0A(1);
// The higher the voltage (closer to AREF) the closer the object is. NOTE:
// the measured voltage will probably not exceed 3.3 volts.
// Every second, print the averaged voltage value (averaged over 20 samples).
while (shouldRun)
{
cout << "AREF: " << GP2Y0A21YK_AREF
cout << "AREF: " << GP2Y0A_AREF
<< ", Voltage value (higher means closer): "
<< volts->value(GP2Y0A21YK_AREF, SAMPLES_PER_QUERY) << endl;
<< volts->value(GP2Y0A_AREF, SAMPLES_PER_QUERY) << endl;
sleep(1);
}