Hi,
Today I tried an experiment to use TelosB user button to do more than one function. e.g. user button can used to activate a spacific procedre or set a value to a variable (almost one function), while we can use it to do more than one function just by clicking on it.
Here an example for three moods each of them connected to specific function. First of all user button used to determine the mood on long click (3 seconds switch to mood Red, 6 seconds switch to mood Yellow, and 9 seconds switch to mood Blue) after mood set user button now will used to do the chosen mood function.
In this example:
I used user button to increase variables each connected to mood.
redVar increases if Red mood selected
yellowVar increases if Yellow mood selected
blueVar increases if Blue mood selected
Here is the code (looks like long but very simple indeed):
// TestUserButtonAppC.nc file
configuration TestUserButtonAppC {}
implementation {
components TestUserButtonC;
components MainC;
TestUserButtonC.Boot -> MainC;
components UserButtonC;
TestUserButtonC.Get -> UserButtonC;
TestUserButtonC.Notify -> UserButtonC;
components LedsC;
TestUserButtonC.Leds -> LedsC;
components new TimerMilliC() as MoodTimerC;
TestUserButtonC.MoodTimer -> MoodTimerC;
components new TimerMilliC() as MoodTimerFuncC;
TestUserButtonC.MoodTimerFunc -> MoodTimerFuncC;
}
// TestUserButtonC.nc file
#include <Timer.h>
#include <UserButton.h>
module TestUserButtonC {
uses {
interface Boot;
interface Get<button_state_t>;
interface Notify<button_state_t>;
interface Leds;
interface Timer<TMilli> as MoodTimer;
interface Timer<TMilli> as MoodTimerFunc;
}
}
implementation {
uint8_t mood;
uint8_t isMoodFuncTimerStart;
uint8_t redVar;
uint8_t yellowVar;
uint8_t blueVar;
event void Boot.booted() {
call Notify.enable();
mood=0;
isMoodFuncTimerStart=0;
redVar=0;
yellowVar=0;
blueVar=0;
}
event void Notify.notify( button_state_t state ) {
if ( state == BUTTON_PRESSED && mood==0) {
call MoodTimer.startPeriodic( 3000 );
} else if ( state == BUTTON_PRESSED && mood!=0 ) {
call MoodTimerFunc.startPeriodic(1000);
isMoodFuncTimerStart = 1;
} else if ( state == BUTTON_RELEASED && isMoodFuncTimerStart==0) {
call MoodTimer.stop();
} else if ( state == BUTTON_RELEASED && isMoodFuncTimerStart==1) {
call MoodTimerFunc.stop();
}
}
event void MoodTimer.fired() {
if(mood==0)
{
mood=1;
call Leds.led0On();
call Leds.led1Off();
call Leds.led2Off();
}
else
if(mood==1)
{
mood=2;
call Leds.led1On();
call Leds.led0Off();
call Leds.led2Off();
}
else
if(mood==2)
{
mood=3;
call Leds.led2On();
call Leds.led1Off();
call Leds.led0Off();
}
else
{
mood=0;
call Leds.led0Off();
call Leds.led1Off();
call Leds.led2Off();
}
}
event void MoodTimerFunc.fired() {
if(mood==1) // RED mood, toggle yellow
{
// write RED procedure here e.g.
redVar= redVar+1;
call Leds.led1Toggle();
}
else
if(mood==2) // Yellow mood, toggle blue
{
// write Yellow procedure here
yellowVar= yellowVar+1;
call Leds.led2Toggle();
}
else
if(mood==3) // Blue mood, toggle red
{
// write Blue procedure here
blueVar= blueVar+1;
call Leds.led0Toggle();
}
}
}
// end.