/* Copyright (c) 2014, Nordic Semiconductor ASA * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include #include #include #include "uart_over_ble.h" #include #include #include #include /** Put the nRF8001 setup in the RAM of the nRF8001. */ #include "services.h" /** Include the services_lock.h to put the setup in the OTP memory of the nRF8001. This would mean that the setup cannot be changed once put in. However this removes the need to do the setup of the nRF8001 on every reset. */ //the following #define will instruct the library to use the interanl //real-time counter of the microcontroller #define USE_INTERNAL_RTC // State machine #define OFFSTATE 0 #define ONLOW 1 #define ONMID 2 #define ONHIGH 3 #define OFFTIME 3000 // single character message tags #define TIME_HEADER 'T' // Header tag for serial time sync message #define TIME_NOW 'N' // Header tag for getting time now #define RTC_PROBE 'P' // Header tag for getting RTC info #define GET_DATA 'G' // Header tag for getting data #define FAN_SPEED 'F' // Header tag for getting Fan speed #define GO_SLEEP 'S' // Header tag for putting device to sleep const unsigned long DEFAULT_TIME = 1451606400; // Jan 1 2016 00:00:00 GMT #ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT static services_pipe_type_mapping_t services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT; #else #define NUMBER_OF_PIPES 0 static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL; #endif /* Store the setup for the nRF8001 in the flash of the AVR to save on RAM */ static const hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] PROGMEM = SETUP_MESSAGES_CONTENT; static struct aci_state_t aci_state; /* Temporary buffer for sending ACI commands */ static hal_aci_evt_t aci_data; /* Timing change state variable */ static bool timing_change_done = false; /* Used to test the UART TX characteristic notification */ static uart_over_ble_t uart_over_ble; static uint8_t uart_buffer[20]; static uint8_t uart_buffer_len = 0; struct RTCx::tm tm; String inputString = ""; // a string to hold incoming data bool stringComplete = false; // whether the string is complete int ArrayIndex = 0; unsigned long TimeArray[100]; //Array to store On/Off timestamp unsigned char OnOffArray[100]; int wakePin = 3; //pin used for waking up int State = OFFSTATE; int PrevState = ONLOW; long debounceDelay = 70; // the debounce time; increase if the output flickers int current; // Current state of the button long millis_held; // How long the button was held (milliseconds) long secs_held; // How long the button was held (seconds) long prev_secs_held; // How long the button was held in the previous check byte previous = HIGH; unsigned long firstTime; // how long since the button was first pressed int GetDataFlag = 0; int GetData = 0; /* Define how assert should function in the BLE library */ void __ble_assert(const char *file, uint16_t line) { while(1); } /* Description: In this template we are using the BTLE as a UART and can send and receive packets. The maximum size of a packet is 20 bytes. When a command it received a response(s) are transmitted back. Since the response is done using a Notification the peer must have opened it(subscribed to it) before any packet is transmitted. The pipe for the UART_TX becomes available once the peer opens it. See section 20.4.1 -> Opening a Transmit pipe In the master control panel, clicking Enable Services will open all the pipes on the nRF8001. The ACI Evt Data Credit provides the radio level ack of a transmitted packet. */ void setup(void) { for(int i=0; i < 100; i++){ TimeArray[i]=0; OnOffArray[i]=0; } pinMode(wakePin, INPUT); //Pin3 is Switch input. pinMode(A1, INPUT); //A1 is battery charger status pinMode(A2, INPUT); //A2 is USB connect detect pinMode(A0, OUTPUT); //A0 is RED LED pinMode(8, OUTPUT); //IO8 is GREEN LED pinMode(9, OUTPUT); //IO9 is BLUE LED pinMode(5, OUTPUT); //IO5 is PWM to fan pinMode(6, OUTPUT); //IO6 controls UV LED digitalWrite(A0, HIGH); // LEDs OFF digitalWrite(6, LOW); // UV LED OFF digitalWrite(8, HIGH); digitalWrite(9, HIGH); Wire.begin(); // The address used by the DS1307 is also used by other devices (eg // MCP3424 ADC). Test for a MCP7941x device first. uint8_t addressList[] = {RTCx::MCP7941xAddress, RTCx::DS1307Address}; // Autoprobe to find a real-time clock. rtc.autoprobe(addressList, sizeof(addressList)); // Enable the battery backup. This happens by default on the DS1307 // but needs to be enabled on the MCP7941x. //rtc.enableBatteryBackup(); // rtc.clearVBAT(); // Ensure the oscillator is running. rtc.startClock(); // if (rtc.getDevice() == RTCx::MCP7941x) { // rtc.getCalibration(); // // rtc.setCalibration(-127); // } rtc.setSQW(RTCx::freq1Hz); //don't output a sqw /** Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001 */ if (NULL != services_pipe_type_mapping) { aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0]; } else { aci_state.aci_setup_info.services_pipe_type_mapping = NULL; } aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES; aci_state.aci_setup_info.setup_msgs = (hal_aci_data_t*) setup_msgs; aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES; /* Tell the ACI library, the MCU to nRF8001 pin connections. * The Active pin is optional and can be marked UNUSED */ aci_state.aci_pins.board_name = BOARD_DEFAULT; //See board.h for details REDBEARLAB_SHIELD_V1_1 or BOARD_DEFAULT aci_state.aci_pins.reqn_pin = SS; //SS for Nordic board, 9 for REDBEARLAB_SHIELD_V1_1 aci_state.aci_pins.rdyn_pin = 2; //3 for Nordic board, 8 for REDBEARLAB_SHIELD_V1_1 aci_state.aci_pins.mosi_pin = MOSI; aci_state.aci_pins.miso_pin = MISO; aci_state.aci_pins.sck_pin = SCK; aci_state.aci_pins.spi_clock_divider = SPI_CLOCK_DIV8; aci_state.aci_pins.reset_pin = 4; aci_state.aci_pins.active_pin = 7; aci_state.aci_pins.optional_chip_sel_pin = UNUSED; aci_state.aci_pins.interface_is_interrupt = false; aci_state.aci_pins.interrupt_number = 1; /* We reset the nRF8001 here by toggling the RESET line connected to the nRF8001 * If the RESET line is not available we call the ACI Radio Reset to soft reset the nRF8001 * then we initialize the data structures required to setup the nRF8001. * We call lib_aci_init() with debug true to enable debug printing for ACI Commands and Events */ lib_aci_init(&aci_state, true); } void uart_over_ble_init(void) { uart_over_ble.uart_rts_local = true; } bool uart_tx(uint8_t *buffer, uint8_t buffer_len) { bool status = false; if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (aci_state.data_credit_available >= 1)) { status = lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, buffer, buffer_len); if (status) { aci_state.data_credit_available--; } } return status; } bool uart_process_control_point_rx(uint8_t *byte, uint8_t length) { bool status = false; aci_ll_conn_params_t *conn_params; if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_TX) ) { // Serial.println(*byte, HEX); switch(*byte) { /* Queues a ACI Disconnect to the nRF8001 when this packet is received. * May cause some of the UART packets being sent to be dropped */ case UART_OVER_BLE_DISCONNECT: /* Parameters: None */ lib_aci_disconnect(&aci_state, ACI_REASON_TERMINATE); status = true; break; /* Queues an ACI Change Timing to the nRF8001 */ case UART_OVER_BLE_LINK_TIMING_REQ: /* Parameters: * Connection interval min: 2 bytes * Connection interval max: 2 bytes * Slave latency: 2 bytes * Timeout: 2 bytes * * Same format as Peripheral Preferred Connection Parameters (See nRFgo * studio -> nRF8001 Configuration -> GAP Settings Refer to the ACI * Change Timing Request in the nRF8001 Product Specifications */ conn_params = (aci_ll_conn_params_t *)(byte+1); lib_aci_change_timing(conn_params->min_conn_interval, conn_params->max_conn_interval, conn_params->slave_latency, conn_params->timeout_mult); status = true; break; /* Clears the RTS of the UART over BLE */ case UART_OVER_BLE_TRANSMIT_STOP: /* Parameters: None */ uart_over_ble.uart_rts_local = false; status = true; break; /* Set the RTS of the UART over BLE */ case UART_OVER_BLE_TRANSMIT_OK: /* Parameters: None */ uart_over_ble.uart_rts_local = true; status = true; break; } } return status; } void aci_loop() { static bool setup_required = false; static bool bootloader_jump_required = false; static uint8_t pipes[] = { PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_PACKET_RX, PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_CONTROL_POINT_TX, PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_CONTROL_POINT_RX_ACK_AUTO }; // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t * aci_evt; aci_evt = &aci_data.evt; switch(aci_evt->evt_opcode) { /** As soon as you reset the nRF8001 you will get an ACI Device Started Event */ case ACI_EVT_DEVICE_STARTED: { aci_state.data_credit_total = aci_evt->params.device_started.credit_available; switch(aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: /** When the device is in the setup mode */ setup_required = true; break; case ACI_DEVICE_STANDBY: //Looking for an iPhone by sending radio advertisements //When an iPhone connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001 if (aci_evt->params.device_started.hw_error) { delay(20); //Magic number used to make sure the HW error event is handled correctly. } else { lib_aci_connect(180/* in seconds */, 0x0050 /* advertising interval 50ms*/); } if (!bootloader_data_store(&aci_state, 180, 0x0050, pipes, sizeof(pipes))) { } break; } } break; //ACI Device Started Event case ACI_EVT_CMD_RSP: //If an ACI command response event comes with an error -> stop if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) { //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command } if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode) { //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET, (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } break; case ACI_EVT_CONNECTED: uart_over_ble_init(); timing_change_done = false; aci_state.data_credit_available = aci_state.data_credit_total; /* Get the device version of the nRF8001 and store it in the Hardware Revision String */ lib_aci_device_version(); break; case ACI_EVT_PIPE_STATUS: if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done)) { lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP. // Used to increase or decrease bandwidth timing_change_done = true; } break; case ACI_EVT_TIMING: lib_aci_set_local_data(&aci_state, PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET, (uint8_t *)&(aci_evt->params.timing.conn_rf_interval), /* Byte aligned */ PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE); break; case ACI_EVT_DISCONNECTED: lib_aci_connect(180/* in seconds */, 0x0100 /* advertising interval 100ms*/); break; case ACI_EVT_DATA_RECEIVED: switch (aci_evt->params.data_received.rx_data.pipe_number) { case PIPE_UART_OVER_BTLE_UART_RX_RX: for(int i=0; ilen - 2; i++) { uart_buffer[i] = aci_evt->params.data_received.rx_data.aci_data[i]; if((char)aci_evt->params.data_received.rx_data.aci_data[i] == TIME_HEADER){ char ack[]="Set Time"; uart_tx((uint8_t *)&ack[0], strlen(ack)); char settime[12]; for(int j=0; jlen - 3; j++) { settime[j] = (char)aci_evt->params.data_received.rx_data.aci_data[j+1]; } // uart_tx((uint8_t *)&settime[0], strlen(settime)); RTCx::time_t t; t = atol(settime); RTCx::gmtime_r(&t, &tm); rtc.setClock(&tm); //set clock with epoch timestamp // setTime(pctime); // Sync Arduino clock to the time received on the serial port i = aci_evt->len; }else if((char)aci_evt->params.data_received.rx_data.aci_data[i] == TIME_NOW){ char nowtime[11]; rtc.readClock(&tm); RTCx::time_t nowt = RTCx::mktime(&tm); sprintf(&nowtime[0],"%ld",nowt); uart_tx((uint8_t *)&nowtime[0], strlen(nowtime)); } else if((char)aci_evt->params.data_received.rx_data.aci_data[i] == RTC_PROBE){ uint8_t addressList[] = {RTCx::MCP7941xAddress, RTCx::DS1307Address}; if (rtc.autoprobe(addressList, sizeof(addressList))) { // Found something, hopefully a clock. char found[]="Autoprobe found "; uart_tx((uint8_t *)&found[0], strlen(found)); switch (rtc.getDevice()){ case RTCx::DS1307:{ char ds1307[]="DS1307"; uart_tx((uint8_t *)&ds1307[0], strlen(ds1307)); break;} case RTCx::MCP7941x:{ char mcp7941x[]="MCP7941x"; uart_tx((uint8_t *)&mcp7941x[0], strlen(mcp7941x)); break;} default:{ char unknown[]="Unknown Device"; uart_tx((uint8_t *)&unknown[0], strlen(unknown)); break;} } }else { char nofound[]="Nothing Found"; uart_tx((uint8_t *)&nofound[0], strlen(nofound)); } } else if((char)aci_evt->params.data_received.rx_data.aci_data[i] == GET_DATA){ GetDataFlag = 1; GetData = 0; }else if((char)aci_evt->params.data_received.rx_data.aci_data[i] == FAN_SPEED){ switch ((char)aci_evt->params.data_received.rx_data.aci_data[i+1]){ case '1':{ State = ONLOW; break;} case '2':{ State = ONMID; break;} case '3':{ State = ONHIGH; break;} default:{ break;} } i = aci_evt->len; }else if((char)aci_evt->params.data_received.rx_data.aci_data[i] == GO_SLEEP){ State = OFFSTATE; } } uart_buffer_len = aci_evt->len - 2; if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX)) { } break; case PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_RX: uart_process_control_point_rx(&aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2); //Subtract for Opcode and Pipe number break; case PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_CONTROL_POINT_RX_ACK_AUTO: if (1 == aci_evt->params.data_received.rx_data.aci_data[0] && lib_aci_is_pipe_available(&aci_state, PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_CONTROL_POINT_TX)) { bootloader_jump_required = true; } break; } break; case ACI_EVT_DATA_CREDIT: aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; break; case ACI_EVT_PIPE_ERROR: //See the appendix in the nRF8001 Product Specication for details on the error codes //Increment the credit available as the data packet was not sent. //The pipe error also represents the Attribute protocol Error Response //sent from the peer and that should not be counted for the credit. if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) { aci_state.data_credit_available++; } break; case ACI_EVT_HW_ERROR: lib_aci_connect(180/* in seconds */, 0x0050 /* advertising interval 50ms*/); break; } } else { //Serial.println(F("No ACI Events available")); // 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 } /* setup_required is set to true when the device starts up and enters setup mode. * It indicates that do_aci_setup() should be called. The flag should be cleared if * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE. */ if(setup_required) { if (SETUP_SUCCESS == do_aci_setup(&aci_state)) { setup_required = false; } } /* If the bootloader_jump_required flag has been set, we attempt to jump to bootloader. * We do a series of checks before jumping. */ if (bootloader_jump_required && aci_state.data_credit_available == aci_state.data_credit_total && lib_aci_is_pipe_available(&aci_state, PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_PACKET_RX) && lib_aci_is_pipe_available(&aci_state, PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_CONTROL_POINT_TX) && lib_aci_is_pipe_available(&aci_state, PIPE_NORDIC_DEVICE_FIRMWARE_UPDATE_SERVICE_DFU_CONTROL_POINT_RX_ACK_AUTO)) { lib_aci_connect(180/* in seconds */, 0x0020 /* advertising interval 20ms*/); bootloader_jump(&aci_state); } } void pin3Interrupt(void) { } void enterSleep(void) { delay(100); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); attachInterrupt(1, pin3Interrupt, LOW); /* Setup pin3 as an interrupt and attach handler. */ sleep_mode(); /* The program will continue from here. */ /* First thing to do is disable sleep. */ sleep_disable(); detachInterrupt(1); } void ButtonRead(int pin) { current = digitalRead(pin); if (current == LOW && previous == HIGH && (millis() - firstTime) > 200) { // if the button state changes to pressed, remember the start time firstTime = millis(); } millis_held = (millis() - firstTime); secs_held = millis_held / 1000; if (millis_held > debounceDelay) { if (current == LOW && secs_held >= 2) { digitalWrite(A0, HIGH); //turn off LEDs digitalWrite(8, HIGH); digitalWrite(9, HIGH); } // check if the button was released since we last checked if (current == HIGH && previous == LOW) { if (secs_held <= 0) { State++; if (State > ONHIGH){ State = ONLOW; } } if (secs_held >= 2) { State = OFFSTATE; } } } previous = current; prev_secs_held = secs_held; } void loop() { //Process any ACI commands or events aci_loop(); ButtonRead(wakePin); if(State != PrevState){ switch (State){ case OFFSTATE:{ char OffState[]="Sleep"; uart_tx((uint8_t *)&OffState[0], strlen(OffState)); rtc.readClock(&tm); TimeArray[ArrayIndex]=RTCx::mktime(&tm); OnOffArray[ArrayIndex]=OFFSTATE; ArrayIndex++; digitalWrite(6, LOW); //Turn off UV LED digitalWrite(A0, HIGH); //Turn off RGB LEDs digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(5, LOW); //Turn off Fan // enterSleep(); break;} case ONLOW:{ char OnState[]="On Low"; uart_tx((uint8_t *)&OnState[0], strlen(OnState)); rtc.readClock(&tm); TimeArray[ArrayIndex]=RTCx::mktime(&tm); OnOffArray[ArrayIndex]=ONLOW; ArrayIndex++; digitalWrite(6, HIGH); // UV LED ON digitalWrite(9, LOW); analogWrite(5,127); // Turn Fan 50% break;} case ONMID:{ char Lowstate[]="On Mid"; uart_tx((uint8_t *)&Lowstate[0], strlen(Lowstate)); digitalWrite(6, HIGH); // UV LED ON digitalWrite(9, LOW); analogWrite(5,191); // Turn Fan 75% break;} case ONHIGH:{ char Midwstate[]="On High"; uart_tx((uint8_t *)&Midwstate[0], strlen(Midwstate)); digitalWrite(6, HIGH); // UV LED ON digitalWrite(9, LOW); analogWrite(5,255); // Turn Fan 100% break;} default:{ char unknown[]="Unknown State"; uart_tx((uint8_t *)&unknown[0], strlen(unknown)); break;} } if(ArrayIndex >= 100){ ArrayIndex = 0; } PrevState = State; } if(GetDataFlag == 1){ if(GetData < ArrayIndex){ char timearr[11]; sprintf(&timearr[0],"%ld",TimeArray[GetData]); if(OnOffArray[GetData] == OFFSTATE){ timearr[10] = 'F'; }else{ timearr[10] = 'N'; } if(uart_tx((uint8_t *)&timearr[0], strlen(timearr))){ GetData++; } }else{ ArrayIndex = 0; GetDataFlag = 0; } } if(digitalRead(A2)){ if(digitalRead(A1)){ digitalWrite(A0, HIGH); digitalWrite(8, LOW); }else{ digitalWrite(A0, LOW); digitalWrite(8, HIGH); } }else{ digitalWrite(A0, HIGH); digitalWrite(8, HIGH); } }