upm  1.3.0
Sensor/Actuator repository for libmraa (v1.7.0)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Functions
ims.h File Reference
Include dependency graph for ims.h:

API Description

/*
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2016 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 <stdio.h>
#include <signal.h>
#include "ims.h"
#include "upm_utilities.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
ims_context* sensor = ims_init(0, IMS_ADDRESS_DEFAULT);
if (!sensor)
{
printf("ims_init() failed\n");
return 1;
}
int i2c_addr_cur = IMS_ADDRESS_DEFAULT + 1;
// Every second, sample the sensor outputs
while (shouldRun)
{
uint16_t version, light, moisture, temp;
if (ims_get_version(sensor, &version) == UPM_SUCCESS &&
ims_get_light(sensor, &light) == UPM_SUCCESS &&
ims_get_moisture(sensor, &moisture) == UPM_SUCCESS &&
ims_get_temperature(sensor, &temp) == UPM_SUCCESS)
printf("Version: %d light: 0x%04x moisture: 0x%04x temp: %3.2f C\n",
version, light, moisture, temp/10.0);
else
{
printf("Failed to read IMS sensor data...\n");
break;
}
// Change the address and continue
if (i2c_addr_cur >= 128) i2c_addr_cur = 1;
printf("Changing device address to 0x%02x\n", i2c_addr_cur);
if (ims_reset_i2c_address(sensor, i2c_addr_cur++) != UPM_SUCCESS)
{
printf("Failed to change IMS I2C address...\n");
break;
}
upm_delay(1);
}
printf("Exiting\n");
ims_close(sensor);
return 0;
}

Go to the source code of this file.

Data Structures

struct  ims_context
 

Functions

ims_contextims_init (int16_t i2c_bus, int16_t i2c_address)
 
void ims_close (ims_context *dev)
 
upm_result_t ims_read (const ims_context *dev, IMS_RD_COMMAND cmd, uint16_t *rd_data)
 
upm_result_t ims_write (const ims_context *dev, IMS_WR_COMMAND cmd, uint8_t wr_data)
 
upm_result_t ims_get_version (const ims_context *dev, uint16_t *rd_data)
 
upm_result_t ims_get_moisture (const ims_context *dev, uint16_t *rd_data)
 
upm_result_t ims_get_light (const ims_context *dev, uint16_t *rd_data)
 
upm_result_t ims_get_temperature (const ims_context *dev, uint16_t *rd_data)
 
upm_result_t ims_reset (const ims_context *dev)
 
upm_result_t ims_reset_i2c_address (ims_context *dev, uint8_t address_new)
 
upm_result_t ims_sleep (const ims_context *dev)
 

Function Documentation

ims_context* ims_init ( int16_t  i2c_bus,
int16_t  i2c_address 
)

Initialize sensor

Parameters
i2c_busTarget I2C bus
i2c_addressTarget I2C address (default is 0x20)
Returns
sensor context pointer
void ims_close ( ims_context dev)

Sensor close method. Cleans up any memory held by this device

Parameters
sensorcontext pointer
upm_result_t ims_read ( const ims_context dev,
IMS_RD_COMMAND  cmd,
uint16_t *  rd_data 
)

Read I2C Moisture Sensor registers

Parameters
devSensor context pointer
cmdRead command
rd_dataData returned from sensor (1 or 2 bytes depending on cmd)
Returns
Function result code

Here is the caller graph for this function:

upm_result_t ims_write ( const ims_context dev,
IMS_WR_COMMAND  cmd,
uint8_t  wr_data 
)

Write I2C Moisture Sensor registers

Parameters
devSensor context pointer
cmdWrite command
wr_dataTarget data to write (only used for IMS_SET_ADDRESS)
Returns
Function result code

Here is the caller graph for this function:

upm_result_t ims_get_version ( const ims_context dev,
uint16_t *  rd_data 
)

Get sensor version

Parameters
devSensor context pointer
rd_dataSensor version
Returns
Function result code

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t ims_get_moisture ( const ims_context dev,
uint16_t *  rd_data 
)

Get moisture reading from sensor

Parameters
devSensor context pointer
rd_dataUnitless, relative capacitance value (used to determine moisture)
Returns
Function result code

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t ims_get_light ( const ims_context dev,
uint16_t *  rd_data 
)

Get light reading from LED on device. The technical data for the I2C moisture sensor specifies a 3 second wait. Loop for 3 seconds checking the GET_BUSY register. IF the sensor is NOT ready after 3 seconds, assume there is NO light and return a max uint16_t (dark) value.

Parameters
devSensor context pointer
rd_dataUnitless, relative value for brightness dark (0xFFFF) —> light (0x0000)
Returns
Function result code

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t ims_get_temperature ( const ims_context dev,
uint16_t *  rd_data 
)

Get temperature reading from device

Parameters
devSensor context pointer
rd_dataTemperature in degrees Celsius * 10 ie, 256 = 25.6 C
Returns
Function result code

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t ims_reset ( const ims_context dev)

Reset sensor

Parameters
devSensor context pointer
Returns
Function result code

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t ims_reset_i2c_address ( ims_context dev,
uint8_t  address_new 
)

Set I2C address AND reset sensor

Parameters
devSensor context pointer
address_newNew I2C for device
Returns
Function result code

Here is the call graph for this function:

Here is the caller graph for this function:

upm_result_t ims_sleep ( const ims_context dev)

Put device into low-power mode. Device wakes on any I2C command.

Parameters
devSensor context pointer
Returns
Function result code

Here is the call graph for this function:

Here is the caller graph for this function: