nRF51822 button, those cheap ones from ebay/aliexpress
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 13:c2ffbb25ed0a
- Parent:
- 12:9bb01e063498
--- a/main.cpp Sun Aug 27 05:48:45 2017 +0000 +++ b/main.cpp Sat Oct 28 05:13:58 2017 +0000 @@ -20,6 +20,8 @@ * * BLE sensor as Beacon advertisements. Intended to function with specific BLE observer. * Tested on nRF51822 targets on mbed. + * + * /BEA/0 is fast press, /BEA/1 is slow press * keywords: todo, tochange */ @@ -38,8 +40,9 @@ /******************************************************************************************* * START tochange: items that may need customization depending on sensors, hardware, and desired behavior *******************************************************************************************/ -const uint16_t Periodic_Update_Seconds = 20; //number of seconds between periodic I/O status re-transmits 900s =15 min. +const uint16_t Periodic_Update_Seconds = 900; //number of seconds between periodic I/O status re-transmits 900s =15 min. #define MyDebugEnb 0 //enables serial output for debug, consumes ~1mA when idle +#define EnablePeriodic 0 uint8_t magnet_near=0; //this I/O, specifically for reed switch sensor @@ -47,8 +50,12 @@ *Syntax: Pin "P0.4" on nRF51822 documentation is mbed "p4". * InterruptIn is pulled-up. GND the pin to activate. */ -InterruptIn button1(p0); //nRF51822 P0.0 -InterruptIn button2(p1); //nRF51822 P0.1 +//InterruptIn button1(p0); //nRF51822 P0.0 +//InterruptIn button2(p1); //nRF51822 P0.1 +InterruptIn button1(p28); //nRF51822 P0.28 is beacon button +//InterruptIn button1(p24); //nRF51822 P0.24 is purple board +//InterruptIn button1(p1); + /****************************************************************************************** * END tochange *******************************************************************************************/ @@ -171,7 +178,8 @@ //ISR for I/O interrupt void buttonPressedCallback(void) { - Tic_Debounce.attach(debounce_Callback, 1); //ok to attach multiple times, recent one wins + //for buttons, make debounce much shorter to make short press and long press easier + Tic_Debounce.attach(debounce_Callback, 0.35); //ok to attach multiple times, recent one wins } //ISR for I/O interrupt @@ -311,16 +319,15 @@ NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; NRF_ADC->TASKS_START = 1; - //while loop doesn't actually loop until reading comlete, use a wait. while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; - wait_ms(1); + wait_ms(2); //save off RESULT before disabling. //uint16_t myresult = (uint16_t)NRF_ADC->RESULT; //disable ADC to lower bat consumption - NRF_ADC->TASKS_STOP = 1; + //NRF_ADC->TASKS_STOP = 1; //perform disable in main loop //NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption return (uint16_t)NRF_ADC->RESULT; // 10 bit @@ -357,7 +364,7 @@ //while loop doesn't actually loop until reading comlete, use a wait. while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; - wait_ms(1); //needed because busy while loop doesn't run. + //wait_ms(2); //needed because busy while loop doesn't run. //save off RESULT before disabling. //uint16_t myresult = (uint16_t)NRF_ADC->RESULT; @@ -430,7 +437,9 @@ while (ble.hasInitialized() == false) { /* spin loop */ } //every X seconds, sends period update, up to 1800 (30 minutes) + #if Periodic_Update_Seconds Tic_Periodic.attach(periodic_Callback, Periodic_Update_Seconds); //send updated I/O every x seconds + #endif Tic_Birthday.attach(clock_reset_Callback, Periodicity); //clock algorithm periodicity @@ -454,65 +463,51 @@ //set both pins to pull-up, so they're not floating when we read state button1.mode(PullUp); - button2.mode(PullUp); + //button2.mode(PullUp); + //for buttons, always wake on press, but not release. + button1.fall(buttonReleasedCallback); //expect either button1 or button2 is grounded, b/c using SPDT reed switch //the "common" pin on the reed switch should be on GND uint8_t button1_state = button1.read(); - uint8_t button2_state = button2.read(); + //uint8_t button2_state = button2.read(); //let's just update the pins on every wake. Insurance against const drain. //if state == 0, pin is grounded. Unset interrupt and float pin, set the other pin for ISR - if ( (button1_state == 0) && (button2_state == 1) ) + //if ( (button1_state == 0) && (button2_state == 1) ) + if (button1_state == 0) { magnet_near = 1; //button1.disable_irq() //don't know if disables IRQ on port or pin - button1.fall(NULL); //disable interrupt - button1.rise(NULL); //disable interrupt - button1.mode(PullNone); //float pin to save battery + //button1.fall(NULL); //disable interrupt + //button1.rise(NULL); //disable interrupt + //button1.mode(PullNone); //float pin to save battery //button2.disable_irq() //don't know if disables IRQ on port or pin - button2.fall(buttonReleasedCallback); //enable interrupt - button2.rise(buttonReleasedCallback); //enable interrupt - button2.mode(PullUp); //pull up on pin to get interrupt + //button2.fall(buttonReleasedCallback); //enable interrupt + //button2.rise(buttonReleasedCallback); //enable interrupt + //button2.mode(PullUp); //pull up on pin to get interrupt #if MyDebugEnb device.printf("=== button 1! %d seconds=== \r\n", seconds_Old); #endif } //end if button2 - else if ( (button1_state == 1) && (button2_state == 0) ) //assume other pin is open circuit + else if (button1_state == 1) //assume other pin is open circuit { magnet_near = 0; //button1.disable_irq() //don't know if disables IRQ on port or pin - button1.fall(buttonReleasedCallback); //enable interrupt - button1.rise(buttonReleasedCallback); //enable interrupt - button1.mode(PullUp); //pull up on pin to get interrupt + //button1.fall(buttonReleasedCallback); //enable interrupt + //button1.rise(buttonReleasedCallback); //enable interrupt + //button1.mode(PullUp); //pull up on pin to get interrupt //button2.disable_irq() //don't know if disables IRQ on port or pin - button2.fall(NULL); //disable interrupt - button2.rise(NULL); //disable interrupt - button2.mode(PullNone); //float pin to save battery + //button2.fall(NULL); //disable interrupt + //button2.rise(NULL); //disable interrupt + //button2.mode(PullNone); //float pin to save battery #if MyDebugEnb device.printf("=== button 2! === %d seconds\r\n", seconds_Old); #endif } //end if button1 - else //odd state, shouldn't happen, suck battery and pullup both pins - { - magnet_near = 2; - //AdvData[4] = 0x33; - //button1.disable_irq() //don't know if disables IRQ on port or pin - button1.fall(buttonReleasedCallback); //disable interrupt - button1.rise(buttonReleasedCallback); //disable interrupt - button1.mode(PullUp); //float pin to save battery - - //button2.disable_irq() //don't know if disables IRQ on port or pin - button2.fall(buttonReleasedCallback); //disable interrupt - button2.rise(buttonReleasedCallback); //disable interrupt - button2.mode(PullUp); //float pin to save battery - #if MyDebugEnb - device.printf("no buttons!! %d seconds\r\n", seconds_Old); - #endif - } //end odd state if (Flag_Update_IO) { @@ -541,7 +536,7 @@ //read and convert analog voltage. Comment out this section if note needed, saves some battery NRF_ADC->TASKS_STOP = 1; float analogreading; - analogreading = (float)read_ADC_pin(); + //analogreading = (float)read_ADC_pin(); //comment out for beacon buttons analogreading = (analogreading * 3.6) / 1024.0; #if MyDebugEnb device.printf("separate analog reading: %.02f \r\n", analogreading); @@ -618,13 +613,13 @@ AdvData[JSON_loc] = 0x22; //ADV_Data[13] = " JSON_loc++; //14 - AdvData[JSON_loc] = 0x6d; //ADV_Data[14] = m + AdvData[JSON_loc] = 'B'; //ADV_Data[14] = B JSON_loc++; //15 - AdvData[JSON_loc] = 0x61; //ADV_Data[15] = a + AdvData[JSON_loc] = 'E'; //ADV_Data[15] = E JSON_loc++; //16 - AdvData[JSON_loc] = 0x67; //ADV_Data[16] = g + AdvData[JSON_loc] = 'A'; //ADV_Data[16] = A JSON_loc++; //17 //for periodic calls, we want to add an extra mqtt level "p", using "/p"