#include #include #include #include #include #include #include #include #include "setup.h" #include "remote.h" /* MPlaty's BLE Light Poles. Make sure to change things in the setup.h file! (DO NOT MODIFY ANYTHING IN THIS FILE) */ // The BLE Server BLEServer* Server = NULL; // The BLE Server Characteristics BLECharacteristic* cPower = NULL; BLECharacteristic* cColour = NULL; BLECharacteristic* cMode = NULL; /* Classes */ // sets up the status led OWIRE oStatus; // sets up the ir remote handler Remote irRemote(R20KEYSPLIT); // Sets up the IRRecv class IRrecv irRecv(IR_PIN); // Not sure if this is required, further tests needed. bool deviceConnected = false; bool oldDeviceConnected = false; // Storage for the clients, unsure if storing them is necessary, but could be for individually controlling them. BLE2902* clientConfigDescriptors[4]; // the following 6 variables are all for the button. will try and shrink these, and label them bool buttonPressed = false; int buttonState = LOW; int lastButtonState = LOW; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; // the current brightness int brightness = DEFAULT_BRIGHTNESS; class MyServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer* Server) { deviceConnected = true; BLEDevice::startAdvertising(); } void onDisconnect(BLEServer* Server) { deviceConnected = false; } }; void load_ble_server() { // Add a service to the server BLEService *sMain = Server->createService(S_MAIN_UUID); // Add the Power characteristic to the server cPower = sMain->createCharacteristic( C_POWER_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); // Add the Colour characteristic to the server cColour = sMain->createCharacteristic( C_COLOUR_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); // Add the Mode characteristic to the server cMode = sMain->createCharacteristic( C_MODE_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); cPower->addDescriptor(new BLE2902()); cColour->addDescriptor(new BLE2902()); cMode->addDescriptor(new BLE2902()); sMain->start(); } void setup() { // Begin the Serial Communication Serial.begin(SERIAL_SPEED); // Setup the Status LED oStatus.begin(STATUS_LED_PIN, false); // Set the Colour of the status LED oStatus.setModeAndColor(OW_SOLID, OW_RED); // Set the power buttons' pinMode. pinMode(POWER_BUTTON_PIN, INPUT_PULLUP); // begin the IR reciever irRecv.enableIRIn(); Serial.println("Booting up BLE Server..."); // Create the BLE Device and name it BLEDevice::init("Light Pole Server"); // Allow for multiple connections BLEDevice::setMTU(500); // Create and set the BLE Server variable Server = BLEDevice::createServer(); // Set the BLE Server callback Server->setCallbacks(new MyServerCallbacks()); // Creates, and attaches the Service and its characteristics to the Server. //load_ble_server(); // Add a service to the server BLEService *sMain = Server->createService(S_MAIN_UUID); // Add the Power characteristic to the server cPower = sMain->createCharacteristic( C_POWER_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); // Add the Colour characteristic to the server cColour = sMain->createCharacteristic( C_COLOUR_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); // Add the Mode characteristic to the server cMode = sMain->createCharacteristic( C_MODE_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); cPower->addDescriptor(new BLE2902()); cColour->addDescriptor(new BLE2902()); cMode->addDescriptor(new BLE2902()); sMain->start(); // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(S_MAIN_UUID); pAdvertising->setScanResponse(false); pAdvertising->setMinPreferred(0x0); delay(1000); BLEDevice::startAdvertising(); } decode_results results; void ir_loop() { // checks if there is IR Data if (irRecv.decode(&results)) { Serial.println("IR RECIEVED"); serialPrintUint64(results.value, HEX); serialPrintUint64(results.value, HEX); Serial.println(""); // sets the raw decoded data to the data variable. buttonData button = irRemote.call(results.value); if (button.type != R_EMPTY && button.button != RB_ERROR) { if (button.type == R_POWER) { if (button.button == RB_ON) { Serial.println("Power ON"); uint8_t data = (uint8_t)brightness; cPower->setValue(&data, 1); cPower->notify(); } else if (button.button == RB_OFF) { Serial.println("Power OFF"); uint8_t data = 0x00; cPower->setValue(&data, 1); cPower->notify(); } } } irRecv.resume(); } } void power_button_loop() { int reading = digitalRead(POWER_BUTTON_PIN); if (reading != lastButtonState) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { Serial.println("Button pressed"); if (deviceConnected) { uint8_t data = 0xFF; //pCharacteristic->setValue(&data, 1); // Set the value as a byte array //pCharacteristic->notify(); Serial.println("Data updated!"); } buttonPressed = true; } else { Serial.println("Button released"); if (deviceConnected) { uint8_t data = 0x00; //pCharacteristic->setValue(&data, 1); // Set the value as a byte array //pCharacteristic->notify(); Serial.println("Data updated!"); } buttonPressed = false; } } } lastButtonState = reading; } void loop() { ir_loop(); if (!deviceConnected && oldDeviceConnected) { Server->startAdvertising(); Serial.println("Start advertising"); oldDeviceConnected = deviceConnected; } if (deviceConnected && !oldDeviceConnected) { // Actions to perform on connecting oldDeviceConnected = deviceConnected; } }