DOC

NesC

By Anne Hall,2014-06-24 05:49
8 views 0
NesC

1. Lesson_1MyApp.nc

    /*

     * Copyright (c) 2004-2007 Crossbow Technology, Inc.

     * All rights reserved.

     * See license.txt file included with the distribution.

     *

     * $Id: MyApp.nc,v 1.1.2.2 2007/04/26 19:59:01 njain Exp $

     */

/**

     * This configuration shows how to use the Timer and LED components

     *

     **/

    configuration MyApp {

    }

    implementation {

     components Main, MyAppM, TimerC, LedsC;

     Main.StdControl -> TimerC.StdControl;

     Main.StdControl -> MyAppM.StdControl;

     MyAppM.Timer -> TimerC.Timer[unique("Timer")];

     MyAppM.Leds -> LedsC.Leds;

    }

2. Lesson_1MyAppM.nc

    /*

     * Copyright (c) 2004-2007 Crossbow Technology, Inc.

     * All rights reserved.

     * See license.txt file included with the distribution.

     *

     * $Id: MyAppM.nc,v 1.1.2.2 2007/04/26 19:59:09 njain Exp $

     */

/**

     * This module shows how to use the Timer and LED components

     **/

    module MyAppM {

     provides {

     interface StdControl;

     }

     uses {

     interface Timer;

     interface Leds;

     }

    }

    implementation {

     /**

     * Initialize the components.

     *

     * @return Always returns SUCCESS

     **/

     command result_t StdControl.init() {

     call Leds.init();

     return SUCCESS;

     }

     /**

     * Start things up. This just sets the rate for the clock component.

     *

     * @return Always returns SUCCESS

     **/

     command result_t StdControl.start() {

     // Start a repeating timer that fires every 1000ms

     return call Timer.start(TIMER_REPEAT, 1000);

     }

     /**

     * Halt execution of the application.

     * This just disables the clock component.

     *

     * @return Always returns SUCCESS

     **/

     command result_t StdControl.stop() {

     return call Timer.stop();

     }

     /**

     * Toggle the red LED in response to the Timer.fired event.

     *

     * @return Always returns SUCCESS

     **/

     event result_t Timer.fired()

     {

     call Leds.redToggle();

     return SUCCESS;

     }

    }

3. Lesson_2MyApp.nc

    /*

     * Copyright (c) 2004-2007 Crossbow Technology, Inc.

     * All rights reserved.

     * See license.txt file included with the distribution.

     *

     * $Id: MyApp.nc,v 1.1.2.2 2007/04/26 19:59:29 njain Exp $

     */

includes sensorboardApp;

/**

     * This module shows how to use the Timer, LED, ADC and Messaging components.

     * Sensor messages are sent to the serial port

     **/

    configuration MyApp {

    }

    implementation {

     components Main, MyAppM, TimerC, LedsC, Photo, GenericComm as Comm;

     Main.StdControl -> TimerC.StdControl;

     Main.StdControl -> MyAppM.StdControl;

     Main.StdControl -> Comm.Control;

     MyAppM.Timer -> TimerC.Timer[unique("Timer")];

     MyAppM.Leds -> LedsC.Leds;

     MyAppM.PhotoControl -> Photo.PhotoStdControl;

     MyAppM.Light -> Photo.ExternalPhotoADC;

     MyAppM.SendMsg -> Comm.SendMsg[AM_XSXMSG]; }

4. Lesson_2MyAppM.nc

    /*

     * Copyright (c) 2004-2007 Crossbow Technology, Inc.

     * All rights reserved.

     * See license.txt file included with the distribution.

     *

     * $Id: MyAppM.nc,v 1.1.2.4 2007/04/26 19:59:38 njain Exp $

     */

includes sensorboardApp;

/**

     * This module shows how to use the Timer, LED, ADC and Messaging components

     * Sensor messages are sent to the serial port

     *

     **/

    module MyAppM {

     provides {

     interface StdControl;

     }

     uses {

     interface Timer;

     interface Leds;

     interface StdControl as PhotoControl;

     interface ADC as Light;

     interface SendMsg;

     }

    }

    implementation {

     bool sending_packet = FALSE;

     TOS_Msg msg_buffer;

     XDataMsg *pack;

     /**

     * Initialize the component.

     *

     * @return Always returns SUCCESS

     **/

     command result_t StdControl.init() {

     call Leds.init();

     call PhotoControl.init();

     // Initialize the message packet with default values

     atomic {

     pack = (XDataMsg *)&(msg_buffer.data);

     pack->xSensorHeader.board_id = SENSOR_BOARD_ID;

     pack->xSensorHeader.packet_id = 2;

     pack->xSensorHeader.node_id = TOS_LOCAL_ADDRESS;

     pack->xSensorHeader.rsvd = 0;

     }

     return SUCCESS;

     }

     /**

     * Start things up. This just sets the rate for the clock component.

     *

     * @return Always returns SUCCESS

     **/

     command result_t StdControl.start() {

     // Start a repeating timer that fires every 1000ms

     return call Timer.start(TIMER_REPEAT, 1000);

     }

     /**

     * Halt execution of the application.

     * This just disables the clock component.

     *

     * @return Always returns SUCCESS

     **/

     command result_t StdControl.stop() {

     return call Timer.stop();

     }

     /**

     * Toggle the red LED in response to the Timer.fired event.

     * Start the Light sensor control and sample the data

     *

     * @return Always returns SUCCESS

     **/

     event result_t Timer.fired()

     {

     call Leds.redToggle();

     call PhotoControl.start();

     call Light.getData();

     return SUCCESS;

     }

     /**

     * Stop the Light sensor control, build the message packet and send

     **/

     void task SendData()

     {

     call PhotoControl.stop();

     if (sending_packet) return;

     atomic sending_packet = TRUE;

     // send message to UART (serial) port

     if (call SendMsg.send(TOS_UART_ADDR,sizeof(XDataMsg),&msg_buffer) !=

    SUCCESS)

     sending_packet = FALSE;

     return;

     }

     /**

     * Light ADC data ready

     * Toggle yellow LED to signal Light sensor data sampled

     *

     * @return Always returns SUCCESS

     **/

     async event result_t Light.dataReady(uint16_t data) {

     atomic pack->xData.datap1.light = data;

     atomic pack->xData.datap1.vref = 417; // a dummy 3V reference voltage,

    1252352/3000 = 417

     post SendData();

     call Leds.yellowToggle();

     return SUCCESS;

     }

     /**

     * Sensor data has been sucessfully sent over the UART (serial port)

     * Toggle green LED to signal message sent

     *

     * @return Always returns SUCCESS

     **/

     event result_t SendMsg.sendDone(TOS_MsgPtr msg, result_t success) {

     call Leds.greenToggle();

     atomic sending_packet = FALSE;

     return SUCCESS;

     }

}

5. Lesson_2Photo.nc

    /*

     * Copyright (c) 2002-2005 Intel Corporation

     * Copyright (c) 2000-2005 The Regents of the University of California

     * Copyright (c) 2003 by Sensicast, Inc.

     * All rights reserved.

     * See license.txt file included with the distribution.

     *

     * $Id: Photo.nc,v 1.1.2.2 2007/04/26 19:59:46 njain Exp $

     */

/*

     *

     * Authors: Jason Hill, David Gay, Philip Levis

     * Date last modified: 6/25/02

     *

     * Modifications:

     * 20Oct03 MJNewman: Add timer used for delays when sensor is switched.

     *

     */

/**

     * @author Jason Hill

     * @author David Gay

     * @author Philip Levis

     * @author Michael Newman

     */

//includes sensorboard;

    configuration Photo

    {

     provides interface ADC as ExternalPhotoADC;

     provides interface StdControl as PhotoStdControl; }

    implementation

    {

     components PhotoM, ADCC, TimerC;

     PhotoStdControl = PhotoM.PhotoStdControl;

     ExternalPhotoADC = PhotoM.ExternalPhotoADC;

     PhotoM.InternalPhotoADC -> ADCC.ADC[TOS_ADC_PHOTO_PORT];

     PhotoM.ADCControl -> ADCC;

     PhotoM.TimerControl -> TimerC;

     PhotoM.PhotoTimer -> TimerC.Timer[unique("Timer")]; }

    6. Lesson_2PhotoM.nc

    /*

     * Copyright (c) 2002-2005 Intel Corporation

     * Copyright (c) 2000-2005 The Regents of the University of California

     * Copyright (c) 2003 by Sensicast, Inc.

     * All rights reserved.

     * See license.txt file included with the distribution.

     *

     * $Id: PhotoM.nc,v 1.1.2.2 2007/04/26 19:59:54 njain Exp $

     */

