Philip Levis wrote:
> The TinyOS programming manual covers this topic (async).

thanks :) I read the programming guide, but I'm still lost.

I managed to create a component (see attached), that basically uses the
decorator pattern to query an underlying Read component at specified
intervals, and at a specified number of times. It will return an
aggregate (the evarage of the readings).

I'm calling this with parameters:

interval:  16  (2kHz)
noSamples: 20


the unerlying component is an AdcReadClient on INPUT_CHANNEL_A2 (msp430).

the readings I get sort of indicate that not all things are happening at
2kHz though. so I tried to replace the underlying sensor with a ReadNow
sensor (AdcReadNowClient). for that, I have to change the uses lines to
ReadNow instead of Read, and make the Sensor.readDone() event async.

but then I get s range of compilation issues - like I can't seem to
signal Read.readDone() from the async ReadNow.readDone() event, for example.

I did read the documentation, maybe I'm just not clever enough - but how
do I make this work?


Akos

#include <Timer.h>

/**
 *  A configuration that reads from a sensor at specified sampling
 *  intervals, and adds the readings up, to return a single value
 *  as the sum of a series of readings
 *
 *  @param interval the interval in 32kHz ticks
 *  @param noSamples the number of samples to sum up as a single reading
 *  @param Sensor the underlying sensor.
 */
generic configuration HighFrequencySensorC(uint32_t interval,
                                           uint16_t noSamples) {
    provides interface Read<uint16_t>;
    uses interface Read<uint16_t> as Sensor;
}

implementation {
    components new HighFrequencySensorP(interval, noSamples)
                                                        as HighFrequencySensor;
    components new Alarm32khz32C() as Alarm;
    components new AlarmToTimerC(T32khz) as Timer;
    components LedsC as Leds;

    Timer.Alarm -> Alarm;

    HighFrequencySensor.Sensor    = Sensor;
    HighFrequencySensor.Timer    -> Timer;
    HighFrequencySensor.Leds     -> Leds;

    Read = HighFrequencySensor;
}
#include <Timer.h>

generic module HighFrequencySensorP(uint32_t interval,
                                    uint16_t noSamples) {
    provides interface Read<uint16_t>;
    uses interface Read<uint16_t> as Sensor;
    uses interface Timer<T32khz> as Timer;
    uses interface Leds;
}

implementation {
    /** the sum of the values read so far from the underlying sensor. */
    uint16_t dataSum;

    /** the number of samples already read. */
    uint16_t samplesRead;

    /**
     *  Initiate a read operation.
     *  Initiate a series of readings, at the specified interval,
     *  a total of noSamples times. add up all the readings as a single
     *  value, and return that in the the Read.readDone() signal.
     *
     *  @return SUCCESS if a readDone() event will eventually come back.
     */
    command error_t Read.read() {
        /* TODO: check if a read is already going on.. */

        samplesRead = 0;
        dataSum     = 0;
        call Timer.startPeriodic(interval);

        return SUCCESS;
    }

    /**
     *  Handle the event when the timer fired.
     *  Initiate a read from the sensor if there are not enough samples
     *  so far. Stop the timer if there are enough samples.
     */
    event void Timer.fired() {
        ++samplesRead;

        if (samplesRead < noSamples) {
            call Sensor.read();
        } else {
            call Timer.stop();
        }
    }

    /**
     *  Event handler for real data reading being ready.
     *
     *  @param result the result of the operation.
     *  @param value the value of the reading.
     */
    event void Sensor.readDone(error_t result, uint16_t value) {
        /** TODO: handle the result parameter */

        if (samplesRead < noSamples) {
            call Leds.led2On();
            dataSum += value;
        } else {
            // if we've read enough data, just return the sum of all of that
            call Leds.led2Off();
            signal Read.readDone(SUCCESS, dataSum / samplesRead);
            samplesRead = 0;
            dataSum     = 0;
        }
    }

}
_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to