Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Button by
main.cpp
- Committer:
- hux
- Date:
- 2017-01-07
- Revision:
- 11:80f2c19ecbce
- Parent:
- 10:7943b5c1117a
- Child:
- 12:0d0ca44397dd
File content as of revision 11:80f2c19ecbce:
// T08N_Smart_Button - Smart Button Node with Button Service
#include "bricks/bricks.h"
#include "LightSwitch.h" // light switch service
//==============================================================================
// Button Handling
//==============================================================================
enum
{
OFF = 0,
ON,
NONE
};
InterruptIn button(BUTTON1);
static Digital buttonState = NONE;
static Bool switchState = 0;
void cbButtonPress() // executes in interupt mode - keep short!
{
buttonState = ON;
}
void cbButtonRelease()
{
buttonState = OFF;
}
void enrollButton(Blob &o) // enroll button functionality
{
button.fall(cbButtonPress); // setup button press callback
button.rise(cbButtonRelease); // setup button release callback
}
//==============================================================================
// Light Switch Service Setup
//==============================================================================
static LightSwitch *lightSwitchPtr;
void enrollService(Blob &o)
{
lightSwitchPtr = new LightSwitch(o,false); // auto-enrolls service
}
//==============================================================================
// BLE Callbacks
//==============================================================================
void onError(Blob &o) // Error Reporting Callback
{
(void) o; // Avoid compiler warnings
blink(FAULT); // indicate advertising
}
void onConnect(Blob &blue) // Connection Callback
{
blink("x x x ",CONNECTED); // indicate advertising
}
void onDisconnect(Blob &o) // Disconnection Callback
{
o.start(); // start advertising on client disconnect
blink(ADVERTISE); // indicate advertising
}
void onSetup(Blob &o)
{
o.onConnect(onConnect); // setup connection callback
o.onDisconnect(onDisconnect); // setup disconnection callback
enrollButton(o); // enroll button functionality
enrollService(o); // enroll service functionality
name(o,"T09N#1.0 Light Switch"); // setup advertising name
device(o,"Light Switch"); // setup device name
data(o,"ABCDEF"); // setup advertising data
peripheral(o,"C:ng",100); // start advertising @ 100 msec interval
blink(ADVERTISE); // show that board is advertising
//buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
}
main(void)
{
Digital currentState, debounceState;
Digital shadowState = NONE;
verbose(); // print all verbose messages
blink(IDLE); // idle blinking - just started!
Blob o; // declare a blob (BLE OBject)
o.init(onSetup,onError); // init BLE base layer, always do first
while (true) // Infinite loop waiting for BLE events
{
if (o.hasInitialized())
{
currentState = buttonState;
wait(0.01);
debounceState = buttonState;
if ((currentState==debounceState) && (currentState != shadowState))
{
if (currentState == ON)
{
switchState = !switchState;
lightSwitchPtr->setSwitch(o,switchState);
lightSwitchPtr->setState(o,switchState);
if (switchState == ON)
blink("xxxx xxxx ");
else
blink("x x ");
}
shadowState = currentState;
}
}
o.sleep(); // low power waiting for BLE events
}
}
