Skip to content

Instantly share code, notes, and snippets.

@thomasvn
Created December 22, 2016 00:32
Show Gist options
  • Save thomasvn/2938f3c6fd6ad30268a06e7a4f7603ad to your computer and use it in GitHub Desktop.
Save thomasvn/2938f3c6fd6ad30268a06e7a4f7603ad to your computer and use it in GitHub Desktop.
A minimal contiki program to play around with LEDs based on buttons pressed
#include "contiki.h"
#include "sys/etimer.h"
#include "dev/leds.h"
#include "dev/watchdog.h"
#include "button-sensor.h"
#include "rf-core/rf-ble.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
#define CC26XX_DEMO_LOOP_INTERVAL (CLOCK_SECOND * 20)
#define CC26XX_DEMO_LEDS_PERIODIC LEDS_YELLOW
#define CC26XX_DEMO_LEDS_BUTTON LEDS_RED
#define CC26XX_DEMO_LEDS_REBOOT LEDS_ALL
/*---------------------------------------------------------------------------*/
#define CC26XX_DEMO_SENSOR_1 &button_left_sensor
#define CC26XX_DEMO_SENSOR_2 &button_right_sensor
/*---------------------------------------------------------------------------*/
static struct etimer et;
/*---------------------------------------------------------------------------*/
PROCESS(sensortag_led_experiment, "sensortag_led_experiment");
AUTOSTART_PROCESSES(&sensortag_led_experiment);
/*---------------------------------------------------------------------------*/
// ev: event type
// data: data passed along with the event
PROCESS_THREAD(sensortag_led_experiment, ev, data)
{
PROCESS_BEGIN();
printf("CC26XX LED Experiment\n");
/* Init the BLE advertisement daemon */
rf_ble_beacond_config(0, BOARD_STRING);
rf_ble_beacond_start();
etimer_set(&et, CC26XX_DEMO_LOOP_INTERVAL);
while(1) {
// PROCESS_YIELD: Wait for an event
// (However, I believe this is non-blocking)
PROCESS_YIELD();
// PROCESS_EVENT_TIMER: This event is sent to a process when an event timer (etimer) has expired.
if(ev == PROCESS_EVENT_TIMER) {
// Toggle LED once timer hits zero, then reset the timer
if(data == &et) {
leds_toggle(CC26XX_DEMO_LEDS_PERIODIC);
etimer_set(&et, CC26XX_DEMO_LOOP_INTERVAL);
}
} else if(ev == sensors_event) {
if(data == CC26XX_DEMO_SENSOR_1) {
// If you hit the left button, toggle the Red LED
printf("Left: Pin %d, press duration %d clock ticks\n",
(CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_STATE),
(CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_DURATION));
if((CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_DURATION) >
CLOCK_SECOND) {
printf("Long button press!\n");
}
leds_toggle(CC26XX_DEMO_LEDS_BUTTON);
} else if(data == CC26XX_DEMO_SENSOR_2) {
// If you hit the right button, turn on both LEDs, then reset
leds_on(CC26XX_DEMO_LEDS_REBOOT);
watchdog_reboot(); // I believe this turns all LEDs off again
}
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
* @}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment