root/nano-RK/projects/basic_sensors/main.c

Revision 597, 3.9 kB (checked in by agr, 2 weeks ago)

audio delay added

Line 
1 /******************************************************************************
2 *  Nano-RK, a real-time operating system for sensor networks.
3 *  Copyright (C) 2007, Real-Time and Multimedia Lab, Carnegie Mellon University
4 *  All rights reserved.
5 *
6 *  This is the Open Source Version of Nano-RK included as part of a Dual
7 *  Licensing Model. If you are unsure which license to use please refer to:
8 *  http://www.nanork.org/nano-RK/wiki/Licensing
9 *
10 *  This program is free software: you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation, version 2.0 of the License.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 *
22 *  Contributing Authors (specific to this file):
23 *  Zane Starr
24 *******************************************************************************/
25
26
27 #include <nrk.h>
28 #include <include.h>
29 #include <ulib.h>
30 #include <stdio.h>
31 #include <avr/sleep.h>
32 #include <hal.h>
33 #include <nrk_error.h>
34 #include <nrk_timer.h>
35 #include <nrk_driver_list.h>
36 #include <nrk_driver.h>
37 #include <ff_basic_sensor.h>
38
39
40 NRK_STK Stack1[NRK_APP_STACKSIZE];
41 nrk_task_type TaskOne;
42 void Task1(void);
43
44
45 void nrk_create_taskset();
46 void nrk_register_drivers();
47 uint8_t kill_stack(uint8_t val);
48
49 int
50 main ()
51 {
52   uint8_t t;
53   nrk_setup_ports();
54   nrk_setup_uart(UART_BAUDRATE_115K2);
55
56   printf( "Starting up...\r\n" );
57
58   nrk_init();
59   nrk_time_set(0,0);
60
61   nrk_register_drivers();
62   nrk_create_taskset ();
63   nrk_start();
64  
65   return 0;
66 }
67
68
69 void Task1()
70 {
71 uint16_t cnt;
72 int8_t i,fd,val;
73 uint16_t buf;
74
75   printf( "My node's address is %d\r\n",NODE_ADDR );
76
77   printf( "Task1 PID=%d\r\n",nrk_get_pid());
78
79  
80   cnt=0;
81   while(1) {
82         // Open ADC device as read
83         fd=nrk_open(FIREFLY_SENSOR_BASIC,READ);
84         if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));
85         nrk_led_toggle(BLUE_LED);
86
87         // Example of setting a sensor
88         val=nrk_set_status(fd,SENSOR_SELECT,BAT);
89         // Read battery first while other sensors warm up
90         val=nrk_read(fd,&buf,2);
91         printf( "Task bat=%d",buf);
92                 val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
93         val=nrk_read(fd,&buf,2);
94         printf( " light=%d",buf);
95         val=nrk_set_status(fd,SENSOR_SELECT,TEMP);
96         val=nrk_read(fd,&buf,2);
97         printf( " temp=%d",buf);
98         val=nrk_set_status(fd,SENSOR_SELECT,ACC_X);
99         val=nrk_read(fd,&buf,2);
100         printf( " acc_x=%d",buf);
101         val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y);
102         val=nrk_read(fd,&buf,2);
103         printf( " acc_y=%d",buf);
104         val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z);
105         val=nrk_read(fd,&buf,2);
106         printf( " acc_z=%d",buf);
107         val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P);
108         nrk_spin_wait_us(60000);
109         val=nrk_read(fd,&buf,2);
110         printf( " audio=%d\r\n",buf);
111         nrk_close(fd);
112         nrk_wait_until_next_period();
113         cnt++;
114         }
115 }
116
117
118 void
119 nrk_create_taskset()
120 {
121   TaskOne.task = Task1;
122   nrk_task_set_stk( &TaskOne, Stack1, NRK_APP_STACKSIZE);
123   TaskOne.prio = 1;
124   TaskOne.FirstActivation = TRUE;
125   TaskOne.Type = BASIC_TASK;
126   TaskOne.SchType = PREEMPTIVE;
127   TaskOne.period.secs = 1;
128   TaskOne.period.nano_secs = 100*NANOS_PER_MS; //*NANOS_PER_MS;
129   TaskOne.cpu_reserve.secs = 1;
130   TaskOne.cpu_reserve.nano_secs =  50*NANOS_PER_MS;
131   TaskOne.offset.secs = 0;
132   TaskOne.offset.nano_secs= 0;
133   nrk_activate_task (&TaskOne);
134
135 }
136
137 void nrk_register_drivers()
138 {
139 int8_t val;
140
141 // Register the Basic FireFly Sensor device driver
142 // Make sure to add:
143 //     #define NRK_MAX_DRIVER_CNT 
144 //     in nrk_cfg.h
145 // Make sure to add:
146 //     SRC += $(ROOT_DIR)/src/drivers/platform/$(PLATFORM_TYPE)/source/ff_basic_sensor.c
147 //     in makefile
148 val=nrk_register_driver( &dev_manager_ff_sensors,FIREFLY_SENSOR_BASIC);
149 if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to load my ADC driver\r\n") );
150
151 }
152
153
Note: See TracBrowser for help on using the browser.