#define PhotoMedit 3

    /*

     * Modification History:

     * 11Nov03 MJNewman 3: TinyOS 1.1 atomic updates.

     * 20Oct03 MJNewman 2: Redo copyright and initial comments.

     * 7May03 MJNewman 1: Add proper delays when sensor is switched.

     * It is important to wait about 10ms from

     * starting a sensor to reading data from the

     * sensor.

     * 7May03 MJNewman 1: Created.

     *

     * This module is a rewrite of the PhotoM.nc module written by

     * Jason Hill, David Gay and Philip Levis. This module solves

     * fundamental sampling problems in their module having to do with

     * waiting for logic to settle when changing between the photo and

     * temperature sensors.

     *

     * ISSUE: Continuous data in ADCC is supposed to sample at some rate

     * controlled by ADCControl.setSamplingRate. The original Photo

     * example does not appear to do this. No support has been added. The

     * implementation of getContinuousData in the current code will sample

     * as fast as it can. When both Photo and Temp samples are requested

     * they will alternate producing one sample every 10 ms.

     */

    // OS component abstraction of the analog photo sensor and temperature // sensor with associated A/D support. This code provides an

    // asynchronous interface to the photo and temperature sensors. One // TimerC timer is used, certain forms of clock use are not compatible // with using a timer. (i.e. the ClockC component)

    //

    // It is important to note that the temperature and photo sensors share // hardware and can not be used at the same time. Proper delays are // implemented here. Using ExternalXxxADC.getData will initiate the // appropriate delays prior to sampling data. getData for temperature // and light may both be invoked in any order and at any time. A // correct sample will be signalled by the corresponding dataReady. //

    // Photo and Temp provide the same interfaces. Exposed interfaces are // ExternalPhotoADC and ExternalPhotoADC as well as PhotoStdControl and // PhotoStdControl. The following routines are the public interface: //

    // xxxStdControl.init initializes the device

    // xxxADC.start starts a particular sensor

    // xxxADC.stop stops a particular sensor, this will also stop // any getContinuousData on that sensor.

    // xxxADC.getData reads data from a sensor. This may be called in // any order. A dataReady event is signalled

    // when the data is ready. Temperature and Photo

    // will wait for each other as needed.

    // xxxADC.getContinuousData reads data from a sensor and when the // read completes triggers an additional getData.

    // Continuous data from both sensors will work but

    // a 10 ms delay will occur between each sample.

    // Continuous data from a single sensor will run

    // at a higher sampling rate.

    //

    // A timer from TimerC is used to manage the delays required between // setting up the hardware and reading the data.

//includes sensorboard;

module PhotoM

    {

     provides interface StdControl as PhotoStdControl;

     provides interface ADC as ExternalPhotoADC;

     uses

     {

     interface ADCControl;

     interface ADC as InternalPhotoADC;

     interface StdControl as TimerControl;

     interface Timer as PhotoTimer;

     }

    }

implementation

    {

     // Logs what the hardware is set up to do.

     enum {

     sensorIdle = 0,

     sensorPhotoStarting,

     sensorPhotoReady,

     sensorTempStarting,

     sensorTempReady,

     } hardwareStatus;

     // Logs what a particular sensor is trying to do. When a single

     // read completes the value reverts to idle.

     typedef enum {

     stateIdle = 0,

     stateReadOnce,

     stateContinuous,

     } SensorState_t;

     SensorState_t photoSensor;

     SensorState_t tempSensor;

     // TRUE when waiting for a sample to be read and another sample can

     // not start. getSample will always be triggered again when this is

     // true.

     bool waitingForSample;

     command result_t PhotoStdControl.init()

     {

     call ADCControl.bindPort(TOS_ADC_PHOTO_PORT, TOSH_ACTUAL_PHOTO_PORT);

     call TimerControl.init();

     atomic photoSensor = stateIdle;

     dbg(DBG_BOOT, "TEMP initialized.\n");

     return call ADCControl.init();

     }

     command result_t PhotoStdControl.start()

     {

Report this document

For any questions or suggestions please email
cust-service@docsford.com