-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathzoul-demo.c
199 lines (177 loc) · 7.12 KB
/
zoul-demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
* Copyright (c) 2015, Zolertia - http://www.zolertia.com
* 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.
*/
/*---------------------------------------------------------------------------*/
/**
* \addtogroup zoul
* @{
*
* \defgroup zoul-examples Zoul examples
* @{
*
* \defgroup zoul-demo Zoul demo application
*
* Example project demonstrating the Zoul module on the RE-Mote and Firefly
* platforms.
*
* - Boot sequence: LEDs flashing (Red, then yellow, finally green)
*
* - etimer/clock : Every LOOP_INTERVAL clock ticks (LOOP_PERIOD secs) the LED
* defined as LEDS_PERIODIC will turn on
* - rtimer : Exactly LEDS_OFF_HYSTERISIS rtimer ticks later,
* LEDS_PERIODIC will turn back off
* - ADC sensors : On-chip VDD / 3, temperature, and Phidget sensor
* values are printed over UART periodically.
* - UART : Every LOOP_INTERVAL the Remote will print something over
* the UART. Receiving an entire line of text over UART (ending
* in \\r) will cause LEDS_SERIAL_IN to toggle
* - Radio comms : BTN_USER sends a rime broadcast. Reception of a rime
* packet will toggle LEDs defined as LEDS_RF_RX
* - Button : Keeping the button pressed will print a counter that
* increments every BUTTON_PRESS_EVENT_INTERVAL ticks
*
* @{
*
* \file
* Example demonstrating the Zoul module on the RE-Mote & Firefly platforms
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "cpu.h"
#include "sys/etimer.h"
#include "sys/rtimer.h"
#include "dev/leds.h"
#include "dev/uart.h"
#include "dev/button-sensor.h"
#include "dev/adc-zoul.h"
#include "dev/zoul-sensors.h"
#include "dev/watchdog.h"
#include "dev/serial-line.h"
#include "dev/sys-ctrl.h"
#include "net/netstack.h"
#include "net/rime/broadcast.h"
#include <stdio.h>
#include <stdint.h>
/*---------------------------------------------------------------------------*/
#define LOOP_PERIOD 8
#define LOOP_INTERVAL (CLOCK_SECOND * LOOP_PERIOD)
#define LEDS_OFF_HYSTERISIS ((RTIMER_SECOND * LOOP_PERIOD) >> 1)
#define LEDS_PERIODIC LEDS_BLUE
#define LEDS_BUTTON LEDS_RED
#define LEDS_SERIAL_IN LEDS_GREEN
#define LEDS_REBOOT LEDS_ALL
#define LEDS_RF_RX (LEDS_YELLOW | LEDS_RED)
#define BUTTON_PRESS_EVENT_INTERVAL (CLOCK_SECOND)
/*---------------------------------------------------------------------------*/
static struct etimer et;
static struct rtimer rt;
static uint16_t counter;
/*---------------------------------------------------------------------------*/
PROCESS(zoul_demo_process, "Zoul demo process");
AUTOSTART_PROCESSES(&zoul_demo_process);
/*---------------------------------------------------------------------------*/
static void
broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
{
leds_toggle(LEDS_RF_RX);
printf("*** Received %u bytes from %u:%u: '0x%04x'\n", packetbuf_datalen(),
from->u8[0], from->u8[1], *(uint16_t *)packetbuf_dataptr());
}
/*---------------------------------------------------------------------------*/
static const struct broadcast_callbacks bc_rx = { broadcast_recv };
static struct broadcast_conn bc;
/*---------------------------------------------------------------------------*/
static void
rt_callback(struct rtimer *t, void *ptr)
{
leds_off(LEDS_PERIODIC);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(zoul_demo_process, ev, data)
{
PROCESS_EXITHANDLER(broadcast_close(&bc))
PROCESS_BEGIN();
counter = 0;
/* Disable the radio duty cycle and keep the radio on */
NETSTACK_MAC.off(1);
broadcast_open(&bc, BROADCAST_CHANNEL, &bc_rx);
/* Configure the user button */
button_sensor.configure(BUTTON_SENSOR_CONFIG_TYPE_INTERVAL,
BUTTON_PRESS_EVENT_INTERVAL);
/* Configure the ADC ports */
adc_zoul.configure(SENSORS_HW_INIT, ZOUL_SENSORS_ADC_ALL);
printf("Zoul test application\n");
etimer_set(&et, LOOP_INTERVAL);
while(1) {
PROCESS_YIELD();
if(ev == PROCESS_EVENT_TIMER) {
leds_on(LEDS_PERIODIC);
printf("-----------------------------------------\n"
"Counter = 0x%08x\n", counter);
printf("VDD = %d mV\n",
vdd3_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED));
printf("Temperature = %d mC\n",
cc2538_temp_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED));
printf("ADC1 = %d raw\n",
adc_zoul.value(ZOUL_SENSORS_ADC1));
printf("ADC3 = %d raw\n",
adc_zoul.value(ZOUL_SENSORS_ADC3));
etimer_set(&et, LOOP_INTERVAL);
rtimer_set(&rt, RTIMER_NOW() + LEDS_OFF_HYSTERISIS, 1,
rt_callback, NULL);
counter++;
} else if(ev == sensors_event) {
if(data == &button_sensor) {
if(button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) ==
BUTTON_SENSOR_PRESSED_LEVEL) {
printf("Button pressed\n");
packetbuf_copyfrom(&counter, sizeof(counter));
broadcast_send(&bc);
} else {
printf("...and released!\n");
}
}
} else if(ev == serial_line_event_message) {
leds_toggle(LEDS_SERIAL_IN);
} else if(ev == button_press_duration_exceeded) {
printf("Button pressed for %d ticks [%u events]\n",
(*((uint8_t *)data) * BUTTON_PRESS_EVENT_INTERVAL),
button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION));
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
* @}
*/