/* * Copyright (c) 2006 Intel Corporation * All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE * file. If you do not find these files, copies can be found by writing to * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, * 94704. Attention: Intel License Inquiry. */
/** * Oscilloscope demo application. See README.txt file in this directory. * * @author David Gay */ #include "Timer.h" #include "Oscilloscope.h"
/* Current local state - interval, version and accumulated readings */ oscilloscope_t local;
uint8_t reading; /* 0 to NREADINGS */
/* When we head an Oscilloscope message, we check it's sample count. If it's ahead of ours, we "jump" forwards (set our count to the received count). However, we must then suppress our next count increment. This is a very simple form of "time" synchronization (for an abstract notion of time). */ bool suppress_count_change;
// Use LEDs to report various status issues. void report_problem() { call Leds.led0Toggle(); } void report_sent() { call Leds.led1Toggle(); } void report_received() { call Leds.led2Toggle(); }
/* If we receive a newer version, update our interval. If we hear from a future count, jump ahead but suppress our own change */ if (omsg->version > local.version) { local.version = omsg->version; local.interval = omsg->interval; startTimer(); } if (omsg->count > local.count) { local.count = omsg->count; suppress_count_change = TRUE; }
return msg; }
/* At each sample period: - if local sample buffer is full, send accumulated samples - read next sample */ event void Timer.fired() { if (reading == NREADINGS) { if (!sendbusy && sizeof local <= call AMSend.maxPayloadLength()) { memcpy(call AMSend.getPayload(&sendbuf), &local, sizeof local); if (call AMSend.send(AM_BROADCAST_ADDR, &sendbuf, sizeof local) == SUCCESS) sendbusy = TRUE; } if (!sendbusy) report_problem();
reading = 0; /* Part 2 of cheap "time sync": increment our count if we didn't jump ahead. */ if (!suppress_count_change) local.count++; suppress_count_change = FALSE; } if (call Read.read() != SUCCESS) report_problem(); }