Contiki-NG
buttons.c
1 /*
2  * Copyright (c) 2019, George Oikonomou - http://www.spd.gr
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*---------------------------------------------------------------------------*/
32 #include "contiki.h"
33 #include "dev/button-hal.h"
34 #include "lib/simEnvChange.h"
35 
36 #include <stdint.h>
37 #include <string.h>
38 /*---------------------------------------------------------------------------*/
39 /* Log configuration */
40 #include "sys/log.h"
41 #define LOG_MODULE "Cooja Button"
42 #define LOG_LEVEL LOG_LEVEL_NONE
43 /*---------------------------------------------------------------------------*/
44 /* Variables required by the Cooja button interface */
45 char simButtonChanged;
46 char simButtonIsDown;
47 char simButtonIsActive = 1;
48 const struct simInterface button_interface;
49 /*---------------------------------------------------------------------------*/
50 /* The cooja virtual button on pin COOJA_BTN_PIN, active low */
51 BUTTON_HAL_BUTTON(button_user, "User button", COOJA_BTN_PIN,
52  GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_ZERO, true);
53 /*---------------------------------------------------------------------------*/
54 BUTTON_HAL_BUTTONS(&button_user);
55 /*---------------------------------------------------------------------------*/
56 static void
57 doInterfaceActionsBeforeTick(void)
58 {
59  if(simButtonChanged) {
60  LOG_DBG("Cooja button changed. simButtonIsDown=%u, ", simButtonIsDown);
61  /*
62  * First check what the simulator wants us to do: press or release. Based
63  * on that, set the correct state for COOJA_BTN_PIN.
64  */
65  if(simButtonIsDown) {
66  /* The button is active low, so clear the pin when pressed */
67  LOG_DBG_("clearing pin");
68  gpio_hal_arch_no_port_clear_pin(COOJA_BTN_PIN);
69  } else {
70  LOG_DBG_("setting pin");
71  gpio_hal_arch_no_port_set_pin(COOJA_BTN_PIN);
72  }
73 
74  /*
75  * Subsequently, simply raise a virtual edge event on the pin, but only if
76  * the interrupt is "enabled"
77  */
78  if(gpio_hal_arch_no_port_pin_cfg_get(COOJA_BTN_PIN) & GPIO_HAL_PIN_CFG_INT_ENABLE) {
79  LOG_DBG_(", triggering edge event");
81  }
82  LOG_DBG_("\n");
83  }
84 
85  simButtonChanged = 0;
86 }
87 /*---------------------------------------------------------------------------*/
88 static void
89 doInterfaceActionsAfterTick(void)
90 {
91 }
92 /*---------------------------------------------------------------------------*/
93 SIM_INTERFACE(button_interface,
94  doInterfaceActionsBeforeTick,
95  doInterfaceActionsAfterTick);
96 /*---------------------------------------------------------------------------*/
97 /*
98  * Everything below needed to satisfy Cooja build dependency, which can be
99  * removed when we change Cooja to no longer expect a button_sensor symbol
100  */
101 #include "lib/sensors.h"
102 #include "dev/button-sensor.h"
103 /*---------------------------------------------------------------------------*/
104 static int
105 value(int type)
106 {
107  return 0;
108 }
109 /*---------------------------------------------------------------------------*/
110 static int
111 configure(int type, int c)
112 {
113  return 0;
114 }
115 /*---------------------------------------------------------------------------*/
116 static int
117 status(int type)
118 {
119  return 0;
120 }
121 /*---------------------------------------------------------------------------*/
122 SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, configure, status);
void gpio_hal_arch_no_port_set_pin(gpio_hal_pin_t pin)
Set a GPIO pin to logical high.
SENSORS & button_sensor
Exports global symbols for the sensor API.
Definition: z1-sensors.c:46
#define BUTTON_HAL_ID_BUTTON_ZERO
Optional button IDs.
Definition: button-hal.h:133
gpio_hal_pin_cfg_t gpio_hal_arch_no_port_pin_cfg_get(gpio_hal_pin_t pin)
Read the configuration of a GPIO pin.
Definition: gpio-hal-arch.c:97
void gpio_hal_arch_no_port_clear_pin(gpio_hal_pin_t pin)
Clear a GPIO pin (logical low)
#define gpio_hal_pin_to_mask(pin)
Convert a pin to a pin mask.
Definition: gpio-hal.h:255
void gpio_hal_event_handler(gpio_hal_port_t port, gpio_hal_pin_mask_t pins)
The platform-independent GPIO event handler.
Header file for the logging system
Header file for the button HAL.