Simple BLE Radio tester and signal strength (RSSI) meter, using microbit radio lib.

Dependencies:   microbit

Committer:
dl7avf
Date:
Thu Jan 12 11:26:14 2017 +0000
Revision:
0:28e881c1eb79
BLE radio tester and signal strength meter, initial.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dl7avf 0:28e881c1eb79 1 /*
dl7avf 0:28e881c1eb79 2 The MIT License (MIT)
dl7avf 0:28e881c1eb79 3
dl7avf 0:28e881c1eb79 4 Copyright (c) 2016 British Broadcasting Corporation.
dl7avf 0:28e881c1eb79 5 This software is provided by Lancaster University by arrangement with the BBC.
dl7avf 0:28e881c1eb79 6
dl7avf 0:28e881c1eb79 7 Permission is hereby granted, free of charge, to any person obtaining a
dl7avf 0:28e881c1eb79 8 copy of this software and associated documentation files (the "Software"),
dl7avf 0:28e881c1eb79 9 to deal in the Software without restriction, including without limitation
dl7avf 0:28e881c1eb79 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
dl7avf 0:28e881c1eb79 11 and/or sell copies of the Software, and to permit persons to whom the
dl7avf 0:28e881c1eb79 12 Software is furnished to do so, subject to the following conditions:
dl7avf 0:28e881c1eb79 13
dl7avf 0:28e881c1eb79 14 The above copyright notice and this permission notice shall be included in
dl7avf 0:28e881c1eb79 15 all copies or substantial portions of the Software.
dl7avf 0:28e881c1eb79 16
dl7avf 0:28e881c1eb79 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dl7avf 0:28e881c1eb79 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dl7avf 0:28e881c1eb79 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
dl7avf 0:28e881c1eb79 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dl7avf 0:28e881c1eb79 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dl7avf 0:28e881c1eb79 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dl7avf 0:28e881c1eb79 23 DEALINGS IN THE SOFTWARE.
dl7avf 0:28e881c1eb79 24 */
dl7avf 0:28e881c1eb79 25
dl7avf 0:28e881c1eb79 26
dl7avf 0:28e881c1eb79 27 /*
dl7avf 0:28e881c1eb79 28 * This is a simple example that uses the micro:bit radio to show if another
dl7avf 0:28e881c1eb79 29 * micro:bit is nearby broadcasting with the same group number.
dl7avf 0:28e881c1eb79 30 * The display will show the received signal strength in -dBm.
dl7avf 0:28e881c1eb79 31 *
dl7avf 0:28e881c1eb79 32 */
dl7avf 0:28e881c1eb79 33
dl7avf 0:28e881c1eb79 34 #include "config.h"
dl7avf 0:28e881c1eb79 35 #include "MicroBit.h"
dl7avf 0:28e881c1eb79 36
dl7avf 0:28e881c1eb79 37 #if MICROBIT_BLE_ENABLED
dl7avf 0:28e881c1eb79 38 #ifdef YOTTA_CFG
dl7avf 0:28e881c1eb79 39 #error "This example needs BLE to be disabled. Use the yotta config.json in the proximit-heart directory to do this"
dl7avf 0:28e881c1eb79 40 #else
dl7avf 0:28e881c1eb79 41 #error "This example needs BLE to be disabled in the microbit config file in the microbit-dal: MicroBitConfig.h"
dl7avf 0:28e881c1eb79 42 #endif
dl7avf 0:28e881c1eb79 43 #endif
dl7avf 0:28e881c1eb79 44
dl7avf 0:28e881c1eb79 45 #ifdef USE_MICROBIT_ELEMENTS
dl7avf 0:28e881c1eb79 46
dl7avf 0:28e881c1eb79 47 // Used microbit components.
dl7avf 0:28e881c1eb79 48
dl7avf 0:28e881c1eb79 49 MicroBitDisplay display;
dl7avf 0:28e881c1eb79 50 MicroBitRadio radio;
dl7avf 0:28e881c1eb79 51 MicroBitMessageBus messageBus;
dl7avf 0:28e881c1eb79 52
dl7avf 0:28e881c1eb79 53 #else
dl7avf 0:28e881c1eb79 54
dl7avf 0:28e881c1eb79 55 // Complete microbit object.
dl7avf 0:28e881c1eb79 56
dl7avf 0:28e881c1eb79 57 MicroBit uBit;
dl7avf 0:28e881c1eb79 58
dl7avf 0:28e881c1eb79 59 #endif
dl7avf 0:28e881c1eb79 60
dl7avf 0:28e881c1eb79 61 static int display = 0, receiving = 0, direction = 1;
dl7avf 0:28e881c1eb79 62 static uint8_t radioRSSI = 98, radioGroup = radioGroupNumber;
dl7avf 0:28e881c1eb79 63 ManagedString rxdata;
dl7avf 0:28e881c1eb79 64 void onData(MicroBitEvent)
dl7avf 0:28e881c1eb79 65 {
dl7avf 0:28e881c1eb79 66 rxdata = uBit.radio.datagram.recv();
dl7avf 0:28e881c1eb79 67 receiving = 3;
dl7avf 0:28e881c1eb79 68 radioRSSI = uBit.radio.getRSSI();
dl7avf 0:28e881c1eb79 69 display = 1;
dl7avf 0:28e881c1eb79 70 }
dl7avf 0:28e881c1eb79 71
dl7avf 0:28e881c1eb79 72 void onButtonA(MicroBitEvent)
dl7avf 0:28e881c1eb79 73 {
dl7avf 0:28e881c1eb79 74 if ( radioGroup != radioGroupNumber )
dl7avf 0:28e881c1eb79 75 {
dl7avf 0:28e881c1eb79 76 radioGroup = radioGroupNumber;
dl7avf 0:28e881c1eb79 77 uBit.radio.setGroup(radioGroup);
dl7avf 0:28e881c1eb79 78 }
dl7avf 0:28e881c1eb79 79 direction = -direction;
dl7avf 0:28e881c1eb79 80 display = 2;
dl7avf 0:28e881c1eb79 81 }
dl7avf 0:28e881c1eb79 82
dl7avf 0:28e881c1eb79 83 void onButtonAlong(MicroBitEvent)
dl7avf 0:28e881c1eb79 84 {
dl7avf 0:28e881c1eb79 85 display = 3;
dl7avf 0:28e881c1eb79 86 }
dl7avf 0:28e881c1eb79 87
dl7avf 0:28e881c1eb79 88 void onButtonAhold(MicroBitEvent)
dl7avf 0:28e881c1eb79 89 {
dl7avf 0:28e881c1eb79 90 display = 3;
dl7avf 0:28e881c1eb79 91 }
dl7avf 0:28e881c1eb79 92
dl7avf 0:28e881c1eb79 93 void onButtonAdouble(MicroBitEvent)
dl7avf 0:28e881c1eb79 94 {
dl7avf 0:28e881c1eb79 95 display = 3;
dl7avf 0:28e881c1eb79 96 }
dl7avf 0:28e881c1eb79 97
dl7avf 0:28e881c1eb79 98 void onButtonBlong(MicroBitEvent)
dl7avf 0:28e881c1eb79 99 {
dl7avf 0:28e881c1eb79 100 display = 4;
dl7avf 0:28e881c1eb79 101 }
dl7avf 0:28e881c1eb79 102
dl7avf 0:28e881c1eb79 103 void onButtonB(MicroBitEvent)
dl7avf 0:28e881c1eb79 104 {
dl7avf 0:28e881c1eb79 105 radioGroup += direction;
dl7avf 0:28e881c1eb79 106 uBit.radio.setGroup(radioGroup);
dl7avf 0:28e881c1eb79 107 display = 2;
dl7avf 0:28e881c1eb79 108 }
dl7avf 0:28e881c1eb79 109
dl7avf 0:28e881c1eb79 110 int main()
dl7avf 0:28e881c1eb79 111 {
dl7avf 0:28e881c1eb79 112 int sleeping;
dl7avf 0:28e881c1eb79 113
dl7avf 0:28e881c1eb79 114 // Initialise the micro:bit runtime.
dl7avf 0:28e881c1eb79 115 uBit.init();
dl7avf 0:28e881c1eb79 116 uBit.radio.setGroup(radioGroupNumber);
dl7avf 0:28e881c1eb79 117 //Setup a handler to run when data is received.
dl7avf 0:28e881c1eb79 118 uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData);
dl7avf 0:28e881c1eb79 119 // Setup some button handlers to allow extra control with buttons.
dl7avf 0:28e881c1eb79 120 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
dl7avf 0:28e881c1eb79 121 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
dl7avf 0:28e881c1eb79 122 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_LONG_CLICK, onButtonAlong);
dl7avf 0:28e881c1eb79 123 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_LONG_CLICK, onButtonBlong);
dl7avf 0:28e881c1eb79 124 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_HOLD, onButtonAhold);
dl7avf 0:28e881c1eb79 125 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_DOUBLE_CLICK, onButtonAdouble);
dl7avf 0:28e881c1eb79 126 uBit.radio.enable();
dl7avf 0:28e881c1eb79 127
dl7avf 0:28e881c1eb79 128 while ( true )
dl7avf 0:28e881c1eb79 129 {
dl7avf 0:28e881c1eb79 130 switch ( display )
dl7avf 0:28e881c1eb79 131 {
dl7avf 0:28e881c1eb79 132 default:
dl7avf 0:28e881c1eb79 133 case 0: // nothing to display?
dl7avf 0:28e881c1eb79 134 uBit.display.print("?");
dl7avf 0:28e881c1eb79 135 sleeping = 1000;
dl7avf 0:28e881c1eb79 136 break;
dl7avf 0:28e881c1eb79 137
dl7avf 0:28e881c1eb79 138 case 1: // display signal strength.
dl7avf 0:28e881c1eb79 139 uBit.display.scroll(radioRSSI);
dl7avf 0:28e881c1eb79 140 sleeping = 500;
dl7avf 0:28e881c1eb79 141 break;
dl7avf 0:28e881c1eb79 142
dl7avf 0:28e881c1eb79 143 case 2: // display group number.
dl7avf 0:28e881c1eb79 144 uBit.display.print("#", 300);
dl7avf 0:28e881c1eb79 145 uBit.display.scroll(radioGroup);
dl7avf 0:28e881c1eb79 146 sleeping = 500;
dl7avf 0:28e881c1eb79 147 break;
dl7avf 0:28e881c1eb79 148
dl7avf 0:28e881c1eb79 149 case 3: // display received data.
dl7avf 0:28e881c1eb79 150 uBit.display.scroll(rxdata);
dl7avf 0:28e881c1eb79 151 sleeping = 500;
dl7avf 0:28e881c1eb79 152 break;
dl7avf 0:28e881c1eb79 153 }
dl7avf 0:28e881c1eb79 154
dl7avf 0:28e881c1eb79 155 if ( receiving )
dl7avf 0:28e881c1eb79 156 {
dl7avf 0:28e881c1eb79 157 receiving -= 1;
dl7avf 0:28e881c1eb79 158 display = receiving && display == 1 ? 1 : 0;
dl7avf 0:28e881c1eb79 159 }
dl7avf 0:28e881c1eb79 160 uBit.sleep(sleeping);
dl7avf 0:28e881c1eb79 161 }
dl7avf 0:28e881c1eb79 162 }