nrf52dk: added examples

This commit is contained in:
Wojciech Bober 2016-01-09 14:44:58 +01:00
parent d39ad95db5
commit 38481c513d
16 changed files with 1826 additions and 0 deletions

View file

@ -0,0 +1,9 @@
CONTIKI_PROJECT = blink-hello
CONTIKI_WITH_RPL=0
NRF52_WITHOUT_SOFTDEVICE=1
all: $(CONTIKI_PROJECT)
CONTIKI = ../../..
include $(CONTIKI)/Makefile.include

View file

@ -0,0 +1,14 @@
Blink Hello example
===================
This example shows basic usage of DK's buttons and LEDs. It also shows basic
usage of Contiki's processes. The application autostarts 5 processes: 4 processes
for button and LED control and 1 to display current temperature to the console.
A process reacts to a press of a respective button (process 1 reacts to button 1, etc.)
and doubles the current blinking frequency. The cycle restarts for beginning when blinking
frequency is greater than 8Hz.
The example requires one DK and it doesn't use SoftDevice. To compile and flash the
example run:
make TARGET=nrf52dk blink-hello.flash

View file

@ -0,0 +1,182 @@
/* This is a very simple hello_world program.
* It aims to demonstrate the co-existence of two processes:
* One of them prints a hello world message and the other blinks the LEDs
*
* It is largely based on hello_world in $(CONTIKI)/examples/sensinode
*
* Author: George Oikonomou - <oikonomou@users.sourceforge.net>
*/
/**
* \addtogroup nrf52dk nRF52 Development Kit
* @{
*
* \addtogroup nrf52dk-examples Demo projects for nRF52 DK
* @{
*
* \defgroup nrf52dk-blink-hello Basic sensors and LEDs demo
* @{
*
* This demo demonstrates use of Contiki processes, sensors, and LEDs
* on nRF52 DK. Pressing a button will start a timer that blinks a
* respective LED (e.g., button 1 controls LED 1). Each time the button
* is pressed blinking frequency is doubled. On 4th press the LED is
* switched off and the sequence can be started from the beginning.
*
* \file Main file for Basic sensors and LEDs demo.
*/
#include <stdio.h> /* For printf() */
#include <inttypes.h>
#include "contiki.h"
#include "dev/leds.h"
#include "dev/temperature-sensor.h"
#include "lib/sensors.h"
#include "button-sensor.h"
/*---------------------------------------------------------------------------*/
PROCESS(blink_process_1, "LED1 blink process");
PROCESS(blink_process_2, "LED2 blink process");
PROCESS(blink_process_3, "LED3 blink process");
PROCESS(blink_process_4, "LED4 blink process");
PROCESS(temp, "Temperautre");
AUTOSTART_PROCESSES(
&blink_process_1,
&blink_process_2,
&blink_process_3,
&blink_process_4,
&temp
);
struct blink_process_ctx {
struct etimer et_blink;
unsigned char c;
const struct sensors_sensor *button;
unsigned char led;
};
static void handle_event(process_event_t ev, process_data_t data, struct blink_process_ctx *ctx)
{
if (ev == PROCESS_EVENT_TIMER && etimer_expired(&ctx->et_blink)) {
leds_toggle(ctx->led);
etimer_set(&ctx->et_blink, CLOCK_SECOND / ctx->c);
printf("Blink %d\n", ctx->led);
} else if (ev == sensors_event && data == ctx->button) {
if (ctx->button->value(BUTTON_SENSOR_VALUE_STATE) == 0) {
if (ctx->c == 0) {
ctx->c = 1;
} else if (ctx->c < 8){
ctx->c <<= 1;
} else {
ctx->c = 0;
leds_off(ctx->led);
}
if (ctx->c) {
etimer_set(&ctx->et_blink, CLOCK_SECOND / ctx->c);
} else {
etimer_stop(&ctx->et_blink);
}
}
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(blink_process_1, ev, data)
{
static struct blink_process_ctx ctx;
PROCESS_BEGIN();
ctx.button = &button_1;
ctx.c = 0;
ctx.led = LEDS_1;
ctx.button->configure(SENSORS_ACTIVE, 1);
while (1) {
PROCESS_WAIT_EVENT();
handle_event(ev, data, &ctx);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(blink_process_2, ev, data)
{
static struct blink_process_ctx ctx;
PROCESS_BEGIN();
ctx.button = &button_2;
ctx.c = 0;
ctx.led = LEDS_2;
ctx.button->configure(SENSORS_ACTIVE, 1);
while (1) {
PROCESS_WAIT_EVENT();
handle_event(ev, data, &ctx);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(blink_process_3, ev, data)
{
static struct blink_process_ctx ctx;
PROCESS_BEGIN();
ctx.button = &button_3;
ctx.c = 0;
ctx.led = LEDS_3;
ctx.button->configure(SENSORS_ACTIVE, 1);
while (1) {
PROCESS_WAIT_EVENT();
handle_event(ev, data, &ctx);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(blink_process_4, ev, data)
{
static struct blink_process_ctx ctx;
PROCESS_BEGIN();
ctx.button = &button_4;
ctx.c = 0;
ctx.led = LEDS_4;
ctx.button->configure(SENSORS_ACTIVE, 1);
while (1) {
PROCESS_WAIT_EVENT();
handle_event(ev, data, &ctx);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(temp, ev, data)
{
static struct etimer tick;
PROCESS_BEGIN();
etimer_set(&tick, CLOCK_SECOND);
while (1) {
PROCESS_WAIT_EVENT();
if (ev == PROCESS_EVENT_TIMER && etimer_expired(&tick)) {
int32_t temp = temperature_sensor.value(0);
printf("temp: %"PRId32".%02"PRId32"\n", temp >> 2, (temp & 0x03)*25);
etimer_reset(&tick);
}
}
PROCESS_END();
}
/**
* @}
* @}
* @}
*/