Add button HAL example

This commit is contained in:
George Oikonomou 2018-03-11 20:43:53 +00:00
parent 155e2c9b0a
commit 4882077a75
4 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,8 @@
CONTIKI_PROJECT = button-hal-example
CONTIKI = ../../..
all: $(CONTIKI_PROJECT)
PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul
include $(CONTIKI)/Makefile.include

View File

@ -0,0 +1,15 @@
# Button HAL Example
This example demonstrates and tests the functionality of the Button HAL.
You can use this example to:
* Understand the logic of the button HAL.
* Test your implementation of arch-specific button HAL components if you are
developing a new port.
This example assumes a device with at least one switch (button or simiar).
# Supported devices
This example is expected to work off-the-shelf on the following boards:
* All CC13xx/CC26xx devices
* All CC2538 devices

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2018, George Oikonomou - http://www.spd.gr
* 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 copyright holder 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 COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDER 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.
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "dev/button-hal.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
PROCESS(button_hal_example, "Button HAL Example");
AUTOSTART_PROCESSES(&button_hal_example);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(button_hal_example, ev, data)
{
button_hal_button_t *btn;
PROCESS_BEGIN();
btn = button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO);
printf("Button HAL example.\n");
printf("%s on pin %u with ID=0, Logic=%s, Pull=%s\n",
BUTTON_HAL_GET_DESCRIPTION(btn), btn->pin,
btn->negative_logic ? "Negative" : "Positive",
btn->pull == GPIO_HAL_PIN_CFG_PULL_UP ? "Pull Up" : "Pull Down");
while(1) {
PROCESS_YIELD();
if(ev == button_hal_press_event) {
btn = (button_hal_button_t *)data;
printf("Press event (%s)\n", BUTTON_HAL_GET_DESCRIPTION(btn));
if(btn == button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO)) {
printf("This was button 0, on pin %u\n", btn->pin);
}
} else if(ev == button_hal_release_event) {
btn = (button_hal_button_t *)data;
printf("Release event (%s)\n", BUTTON_HAL_GET_DESCRIPTION(btn));
} else if(ev == button_hal_periodic_event) {
btn = (button_hal_button_t *)data;
printf("Periodic event, %u seconds (%s)\n", btn->press_duration_seconds,
BUTTON_HAL_GET_DESCRIPTION(btn));
if(btn->press_duration_seconds > 5) {
printf("%s pressed for more than 5 secs. Do custom action\n",
BUTTON_HAL_GET_DESCRIPTION(btn));
}
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2018, George Oikonomou - http://www.spd.gr
* 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 copyright holder 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 COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDER 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.
*/
/*---------------------------------------------------------------------------*/
#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_
/*---------------------------------------------------------------------------*/
/* Force button descriptions */
#define BUTTON_HAL_CONF_WITH_DESCRIPTION 1
/*---------------------------------------------------------------------------*/
#endif /* PROJECT_CONF_H_ */
/*---------------------------------------------------------------------------*/