Last active
November 19, 2023 21:55
-
-
Save sandeepmistry/6a6dfe9021b19bfddd60 to your computer and use it in GitHub Desktop.
BLEPeripheral nRF51822 - Low Power (Interrupt) Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import libraries (BLEPeripheral depends on SPI) | |
#include <SPI.h> | |
#include <BLEPeripheral.h> | |
// define pins (varies per shield/board) | |
#define BLE_REQ 10 | |
#define BLE_RDY 2 | |
#define BLE_RST 9 | |
// LED pin | |
#define LED_PIN 3 | |
// create peripheral instance, see pinouts above | |
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST); | |
// create service | |
BLEService ledService = BLEService("19b10000e8f2537e4f6cd104768a1214"); | |
// create switch characteristic | |
BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite); | |
void setup() { | |
Serial.begin(9600); | |
#if defined (__AVR_ATmega32U4__) | |
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board | |
#endif | |
// set LED pin to output mode | |
pinMode(LED_PIN, OUTPUT); | |
// set advertised local name and service UUID | |
blePeripheral.setLocalName("LED"); | |
blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); | |
blePeripheral.setAdvertisingInterval(1000); | |
// add service and characteristic | |
blePeripheral.addAttribute(ledService); | |
blePeripheral.addAttribute(switchCharacteristic); | |
// assign event handlers for connected, disconnected to peripheral | |
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); | |
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); | |
// assign event handlers for characteristic | |
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten); | |
// begin initialization | |
blePeripheral.begin(); | |
Serial.println(F("BLE LED Peripheral")); | |
// enable low power mode and interrupt | |
sd_power_mode_set(NRF_POWER_MODE_LOWPWR); | |
sd_nvic_EnableIRQ(SWI2_IRQn); | |
} | |
void loop() { | |
// wait for event/interrupt (low power mode) | |
Serial.println(F("low power enter")); | |
sd_app_evt_wait(); | |
Serial.println(F("low power exit")); | |
// poll peripheral | |
blePeripheral.poll(); | |
} | |
void blePeripheralConnectHandler(BLECentral& central) { | |
// central connected event handler | |
Serial.print(F("Connected event, central: ")); | |
Serial.println(central.address()); | |
} | |
void blePeripheralDisconnectHandler(BLECentral& central) { | |
// central disconnected event handler | |
Serial.print(F("Disconnected event, central: ")); | |
Serial.println(central.address()); | |
} | |
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) { | |
// central wrote new value to characteristic, update LED | |
Serial.print(F("Characteristic event, writen: ")); | |
if (switchCharacteristic.value()) { | |
Serial.println(F("LED on")); | |
digitalWrite(LED_PIN, HIGH); | |
} else { | |
Serial.println(F("LED off")); | |
digitalWrite(LED_PIN, LOW); | |
} | |
} | |
void SWI2_IRQHandler(void) { | |
// no-op | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment