[Tinyos-contrib-commits]
CVS: tinyos-1.x/contrib/shockfish/apps/GlowLight
GlowLightC.nc, NONE, 1.1 GlowLightM.nc, NONE, 1.1 LedsC.nc,
NONE, 1.1 Makefile, NONE, 1.1 README.GlowLight, NONE, 1.1
rogmeier
rogmeier at users.sourceforge.net
Wed Sep 20 05:42:59 PDT 2006
Update of /cvsroot/tinyos/tinyos-1.x/contrib/shockfish/apps/GlowLight
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv14024
Added Files:
GlowLightC.nc GlowLightM.nc LedsC.nc Makefile README.GlowLight
Log Message:
TinyNode demonstration, loaded on the TinyNode Demokit
--- NEW FILE: GlowLightC.nc ---
/**
* Configuration for GlowLight
*
* Copyright (C) 2005 Shockfish SA
*
* Authors: Maxime Muller
*
**/
configuration GlowLightC { }
implementation
{
components Main,
GlowLightM,
LedsIntensityC,
TimerC,
LightC,
RandomLFSR,
OscopeC,
GenericComm;
Main.StdControl -> GlowLightM;
Main.StdControl -> LedsIntensityC;
Main.StdControl -> TimerC;
Main.StdControl -> LightC;
Main.StdControl -> OscopeC;
Main.StdControl -> GenericComm;
GlowLightM.LedsI -> LedsIntensityC;
GlowLightM.LightADC -> LightC;
GlowLightM.Random -> RandomLFSR;
GlowLightM.Timer -> TimerC.Timer[unique("Timer")];
GlowLightM.Blink -> TimerC.Timer[unique("Blink")];
GlowLightM.Node0 -> OscopeC.Oscope[0];
GlowLightM.Node1 -> OscopeC.Oscope[1];
GlowLightM.Node2 -> OscopeC.Oscope[2];
GlowLightM.SendMsg -> GenericComm.SendMsg[5];
GlowLightM.ReceiveMsg -> GenericComm.ReceiveMsg[5];
}
--- NEW FILE: GlowLightM.nc ---
/**
* Module for GlowLight
*
* Copyright (C) 2005 Shockfish SA
*
* Authors: Maxime Muller
**/
/**
* Dim the led according to light sensor and msg from
* other TinyNodes.
* Every msg sent or receive is fwd to the UART
* throught OscopeUART lib
**/
module GlowLightM
{
provides interface StdControl;
uses {
interface Timer;
interface Timer as Blink;
interface ADC as LightADC;
interface LedsIntensity as LedsI;
interface Oscope as OLight;
interface Random;
interface SendMsg;
interface ReceiveMsg;
interface Oscope as Node0;
interface Oscope as Node1;
interface Oscope as Node2;
}
}
implementation
{
enum {
LED_HIGH = 254, // maximum led value
TIMER_FREQ = 300, // sampling Freq
#ifdef RND_TIMER
RND_INT = 20,
#endif
DELTA = 150, // difference between MAX/MIN_LIGHT at init
MIN_TRESH = 10, // avoid 0 value as it tends to spike
BLINK_TIME = 5 // set the red LED on for 5ms
};
typedef struct {
uint16_t nodeId;
uint16_t ledIntensity;
uint8_t cnter;
} GlowMsg_t;
norace TOS_Msg m_msg;
norace bool isSending = FALSE;
norace bool init = TRUE; // to set first value of MAX/MIN_LIGHT
uint16_t counter = 0;
norace static int MAX_LIGHT, MIN_LIGHT, CNTER_MOD = 10;
norace uint16_t lightValue;
void blink();
command result_t StdControl.init() {
MAX_LIGHT = 0;
MIN_LIGHT = 0;
return SUCCESS;
}
command result_t StdControl.start() {
return call Timer.start(TIMER_ONE_SHOT, TIMER_FREQ);
}
command result_t StdControl.stop() {
return rcombine(call Timer.stop(), call Blink.stop());
}
task void NoticeError() {
TOSH_SET_RED_LED_PIN();
call Blink.start(TIMER_ONE_SHOT, 2000);
}
/*
** bcast our value to other TinyNodes
*/
task void bcast() {
uint16_t oldVal = lightValue;
float tmpVal = lightValue;
tmpVal = (tmpVal-MIN_LIGHT)/(MAX_LIGHT-MIN_LIGHT);
lightValue =tmpVal* LED_HIGH;
lightValue = lightValue%LED_HIGH+1;
if (lightValue < MIN_TRESH)
lightValue = oldVal;
if (!isSending) {
GlowMsg_t* body = (GlowMsg_t*)m_msg.data;
isSending = TRUE;
counter++;
counter %=CNTER_MOD;
body->nodeId = TOS_LOCAL_ADDRESS;
body->ledIntensity = lightValue;
body->cnter = counter;
call LedsI.set(TOS_LOCAL_ADDRESS,lightValue);
if ( call SendMsg.send(TOS_BCAST_ADDR,
sizeof(GlowMsg_t),
&m_msg) == FAIL)
isSending = FALSE;
if (counter==0 && isSending)
blink();
}
}
async event result_t LightADC.dataReady(uint16_t data) {
lightValue=data;
if (init) {
MAX_LIGHT = data+DELTA;
MIN_LIGHT = data-DELTA;
if (MIN_LIGHT < MIN_TRESH ) MIN_LIGHT = MIN_TRESH;
init = FALSE;
}
else {
if (data > MAX_LIGHT)
MAX_LIGHT = data;
if (MIN_TRESH < data && data < MIN_LIGHT)
MIN_LIGHT = data;
if (lightValue != MAX_LIGHT && lightValue != MIN_LIGHT) {
if(!post bcast())
post NoticeError();
}
}
return SUCCESS;
}
event result_t SendMsg.sendDone( TOS_MsgPtr msg, result_t success )
{
if (isSending)
isSending = FALSE;
if(!success) {
TOSH_SET_RED_LED_PIN();
call Blink.start(TIMER_ONE_SHOT, 2000);
}
switch (TOS_LOCAL_ADDRESS) {
case 0:
call Node0.put(lightValue); break;
case 1:
call Node1.put(lightValue); break;
case 2:
call Node2.put(lightValue); break;
default: break;
}
return success;
}
/*
** set our own led to the received value
** and fwd it to proper oscope channel
*/
event TOS_MsgPtr ReceiveMsg.receive( TOS_MsgPtr msg )
{
GlowMsg_t* body = (GlowMsg_t*)msg->data;
if (body->nodeId != TOS_LOCAL_ADDRESS ) {
call LedsI.set(body->nodeId, body->ledIntensity);
if (body->cnter%CNTER_MOD==0)
blink();
switch (body->nodeId) {
case 0:
call Node0.put(body->ledIntensity); break;
case 1:
call Node1.put(body->ledIntensity); break;
case 2:
call Node2.put(body->ledIntensity); break;
default: break;
}
}
return msg;
}
event result_t Timer.fired()
{
#ifdef RND_TIMER
uint16_t rndTimer;
rndTimer = ((call Random.rand() & 0xfff) + 1)%RND_INT;
call LightADC.getData();
call Timer.start(TIMER_ONE_SHOT, TIMER_FREQ-RND_INT/2+rndTimer);
#else
call LightADC.getData();
call Timer.start(TIMER_ONE_SHOT, TIMER_FREQ);
#endif
return SUCCESS;
}
event result_t Blink.fired() {
TOSH_CLR_RED_LED_PIN();
return SUCCESS;
}
void blink() {
TOSH_SET_RED_LED_PIN();
TOSH_uwait(2000);
TOSH_CLR_RED_LED_PIN();
}
}
--- NEW FILE: LedsC.nc ---
// $Id: LedsC.nc,v 1.1 2006/09/20 12:42:56 rogmeier Exp $
/* tab:4
* "Copyright (c) 2000-2003 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Copyright (c) 2002-2003 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.
*/
/*
*
* Authors: Jason Hill, David Gay, Philip Levis
* Date last modified: 6/2/03
*
*/
/**
* @author Jason Hill
* @author David Gay
* @author Philip Levis
*/
module LedsC {
provides interface Leds;
}
implementation
{
uint8_t ledsOn;
enum {
RED_BIT = 1,
GREEN_BIT = 2,
YELLOW_BIT = 4
};
async command result_t Leds.init() {
atomic {
ledsOn = 0;
dbg(DBG_BOOT, "LEDS: initialized.\n");
TOSH_MAKE_RED_LED_OUTPUT();
TOSH_MAKE_RED_LED2_OUTPUT();
TOSH_MAKE_YELLOW_LED_OUTPUT();
TOSH_MAKE_GREEN_LED_OUTPUT();
TOSH_CLR_RED_LED_PIN();
TOSH_CLR_RED_LED2_PIN();
TOSH_CLR_YELLOW_LED_PIN();
TOSH_CLR_GREEN_LED_PIN();
}
return SUCCESS;
}
async command result_t Leds.redOn() {
dbg(DBG_LED, "LEDS: Red on.\n");
atomic {
TOSH_SET_RED_LED2_PIN();
ledsOn |= RED_BIT;
}
return SUCCESS;
}
async command result_t Leds.redOff() {
dbg(DBG_LED, "LEDS: Red off.\n");
atomic {
TOSH_CLR_RED_LED2_PIN();
ledsOn &= ~RED_BIT;
}
return SUCCESS;
}
async command result_t Leds.redToggle() {
result_t rval;
atomic {
if (ledsOn & RED_BIT)
rval = call Leds.redOff();
else
rval = call Leds.redOn();
}
return rval;
}
async command result_t Leds.greenOn() {
dbg(DBG_LED, "LEDS: Green on.\n");
atomic {
TOSH_SET_GREEN_LED_PIN();
ledsOn |= GREEN_BIT;
}
return SUCCESS;
}
async command result_t Leds.greenOff() {
dbg(DBG_LED, "LEDS: Green off.\n");
atomic {
TOSH_CLR_GREEN_LED_PIN();
ledsOn &= ~GREEN_BIT;
}
return SUCCESS;
}
async command result_t Leds.greenToggle() {
result_t rval;
atomic {
if (ledsOn & GREEN_BIT)
rval = call Leds.greenOff();
else
rval = call Leds.greenOn();
}
return rval;
}
async command result_t Leds.yellowOn() {
dbg(DBG_LED, "LEDS: Yellow on.\n");
atomic {
TOSH_SET_YELLOW_LED_PIN();
ledsOn |= YELLOW_BIT;
}
return SUCCESS;
}
async command result_t Leds.yellowOff() {
dbg(DBG_LED, "LEDS: Yellow off.\n");
atomic {
TOSH_CLR_YELLOW_LED_PIN();
ledsOn &= ~YELLOW_BIT;
}
return SUCCESS;
}
async command result_t Leds.yellowToggle() {
result_t rval;
atomic {
if (ledsOn & YELLOW_BIT)
rval = call Leds.yellowOff();
else
rval = call Leds.yellowOn();
}
return rval;
}
async command uint8_t Leds.get() {
uint8_t rval;
atomic {
rval = ledsOn;
}
return rval;
}
async command result_t Leds.set(uint8_t ledsNum) {
atomic {
ledsOn = (ledsNum & 0x7);
if (ledsOn & GREEN_BIT)
TOSH_SET_GREEN_LED_PIN();
else
TOSH_CLR_GREEN_LED_PIN();
if (ledsOn & YELLOW_BIT )
TOSH_SET_YELLOW_LED_PIN();
else
TOSH_CLR_YELLOW_LED_PIN();
if (ledsOn & RED_BIT)
TOSH_SET_RED_LED2_PIN();
else
TOSH_CLR_RED_LED2_PIN();
}
return SUCCESS;
}
}
--- NEW FILE: Makefile ---
COMPONENT ?= GlowLightC
CFLAGS += -DOSCOPE_MAX_CHANNELS=3
CFLAGS += -DSEND_TO_UART
PFLAGS += -I$./
PFLAGS += -I$(TOSDIR)/../contrib/shockfish/tos/lib/OscopeUART
PFLAGS += -I$(TOSDIR)/lib/Oscope
PFLAGS += -I$(TOSDIR)/../contrib/shockfish/tos/sensorboards/extboard/
CFLAGS += -I%T/lib/LedsIntensity
include $(MAKERULES)
--- NEW FILE: README.GlowLight ---
make tinynode install.TOS_LOCAL_ADDRESS bsl,/dev/ttyS0
with TOS_LOCAL_ADDRESS unique in [0..2].
This will define the network id and the led number.
More information about the Tinyos-contrib-commits
mailing list