contiki/examples/stm32nucleo-spirit1/sensor-demo/sensor-demo.c

145 lines
5.3 KiB
C
Raw Normal View History

/*
* Copyright (c) 2012, STMicroelectronics.
* All rights reserved.
2015-10-09 17:40:39 +00:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2015-10-09 17:40:39 +00:00
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2015-10-09 17:40:39 +00:00
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
2015-10-09 17:40:39 +00:00
*
2015-07-24 14:30:10 +00:00
*/
/*---------------------------------------------------------------------------*/
2015-07-24 14:30:10 +00:00
#include "contiki.h"
#include <stdio.h> /* For printf() */
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"
#include "net/ip/uip-udp-packet.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
2015-09-07 17:59:03 +00:00
#include "dev/radio-sensor.h"
#include "dev/sensor-common.h"
#include "st-lib.h"
2015-07-24 14:30:10 +00:00
#ifdef X_NUCLEO_IKS01A1
2015-07-24 14:30:10 +00:00
#include "dev/temperature-sensor.h"
#include "dev/humidity-sensor.h"
#include "dev/pressure-sensor.h"
#include "dev/magneto-sensor.h"
#include "dev/acceleration-sensor.h"
#include "dev/gyroscope-sensor.h"
#endif /*X_NUCLEO_IKS01A1*/
2015-07-24 14:30:10 +00:00
#define DEBUG DEBUG_PRINT
#include "net/ip/uip-debug.h"
2015-10-09 17:40:39 +00:00
#define PRINT_INTERVAL 5 * CLOCK_SECOND
2015-07-24 14:30:10 +00:00
/*---------------------------------------------------------------------------*/
PROCESS(sensor_demo_process, "Sensor demo process");
AUTOSTART_PROCESSES(&sensor_demo_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(sensor_demo_process, ev, data)
{
static struct etimer etimer;
static unsigned long _button_pressed;
2015-10-09 17:40:39 +00:00
static int sensor_value = 0;
2015-07-24 14:30:10 +00:00
PROCESS_BEGIN();
PROCESS_PAUSE();
SENSORS_ACTIVATE(button_sensor);
2015-09-07 17:59:03 +00:00
SENSORS_ACTIVATE(radio_sensor);
#ifdef X_NUCLEO_IKS01A1
2015-07-24 14:30:10 +00:00
SENSORS_ACTIVATE(temperature_sensor);
SENSORS_ACTIVATE(humidity_sensor);
SENSORS_ACTIVATE(pressure_sensor);
SENSORS_ACTIVATE(magneto_sensor);
SENSORS_ACTIVATE(acceleration_sensor);
SENSORS_ACTIVATE(gyroscope_sensor);
#endif /*X_NUCLEO_IKS01A1*/
2015-07-24 14:30:10 +00:00
2015-10-09 17:40:39 +00:00
while(1) {
2015-07-24 14:30:10 +00:00
etimer_set(&etimer, PRINT_INTERVAL);
2015-10-09 17:40:39 +00:00
2015-07-24 14:30:10 +00:00
PROCESS_WAIT_EVENT();
2015-10-09 17:40:39 +00:00
if(ev == sensors_event && data == &button_sensor) {
2015-07-24 14:30:10 +00:00
printf("Sensor event detected: Button Pressed.\n\n");
printf("Toggling Leds\n");
_button_pressed++;
leds_toggle(LEDS_ALL);
}
2015-10-09 17:40:39 +00:00
printf("Button state:\t%s (pressed %lu times)\n", button_sensor.value(0) ? "Released" : "Pressed",
_button_pressed);
2015-07-24 14:30:10 +00:00
#ifdef X_NUCLEO_IKS01A1
2015-10-09 17:40:39 +00:00
printf("LEDs status:\tRED:n/a GREEN:%s\n", leds_get() & LEDS_GREEN ? "on" : "off");
2015-07-24 14:30:10 +00:00
#else
2015-10-09 17:40:39 +00:00
printf("LEDs status:\tRED:%s GREEN:%s\n", leds_get() & LEDS_RED ? "on" : "off",
leds_get() & LEDS_GREEN ? "on" : "off");
#endif /*X_NUCLEO_IKS01A1*/
2015-09-07 17:59:03 +00:00
sensor_value = radio_sensor.value(RADIO_SENSOR_LAST_PACKET);
2015-10-09 17:40:39 +00:00
printf("Radio (RSSI):\t%d.%d dBm\n", sensor_value / 10, ABS_VALUE(sensor_value) % 10);
2015-09-07 17:59:03 +00:00
printf("Radio (LQI):\t%d\n", radio_sensor.value(RADIO_SENSOR_LAST_VALUE));
2015-07-24 14:30:10 +00:00
#ifdef X_NUCLEO_IKS01A1
2015-09-07 17:59:03 +00:00
sensor_value = temperature_sensor.value(0);
2015-10-09 17:40:39 +00:00
printf("Temperature:\t%d.%d C\n", sensor_value / 10, ABS_VALUE(sensor_value) % 10);
2015-07-24 14:30:10 +00:00
2015-09-07 17:59:03 +00:00
sensor_value = humidity_sensor.value(0);
2015-10-09 17:40:39 +00:00
printf("Humidity:\t%d.%d rH\n", sensor_value / 10, ABS_VALUE(sensor_value) % 10);
2015-07-24 14:30:10 +00:00
2015-09-07 17:59:03 +00:00
sensor_value = pressure_sensor.value(0);
2015-10-09 17:40:39 +00:00
printf("Pressure:\t%d.%d mbar\n", sensor_value / 10, ABS_VALUE(sensor_value) % 10);
2015-07-24 14:30:10 +00:00
2015-10-09 17:40:39 +00:00
/* NOTE: this demo uses the mapping of ST Nucleo sensors on Contiki sensor API.
* For a real use case of sensors like acceleration, magneto and gyroscope,
* it is better to directly call the ST lib to get the three value (X/Y/Z)
* at once.
*/
2015-07-24 14:30:10 +00:00
printf("Magneto:\t%d/%d/%d (X/Y/Z) mgauss\n", magneto_sensor.value(X_AXIS),
2015-10-09 17:40:39 +00:00
magneto_sensor.value(Y_AXIS),
magneto_sensor.value(Z_AXIS));
2015-07-24 14:30:10 +00:00
printf("Acceleration:\t%d/%d/%d (X/Y/Z) mg\n", acceleration_sensor.value(X_AXIS),
2015-10-09 17:40:39 +00:00
acceleration_sensor.value(Y_AXIS),
acceleration_sensor.value(Z_AXIS));
2015-07-24 14:30:10 +00:00
printf("Gyroscope:\t%d/%d/%d (X/Y/Z) mdps\n", gyroscope_sensor.value(X_AXIS),
2015-10-09 17:40:39 +00:00
gyroscope_sensor.value(Y_AXIS),
gyroscope_sensor.value(Z_AXIS));
#endif /*X_NUCLEO_IKS01A1*/
2015-07-24 14:30:10 +00:00
2015-10-09 17:40:39 +00:00
printf("\n");
2015-07-24 14:30:10 +00:00
}
2015-10-09 17:40:39 +00:00
2015-07-24 14:30:10 +00:00
PROCESS_END();
}
/*---------------------------------------------------------------------------*/