grove: sliding potentiometer

Signed-off-by: Mihai Tudor Panu <mihai.t.panu@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Mihai Tudor Panu 2014-11-06 10:24:56 -08:00 committed by Brendan Le Foll
parent b2ffcdd9ea
commit cebc339d91
7 changed files with 215 additions and 0 deletions

BIN
docs/images/groveslide.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -1,6 +1,8 @@
add_executable (hmc5883l-example hmc5883l.cxx)
add_executable (groveled-example groveled.cxx)
add_executable (grovetemp-example grovetemp.cxx)
add_executable (groverotary-example groverotary.cxx)
add_executable (groveslide-example groveslide.cxx)
add_executable (lcm-lcd-example lcm-lcd.cxx)
add_executable (rgb-lcd-example rgb-lcd.cxx)
add_executable (buzzer-sound-example buzzer-sound.cxx)
@ -77,6 +79,8 @@ include_directories (${PROJECT_SOURCE_DIR}/src/tsl2561)
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (grovetemp-example grove ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groverotary-example grove ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveslide-example grove ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (lcm-lcd-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (rgb-lcd-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (buzzer-sound-example buzzer ${CMAKE_THREAD_LIBS_INIT})

50
examples/groveslide.cxx Normal file
View File

@ -0,0 +1,50 @@
/*
* Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include "grove.h"
using namespace std;
int main ()
{
//! [Interesting]
upm::GroveSlide* slide = new upm::GroveSlide(0); // Instantiate new grove slide potentiometer on analog pin A0
cout << slide->name() << endl;
while(true) {
float adc_value = slide->raw_value(); // Read raw value
float volts = slide->voltage_value(); // Read voltage, board reference set at 5.0V
fprintf(stdout, "%4d = %.2f V\n", (uint16_t)adc_value, volts);
usleep(2500000); // Sleep for 2.5s
}
//! [Interesting]
delete slide;
return 0;
}

View File

@ -0,0 +1,42 @@
/*
* Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var upm_grove = require('jsupm_grove');
//setup access analog input Analog pin #0 (A0)
var groveSlide = new upm_grove.GroveSlide(0);
loop();
function loop()
{
var raw = groveSlide.raw_value();
var volts = groveSlide.voltage_value();
//write the slider values to the console
console.log("Slider Value: " + raw + " = " + volts.toFixed(2) + " V");
//wait 2 s then call function again
setTimeout(loop, 2000);
}

View File

@ -0,0 +1,39 @@
# Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
# Copyright (c) 2014 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from time import sleep
import pyupm_grove as grove
# New Grove Slider on AIO pin 0
slider = grove.GroveSlide(0)
# Loop indefinitely
while True:
# Read values
raw = slider.raw_value()
volts = slider.voltage_value()
print "Slider value: ", raw , " = %.2f" % volts , " V"
# Sleep for 2.5 s
sleep(2.5)

View File

@ -160,3 +160,36 @@ float GroveRotary::rel_rad()
{
return GroveRotary::rel_deg() * M_PI / 180.0;
}
//// GroveSlide ////
GroveSlide::GroveSlide(unsigned int pin, float ref_voltage)
{
mraa_init();
m_aio = mraa_aio_init(pin);
m_ref_voltage = ref_voltage;
m_name = "Slide Potentiometer";
}
GroveSlide::~GroveSlide()
{
mraa_aio_close(m_aio);
}
float GroveSlide::raw_value()
{
return (float) mraa_aio_read(m_aio);
}
float GroveSlide::voltage_value()
{
// conversion to Volts
float a = GroveSlide::raw_value();
a = m_ref_voltage * a / 1023.0 ;
return a;
}
float GroveSlide::ref_voltage()
{
return m_ref_voltage;
}

View File

@ -200,4 +200,51 @@ class GroveRotary: public Grove {
static const int m_max_angle = 300;
};
/**
* @brief C++ API for Grove Slide Potentiometer
*
* Very basic UPM module for Grove slide potentiometer on analog,
* returns either raw value or scaled voltage value.
*
* @ingroup grove analog
* @snippet groveslide.cxx Interesting
* @image html groveslide.jpeg
*/
class GroveSlide: public Grove {
public:
/**
* Grove analog slide potentiometer constructor
*
* @param pin number of analog pin to use
*
* @param ref_voltage the reference voltage the board is set to as float, e.g. 3.3 or 5.0 (default)
*/
GroveSlide(unsigned int pin, float ref_voltage = 5.0);
/**
* GroveSlide Destructor
*/
~GroveSlide();
/**
* Get raw value from AIO pin
*
* @return the raw value from the ADC
*/
float raw_value();
/**
* Get the voltage value from the pin
*
* @return the voltage reading based on the reference voltage
*/
float voltage_value();
/**
* Get the board's reference voltage passed on object initialization
*
* @return the reference voltage the class was set for
*/
float ref_voltage();
private:
mraa_aio_context m_aio;
float m_ref_voltage;
};
}