Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

Committer:
wim
Date:
Sat Aug 20 12:49:44 2011 +0000
Revision:
2:1dab1089c332
First commit, testloop

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 2:1dab1089c332 1 /* STANAG Codes - LTD STANAG Codes
wim 2:1dab1089c332 2 * Copyright (c) 2011 Wim Huiskamp
wim 2:1dab1089c332 3 *
wim 2:1dab1089c332 4 * Released under the MIT License: http://mbed.org/license/mit
wim 2:1dab1089c332 5 *
wim 2:1dab1089c332 6 * version 0.2 Initial Release
wim 2:1dab1089c332 7 */
wim 2:1dab1089c332 8 #include "mbed.h"
wim 2:1dab1089c332 9 #include "STANAG_Codes.h"
wim 2:1dab1089c332 10
wim 2:1dab1089c332 11
wim 2:1dab1089c332 12 /** Create a STANAG Codes object
wim 2:1dab1089c332 13 *
wim 2:1dab1089c332 14 * @param none
wim 2:1dab1089c332 15 * @brief
wim 2:1dab1089c332 16 */
wim 2:1dab1089c332 17 STANAG_Codes::STANAG_Codes() {
wim 2:1dab1089c332 18
wim 2:1dab1089c332 19 _init();
wim 2:1dab1089c332 20 }
wim 2:1dab1089c332 21
wim 2:1dab1089c332 22 /** Init STANAG_Codes()
wim 2:1dab1089c332 23 * @param
wim 2:1dab1089c332 24 * @returns
wim 2:1dab1089c332 25 */
wim 2:1dab1089c332 26 void STANAG_Codes::_init(void) {
wim 2:1dab1089c332 27
wim 2:1dab1089c332 28 for (_codeIdx=0; _codeIdx < D_STANAG_CODES; _codeIdx++) {
wim 2:1dab1089c332 29
wim 2:1dab1089c332 30 for (_digitIdx=0; _digitIdx < D_STANAG_DIGITS; _digitIdx++) {
wim 2:1dab1089c332 31 _codes[_codeIdx][_digitIdx] = STANAG_DEFAULTS[_codeIdx][_digitIdx];
wim 2:1dab1089c332 32 }
wim 2:1dab1089c332 33 }
wim 2:1dab1089c332 34
wim 2:1dab1089c332 35 #if(0)
wim 2:1dab1089c332 36 for (_codeIdx=0; _codeIdx < D_STANAG_CODES; _codeIdx++) {
wim 2:1dab1089c332 37 _codes[_codeIdx] = STANAG_DEFAULTS[_codeIdx];
wim 2:1dab1089c332 38 }
wim 2:1dab1089c332 39 #endif
wim 2:1dab1089c332 40
wim 2:1dab1089c332 41 _codeIdx = 0;
wim 2:1dab1089c332 42 _digitIdx = 0;
wim 2:1dab1089c332 43 }
wim 2:1dab1089c332 44
wim 2:1dab1089c332 45
wim 2:1dab1089c332 46 /** Set the code Index of the selected STANAG Code
wim 2:1dab1089c332 47 * @param uint8_t codeIdx
wim 2:1dab1089c332 48 * @returns value of STANAG code Index
wim 2:1dab1089c332 49 */
wim 2:1dab1089c332 50 uint8_t STANAG_Codes::setCodeIdx(uint8_t codeIdx) {
wim 2:1dab1089c332 51
wim 2:1dab1089c332 52 //Check upper boundary
wim 2:1dab1089c332 53 if (codeIdx > D_STANAG_CODES-1) {
wim 2:1dab1089c332 54 _codeIdx = D_STANAG_CODES-1;
wim 2:1dab1089c332 55 }
wim 2:1dab1089c332 56 else {
wim 2:1dab1089c332 57 _codeIdx = codeIdx;
wim 2:1dab1089c332 58 };
wim 2:1dab1089c332 59
wim 2:1dab1089c332 60 return _codeIdx;
wim 2:1dab1089c332 61 }
wim 2:1dab1089c332 62
wim 2:1dab1089c332 63
wim 2:1dab1089c332 64 /** Get code Index of the selected STANAG Code
wim 2:1dab1089c332 65 * @param
wim 2:1dab1089c332 66 * @returns value of STANAG code Index
wim 2:1dab1089c332 67 */
wim 2:1dab1089c332 68 uint8_t STANAG_Codes::getCodeIdx() {
wim 2:1dab1089c332 69
wim 2:1dab1089c332 70 return _codeIdx;
wim 2:1dab1089c332 71 }
wim 2:1dab1089c332 72
wim 2:1dab1089c332 73
wim 2:1dab1089c332 74 /** Increment code Index of the selected STANAG Code
wim 2:1dab1089c332 75 * @param
wim 2:1dab1089c332 76 * @returns value of STANAG code Index
wim 2:1dab1089c332 77 */
wim 2:1dab1089c332 78 uint8_t STANAG_Codes::incCodeIdx() {
wim 2:1dab1089c332 79
wim 2:1dab1089c332 80 _codeIdx++;
wim 2:1dab1089c332 81
wim 2:1dab1089c332 82 //Check upper boundary and wrap around
wim 2:1dab1089c332 83 if (_codeIdx > D_STANAG_CODES-1) {
wim 2:1dab1089c332 84 _codeIdx = 0;
wim 2:1dab1089c332 85 };
wim 2:1dab1089c332 86
wim 2:1dab1089c332 87 return _codeIdx;
wim 2:1dab1089c332 88 }
wim 2:1dab1089c332 89
wim 2:1dab1089c332 90
wim 2:1dab1089c332 91 /** Set the digit index of the selected STANAG Code
wim 2:1dab1089c332 92 * @param uint8_t digitIdx
wim 2:1dab1089c332 93 * @returns value of the digit index
wim 2:1dab1089c332 94 */
wim 2:1dab1089c332 95 uint8_t STANAG_Codes::setDigitIdx(uint8_t digitIdx) {
wim 2:1dab1089c332 96
wim 2:1dab1089c332 97 //Check upper boundary
wim 2:1dab1089c332 98 if (digitIdx > D_STANAG_DIGITS-1) {
wim 2:1dab1089c332 99 _digitIdx = D_STANAG_DIGITS-1;
wim 2:1dab1089c332 100 }
wim 2:1dab1089c332 101 else {
wim 2:1dab1089c332 102 _digitIdx = digitIdx;
wim 2:1dab1089c332 103 };
wim 2:1dab1089c332 104
wim 2:1dab1089c332 105 return _digitIdx;
wim 2:1dab1089c332 106 }
wim 2:1dab1089c332 107
wim 2:1dab1089c332 108
wim 2:1dab1089c332 109 /** Get digit Index of the selected STANAG Code
wim 2:1dab1089c332 110 * @param
wim 2:1dab1089c332 111 * @returns value of digit Index
wim 2:1dab1089c332 112 */
wim 2:1dab1089c332 113 uint8_t STANAG_Codes::getDigitIdx() {
wim 2:1dab1089c332 114
wim 2:1dab1089c332 115 return _digitIdx;
wim 2:1dab1089c332 116 }
wim 2:1dab1089c332 117
wim 2:1dab1089c332 118
wim 2:1dab1089c332 119 /** Increment digit Index of the selected STANAG Code
wim 2:1dab1089c332 120 * @param
wim 2:1dab1089c332 121 * @returns value of digit Index
wim 2:1dab1089c332 122 */
wim 2:1dab1089c332 123 uint8_t STANAG_Codes::incDigitIdx() {
wim 2:1dab1089c332 124
wim 2:1dab1089c332 125 _digitIdx++;
wim 2:1dab1089c332 126
wim 2:1dab1089c332 127 //Check upper boundary and wrap around
wim 2:1dab1089c332 128 if (_digitIdx > D_STANAG_DIGITS-1) {
wim 2:1dab1089c332 129 _digitIdx = 0;
wim 2:1dab1089c332 130 };
wim 2:1dab1089c332 131
wim 2:1dab1089c332 132 return _digitIdx;
wim 2:1dab1089c332 133 }
wim 2:1dab1089c332 134
wim 2:1dab1089c332 135
wim 2:1dab1089c332 136 /** Increment Digit of the selected STANAG Code
wim 2:1dab1089c332 137 * @param
wim 2:1dab1089c332 138 * @returns value of Digit
wim 2:1dab1089c332 139 */
wim 2:1dab1089c332 140 uint8_t STANAG_Codes::incDigit() {
wim 2:1dab1089c332 141 uint8_t digit;
wim 2:1dab1089c332 142
wim 2:1dab1089c332 143 //Get digit value and increment
wim 2:1dab1089c332 144 digit = _codes[_codeIdx][_digitIdx];
wim 2:1dab1089c332 145 digit++;
wim 2:1dab1089c332 146
wim 2:1dab1089c332 147 //NOTE: maybe additional checks required. Valid codes seem to start with a "1" ???
wim 2:1dab1089c332 148
wim 2:1dab1089c332 149 //Check upper boundary and wrap around
wim 2:1dab1089c332 150 if (digit > 9) {
wim 2:1dab1089c332 151 digit=0;
wim 2:1dab1089c332 152 };
wim 2:1dab1089c332 153
wim 2:1dab1089c332 154 // Save updated digit
wim 2:1dab1089c332 155 _codes[_codeIdx][_digitIdx]=digit;
wim 2:1dab1089c332 156
wim 2:1dab1089c332 157 return digit;
wim 2:1dab1089c332 158 }
wim 2:1dab1089c332 159
wim 2:1dab1089c332 160
wim 2:1dab1089c332 161
wim 2:1dab1089c332 162 /** Compute the integer value of the selected STANAG Code
wim 2:1dab1089c332 163 * @param uint8_t codeIdx
wim 2:1dab1089c332 164 * @returns integer value of STANAG code
wim 2:1dab1089c332 165 */
wim 2:1dab1089c332 166 int STANAG_Codes::getCode(uint8_t codeIdx) {
wim 2:1dab1089c332 167 uint8_t digitIdx;
wim 2:1dab1089c332 168 int code;
wim 2:1dab1089c332 169
wim 2:1dab1089c332 170 code=0;
wim 2:1dab1089c332 171 for (digitIdx=0; digitIdx < D_STANAG_DIGITS; digitIdx++) {
wim 2:1dab1089c332 172
wim 2:1dab1089c332 173 // ..., thousands, hundreds, tens, units
wim 2:1dab1089c332 174 code = code + _codes[codeIdx][digitIdx] * DECIMALS[D_STANAG_DIGITS - 1 - digitIdx];
wim 2:1dab1089c332 175 };
wim 2:1dab1089c332 176
wim 2:1dab1089c332 177 return code;
wim 2:1dab1089c332 178 }
wim 2:1dab1089c332 179
wim 2:1dab1089c332 180
wim 2:1dab1089c332 181 /** Compute the integer value of the currently selected STANAG Code
wim 2:1dab1089c332 182 * @param
wim 2:1dab1089c332 183 * @returns integer value of STANAG code
wim 2:1dab1089c332 184 */
wim 2:1dab1089c332 185 int STANAG_Codes::getCode() {
wim 2:1dab1089c332 186
wim 2:1dab1089c332 187 return getCode(_codeIdx);
wim 2:1dab1089c332 188 }
wim 2:1dab1089c332 189
wim 2:1dab1089c332 190
wim 2:1dab1089c332 191 /** Compute and Set the digits of the selected STANAG Code
wim 2:1dab1089c332 192 * @param uint8_t codeIdx
wim 2:1dab1089c332 193 * @param int code
wim 2:1dab1089c332 194 * @returns valid integer value of STANAG code
wim 2:1dab1089c332 195 */
wim 2:1dab1089c332 196 int STANAG_Codes::setCode(uint8_t codeIdx, int code) {
wim 2:1dab1089c332 197 uint8_t digitIdx;
wim 2:1dab1089c332 198 int temp;
wim 2:1dab1089c332 199
wim 2:1dab1089c332 200 //Check lower boundary
wim 2:1dab1089c332 201 if (code<0) {
wim 2:1dab1089c332 202 code=0;
wim 2:1dab1089c332 203 }
wim 2:1dab1089c332 204 else {
wim 2:1dab1089c332 205 //Check upper boundary
wim 2:1dab1089c332 206 if (code > DECIMALS[D_STANAG_DIGITS]) {
wim 2:1dab1089c332 207 code = DECIMALS[D_STANAG_DIGITS] - 1;
wim 2:1dab1089c332 208 }
wim 2:1dab1089c332 209 };
wim 2:1dab1089c332 210
wim 2:1dab1089c332 211 // Extract decimal digits
wim 2:1dab1089c332 212 temp=code;
wim 2:1dab1089c332 213 for (digitIdx=0; digitIdx < D_STANAG_DIGITS; digitIdx++) {
wim 2:1dab1089c332 214
wim 2:1dab1089c332 215 // ..., thousands, hundreds, tens, units
wim 2:1dab1089c332 216 _codes[codeIdx][digitIdx] = temp / DECIMALS[D_STANAG_DIGITS - 1 - digitIdx]; // Extract most significant decimal digit
wim 2:1dab1089c332 217 temp = temp % DECIMALS[D_STANAG_DIGITS - 1 - digitIdx]; // Remainder for next digit
wim 2:1dab1089c332 218 };
wim 2:1dab1089c332 219
wim 2:1dab1089c332 220 return code;
wim 2:1dab1089c332 221 }
wim 2:1dab1089c332 222
wim 2:1dab1089c332 223
wim 2:1dab1089c332 224 /** Compute and Set the digits of the currently selected STANAG Code
wim 2:1dab1089c332 225 * @param int code
wim 2:1dab1089c332 226 * @returns valid integer value of STANAG code
wim 2:1dab1089c332 227 */
wim 2:1dab1089c332 228 int STANAG_Codes::setCode(int code) {
wim 2:1dab1089c332 229
wim 2:1dab1089c332 230 return setCode(_codeIdx, code);
wim 2:1dab1089c332 231 }
wim 2:1dab1089c332 232
wim 2:1dab1089c332 233
wim 2:1dab1089c332 234
wim 2:1dab1089c332 235 #if(0)
wim 2:1dab1089c332 236 int STANAG_Codes::setCodeDigits(uint8_t codeIdx, STANAG_Code_t STANAG_code) {
wim 2:1dab1089c332 237 }
wim 2:1dab1089c332 238
wim 2:1dab1089c332 239 STANAG_Code_t STANAG_Codes::getCodeDigits(uint8_t codeIdx) {
wim 2:1dab1089c332 240 }
wim 2:1dab1089c332 241 #endif
wim 2:1dab1089c332 242
wim 2:1dab1089c332 243
wim 2:1dab1089c332 244
wim 2:1dab1089c332 245 /*****************************************************************************/
wim 2:1dab1089c332 246 /****************************** END OF FILE ********************************/
wim 2:1dab1089c332 247 /*****************************************************************************/
wim 2:1dab1089c332 248
wim 2:1dab1089c332 249