Contiki 3.x
ipso-button.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Yanzi Networks AB.
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  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /**
32  * \addtogroup ipso-objects
33  * @{
34  */
35 
36 /**
37  * \file
38  * Implementation of OMA LWM2M / IPSO button as a digital input
39  * \author
40  * Joakim Eriksson <joakime@sics.se>
41  * Niclas Finne <nfi@sics.se>
42  */
43 
44 #include "contiki.h"
45 #include "lwm2m-object.h"
46 #include "lwm2m-engine.h"
47 #include "er-coap-engine.h"
48 
49 #define DEBUG 0
50 #if DEBUG
51 #include <stdio.h>
52 #define PRINTF(...) printf(__VA_ARGS__)
53 #else
54 #define PRINTF(...)
55 #endif
56 
57 #if PLATFORM_HAS_BUTTON
58 #include "dev/button-sensor.h"
59 
60 PROCESS(ipso_button_process, "ipso-button");
61 #endif /* PLATFORM_HAS_BUTTON */
62 
63 static int input_state = 0;
64 static int polarity = 0;
65 static int32_t counter = 0;
66 static int32_t edge_selection = 3;
67 static int32_t debounce_time = 10;
68 /*---------------------------------------------------------------------------*/
69 static int
70 read_state(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outsize)
71 {
72  int value;
73  if(polarity == 0) {
74  value = input_state ? 1 : 0;
75  } else {
76  value = input_state ? 0 : 1;
77  }
78  PRINTF("Read button state (polarity=%d, state=%d): %d\n",
79  polarity, input_state, value);
80  return ctx->writer->write_boolean(ctx, outbuf, outsize, value);
81 }
82 /*---------------------------------------------------------------------------*/
83 static int
84 reset_counter(lwm2m_context_t *ctx, const uint8_t *arg, size_t len,
85  uint8_t *outbuf, size_t outlen)
86 {
87  counter = 0;
88  return 0;
89 }
90 /*---------------------------------------------------------------------------*/
91 LWM2M_RESOURCES(button_resources,
92  LWM2M_RESOURCE_CALLBACK(5500, { read_state, NULL, NULL }),
93  LWM2M_RESOURCE_INTEGER_VAR(5501, &counter),
94  LWM2M_RESOURCE_BOOLEAN_VAR(5502, &polarity),
95  LWM2M_RESOURCE_INTEGER_VAR(5503, &debounce_time),
96  LWM2M_RESOURCE_INTEGER_VAR(5504, &edge_selection),
97  LWM2M_RESOURCE_CALLBACK(5505, { NULL, NULL, reset_counter }),
98  LWM2M_RESOURCE_STRING(5751, "Button")
99  );
100 LWM2M_INSTANCES(button_instances,
101  LWM2M_INSTANCE(0, button_resources));
102 LWM2M_OBJECT(button, 3200, button_instances);
103 /*---------------------------------------------------------------------------*/
104 void
105 ipso_button_init(void)
106 {
107  /* register this device and its handlers - the handlers automatically
108  sends in the object to handle */
109  lwm2m_engine_register_object(&button);
110 
111 #if PLATFORM_HAS_BUTTON
112  process_start(&ipso_button_process, NULL);
113 #endif /* PLATFORM_HAS_BUTTON */
114 }
115 /*---------------------------------------------------------------------------*/
116 #if PLATFORM_HAS_BUTTON
117 PROCESS_THREAD(ipso_button_process, ev, data)
118 {
119  static struct etimer timer;
120  int32_t time;
121 
122  PROCESS_BEGIN();
123 
124  SENSORS_ACTIVATE(button_sensor);
125 
126  while(1) {
128 
129  if(ev == sensors_event && data == &button_sensor) {
130  if(!input_state) {
131  input_state = 1;
132  counter++;
133  if((edge_selection & 2) != 0) {
134  lwm2m_object_notify_observers(&button, "/0/5500");
135  }
136  lwm2m_object_notify_observers(&button, "/0/5501");
137 
138  time = (debounce_time * CLOCK_SECOND / 1000);
139  if(time < 1) {
140  time = 1;
141  }
142  etimer_set(&timer, (clock_time_t)time);
143  }
144  } else if(ev == PROCESS_EVENT_TIMER && data == &timer) {
145  if(!input_state) {
146  /* Button is not in pressed state */
147  } else if(button_sensor.value(0) != 0) {
148  /* Button is still pressed */
150  } else {
151  input_state = 0;
152  if((edge_selection & 1) != 0) {
153  lwm2m_object_notify_observers(&button, "/0/5500");
154  }
155  }
156  }
157  }
158 
159  PROCESS_END();
160 }
161 #endif /* PLATFORM_HAS_BUTTON */
162 /*---------------------------------------------------------------------------*/
163 /** @} */
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
Header file for the Contiki OMA LWM2M object API
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
A timer.
Definition: timer.h:86
A timer.
Definition: etimer.h:76
Header file for the Contiki OMA LWM2M engine
#define NULL
The null pointer.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
CoAP implementation for the REST Engine.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120