BLE UART example
Fork of Nucleo_BLE_BlueNRG by
utils/src/Utils.cpp@0:a948f5f3904c, 2014-12-19 (annotated)
- Committer:
- sjallouli
- Date:
- Fri Dec 19 18:56:07 2014 +0000
- Revision:
- 0:a948f5f3904c
BLE_BlueNRG for Nucleo board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:a948f5f3904c | 1 | /* mbed Microcontroller Library |
sjallouli | 0:a948f5f3904c | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:a948f5f3904c | 3 | * |
sjallouli | 0:a948f5f3904c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:a948f5f3904c | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:a948f5f3904c | 6 | * You may obtain a copy of the License at |
sjallouli | 0:a948f5f3904c | 7 | * |
sjallouli | 0:a948f5f3904c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:a948f5f3904c | 9 | * |
sjallouli | 0:a948f5f3904c | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:a948f5f3904c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:a948f5f3904c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:a948f5f3904c | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:a948f5f3904c | 14 | * limitations under the License. |
sjallouli | 0:a948f5f3904c | 15 | */ |
sjallouli | 0:a948f5f3904c | 16 | |
sjallouli | 0:a948f5f3904c | 17 | #include "Utils.h" |
sjallouli | 0:a948f5f3904c | 18 | |
sjallouli | 0:a948f5f3904c | 19 | #if NEED_CONSOLE_OUTPUT |
sjallouli | 0:a948f5f3904c | 20 | Serial pc(USBTX, USBRX); |
sjallouli | 0:a948f5f3904c | 21 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
sjallouli | 0:a948f5f3904c | 22 | |
sjallouli | 0:a948f5f3904c | 23 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 24 | /*! |
sjallouli | 0:a948f5f3904c | 25 | @brief sets values of EN_HIGH_POWER and PA_LEVEL corresponding to dBMLevel of tx power |
sjallouli | 0:a948f5f3904c | 26 | |
sjallouli | 0:a948f5f3904c | 27 | @returns value of tx power in dbm actually set |
sjallouli | 0:a948f5f3904c | 28 | |
sjallouli | 0:a948f5f3904c | 29 | @params[in] dBMLevel |
sjallouli | 0:a948f5f3904c | 30 | dBMLevel of tx power to be set |
sjallouli | 0:a948f5f3904c | 31 | |
sjallouli | 0:a948f5f3904c | 32 | @params[in] dBMLevel |
sjallouli | 0:a948f5f3904c | 33 | dBMLevel of tx power to be set |
sjallouli | 0:a948f5f3904c | 34 | |
sjallouli | 0:a948f5f3904c | 35 | @endcode |
sjallouli | 0:a948f5f3904c | 36 | */ |
sjallouli | 0:a948f5f3904c | 37 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 38 | double getHighPowerAndPALevelValue(int8_t dBMLevel, int8_t& EN_HIGH_POWER, int8_t& PA_LEVEL) { |
sjallouli | 0:a948f5f3904c | 39 | double dbm = (double) dBMLevel; |
sjallouli | 0:a948f5f3904c | 40 | if(dbm<-18.0) { |
sjallouli | 0:a948f5f3904c | 41 | dbm = -18; |
sjallouli | 0:a948f5f3904c | 42 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 43 | PA_LEVEL = 0; |
sjallouli | 0:a948f5f3904c | 44 | } |
sjallouli | 0:a948f5f3904c | 45 | else if(dbm>8.0) { |
sjallouli | 0:a948f5f3904c | 46 | dbm = 8; |
sjallouli | 0:a948f5f3904c | 47 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 48 | PA_LEVEL = 7; |
sjallouli | 0:a948f5f3904c | 49 | } |
sjallouli | 0:a948f5f3904c | 50 | |
sjallouli | 0:a948f5f3904c | 51 | // As a policy we are setting tx power level to the higher side |
sjallouli | 0:a948f5f3904c | 52 | if((dbm>-18.0) && (dbm<=-15)) { |
sjallouli | 0:a948f5f3904c | 53 | // set tx power to -15dBM |
sjallouli | 0:a948f5f3904c | 54 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 55 | PA_LEVEL = 0; |
sjallouli | 0:a948f5f3904c | 56 | } |
sjallouli | 0:a948f5f3904c | 57 | else if((dbm>-15) && (dbm<=-14.7)) { |
sjallouli | 0:a948f5f3904c | 58 | // set tx power to -14.7dBM |
sjallouli | 0:a948f5f3904c | 59 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 60 | PA_LEVEL = 1; |
sjallouli | 0:a948f5f3904c | 61 | } |
sjallouli | 0:a948f5f3904c | 62 | else if((dbm>-14.7) && (dbm<=-11.7)) { |
sjallouli | 0:a948f5f3904c | 63 | // set tx power to -11.7dBM |
sjallouli | 0:a948f5f3904c | 64 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 65 | PA_LEVEL = 1; |
sjallouli | 0:a948f5f3904c | 66 | } |
sjallouli | 0:a948f5f3904c | 67 | else if((dbm>-11.7) && (dbm<=-11.4)) { |
sjallouli | 0:a948f5f3904c | 68 | // set tx power to -11.4dBM |
sjallouli | 0:a948f5f3904c | 69 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 70 | PA_LEVEL = 2; |
sjallouli | 0:a948f5f3904c | 71 | } |
sjallouli | 0:a948f5f3904c | 72 | else if((dbm>-11.4) && (dbm<=-8.4)) { |
sjallouli | 0:a948f5f3904c | 73 | // set tx power to -8.4dBM |
sjallouli | 0:a948f5f3904c | 74 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 75 | PA_LEVEL = 2; |
sjallouli | 0:a948f5f3904c | 76 | } |
sjallouli | 0:a948f5f3904c | 77 | else if((dbm>-8.4) && (dbm<=-8.1)) { |
sjallouli | 0:a948f5f3904c | 78 | // set tx power to -8.1dBM |
sjallouli | 0:a948f5f3904c | 79 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 80 | PA_LEVEL = 3; |
sjallouli | 0:a948f5f3904c | 81 | } |
sjallouli | 0:a948f5f3904c | 82 | else if((dbm>-8.1) && (dbm<=-5.1)) { |
sjallouli | 0:a948f5f3904c | 83 | // set tx power to -5.1dBM |
sjallouli | 0:a948f5f3904c | 84 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 85 | PA_LEVEL = 3; |
sjallouli | 0:a948f5f3904c | 86 | } |
sjallouli | 0:a948f5f3904c | 87 | else if((dbm>-5.1) && (dbm<=-4.9)) { |
sjallouli | 0:a948f5f3904c | 88 | // set tx power to -4.9dBM |
sjallouli | 0:a948f5f3904c | 89 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 90 | PA_LEVEL = 4; |
sjallouli | 0:a948f5f3904c | 91 | } |
sjallouli | 0:a948f5f3904c | 92 | else if((dbm>-4.9) && (dbm<=-2.1)) { |
sjallouli | 0:a948f5f3904c | 93 | // set tx power to -2.1dBM |
sjallouli | 0:a948f5f3904c | 94 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 95 | PA_LEVEL = 4; |
sjallouli | 0:a948f5f3904c | 96 | } |
sjallouli | 0:a948f5f3904c | 97 | else if((dbm>-2.1) && (dbm<=-1.6)) { |
sjallouli | 0:a948f5f3904c | 98 | // set tx power to -1.6dBM |
sjallouli | 0:a948f5f3904c | 99 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 100 | PA_LEVEL = 5; |
sjallouli | 0:a948f5f3904c | 101 | } |
sjallouli | 0:a948f5f3904c | 102 | else if((dbm>-1.6) && (dbm<=1.4)) { |
sjallouli | 0:a948f5f3904c | 103 | // set tx power to -1.6dBM |
sjallouli | 0:a948f5f3904c | 104 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 105 | PA_LEVEL = 5; |
sjallouli | 0:a948f5f3904c | 106 | } |
sjallouli | 0:a948f5f3904c | 107 | else if((dbm>1.4) && (dbm<=1.7)) { |
sjallouli | 0:a948f5f3904c | 108 | // set tx power to 1.7dBM |
sjallouli | 0:a948f5f3904c | 109 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 110 | PA_LEVEL = 6; |
sjallouli | 0:a948f5f3904c | 111 | } |
sjallouli | 0:a948f5f3904c | 112 | else if((dbm>1.7) && (dbm<=4.7)) { |
sjallouli | 0:a948f5f3904c | 113 | // set tx power to 4.7dBM |
sjallouli | 0:a948f5f3904c | 114 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 115 | PA_LEVEL = 6; |
sjallouli | 0:a948f5f3904c | 116 | } |
sjallouli | 0:a948f5f3904c | 117 | else if((dbm>4.7) && (dbm<=5.0)) { |
sjallouli | 0:a948f5f3904c | 118 | // set tx power to 5.0dBM |
sjallouli | 0:a948f5f3904c | 119 | EN_HIGH_POWER = 0; |
sjallouli | 0:a948f5f3904c | 120 | PA_LEVEL = 7; |
sjallouli | 0:a948f5f3904c | 121 | } |
sjallouli | 0:a948f5f3904c | 122 | else if((dbm>5.0) && (dbm<=8)) { |
sjallouli | 0:a948f5f3904c | 123 | // set tx power to 8.0dBM |
sjallouli | 0:a948f5f3904c | 124 | EN_HIGH_POWER = 1; |
sjallouli | 0:a948f5f3904c | 125 | PA_LEVEL = 7; |
sjallouli | 0:a948f5f3904c | 126 | } |
sjallouli | 0:a948f5f3904c | 127 | |
sjallouli | 0:a948f5f3904c | 128 | return dbm; |
sjallouli | 0:a948f5f3904c | 129 | } |