Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of MCU sleep #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Adafruit_BLE_UART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Adafruit_BLE_UART::Adafruit_BLE_UART(int8_t req, int8_t rdy, int8_t rst)

rx_event = NULL;
aci_event = NULL;
sleep_cb = NULL;

memset(device_name, 0x00, 8);

Expand All @@ -175,6 +176,10 @@ void Adafruit_BLE_UART::setRXcallback(rx_callback rxEvent) {
rx_event = rxEvent;
}

void Adafruit_BLE_UART::setSLEEPcallback(sleep_callback scb){
sleep_cb = scb;
}

/**************************************************************************/
/*!
Transmits data out via the TX characteristic (when available)
Expand Down Expand Up @@ -458,6 +463,7 @@ void Adafruit_BLE_UART::pollACI()
// No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep
// Arduino can go to sleep now
// Wakeup from sleep from the RDYN line
if (sleep_cb) sleep_cb();
}
}

Expand Down
7 changes: 6 additions & 1 deletion Adafruit_BLE_UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ extern "C"
/* Callback prototypes */
typedef void (*aci_callback)(aci_evt_opcode_t event);
typedef void (*rx_callback) (uint8_t *buffer, uint8_t len);
typedef void (*sleep_callback)(void);

}

class Adafruit_BLE_UART : public Stream
Expand All @@ -55,6 +57,8 @@ class Adafruit_BLE_UART : public Stream

void setACIcallback(aci_callback aciEvent = NULL);
void setRXcallback(rx_callback rxEvent = NULL);
void setSLEEPcallback(sleep_callback sleepCb = NULL);

void setDeviceName(const char * deviceName);

// Stream compatibility
Expand All @@ -71,7 +75,8 @@ class Adafruit_BLE_UART : public Stream

// callbacks you can set with setCallback function for user extension
aci_callback aci_event;
rx_callback rx_event;
rx_callback rx_event;
sleep_callback sleep_cb;

bool debugMode;
uint16_t adv_timeout;
Expand Down
102 changes: 102 additions & 0 deletions examples/powersave/powersave.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*********************************************************************
This is an example for our nRF8001 Bluetooth Low Energy Breakout with MCU being sleeping most of the time.

Written by Amitesh Singh/ami <singh.amitesh@gmail.com>

MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in any redistribution
*********************************************************************/
#include <avr/power.h>
#include <avr/sleep.h>

#include <SPI.h>
#include "Adafruit_BLE_UART.h"

// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9

Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

volatile void powerDownSleep()
{
Serial.println("Arduino: Sleep");
Serial.flush();
delay(5);

set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();

sleep_disable();

Serial.println("Arduino: Woke up");
}


/**************************************************************************/
/*!
Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
while(!Serial); // Leonardo/Micro should wait for serial init
Serial.println(F("nRF8001 powersave"));

BTLEserial.setDeviceName("BLE"); /* 7 characters max! */
BTLEserial.setSLEEPcallback(powerDownSleep);
// 1s interval, 9uA current usage
if (!BTLEserial.begin(0, 1600))
{
Serial.println(F("Couldn't find nRF8001, check wiring?"));
}
}

/**************************************************************************/
/*!
Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;

void loop()
{
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();

// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}

if (status == ACI_EVT_CONNECTED) {
// Lets see if there's any data for us!
if (BTLEserial.available()) {
Serial.print("* "); Serial.print(BTLEserial.available()); Serial.println(F(" bytes available from BTLE"));
}
// OK while we still have something to read, get a character and print it out
while (BTLEserial.available()) {
char c = BTLEserial.read();
Serial.print(c);
}
}
Serial.flush();
delay(10);
}