BLE ADV sensor for 2-pin interrupt (i.e. window/door sensor w/ reed switch)
Dependencies: BLE_API mbed nRF51822
Basic door/window sensor for nRF51822 BLE modules.
main.cpp@12:9bb01e063498, 2017-08-27 (annotated)
- Committer:
- electronichamsters
- Date:
- Sun Aug 27 05:48:45 2017 +0000
- Revision:
- 12:9bb01e063498
- Parent:
- 11:4f925834167d
make periodic updates based on i/o events. comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
electronichamsters | 12:9bb01e063498 | 1 | /* |
electronichamsters | 12:9bb01e063498 | 2 | * Copyright (c) Eric Tsai 2017 |
electronichamsters | 12:9bb01e063498 | 3 | * |
electronichamsters | 12:9bb01e063498 | 4 | * |
electronichamsters | 12:9bb01e063498 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
electronichamsters | 12:9bb01e063498 | 6 | * you may not use this file except in compliance with the License. |
electronichamsters | 12:9bb01e063498 | 7 | * You may obtain a copy of the License at |
electronichamsters | 12:9bb01e063498 | 8 | * |
electronichamsters | 12:9bb01e063498 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
electronichamsters | 12:9bb01e063498 | 10 | * |
electronichamsters | 12:9bb01e063498 | 11 | * Unless required by applicable law or agreed to in writing, software |
electronichamsters | 12:9bb01e063498 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
electronichamsters | 12:9bb01e063498 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
electronichamsters | 12:9bb01e063498 | 14 | * See the License for the specific language governing permissions and |
electronichamsters | 12:9bb01e063498 | 15 | * limitations under the License. |
electronichamsters | 12:9bb01e063498 | 16 | * |
electronichamsters | 12:9bb01e063498 | 17 | * |
electronichamsters | 12:9bb01e063498 | 18 | * Credit: started with the basic BLE Temperature Beacon code from mbed Bluetooth Low Energy team |
electronichamsters | 12:9bb01e063498 | 19 | * https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_TemperatureBeacon/file/0a8bbb6dea16/main.cpp |
electronichamsters | 12:9bb01e063498 | 20 | * |
electronichamsters | 12:9bb01e063498 | 21 | * BLE sensor as Beacon advertisements. Intended to function with specific BLE observer. |
electronichamsters | 12:9bb01e063498 | 22 | * Tested on nRF51822 targets on mbed. |
electronichamsters | 12:9bb01e063498 | 23 | * keywords: todo, tochange |
electronichamsters | 12:9bb01e063498 | 24 | */ |
electronichamsters | 8:46c5e0bfab05 | 25 | |
sunsmile2015 | 0:3dc6e424dba0 | 26 | |
electronichamsters | 8:46c5e0bfab05 | 27 | extern "C" |
electronichamsters | 8:46c5e0bfab05 | 28 | { |
electronichamsters | 12:9bb01e063498 | 29 | #include "nrf_ecb.h" //required to call the ecb functions for encryption |
electronichamsters | 8:46c5e0bfab05 | 30 | } |
electronichamsters | 8:46c5e0bfab05 | 31 | |
sunsmile2015 | 0:3dc6e424dba0 | 32 | #include "mbed.h" |
rgrover1 | 5:f4d74a8cad43 | 33 | #include "toolchain.h" |
sunsmile2015 | 0:3dc6e424dba0 | 34 | #include "ble/BLE.h" |
sunsmile2015 | 0:3dc6e424dba0 | 35 | #include "TMP_nrf51/TMP_nrf51.h" |
sunsmile2015 | 0:3dc6e424dba0 | 36 | |
electronichamsters | 8:46c5e0bfab05 | 37 | |
electronichamsters | 12:9bb01e063498 | 38 | /******************************************************************************************* |
electronichamsters | 12:9bb01e063498 | 39 | * START tochange: items that may need customization depending on sensors, hardware, and desired behavior |
electronichamsters | 12:9bb01e063498 | 40 | *******************************************************************************************/ |
electronichamsters | 12:9bb01e063498 | 41 | const uint16_t Periodic_Update_Seconds = 20; //number of seconds between periodic I/O status re-transmits 900s =15 min. |
electronichamsters | 12:9bb01e063498 | 42 | #define MyDebugEnb 0 //enables serial output for debug, consumes ~1mA when idle |
electronichamsters | 12:9bb01e063498 | 43 | uint8_t magnet_near=0; //this I/O, specifically for reed switch sensor |
electronichamsters | 10:79e8f92ef156 | 44 | |
electronichamsters | 11:4f925834167d | 45 | |
electronichamsters | 12:9bb01e063498 | 46 | /* hardware interrupt pins, selected based on hardware |
electronichamsters | 12:9bb01e063498 | 47 | *Syntax: Pin "P0.4" on nRF51822 documentation is mbed "p4". |
electronichamsters | 12:9bb01e063498 | 48 | * InterruptIn is pulled-up. GND the pin to activate. |
electronichamsters | 12:9bb01e063498 | 49 | */ |
electronichamsters | 12:9bb01e063498 | 50 | InterruptIn button1(p0); //nRF51822 P0.0 |
electronichamsters | 12:9bb01e063498 | 51 | InterruptIn button2(p1); //nRF51822 P0.1 |
electronichamsters | 12:9bb01e063498 | 52 | /****************************************************************************************** |
electronichamsters | 12:9bb01e063498 | 53 | * END tochange |
electronichamsters | 12:9bb01e063498 | 54 | *******************************************************************************************/ |
electronichamsters | 8:46c5e0bfab05 | 55 | |
electronichamsters | 12:9bb01e063498 | 56 | |
electronichamsters | 8:46c5e0bfab05 | 57 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 58 | // if you see ~1mA consumption during sleep, that's because MyDebugEnb==1, it's enabled. |
electronichamsters | 8:46c5e0bfab05 | 59 | Serial device(p9, p11); //nRF51822 uart : TX=p9. RX=p11 |
electronichamsters | 8:46c5e0bfab05 | 60 | #endif |
electronichamsters | 8:46c5e0bfab05 | 61 | |
electronichamsters | 12:9bb01e063498 | 62 | static Ticker Tic_Stop_Adv; //used to stop advertising after X seconds |
electronichamsters | 12:9bb01e063498 | 63 | static Ticker Tic_Debounce; //debounce I/O |
electronichamsters | 12:9bb01e063498 | 64 | static Ticker Tic_Periodic; //transmit sensor data on a periodic basis outside I/O events |
electronichamsters | 8:46c5e0bfab05 | 65 | |
electronichamsters | 12:9bb01e063498 | 66 | const uint16_t Periodicity = 1800; //birthday periodicity used for spoof checking, must match gateway. Should be 1800 seconds for 30minutes |
electronichamsters | 12:9bb01e063498 | 67 | static Timer Tmr_From_Birthday; //holds number of seconds since birthday, for spoof detection |
electronichamsters | 12:9bb01e063498 | 68 | static Ticker Tic_Birthday; //resets Tmr_From_Birthday every Periodicity seconds, for spoof detection |
electronichamsters | 12:9bb01e063498 | 69 | |
electronichamsters | 8:46c5e0bfab05 | 70 | |
electronichamsters | 12:9bb01e063498 | 71 | static bool Flag_Update_IO = false; //flag to indicate event is hardware interrupt |
electronichamsters | 12:9bb01e063498 | 72 | static bool Flag_Periodic_Call = false; //flag to indicate event is periodic callback |
electronichamsters | 12:9bb01e063498 | 73 | static bool Flag_Detach_Adv_Tic = false; //flag to stop advertising |
electronichamsters | 12:9bb01e063498 | 74 | |
electronichamsters | 12:9bb01e063498 | 75 | /* Optional: Device Name, add for human read-ability */ |
electronichamsters | 12:9bb01e063498 | 76 | const static char DEVICE_NAME[] = "LOL"; |
electronichamsters | 8:46c5e0bfab05 | 77 | |
electronichamsters | 8:46c5e0bfab05 | 78 | |
electronichamsters | 12:9bb01e063498 | 79 | //Advertisement Data |
electronichamsters | 12:9bb01e063498 | 80 | //note: AdvData[] holds bytes [5] to byte [30] of entire advertising data. The user content part after ADV flag and header |
electronichamsters | 12:9bb01e063498 | 81 | static uint8_t AdvData[] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; //26 Bytes manufacturer specific data |
electronichamsters | 12:9bb01e063498 | 82 | char buffer[10]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //hold I/O reading json |
electronichamsters | 12:9bb01e063498 | 83 | char bat_volt_char[6] = {0, 0, 0, 0, 0, 0}; //hold json for battery reading |
electronichamsters | 12:9bb01e063498 | 84 | uint8_t Adv_First_Section[10]; //holds the first several bytes with a pattern indicating this sensor is "one of ours" |
electronichamsters | 12:9bb01e063498 | 85 | uint8_t mac_reverse[6] = {0x0,0x0,0x0,0x0,0x0,0x0}; //mac address for this module |
rgrover1 | 5:f4d74a8cad43 | 86 | |
electronichamsters | 12:9bb01e063498 | 87 | /***** Advertisement structure is 31 Bytes **************** |
electronichamsters | 12:9bb01e063498 | 88 | |
electronichamsters | 12:9bb01e063498 | 89 | https://docs.mbed.com/docs/ble-intros/en/latest/Advanced/CustomGAP/ |
electronichamsters | 8:46c5e0bfab05 | 90 | |
electronichamsters | 12:9bb01e063498 | 91 | Full Advertisement: |
electronichamsters | 12:9bb01e063498 | 92 | First 5 bytes are set by stack according to flag and header parameters. |
electronichamsters | 12:9bb01e063498 | 93 | Last 26 bytes are user data |
electronichamsters | 12:9bb01e063498 | 94 | -- tabbed -- |
electronichamsters | 12:9bb01e063498 | 95 | Byte 0 | AD1 Length | 0x02 | AD1 is 2 bytes long |
electronichamsters | 12:9bb01e063498 | 96 | Byte 1 | AD1 Type | 0x01 | AD1 Data interpreted as flag |
electronichamsters | 12:9bb01e063498 | 97 | Byte 2 | AD1 Data 0 | 0x06 | AD1 Data flag mean "00000110" |
electronichamsters | 12:9bb01e063498 | 98 | Byte 3 | AD2 Length | 0x1B | AD2 is 27 bytes (0x1B) long (rest of this data) |
electronichamsters | 12:9bb01e063498 | 99 | Byte 4 | AD2 Type | 0xFF | 0xFF mean Manufacturer Specific Data |
electronichamsters | 12:9bb01e063498 | 100 | Byte 5 | AD2 Data 0 | ADV_Data[0] | "our device" flag, MAC[3] |
electronichamsters | 12:9bb01e063498 | 101 | Byte 6 | AD2 Data 1 | ADV_Data[1] | "out device" flag, MAC[2] |
electronichamsters | 12:9bb01e063498 | 102 | Byte 7 | AD2 Data 2 | ADV_Data[2] | "out device" flag, MAC[1] |
electronichamsters | 12:9bb01e063498 | 103 | Byte 8 | AD2 Data 3 | ADV_Data[3] | "out device" flag, MAC[0] |
electronichamsters | 12:9bb01e063498 | 104 | Byte 9 | AD2 Data 4 | ADV_Data[4] | battery voltage json MSB, ie 3 in 3.14 |
electronichamsters | 12:9bb01e063498 | 105 | Byte 10 | AD2 Data 5 | ADV_Data[5] | battery voltage json |
electronichamsters | 12:9bb01e063498 | 106 | Byte 11 | AD2 Data 6 | ADV_Data[6] | battery voltage json |
electronichamsters | 12:9bb01e063498 | 107 | Byte 12 | AD2 Data 7 | ADV_Data[7] | battery voltage json LSB, ie 4 in 3.14 |
electronichamsters | 12:9bb01e063498 | 108 | Byte 13 | AD2 Data 8 | ADV_Data[8] | reserved |
electronichamsters | 12:9bb01e063498 | 109 | Byte 14 | AD2 Data 9 | ADV_Data[9] | reserved |
electronichamsters | 12:9bb01e063498 | 110 | Byte 15 | AD2 Data 10 | ADV_Data[10] Encrypted | spoof - clock high byte, range 0 to 1800 seconds |
electronichamsters | 12:9bb01e063498 | 111 | Byte 16 | AD2 Data 11 | ADV_Data[11] Encrypted | spoof - clock low byte |
electronichamsters | 12:9bb01e063498 | 112 | Byte 17 | AD2 Data 12 | ADV_Data[12] Encrypted | Xmit_Cnt - increments per transmit event, 0-255 |
electronichamsters | 12:9bb01e063498 | 113 | Byte 18 | AD2 Data 13 | ADV_Data[13] Encrypted | JSON[0] |
electronichamsters | 12:9bb01e063498 | 114 | Byte 19 | AD2 Data 14 | ADV_Data[14] Encrypted | JSON[1] |
electronichamsters | 12:9bb01e063498 | 115 | Byte 20 | AD2 Data 15 | ADV_Data[15] Encrypted | JSON[2] |
electronichamsters | 12:9bb01e063498 | 116 | Byte 21 | AD2 Data 16 | ADV_Data[16] Encrypted | JSON[3] |
electronichamsters | 12:9bb01e063498 | 117 | Byte 22 | AD2 Data 17 | ADV_Data[17] Encrypted | JSON[4] |
electronichamsters | 12:9bb01e063498 | 118 | Byte 23 | AD2 Data 18 | ADV_Data[18] Encrypted | JSON[5] |
electronichamsters | 12:9bb01e063498 | 119 | Byte 24 | AD2 Data 19 | ADV_Data[19] Encrypted | JSON[6] |
electronichamsters | 12:9bb01e063498 | 120 | Byte 25 | AD2 Data 20 | ADV_Data[20] Encrypted | JSON[7] |
electronichamsters | 12:9bb01e063498 | 121 | Byte 26 | AD2 Data 21 | ADV_Data[21] Encrypted | JSON[8] |
electronichamsters | 12:9bb01e063498 | 122 | Byte 27 | AD2 Data 22 | ADV_Data[22] Encrypted | JSON[9] |
electronichamsters | 12:9bb01e063498 | 123 | Byte 28 | AD2 Data 23 | ADV_Data[23] Encrypted | JSON[10] |
electronichamsters | 12:9bb01e063498 | 124 | Byte 29 | AD2 Data 24 | ADV_Data[24] Encrypted | JSON[11] |
electronichamsters | 12:9bb01e063498 | 125 | Byte 30 | AD2 Data 25 | ADV_Data[25] Encrypted | JSON[12] |
electronichamsters | 8:46c5e0bfab05 | 126 | |
electronichamsters | 12:9bb01e063498 | 127 | ***************************************************/ |
electronichamsters | 8:46c5e0bfab05 | 128 | |
electronichamsters | 8:46c5e0bfab05 | 129 | |
electronichamsters | 8:46c5e0bfab05 | 130 | static uint8_t key[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 131 | //26 bytes adv data |
electronichamsters | 8:46c5e0bfab05 | 132 | static uint8_t encrypted[26] = {0x0,0x0,0x0,0x1,0x1,0x1,0x2,0x2,0x2,0x3,0x3,0x3,0x4,0x4,0x4,0x5,0x5,0x5,0x6,0x6,0x6,0x7,0x7,0x7,0x8,0x8}; /* Example of hex data */ |
electronichamsters | 8:46c5e0bfab05 | 133 | //static uint8_t key_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 134 | static uint8_t key_buf[16] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1, 0x2}; |
electronichamsters | 8:46c5e0bfab05 | 135 | static uint8_t src_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 136 | static uint8_t des_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 8:46c5e0bfab05 | 137 | |
electronichamsters | 10:79e8f92ef156 | 138 | uint8_t Xmit_Cnt = 1; |
electronichamsters | 8:46c5e0bfab05 | 139 | |
electronichamsters | 8:46c5e0bfab05 | 140 | |
electronichamsters | 8:46c5e0bfab05 | 141 | |
electronichamsters | 8:46c5e0bfab05 | 142 | /* **** NOT USED **** */ |
electronichamsters | 8:46c5e0bfab05 | 143 | //16byte UUID loading happens here |
electronichamsters | 8:46c5e0bfab05 | 144 | //Look at <GapAdvertisingData.h> for rest of definition |
sunsmile2015 | 4:e5fa4c8838db | 145 | struct ApplicationData_t { |
electronichamsters | 8:46c5e0bfab05 | 146 | //Byte 0: AppID High Byte |
electronichamsters | 8:46c5e0bfab05 | 147 | //Byte 1: AppID Low Byte |
electronichamsters | 8:46c5e0bfab05 | 148 | //Byte 2: sensor High Word |
electronichamsters | 8:46c5e0bfab05 | 149 | //Byte 3: |
electronichamsters | 8:46c5e0bfab05 | 150 | //Byte 4: |
electronichamsters | 8:46c5e0bfab05 | 151 | //Byte 5: sensor Low Byte |
electronichamsters | 8:46c5e0bfab05 | 152 | |
electronichamsters | 8:46c5e0bfab05 | 153 | |
electronichamsters | 8:46c5e0bfab05 | 154 | //app ID is 16 bit, (0xFEFE) |
electronichamsters | 8:46c5e0bfab05 | 155 | uint16_t applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */ |
electronichamsters | 8:46c5e0bfab05 | 156 | |
electronichamsters | 8:46c5e0bfab05 | 157 | TMP_nrf51::TempSensorValue_t tmpSensorValue; /* this is a float (32-bit), user data */ |
rgrover1 | 5:f4d74a8cad43 | 158 | } PACKED; |
sunsmile2015 | 0:3dc6e424dba0 | 159 | |
electronichamsters | 8:46c5e0bfab05 | 160 | |
electronichamsters | 8:46c5e0bfab05 | 161 | |
electronichamsters | 8:46c5e0bfab05 | 162 | void debounce_Callback(void) |
electronichamsters | 8:46c5e0bfab05 | 163 | { |
electronichamsters | 12:9bb01e063498 | 164 | Tic_Debounce.detach(); |
electronichamsters | 12:9bb01e063498 | 165 | Flag_Update_IO = true; //start advertising |
electronichamsters | 8:46c5e0bfab05 | 166 | /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access |
electronichamsters | 8:46c5e0bfab05 | 167 | * BLE device API from the main thread. */ |
electronichamsters | 12:9bb01e063498 | 168 | |
electronichamsters | 8:46c5e0bfab05 | 169 | } |
electronichamsters | 8:46c5e0bfab05 | 170 | |
electronichamsters | 12:9bb01e063498 | 171 | //ISR for I/O interrupt |
electronichamsters | 8:46c5e0bfab05 | 172 | void buttonPressedCallback(void) |
sunsmile2015 | 2:b935358da5ba | 173 | { |
electronichamsters | 12:9bb01e063498 | 174 | Tic_Debounce.attach(debounce_Callback, 1); //ok to attach multiple times, recent one wins |
electronichamsters | 8:46c5e0bfab05 | 175 | } |
rgrover1 | 5:f4d74a8cad43 | 176 | |
electronichamsters | 12:9bb01e063498 | 177 | //ISR for I/O interrupt |
electronichamsters | 8:46c5e0bfab05 | 178 | void buttonReleasedCallback(void) |
electronichamsters | 8:46c5e0bfab05 | 179 | { |
electronichamsters | 8:46c5e0bfab05 | 180 | |
electronichamsters | 12:9bb01e063498 | 181 | Tic_Debounce.attach(debounce_Callback, 1); |
electronichamsters | 8:46c5e0bfab05 | 182 | } |
electronichamsters | 8:46c5e0bfab05 | 183 | |
electronichamsters | 8:46c5e0bfab05 | 184 | |
electronichamsters | 8:46c5e0bfab05 | 185 | void stop_adv_Callback(void) |
electronichamsters | 8:46c5e0bfab05 | 186 | { |
electronichamsters | 8:46c5e0bfab05 | 187 | //stops advertising after X seconds |
electronichamsters | 8:46c5e0bfab05 | 188 | /* Note that the Callback() executes in interrupt context, so it is safer to do |
rgrover1 | 5:f4d74a8cad43 | 189 | * heavy-weight sensor polling from the main thread (where we should be able to block safely, if needed). */ |
electronichamsters | 12:9bb01e063498 | 190 | Flag_Detach_Adv_Tic = true; |
electronichamsters | 12:9bb01e063498 | 191 | |
electronichamsters | 12:9bb01e063498 | 192 | } |
electronichamsters | 8:46c5e0bfab05 | 193 | |
electronichamsters | 12:9bb01e063498 | 194 | /* **************************************** |
electronichamsters | 12:9bb01e063498 | 195 | * Decides what actions need to be performed on periodic basis |
electronichamsters | 12:9bb01e063498 | 196 | *******************************************/ |
electronichamsters | 12:9bb01e063498 | 197 | void periodic_Callback(void) |
electronichamsters | 12:9bb01e063498 | 198 | { |
electronichamsters | 12:9bb01e063498 | 199 | Flag_Update_IO = true; |
electronichamsters | 12:9bb01e063498 | 200 | Flag_Periodic_Call = true; |
electronichamsters | 8:46c5e0bfab05 | 201 | } |
electronichamsters | 8:46c5e0bfab05 | 202 | |
electronichamsters | 8:46c5e0bfab05 | 203 | |
electronichamsters | 12:9bb01e063498 | 204 | /* **************************************** |
electronichamsters | 12:9bb01e063498 | 205 | * No RTC available, tickers only have a 35 minute range. |
electronichamsters | 12:9bb01e063498 | 206 | * So periodicity for spoof avoidance is set to 30 minutes |
electronichamsters | 12:9bb01e063498 | 207 | *******************************************/ |
electronichamsters | 10:79e8f92ef156 | 208 | void clock_reset_Callback(void) |
electronichamsters | 10:79e8f92ef156 | 209 | { |
electronichamsters | 10:79e8f92ef156 | 210 | #if MyDebugEnb |
electronichamsters | 10:79e8f92ef156 | 211 | device.printf("===== reset timer ====="); |
electronichamsters | 10:79e8f92ef156 | 212 | device.printf("\r\n"); |
electronichamsters | 10:79e8f92ef156 | 213 | #endif |
electronichamsters | 12:9bb01e063498 | 214 | Tmr_From_Birthday.reset(); |
electronichamsters | 10:79e8f92ef156 | 215 | }; |
electronichamsters | 10:79e8f92ef156 | 216 | |
electronichamsters | 12:9bb01e063498 | 217 | |
rgrover1 | 5:f4d74a8cad43 | 218 | void setupApplicationData(ApplicationData_t &appData) |
sunsmile2015 | 4:e5fa4c8838db | 219 | { |
electronichamsters | 8:46c5e0bfab05 | 220 | // two byte ID: 0xFEFE |
electronichamsters | 8:46c5e0bfab05 | 221 | static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE; //2 byte application ID |
andresag | 7:0a8bbb6dea16 | 222 | |
sunsmile2015 | 4:e5fa4c8838db | 223 | appData.applicationSpecificId = APP_SPECIFIC_ID_TEST; |
sunsmile2015 | 0:3dc6e424dba0 | 224 | } |
sunsmile2015 | 0:3dc6e424dba0 | 225 | |
electronichamsters | 8:46c5e0bfab05 | 226 | |
electronichamsters | 8:46c5e0bfab05 | 227 | |
andresag | 7:0a8bbb6dea16 | 228 | /** |
andresag | 7:0a8bbb6dea16 | 229 | * This function is called when the ble initialization process has failled |
andresag | 7:0a8bbb6dea16 | 230 | */ |
andresag | 7:0a8bbb6dea16 | 231 | void onBleInitError(BLE &ble, ble_error_t error) |
andresag | 7:0a8bbb6dea16 | 232 | { |
andresag | 7:0a8bbb6dea16 | 233 | /* Initialization error handling should go here */ |
andresag | 7:0a8bbb6dea16 | 234 | } |
andresag | 7:0a8bbb6dea16 | 235 | |
electronichamsters | 12:9bb01e063498 | 236 | |
electronichamsters | 12:9bb01e063498 | 237 | |
andresag | 7:0a8bbb6dea16 | 238 | /** |
andresag | 7:0a8bbb6dea16 | 239 | * Callback triggered when the ble initialization process has finished |
andresag | 7:0a8bbb6dea16 | 240 | */ |
andresag | 7:0a8bbb6dea16 | 241 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
sunsmile2015 | 2:b935358da5ba | 242 | { |
andresag | 7:0a8bbb6dea16 | 243 | BLE& ble = params->ble; |
andresag | 7:0a8bbb6dea16 | 244 | ble_error_t error = params->error; |
andresag | 7:0a8bbb6dea16 | 245 | |
andresag | 7:0a8bbb6dea16 | 246 | if (error != BLE_ERROR_NONE) { |
andresag | 7:0a8bbb6dea16 | 247 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 7:0a8bbb6dea16 | 248 | onBleInitError(ble, error); |
andresag | 7:0a8bbb6dea16 | 249 | return; |
andresag | 7:0a8bbb6dea16 | 250 | } |
andresag | 7:0a8bbb6dea16 | 251 | |
andresag | 7:0a8bbb6dea16 | 252 | /* Ensure that it is the default instance of BLE */ |
andresag | 7:0a8bbb6dea16 | 253 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 7:0a8bbb6dea16 | 254 | return; |
andresag | 7:0a8bbb6dea16 | 255 | } |
electronichamsters | 8:46c5e0bfab05 | 256 | |
electronichamsters | 8:46c5e0bfab05 | 257 | /* Set device name characteristic data */ |
electronichamsters | 8:46c5e0bfab05 | 258 | ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME); |
andresag | 7:0a8bbb6dea16 | 259 | |
rgrover1 | 5:f4d74a8cad43 | 260 | /* Setup advertising payload */ |
electronichamsters | 8:46c5e0bfab05 | 261 | //set modes "no EDR", "discoverable" for beacon type advertisements |
rgrover1 | 5:f4d74a8cad43 | 262 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
electronichamsters | 8:46c5e0bfab05 | 263 | |
electronichamsters | 11:4f925834167d | 264 | |
electronichamsters | 8:46c5e0bfab05 | 265 | //from GAP example |
electronichamsters | 8:46c5e0bfab05 | 266 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
electronichamsters | 8:46c5e0bfab05 | 267 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); |
electronichamsters | 8:46c5e0bfab05 | 268 | |
electronichamsters | 8:46c5e0bfab05 | 269 | /* Setup advertising parameters: not connectable */ |
electronichamsters | 8:46c5e0bfab05 | 270 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
electronichamsters | 12:9bb01e063498 | 271 | ble.gap().setAdvertisingInterval(900); //one advertisment every 300ms. Self tickers, so you don't have to worry. |
electronichamsters | 8:46c5e0bfab05 | 272 | |
electronichamsters | 12:9bb01e063498 | 273 | } |
electronichamsters | 8:46c5e0bfab05 | 274 | |
electronichamsters | 8:46c5e0bfab05 | 275 | |
electronichamsters | 11:4f925834167d | 276 | //not needed anymore |
electronichamsters | 8:46c5e0bfab05 | 277 | void my_analogin_init(void) |
electronichamsters | 8:46c5e0bfab05 | 278 | { |
electronichamsters | 10:79e8f92ef156 | 279 | |
electronichamsters | 8:46c5e0bfab05 | 280 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 281 | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 282 | //(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 283 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 284 | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 285 | //(ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos) | |
electronichamsters | 8:46c5e0bfab05 | 286 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
electronichamsters | 10:79e8f92ef156 | 287 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
electronichamsters | 8:46c5e0bfab05 | 288 | } |
sunsmile2015 | 2:b935358da5ba | 289 | |
electronichamsters | 12:9bb01e063498 | 290 | |
electronichamsters | 12:9bb01e063498 | 291 | /* **************************************** |
electronichamsters | 12:9bb01e063498 | 292 | * Read battery voltage using bandgap reference |
electronichamsters | 12:9bb01e063498 | 293 | * shunt Vdd to ADC, thanks to Marcelo Salazar's notes here: |
electronichamsters | 12:9bb01e063498 | 294 | * https://developer.mbed.org/users/MarceloSalazar/notebook/measuring-battery-voltage-with-nordic-nrf51x/ |
electronichamsters | 12:9bb01e063498 | 295 | *******************************************/ |
electronichamsters | 12:9bb01e063498 | 296 | uint16_t read_bat_volt(void) |
electronichamsters | 8:46c5e0bfab05 | 297 | { |
electronichamsters | 10:79e8f92ef156 | 298 | //10 bit resolution, route Vdd as analog input, set ADC ref to VBG band gap |
electronichamsters | 10:79e8f92ef156 | 299 | //disable analog pin select "PSEL" because we're using Vdd as analog input |
electronichamsters | 10:79e8f92ef156 | 300 | //no external voltage reference |
electronichamsters | 10:79e8f92ef156 | 301 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
electronichamsters | 10:79e8f92ef156 | 302 | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 303 | //(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 304 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 305 | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 306 | //(ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 307 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
electronichamsters | 10:79e8f92ef156 | 308 | |
electronichamsters | 10:79e8f92ef156 | 309 | //NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
electronichamsters | 10:79e8f92ef156 | 310 | //NRF_ADC->CONFIG |= ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos; |
electronichamsters | 10:79e8f92ef156 | 311 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
electronichamsters | 10:79e8f92ef156 | 312 | NRF_ADC->TASKS_START = 1; |
electronichamsters | 10:79e8f92ef156 | 313 | |
electronichamsters | 10:79e8f92ef156 | 314 | |
electronichamsters | 10:79e8f92ef156 | 315 | //while loop doesn't actually loop until reading comlete, use a wait. |
electronichamsters | 10:79e8f92ef156 | 316 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; |
electronichamsters | 12:9bb01e063498 | 317 | wait_ms(1); |
electronichamsters | 10:79e8f92ef156 | 318 | |
electronichamsters | 10:79e8f92ef156 | 319 | //save off RESULT before disabling. |
electronichamsters | 10:79e8f92ef156 | 320 | //uint16_t myresult = (uint16_t)NRF_ADC->RESULT; |
electronichamsters | 10:79e8f92ef156 | 321 | |
electronichamsters | 10:79e8f92ef156 | 322 | //disable ADC to lower bat consumption |
electronichamsters | 10:79e8f92ef156 | 323 | NRF_ADC->TASKS_STOP = 1; |
electronichamsters | 10:79e8f92ef156 | 324 | //NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption |
electronichamsters | 10:79e8f92ef156 | 325 | |
electronichamsters | 10:79e8f92ef156 | 326 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
electronichamsters | 10:79e8f92ef156 | 327 | //return myresult; |
electronichamsters | 12:9bb01e063498 | 328 | } //end read_bat_volt |
electronichamsters | 12:9bb01e063498 | 329 | |
electronichamsters | 12:9bb01e063498 | 330 | |
electronichamsters | 10:79e8f92ef156 | 331 | |
electronichamsters | 12:9bb01e063498 | 332 | /* **************************************** |
electronichamsters | 12:9bb01e063498 | 333 | * Read battery voltage using bandgap reference |
electronichamsters | 12:9bb01e063498 | 334 | * shunt analog pin to ADC, from API here |
electronichamsters | 12:9bb01e063498 | 335 | * https://developer.mbed.org/users/mbed_official/code/mbed-src/file/cb4253f91ada/targets/hal/TARGET_NORDIC/TARGET_NRF51822/analogin_api.c |
electronichamsters | 12:9bb01e063498 | 336 | *******************************************/ |
electronichamsters | 12:9bb01e063498 | 337 | uint16_t read_ADC_pin(void) |
electronichamsters | 10:79e8f92ef156 | 338 | { |
electronichamsters | 10:79e8f92ef156 | 339 | |
electronichamsters | 10:79e8f92ef156 | 340 | //10 bit resolution, route PSEL pin as 1/3 input sel, |
electronichamsters | 10:79e8f92ef156 | 341 | //set ADC ref to VBG band gap |
electronichamsters | 10:79e8f92ef156 | 342 | //set AnalogInput4 as input pin (this is P0.03) |
electronichamsters | 10:79e8f92ef156 | 343 | //no external voltage reference |
electronichamsters | 10:79e8f92ef156 | 344 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
electronichamsters | 10:79e8f92ef156 | 345 | //(ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 346 | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 347 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 348 | //ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 349 | (ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos) | |
electronichamsters | 10:79e8f92ef156 | 350 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
electronichamsters | 10:79e8f92ef156 | 351 | //set pin select to AnalogInput4 = pin 7 = p0.03 = AIN4 |
electronichamsters | 10:79e8f92ef156 | 352 | //NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
electronichamsters | 10:79e8f92ef156 | 353 | //NRF_ADC->CONFIG |= ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos; |
electronichamsters | 10:79e8f92ef156 | 354 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
electronichamsters | 8:46c5e0bfab05 | 355 | NRF_ADC->TASKS_START = 1; |
electronichamsters | 8:46c5e0bfab05 | 356 | |
electronichamsters | 8:46c5e0bfab05 | 357 | |
electronichamsters | 8:46c5e0bfab05 | 358 | //while loop doesn't actually loop until reading comlete, use a wait. |
electronichamsters | 8:46c5e0bfab05 | 359 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}; |
electronichamsters | 12:9bb01e063498 | 360 | wait_ms(1); //needed because busy while loop doesn't run. |
electronichamsters | 8:46c5e0bfab05 | 361 | |
electronichamsters | 8:46c5e0bfab05 | 362 | //save off RESULT before disabling. |
electronichamsters | 8:46c5e0bfab05 | 363 | //uint16_t myresult = (uint16_t)NRF_ADC->RESULT; |
electronichamsters | 8:46c5e0bfab05 | 364 | |
electronichamsters | 8:46c5e0bfab05 | 365 | //disable ADC to lower bat consumption |
electronichamsters | 8:46c5e0bfab05 | 366 | //NRF_ADC->TASKS_STOP = 1; |
electronichamsters | 8:46c5e0bfab05 | 367 | //NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption |
electronichamsters | 8:46c5e0bfab05 | 368 | |
electronichamsters | 8:46c5e0bfab05 | 369 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
electronichamsters | 8:46c5e0bfab05 | 370 | //return myresult; |
electronichamsters | 12:9bb01e063498 | 371 | } //end read_ADC_pin |
sunsmile2015 | 4:e5fa4c8838db | 372 | |
electronichamsters | 10:79e8f92ef156 | 373 | |
electronichamsters | 12:9bb01e063498 | 374 | /* **************************************** |
electronichamsters | 12:9bb01e063498 | 375 | * Pattern scheme indicating "one of ours" |
electronichamsters | 12:9bb01e063498 | 376 | * generate first part of ADV data so that observer can recognize it as "one of ours". |
electronichamsters | 12:9bb01e063498 | 377 | * use specific schema to decide how we're recognizing our sensor ADV |
electronichamsters | 12:9bb01e063498 | 378 | *******************************************/ |
electronichamsters | 10:79e8f92ef156 | 379 | void hash_first_section(uint8_t * dest, const uint8_t * mac_addr, const char * bat_volt_str) |
electronichamsters | 10:79e8f92ef156 | 380 | { |
electronichamsters | 10:79e8f92ef156 | 381 | dest[0] = mac_addr[3]; |
electronichamsters | 10:79e8f92ef156 | 382 | dest[1] = mac_addr[2]; |
electronichamsters | 10:79e8f92ef156 | 383 | dest[2] = mac_addr[1]; |
electronichamsters | 10:79e8f92ef156 | 384 | dest[3] = mac_addr[0]; |
electronichamsters | 10:79e8f92ef156 | 385 | dest[4] = bat_volt_str[0]; |
electronichamsters | 10:79e8f92ef156 | 386 | dest[5] = bat_volt_str[1]; |
electronichamsters | 10:79e8f92ef156 | 387 | dest[6] = bat_volt_str[2]; |
electronichamsters | 10:79e8f92ef156 | 388 | dest[7] = bat_volt_str[3]; |
electronichamsters | 10:79e8f92ef156 | 389 | dest[8] = 0x10; |
electronichamsters | 10:79e8f92ef156 | 390 | dest[9] = 0x11; |
electronichamsters | 12:9bb01e063498 | 391 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 392 | |
electronichamsters | 12:9bb01e063498 | 393 | device.printf("hash array: "); |
electronichamsters | 12:9bb01e063498 | 394 | for (int i=0; i<10; i++) |
electronichamsters | 12:9bb01e063498 | 395 | { |
electronichamsters | 12:9bb01e063498 | 396 | device.printf("%x ", dest[i]); |
electronichamsters | 12:9bb01e063498 | 397 | } |
electronichamsters | 12:9bb01e063498 | 398 | device.printf("\r\n"); |
electronichamsters | 12:9bb01e063498 | 399 | #endif |
electronichamsters | 10:79e8f92ef156 | 400 | } |
electronichamsters | 10:79e8f92ef156 | 401 | |
electronichamsters | 10:79e8f92ef156 | 402 | |
electronichamsters | 12:9bb01e063498 | 403 | /* **************************************** |
electronichamsters | 12:9bb01e063498 | 404 | * |
electronichamsters | 12:9bb01e063498 | 405 | * Main Loop |
electronichamsters | 12:9bb01e063498 | 406 | * |
electronichamsters | 12:9bb01e063498 | 407 | *******************************************/ |
sunsmile2015 | 2:b935358da5ba | 408 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 409 | { |
electronichamsters | 8:46c5e0bfab05 | 410 | |
electronichamsters | 12:9bb01e063498 | 411 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 412 | device.baud(9600); |
electronichamsters | 12:9bb01e063498 | 413 | device.printf("started sensor node 36 "); |
electronichamsters | 12:9bb01e063498 | 414 | device.printf("\r\n"); |
electronichamsters | 12:9bb01e063498 | 415 | #endif |
electronichamsters | 8:46c5e0bfab05 | 416 | |
electronichamsters | 8:46c5e0bfab05 | 417 | |
electronichamsters | 12:9bb01e063498 | 418 | Tmr_From_Birthday.start(); //tracks # sec since birthday |
electronichamsters | 8:46c5e0bfab05 | 419 | |
sunsmile2015 | 0:3dc6e424dba0 | 420 | |
andresag | 7:0a8bbb6dea16 | 421 | BLE &ble = BLE::Instance(); |
andresag | 7:0a8bbb6dea16 | 422 | ble.init(bleInitComplete); |
electronichamsters | 8:46c5e0bfab05 | 423 | |
electronichamsters | 8:46c5e0bfab05 | 424 | float bat_reading; //hold battery voltage reading (Vbg/Vcc) |
electronichamsters | 8:46c5e0bfab05 | 425 | |
electronichamsters | 8:46c5e0bfab05 | 426 | my_analogin_init();//routes band-gap to analog input |
andresag | 7:0a8bbb6dea16 | 427 | |
andresag | 7:0a8bbb6dea16 | 428 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 7:0a8bbb6dea16 | 429 | * BLE object is used in the main loop below. */ |
electronichamsters | 8:46c5e0bfab05 | 430 | while (ble.hasInitialized() == false) { /* spin loop */ } |
electronichamsters | 8:46c5e0bfab05 | 431 | |
electronichamsters | 8:46c5e0bfab05 | 432 | //every X seconds, sends period update, up to 1800 (30 minutes) |
electronichamsters | 12:9bb01e063498 | 433 | Tic_Periodic.attach(periodic_Callback, Periodic_Update_Seconds); //send updated I/O every x seconds |
electronichamsters | 12:9bb01e063498 | 434 | Tic_Birthday.attach(clock_reset_Callback, Periodicity); //clock algorithm periodicity |
rgrover1 | 5:f4d74a8cad43 | 435 | |
electronichamsters | 10:79e8f92ef156 | 436 | |
electronichamsters | 12:9bb01e063498 | 437 | ble.getAddress(0,mac_reverse); //last byte of MAC (as shown on phone app) is at mac[0], not mac[6]; |
electronichamsters | 12:9bb01e063498 | 438 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 439 | device.printf("mac = "); |
electronichamsters | 12:9bb01e063498 | 440 | for (int i=0; i<6; i++) //prints out MAC address in reverse order; opps. |
electronichamsters | 12:9bb01e063498 | 441 | { |
electronichamsters | 12:9bb01e063498 | 442 | device.printf("%x:", mac_reverse[i]); |
electronichamsters | 12:9bb01e063498 | 443 | } |
electronichamsters | 12:9bb01e063498 | 444 | device.printf("\r\n"); |
electronichamsters | 12:9bb01e063498 | 445 | #endif |
electronichamsters | 12:9bb01e063498 | 446 | while (true) |
electronichamsters | 12:9bb01e063498 | 447 | { //Main Loop |
electronichamsters | 12:9bb01e063498 | 448 | |
electronichamsters | 12:9bb01e063498 | 449 | uint16_t seconds_Old =(uint16_t)(Tmr_From_Birthday.read_ms()/1000); // 0-1800 seconds (30 minutes) |
electronichamsters | 12:9bb01e063498 | 450 | |
electronichamsters | 12:9bb01e063498 | 451 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 452 | device.printf("current time in seconds: %d \r\n", seconds_Old); |
electronichamsters | 12:9bb01e063498 | 453 | #endif |
electronichamsters | 12:9bb01e063498 | 454 | |
electronichamsters | 8:46c5e0bfab05 | 455 | //set both pins to pull-up, so they're not floating when we read state |
electronichamsters | 8:46c5e0bfab05 | 456 | button1.mode(PullUp); |
electronichamsters | 8:46c5e0bfab05 | 457 | button2.mode(PullUp); |
electronichamsters | 8:46c5e0bfab05 | 458 | |
electronichamsters | 8:46c5e0bfab05 | 459 | //expect either button1 or button2 is grounded, b/c using SPDT reed switch |
electronichamsters | 8:46c5e0bfab05 | 460 | //the "common" pin on the reed switch should be on GND |
electronichamsters | 8:46c5e0bfab05 | 461 | uint8_t button1_state = button1.read(); |
electronichamsters | 8:46c5e0bfab05 | 462 | uint8_t button2_state = button2.read(); |
electronichamsters | 8:46c5e0bfab05 | 463 | |
electronichamsters | 8:46c5e0bfab05 | 464 | |
electronichamsters | 8:46c5e0bfab05 | 465 | //let's just update the pins on every wake. Insurance against const drain. |
electronichamsters | 12:9bb01e063498 | 466 | //if state == 0, pin is grounded. Unset interrupt and float pin, set the other pin for ISR |
electronichamsters | 8:46c5e0bfab05 | 467 | if ( (button1_state == 0) && (button2_state == 1) ) |
electronichamsters | 8:46c5e0bfab05 | 468 | { |
electronichamsters | 8:46c5e0bfab05 | 469 | magnet_near = 1; |
electronichamsters | 8:46c5e0bfab05 | 470 | //button1.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 471 | button1.fall(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 472 | button1.rise(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 473 | button1.mode(PullNone); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 474 | |
electronichamsters | 8:46c5e0bfab05 | 475 | //button2.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 10:79e8f92ef156 | 476 | button2.fall(buttonReleasedCallback); //enable interrupt |
electronichamsters | 10:79e8f92ef156 | 477 | button2.rise(buttonReleasedCallback); //enable interrupt |
electronichamsters | 10:79e8f92ef156 | 478 | button2.mode(PullUp); //pull up on pin to get interrupt |
electronichamsters | 12:9bb01e063498 | 479 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 480 | device.printf("=== button 1! %d seconds=== \r\n", seconds_Old); |
electronichamsters | 12:9bb01e063498 | 481 | #endif |
electronichamsters | 12:9bb01e063498 | 482 | } //end if button2 |
electronichamsters | 8:46c5e0bfab05 | 483 | else if ( (button1_state == 1) && (button2_state == 0) ) //assume other pin is open circuit |
electronichamsters | 8:46c5e0bfab05 | 484 | { |
electronichamsters | 8:46c5e0bfab05 | 485 | magnet_near = 0; |
electronichamsters | 8:46c5e0bfab05 | 486 | //button1.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 10:79e8f92ef156 | 487 | button1.fall(buttonReleasedCallback); //enable interrupt |
electronichamsters | 10:79e8f92ef156 | 488 | button1.rise(buttonReleasedCallback); //enable interrupt |
electronichamsters | 10:79e8f92ef156 | 489 | button1.mode(PullUp); //pull up on pin to get interrupt |
electronichamsters | 8:46c5e0bfab05 | 490 | |
electronichamsters | 8:46c5e0bfab05 | 491 | //button2.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 492 | button2.fall(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 493 | button2.rise(NULL); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 494 | button2.mode(PullNone); //float pin to save battery |
electronichamsters | 12:9bb01e063498 | 495 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 496 | device.printf("=== button 2! === %d seconds\r\n", seconds_Old); |
electronichamsters | 12:9bb01e063498 | 497 | #endif |
electronichamsters | 12:9bb01e063498 | 498 | } //end if button1 |
electronichamsters | 8:46c5e0bfab05 | 499 | else //odd state, shouldn't happen, suck battery and pullup both pins |
electronichamsters | 8:46c5e0bfab05 | 500 | { |
electronichamsters | 8:46c5e0bfab05 | 501 | magnet_near = 2; |
electronichamsters | 8:46c5e0bfab05 | 502 | //AdvData[4] = 0x33; |
electronichamsters | 8:46c5e0bfab05 | 503 | //button1.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 504 | button1.fall(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 505 | button1.rise(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 506 | button1.mode(PullUp); //float pin to save battery |
electronichamsters | 8:46c5e0bfab05 | 507 | |
electronichamsters | 8:46c5e0bfab05 | 508 | //button2.disable_irq() //don't know if disables IRQ on port or pin |
electronichamsters | 8:46c5e0bfab05 | 509 | button2.fall(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 510 | button2.rise(buttonReleasedCallback); //disable interrupt |
electronichamsters | 8:46c5e0bfab05 | 511 | button2.mode(PullUp); //float pin to save battery |
electronichamsters | 12:9bb01e063498 | 512 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 513 | device.printf("no buttons!! %d seconds\r\n", seconds_Old); |
electronichamsters | 12:9bb01e063498 | 514 | #endif |
electronichamsters | 12:9bb01e063498 | 515 | } //end odd state |
electronichamsters | 8:46c5e0bfab05 | 516 | |
electronichamsters | 8:46c5e0bfab05 | 517 | |
electronichamsters | 12:9bb01e063498 | 518 | if (Flag_Update_IO) { |
andresag | 7:0a8bbb6dea16 | 519 | /* Do blocking calls or whatever hardware-specific action is |
andresag | 7:0a8bbb6dea16 | 520 | * necessary to poll the sensor. */ |
electronichamsters | 8:46c5e0bfab05 | 521 | |
electronichamsters | 12:9bb01e063498 | 522 | //call attach again on periodic update to reset ticker |
electronichamsters | 12:9bb01e063498 | 523 | //next periodic updates happens Perioidc_Update_Seconds after I/O events |
electronichamsters | 12:9bb01e063498 | 524 | Tic_Periodic.attach(periodic_Callback, Periodic_Update_Seconds); |
electronichamsters | 10:79e8f92ef156 | 525 | Xmit_Cnt++; //increment transmit counter when updating I/O |
electronichamsters | 10:79e8f92ef156 | 526 | |
electronichamsters | 10:79e8f92ef156 | 527 | |
electronichamsters | 12:9bb01e063498 | 528 | //read and convert battery voltage |
electronichamsters | 12:9bb01e063498 | 529 | bat_reading = (float)read_bat_volt(); |
electronichamsters | 8:46c5e0bfab05 | 530 | bat_reading = (bat_reading * 3.6) / 1024.0; |
electronichamsters | 12:9bb01e063498 | 531 | #if MyDebugEnb |
electronichamsters | 8:46c5e0bfab05 | 532 | device.printf("bat reading: %f \r\n", bat_reading); |
electronichamsters | 12:9bb01e063498 | 533 | #endif |
electronichamsters | 12:9bb01e063498 | 534 | //write battery voltage |
electronichamsters | 12:9bb01e063498 | 535 | uint8_t total_chars; |
electronichamsters | 12:9bb01e063498 | 536 | memset(&bat_volt_char[0], 0, sizeof(bat_volt_char)); //clear out buffer |
electronichamsters | 12:9bb01e063498 | 537 | //convert battery voltage float value to string reprsentation to 2 decimal places, and save the size of string. |
electronichamsters | 12:9bb01e063498 | 538 | total_chars = sprintf (bat_volt_char, "%.2f", bat_reading); |
electronichamsters | 8:46c5e0bfab05 | 539 | |
electronichamsters | 12:9bb01e063498 | 540 | |
electronichamsters | 12:9bb01e063498 | 541 | //read and convert analog voltage. Comment out this section if note needed, saves some battery |
electronichamsters | 10:79e8f92ef156 | 542 | NRF_ADC->TASKS_STOP = 1; |
electronichamsters | 10:79e8f92ef156 | 543 | float analogreading; |
electronichamsters | 12:9bb01e063498 | 544 | analogreading = (float)read_ADC_pin(); |
electronichamsters | 10:79e8f92ef156 | 545 | analogreading = (analogreading * 3.6) / 1024.0; |
electronichamsters | 10:79e8f92ef156 | 546 | #if MyDebugEnb |
electronichamsters | 10:79e8f92ef156 | 547 | device.printf("separate analog reading: %.02f \r\n", analogreading); |
electronichamsters | 10:79e8f92ef156 | 548 | #endif |
electronichamsters | 10:79e8f92ef156 | 549 | |
electronichamsters | 12:9bb01e063498 | 550 | //disable ADC to save power |
electronichamsters | 8:46c5e0bfab05 | 551 | NRF_ADC->TASKS_STOP = 1; |
electronichamsters | 8:46c5e0bfab05 | 552 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; //disable to shutdown ADC & lower bat consumption |
electronichamsters | 8:46c5e0bfab05 | 553 | |
electronichamsters | 10:79e8f92ef156 | 554 | |
electronichamsters | 12:9bb01e063498 | 555 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 556 | device.printf("char buff: %c%c%c%c \r\n", bat_volt_char[0], bat_volt_char[1], bat_volt_char[2], bat_volt_char[3]); |
electronichamsters | 12:9bb01e063498 | 557 | device.printf("num chars: %d \r\n", total_chars); |
electronichamsters | 12:9bb01e063498 | 558 | #endif |
electronichamsters | 8:46c5e0bfab05 | 559 | |
electronichamsters | 8:46c5e0bfab05 | 560 | |
electronichamsters | 12:9bb01e063498 | 561 | //Generate "First Section" for ADV_Data so gateway will recognize our advertisement pattern |
electronichamsters | 12:9bb01e063498 | 562 | hash_first_section(Adv_First_Section, mac_reverse, bat_volt_char); |
electronichamsters | 8:46c5e0bfab05 | 563 | |
electronichamsters | 10:79e8f92ef156 | 564 | |
electronichamsters | 12:9bb01e063498 | 565 | /* **************************************** |
electronichamsters | 12:9bb01e063498 | 566 | * start writing out ADVData array |
electronichamsters | 12:9bb01e063498 | 567 | * todo: this is easy to write but hard to read. Maybe make it easy to read and hard to write? |
electronichamsters | 12:9bb01e063498 | 568 | ******************************************/ |
electronichamsters | 10:79e8f92ef156 | 569 | memset(&AdvData[0], 0, sizeof(AdvData)); |
electronichamsters | 10:79e8f92ef156 | 570 | uint8_t JSON_loc=0; //AdvData[0] |
electronichamsters | 10:79e8f92ef156 | 571 | |
electronichamsters | 12:9bb01e063498 | 572 | AdvData[0] = Adv_First_Section[0]; //"our device" flag, MAC[3] |
electronichamsters | 12:9bb01e063498 | 573 | JSON_loc++; //JSON_loc == 1 |
electronichamsters | 12:9bb01e063498 | 574 | AdvData[1] = Adv_First_Section[1]; //"out device" flag, MAC[2]... |
electronichamsters | 12:9bb01e063498 | 575 | JSON_loc++; //JSON_loc == 2 |
electronichamsters | 12:9bb01e063498 | 576 | AdvData[2] = Adv_First_Section[2]; |
electronichamsters | 12:9bb01e063498 | 577 | JSON_loc++; //JSON_loc == 3 |
electronichamsters | 12:9bb01e063498 | 578 | AdvData[3] = Adv_First_Section[3]; |
electronichamsters | 12:9bb01e063498 | 579 | JSON_loc++; //JSON_loc == 4 |
electronichamsters | 12:9bb01e063498 | 580 | AdvData[4] = Adv_First_Section[4]; |
electronichamsters | 12:9bb01e063498 | 581 | JSON_loc++; //JSON_loc == 5 |
electronichamsters | 12:9bb01e063498 | 582 | AdvData[5] = Adv_First_Section[5]; |
electronichamsters | 12:9bb01e063498 | 583 | JSON_loc++; //JSON_loc == 6 |
electronichamsters | 12:9bb01e063498 | 584 | AdvData[6] = Adv_First_Section[6]; |
electronichamsters | 10:79e8f92ef156 | 585 | JSON_loc++; |
electronichamsters | 12:9bb01e063498 | 586 | AdvData[7] = Adv_First_Section[7]; |
electronichamsters | 10:79e8f92ef156 | 587 | JSON_loc++; |
electronichamsters | 12:9bb01e063498 | 588 | AdvData[8] = Adv_First_Section[8]; |
electronichamsters | 10:79e8f92ef156 | 589 | JSON_loc++; |
electronichamsters | 12:9bb01e063498 | 590 | AdvData[9] = Adv_First_Section[9]; |
electronichamsters | 8:46c5e0bfab05 | 591 | JSON_loc++; |
electronichamsters | 10:79e8f92ef156 | 592 | |
electronichamsters | 12:9bb01e063498 | 593 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 594 | device.printf("ADV first 10 array: "); |
electronichamsters | 12:9bb01e063498 | 595 | for (int i=0; i<10; i++) |
electronichamsters | 12:9bb01e063498 | 596 | { |
electronichamsters | 12:9bb01e063498 | 597 | device.printf("%x ", AdvData[i]); |
electronichamsters | 12:9bb01e063498 | 598 | } |
electronichamsters | 12:9bb01e063498 | 599 | device.printf("\r\n"); |
electronichamsters | 12:9bb01e063498 | 600 | #endif |
electronichamsters | 10:79e8f92ef156 | 601 | |
electronichamsters | 10:79e8f92ef156 | 602 | |
electronichamsters | 10:79e8f92ef156 | 603 | JSON_loc = 10; |
electronichamsters | 12:9bb01e063498 | 604 | //Start of encrypted user data |
electronichamsters | 12:9bb01e063498 | 605 | |
electronichamsters | 12:9bb01e063498 | 606 | //[10] and [11] hold 2 bytes for how many seconds since birthday, little endian |
electronichamsters | 12:9bb01e063498 | 607 | AdvData[10] = seconds_Old & 0xFF; |
electronichamsters | 8:46c5e0bfab05 | 608 | JSON_loc++; |
electronichamsters | 12:9bb01e063498 | 609 | AdvData[11] = (seconds_Old >> 8) & 0xFF; |
electronichamsters | 10:79e8f92ef156 | 610 | JSON_loc++; |
electronichamsters | 12:9bb01e063498 | 611 | |
electronichamsters | 10:79e8f92ef156 | 612 | AdvData[12] = Xmit_Cnt; |
electronichamsters | 8:46c5e0bfab05 | 613 | JSON_loc++; |
electronichamsters | 12:9bb01e063498 | 614 | |
electronichamsters | 10:79e8f92ef156 | 615 | //start of jason data |
electronichamsters | 12:9bb01e063498 | 616 | //"mag": |
electronichamsters | 10:79e8f92ef156 | 617 | JSON_loc = 13; |
electronichamsters | 12:9bb01e063498 | 618 | AdvData[JSON_loc] = 0x22; //ADV_Data[13] = " |
electronichamsters | 10:79e8f92ef156 | 619 | JSON_loc++; //14 |
electronichamsters | 8:46c5e0bfab05 | 620 | |
electronichamsters | 12:9bb01e063498 | 621 | AdvData[JSON_loc] = 0x6d; //ADV_Data[14] = m |
electronichamsters | 10:79e8f92ef156 | 622 | JSON_loc++; //15 |
electronichamsters | 10:79e8f92ef156 | 623 | |
electronichamsters | 12:9bb01e063498 | 624 | AdvData[JSON_loc] = 0x61; //ADV_Data[15] = a |
electronichamsters | 10:79e8f92ef156 | 625 | JSON_loc++; //16 |
electronichamsters | 10:79e8f92ef156 | 626 | |
electronichamsters | 12:9bb01e063498 | 627 | AdvData[JSON_loc] = 0x67; //ADV_Data[16] = g |
electronichamsters | 10:79e8f92ef156 | 628 | JSON_loc++; //17 |
electronichamsters | 8:46c5e0bfab05 | 629 | |
electronichamsters | 12:9bb01e063498 | 630 | //for periodic calls, we want to add an extra mqtt level "p", using "/p" |
electronichamsters | 12:9bb01e063498 | 631 | //to delineate between MQTT publishes from real world I/O interrupts vs timer interrupts |
electronichamsters | 12:9bb01e063498 | 632 | if (Flag_Periodic_Call) |
electronichamsters | 8:46c5e0bfab05 | 633 | { |
electronichamsters | 12:9bb01e063498 | 634 | AdvData[JSON_loc] = 0x2f; // ADV_Data[17] = / |
electronichamsters | 12:9bb01e063498 | 635 | JSON_loc++; //18 |
electronichamsters | 12:9bb01e063498 | 636 | AdvData[JSON_loc] = 0x70; // ADV_Data[18] =p |
electronichamsters | 12:9bb01e063498 | 637 | JSON_loc++; //19 |
electronichamsters | 12:9bb01e063498 | 638 | } |
electronichamsters | 8:46c5e0bfab05 | 639 | |
electronichamsters | 12:9bb01e063498 | 640 | AdvData[JSON_loc] = 0x22; //ADV_Data[17 or 19] = " |
electronichamsters | 10:79e8f92ef156 | 641 | JSON_loc++; //20 |
electronichamsters | 8:46c5e0bfab05 | 642 | |
electronichamsters | 12:9bb01e063498 | 643 | AdvData[JSON_loc] = 0x3a; //ADV_Data[18 or 20] = : |
electronichamsters | 12:9bb01e063498 | 644 | JSON_loc++; //21 |
electronichamsters | 8:46c5e0bfab05 | 645 | |
electronichamsters | 12:9bb01e063498 | 646 | //convert magnet variable to string, for magnet sensor, this is easy |
electronichamsters | 12:9bb01e063498 | 647 | //since we only have 1 or 0, but this also works for analog values |
electronichamsters | 8:46c5e0bfab05 | 648 | memset(&buffer[0], 0, sizeof(buffer)); //clear out buffer |
electronichamsters | 10:79e8f92ef156 | 649 | total_chars = sprintf (buffer, "%d", magnet_near); //returns total number of characters, which is 1 character. |
electronichamsters | 8:46c5e0bfab05 | 650 | for (int i=0; i < total_chars; i++) |
electronichamsters | 8:46c5e0bfab05 | 651 | { |
electronichamsters | 8:46c5e0bfab05 | 652 | AdvData[JSON_loc] = buffer[i]; |
electronichamsters | 10:79e8f92ef156 | 653 | JSON_loc++; //23 |
electronichamsters | 8:46c5e0bfab05 | 654 | } //JSON_loc left at location of next character |
electronichamsters | 8:46c5e0bfab05 | 655 | |
electronichamsters | 12:9bb01e063498 | 656 | |
electronichamsters | 12:9bb01e063498 | 657 | //AdvData[JSON_loc] = 0x0; //since AdvData was cleared to start with, we don't need to null term |
electronichamsters | 8:46c5e0bfab05 | 658 | |
rgrover1 | 5:f4d74a8cad43 | 659 | ApplicationData_t appData; |
rgrover1 | 5:f4d74a8cad43 | 660 | setupApplicationData(appData); |
electronichamsters | 8:46c5e0bfab05 | 661 | |
electronichamsters | 12:9bb01e063498 | 662 | /********************* |
electronichamsters | 12:9bb01e063498 | 663 | * start encrypting last 16 bytes of ADV_Data |
electronichamsters | 12:9bb01e063498 | 664 | *********************/ |
electronichamsters | 8:46c5e0bfab05 | 665 | for (int i=0; i<16; i++) |
electronichamsters | 8:46c5e0bfab05 | 666 | { |
electronichamsters | 10:79e8f92ef156 | 667 | src_buf[i] = AdvData[i+10]; //start of encrypted section is at AdvData[10] |
electronichamsters | 8:46c5e0bfab05 | 668 | } |
electronichamsters | 8:46c5e0bfab05 | 669 | nrf_ecb_init(); |
electronichamsters | 8:46c5e0bfab05 | 670 | nrf_ecb_set_key(key_buf); |
electronichamsters | 8:46c5e0bfab05 | 671 | bool successful_ecb = nrf_ecb_crypt(des_buf, src_buf); |
electronichamsters | 12:9bb01e063498 | 672 | #if MyDebugEnb |
electronichamsters | 12:9bb01e063498 | 673 | device.printf("success ecb = %d \r\n", successful_ecb); |
electronichamsters | 12:9bb01e063498 | 674 | device.printf("src_buf: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \r\n", src_buf[0], src_buf[1], src_buf[2], src_buf[3], src_buf[4], src_buf[5], src_buf[6], src_buf[7], src_buf[8], src_buf[9], src_buf[10], src_buf[11], src_buf[12], src_buf[13], src_buf[14], src_buf[15]); |
electronichamsters | 12:9bb01e063498 | 675 | device.printf("des_buf: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x \r\n", des_buf[0], des_buf[1], des_buf[2], des_buf[3], des_buf[4], des_buf[5], des_buf[6], des_buf[7], des_buf[8], des_buf[9], des_buf[10], des_buf[11], des_buf[12], des_buf[13], des_buf[14], des_buf[15]); |
electronichamsters | 12:9bb01e063498 | 676 | #endif |
electronichamsters | 12:9bb01e063498 | 677 | for (int i=0; i<16; i++) //replace last 16 bytes with encrypted 16 bytes |
electronichamsters | 8:46c5e0bfab05 | 678 | { |
electronichamsters | 10:79e8f92ef156 | 679 | AdvData[i+10] = des_buf[i]; |
electronichamsters | 8:46c5e0bfab05 | 680 | } |
electronichamsters | 8:46c5e0bfab05 | 681 | |
electronichamsters | 12:9bb01e063498 | 682 | //set payload for advertisement to our custom manufactured data. First 5 bytes is BLE standard, last 26 bytes is our array |
electronichamsters | 8:46c5e0bfab05 | 683 | //ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *) &appData, sizeof(ApplicationData_t)); |
electronichamsters | 8:46c5e0bfab05 | 684 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); |
electronichamsters | 8:46c5e0bfab05 | 685 | |
electronichamsters | 12:9bb01e063498 | 686 | Flag_Update_IO = false; |
electronichamsters | 12:9bb01e063498 | 687 | Flag_Periodic_Call = false; |
electronichamsters | 8:46c5e0bfab05 | 688 | |
electronichamsters | 8:46c5e0bfab05 | 689 | ble.gap().startAdvertising(); |
electronichamsters | 12:9bb01e063498 | 690 | Tic_Stop_Adv.attach(stop_adv_Callback, 3); /* trigger turn off advertisement after X seconds */ |
electronichamsters | 8:46c5e0bfab05 | 691 | |
electronichamsters | 12:9bb01e063498 | 692 | }//end Flag_Update_IO |
electronichamsters | 8:46c5e0bfab05 | 693 | |
electronichamsters | 8:46c5e0bfab05 | 694 | |
electronichamsters | 12:9bb01e063498 | 695 | if (Flag_Detach_Adv_Tic == true) //ticker callback flag to stop advertising |
electronichamsters | 8:46c5e0bfab05 | 696 | { |
electronichamsters | 8:46c5e0bfab05 | 697 | ble.gap().stopAdvertising(); //may be safer to execute BLE operations in main |
electronichamsters | 12:9bb01e063498 | 698 | Tic_Stop_Adv.detach(); |
electronichamsters | 12:9bb01e063498 | 699 | Flag_Detach_Adv_Tic = false; |
electronichamsters | 8:46c5e0bfab05 | 700 | } |
electronichamsters | 12:9bb01e063498 | 701 | |
electronichamsters | 8:46c5e0bfab05 | 702 | |
electronichamsters | 12:9bb01e063498 | 703 | ble.waitForEvent(); //sleeps until interrupt form ticker or I/O |
electronichamsters | 8:46c5e0bfab05 | 704 | }//end forever while |
electronichamsters | 8:46c5e0bfab05 | 705 | }//end main |