2010-11-07 09:41:48 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* A simple program for testing the adxl345 on-board accelerometer of the
|
|
|
|
* Zolertia Z1. Enables interrupts and registers callbacks for them. Then
|
|
|
|
* starts a constantly running readout of acceleration data.
|
|
|
|
* \author
|
|
|
|
* Marcus Lundén, SICS <mlunden@sics.se>
|
|
|
|
* Enric M. Calvo, Zolertia <ecalvo@zolertia.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "contiki.h"
|
2016-02-22 11:24:50 +01:00
|
|
|
#include "dev/leds.h"
|
2011-12-13 17:35:04 +01:00
|
|
|
#include "dev/adxl345.h"
|
2015-11-24 13:00:23 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#define LED_INT_ONTIME (CLOCK_SECOND / 2)
|
2010-11-07 09:41:48 +01:00
|
|
|
#define ACCM_READ_INTERVAL CLOCK_SECOND
|
2015-11-24 13:00:23 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static process_event_t led_off_event;
|
|
|
|
static struct etimer led_etimer;
|
|
|
|
static struct etimer et;
|
2010-11-07 09:41:48 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS(accel_process, "Test Accel process");
|
|
|
|
PROCESS(led_process, "LED handling process");
|
|
|
|
AUTOSTART_PROCESSES(&accel_process, &led_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-11-24 13:00:23 +01:00
|
|
|
/* As several interrupts can be mapped to one interrupt pin, when interrupt
|
|
|
|
* strikes, the adxl345 interrupt source register is read. This function prints
|
|
|
|
* out which interrupts occurred. Note that this will include all interrupts,
|
|
|
|
* even those mapped to 'the other' pin, and those that will always signal even
|
|
|
|
* if not enabled (such as watermark).
|
|
|
|
*/
|
2010-11-07 09:41:48 +01:00
|
|
|
void
|
2015-11-24 13:00:23 +01:00
|
|
|
print_int(uint16_t reg)
|
|
|
|
{
|
2010-11-07 09:41:48 +01:00
|
|
|
if(reg & ADXL345_INT_FREEFALL) {
|
|
|
|
printf("Freefall ");
|
|
|
|
}
|
|
|
|
if(reg & ADXL345_INT_INACTIVITY) {
|
|
|
|
printf("InActivity ");
|
|
|
|
}
|
|
|
|
if(reg & ADXL345_INT_ACTIVITY) {
|
|
|
|
printf("Activity ");
|
|
|
|
}
|
|
|
|
if(reg & ADXL345_INT_DOUBLETAP) {
|
|
|
|
printf("DoubleTap ");
|
|
|
|
}
|
|
|
|
if(reg & ADXL345_INT_TAP) {
|
|
|
|
printf("Tap ");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* accelerometer free fall detection callback */
|
|
|
|
|
|
|
|
void
|
2015-11-24 13:00:23 +01:00
|
|
|
accm_ff_cb(uint8_t reg)
|
|
|
|
{
|
2016-02-22 11:24:50 +01:00
|
|
|
leds_on(LEDS_BLUE);
|
2015-11-24 13:00:23 +01:00
|
|
|
process_post(&led_process, led_off_event, NULL);
|
|
|
|
printf("~~[%u] Freefall detected! (0x%02X) -- ",
|
|
|
|
((uint16_t)clock_time()) / 128, reg);
|
2010-11-07 09:41:48 +01:00
|
|
|
print_int(reg);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* accelerometer tap and double tap detection callback */
|
|
|
|
|
|
|
|
void
|
2015-11-24 13:00:23 +01:00
|
|
|
accm_tap_cb(uint8_t reg)
|
|
|
|
{
|
|
|
|
process_post(&led_process, led_off_event, NULL);
|
|
|
|
if(reg & ADXL345_INT_DOUBLETAP) {
|
2016-02-22 11:24:50 +01:00
|
|
|
leds_on(LEDS_GREEN);
|
2015-11-24 13:00:23 +01:00
|
|
|
printf("~~[%u] DoubleTap detected! (0x%02X) -- ",
|
|
|
|
((uint16_t)clock_time()) / 128, reg);
|
2010-11-07 09:41:48 +01:00
|
|
|
} else {
|
2016-02-22 11:24:50 +01:00
|
|
|
leds_on(LEDS_RED);
|
2015-11-24 13:00:23 +01:00
|
|
|
printf("~~[%u] Tap detected! (0x%02X) -- ",
|
|
|
|
((uint16_t)clock_time()) / 128, reg);
|
2010-11-07 09:41:48 +01:00
|
|
|
}
|
|
|
|
print_int(reg);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(led_process, ev, data) {
|
|
|
|
PROCESS_BEGIN();
|
2015-11-24 13:00:23 +01:00
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == led_off_event);
|
|
|
|
etimer_set(&led_etimer, LED_INT_ONTIME);
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&led_etimer));
|
2016-02-22 11:24:50 +01:00
|
|
|
leds_off(LEDS_RED + LEDS_GREEN + LEDS_BLUE);
|
2010-11-07 09:41:48 +01:00
|
|
|
}
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Main process, setups */
|
2016-02-22 12:06:38 +01:00
|
|
|
PROCESS_THREAD(accel_process, ev, data)
|
|
|
|
{
|
2010-11-07 09:41:48 +01:00
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2016-02-22 12:06:38 +01:00
|
|
|
int16_t x, y, z;
|
|
|
|
|
|
|
|
/* Register the event used for lighting up an LED when interrupt strikes. */
|
|
|
|
led_off_event = process_alloc_event();
|
2010-11-07 09:41:48 +01:00
|
|
|
|
2016-02-22 12:06:38 +01:00
|
|
|
/* Start and setup the accelerometer with default values, eg no interrupts
|
|
|
|
* enabled.
|
|
|
|
*/
|
|
|
|
SENSORS_ACTIVATE(adxl345);
|
2010-11-07 09:41:48 +01:00
|
|
|
|
2016-02-22 12:06:38 +01:00
|
|
|
/* Register the callback functions for each interrupt */
|
|
|
|
ACCM_REGISTER_INT1_CB(accm_ff_cb);
|
|
|
|
ACCM_REGISTER_INT2_CB(accm_tap_cb);
|
2010-11-07 09:41:48 +01:00
|
|
|
|
2016-02-22 12:06:38 +01:00
|
|
|
/* Set what strikes the corresponding interrupts. Several interrupts per
|
|
|
|
* pin is possible. For the eight possible interrupts, see adxl345.h and
|
|
|
|
* adxl345 datasheet.
|
|
|
|
*/
|
|
|
|
accm_set_irq(ADXL345_INT_FREEFALL, ADXL345_INT_TAP + ADXL345_INT_DOUBLETAP);
|
2010-11-07 09:41:48 +01:00
|
|
|
|
2016-02-22 12:06:38 +01:00
|
|
|
while(1) {
|
|
|
|
x = adxl345.value(X_AXIS);
|
|
|
|
y = adxl345.value(Y_AXIS);
|
|
|
|
z = adxl345.value(Z_AXIS);
|
|
|
|
printf("x: %d y: %d z: %d\n", x, y, z);
|
2010-11-07 09:41:48 +01:00
|
|
|
|
2016-02-22 12:06:38 +01:00
|
|
|
etimer_set(&et, ACCM_READ_INTERVAL);
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
|
2010-11-07 09:41:48 +01:00
|
|
|
}
|
2016-02-22 12:06:38 +01:00
|
|
|
|
2010-11-07 09:41:48 +01:00
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|