sensor-app/senso.c

Date:
2005/10/05
Author:
Jan Beutel <j.beutel@ieee.org>
Example application to show the use of generic sensors. Here we use a teco particles ssmall sensor attached via an usbprog board.

For the moment, the monitoring function to be used as a deployment-support network target is removed. To use it include all monitor lines and set the baud rate to 19200. It requires tp compile nut/OS with -DNUT_PERFMON.

00001 /*
00002  * Copyright (C) 2000-2006 by ETH Zurich
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
00021  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.btnode.ethz.ch
00031  *
00032  * $Id: senso.c,v 1.6 2006/11/09 14:13:40 yuecelm Exp $
00033  * 
00034  */
00035 
00080 #include <string.h>
00081 #include <stdio.h>
00082 #include <stdlib.h>
00083 #include <io.h>
00084 #include <hardware/btn-hardware.h>
00085 #include <terminal/btn-terminal.h>
00086 #include <terminal/btn-cmds.h>
00087 #include <dev/adc.h>
00088 #include <dev/usartavr.h>
00089 #include <led/btn-led.h>
00090 #include <sys/thread.h>
00091 #include <sys/timer.h>
00092 #include <dev/twif.h>
00093 #include "monitor.h"
00094 #include "../../extras/teco_ssmall/micsampler.h"
00095 #include "../../extras/teco_ssmall/tsl2550.h"
00096 #include "program_version.h"
00097 
00098 #include <avr/wdt.h>
00099 
00100 #define UART_SPEED 19200  //was 19200 for use of monitoring function
00101 
00102 extern void _jtag_off(u_char *arg);
00103 
00104 void sensorLoop(void)
00105 {
00106     NutThreadSetPriority(180);
00107     
00108     // init twi
00109     TwInit(20); // set our slave addr = 20
00110     
00111     // start light sensor
00112     if(tsl_init())
00113         DEBUGT("ERROR: no light sensor found\n");
00114     
00115     // start microphone sampling thread
00116     ADCInit();
00117     mic_init();
00118     
00119     for(;;){
00120         u_char i, channel0, channel1;
00121 
00122         for(i = 0; i < 10; i++){
00123             u_short val = mic_read();
00124             mic_set_leds(val);
00125             if( val >= 1015 )
00126                 DEBUGT("ALARM: Microphone = %d\n", val);
00127                 
00128             NutSleep(100);
00129         }
00130   
00131         // NOTE: TLS2550 needs 800ms integration time for both channels
00132         if(tsl_read(&channel0, &channel1)){
00133             DEBUGT("ERROR: could not read light sensor\n");
00134             btn_led_add_pattern(BTN_LED_PATTERN_OFF_ON_OFF,9,1,BTN_LED_INFINITE);
00135             DEBUGT("ERROR: system error\n");
00136             DEBUGT("ERROR: standby for reboot\n");
00137             NutSleep(1015);
00138             wdt_enable(WDTO_500MS);
00139         }else{
00140             u_short val = tsl_calculate_lux(channel0, channel1);
00141             if( val >= 220 )
00142                 DEBUGT("ALARM: Illuminance = %d lx\n", val);
00143         }
00144     }
00145 }
00146 
00147 int main(void)
00148 {
00149     // hardware init
00150     btn_hardware_init();
00151     btn_led_init(1);
00152     // sensor board power
00153     btn_hardware_io_power(1);
00154     // if additional light sensor (connected to PF7) is used jtag hes to be disabled by
00155     // _jtag_off(NULL);
00156     
00157     // init uart and terminal
00158     NutRegisterDevice(&APP_UART, 0, 0);
00159     freopen(APP_UART.dev_name, "r+", stdout);
00160     u_long baud = UART_SPEED;
00161     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00162     btn_terminal_init(stdout, "[senso]$");
00163     
00164     DEBUGB("\n# ----------------------------------------");
00165     DEBUGB("\n# Senso: a Sensor-Demo (c) 2005 ETH Zurich\n");
00166     DEBUGT("# program version: %s\n", PROGRAM_VERSION);
00167     DEBUGB("# ----------------------------------------\n");
00168     NutSleep(200);
00169  
00170     // register cmds
00171     btn_cmds_register_cmds();
00172     mon_init(NULL, (u_char *) PROGRAM_VERSION);
00173     mon_register_cmds();
00174     
00175     // start terminal
00176     btn_terminal_run(BTN_TERMINAL_FORK, 1024);
00177     
00178     // main loop
00179     sensorLoop();
00180     
00181     return 1;
00182 }

Generated on Wed Apr 29 11:12:28 2009 for BTnut System Software by doxygen 1.5.1