Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
XBeeDataTypes.cpp@3:48f7780963e2, 2012-04-12 (annotated)
- Committer:
- yamaguch
- Date:
- Thu Apr 12 10:20:17 2012 +0000
- Revision:
- 3:48f7780963e2
1.0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yamaguch | 3:48f7780963e2 | 1 | /* |
| yamaguch | 3:48f7780963e2 | 2 | Copyright (c) 2012, Senio Networks, Inc. |
| yamaguch | 3:48f7780963e2 | 3 | |
| yamaguch | 3:48f7780963e2 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| yamaguch | 3:48f7780963e2 | 5 | of this software and associated documentation files (the "Software"), to deal |
| yamaguch | 3:48f7780963e2 | 6 | in the Software without restriction, including without limitation the rights |
| yamaguch | 3:48f7780963e2 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| yamaguch | 3:48f7780963e2 | 8 | copies of the Software, and to permit persons to whom the Software is |
| yamaguch | 3:48f7780963e2 | 9 | furnished to do so, subject to the following conditions: |
| yamaguch | 3:48f7780963e2 | 10 | |
| yamaguch | 3:48f7780963e2 | 11 | The above copyright notice and this permission notice shall be included in |
| yamaguch | 3:48f7780963e2 | 12 | all copies or substantial portions of the Software. |
| yamaguch | 3:48f7780963e2 | 13 | |
| yamaguch | 3:48f7780963e2 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| yamaguch | 3:48f7780963e2 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| yamaguch | 3:48f7780963e2 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| yamaguch | 3:48f7780963e2 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| yamaguch | 3:48f7780963e2 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| yamaguch | 3:48f7780963e2 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| yamaguch | 3:48f7780963e2 | 20 | THE SOFTWARE. |
| yamaguch | 3:48f7780963e2 | 21 | |
| yamaguch | 3:48f7780963e2 | 22 | $Id: XBeeDataTypes.cpp,v 1.3 2012/03/23 09:05:19 yamaguchi Exp yamaguchi $ |
| yamaguch | 3:48f7780963e2 | 23 | */ |
| yamaguch | 3:48f7780963e2 | 24 | |
| yamaguch | 3:48f7780963e2 | 25 | #include "XBeeDataTypes.h" |
| yamaguch | 3:48f7780963e2 | 26 | |
| yamaguch | 3:48f7780963e2 | 27 | XBeeAddress64::XBeeAddress64() : address64() { |
| yamaguch | 3:48f7780963e2 | 28 | } |
| yamaguch | 3:48f7780963e2 | 29 | |
| yamaguch | 3:48f7780963e2 | 30 | XBeeAddress64::XBeeAddress64(const char *address) { |
| yamaguch | 3:48f7780963e2 | 31 | memcpy(address64, address, 8); |
| yamaguch | 3:48f7780963e2 | 32 | } |
| yamaguch | 3:48f7780963e2 | 33 | |
| yamaguch | 3:48f7780963e2 | 34 | XBeeAddress64::XBeeAddress64(uint64_t address) { |
| yamaguch | 3:48f7780963e2 | 35 | uint64_t a = address; |
| yamaguch | 3:48f7780963e2 | 36 | for (int i = 0; i < 8; i++, a >>= 8) |
| yamaguch | 3:48f7780963e2 | 37 | address64[7 - i] = a & 255; |
| yamaguch | 3:48f7780963e2 | 38 | } |
| yamaguch | 3:48f7780963e2 | 39 | |
| yamaguch | 3:48f7780963e2 | 40 | XBeeAddress64::XBeeAddress64(uint32_t high, uint32_t low) { |
| yamaguch | 3:48f7780963e2 | 41 | uint64_t a = uint64_t(high) << 32 | uint64_t(low); |
| yamaguch | 3:48f7780963e2 | 42 | for (int i = 0; i < 8; i++, a >>= 8) |
| yamaguch | 3:48f7780963e2 | 43 | address64[7 - i] = a & 255; |
| yamaguch | 3:48f7780963e2 | 44 | } |
| yamaguch | 3:48f7780963e2 | 45 | |
| yamaguch | 3:48f7780963e2 | 46 | XBeeAddress64::operator uint64_t() { |
| yamaguch | 3:48f7780963e2 | 47 | uint64_t a = address64[0]; |
| yamaguch | 3:48f7780963e2 | 48 | for (int i = 1; i < 8; i++) { |
| yamaguch | 3:48f7780963e2 | 49 | a <<= 8; |
| yamaguch | 3:48f7780963e2 | 50 | a |= address64[i]; |
| yamaguch | 3:48f7780963e2 | 51 | } |
| yamaguch | 3:48f7780963e2 | 52 | return a; |
| yamaguch | 3:48f7780963e2 | 53 | } |
| yamaguch | 3:48f7780963e2 | 54 | |
| yamaguch | 3:48f7780963e2 | 55 | bool XBeeAddress64::operator ==(XBeeAddress64& addr) { |
| yamaguch | 3:48f7780963e2 | 56 | return *(uint64_t *) address64 == *(uint64_t *) (addr.address64); |
| yamaguch | 3:48f7780963e2 | 57 | } |
| yamaguch | 3:48f7780963e2 | 58 | |
| yamaguch | 3:48f7780963e2 | 59 | bool XBeeAddress64::operator ==(const char* addr) { |
| yamaguch | 3:48f7780963e2 | 60 | return *(uint64_t *) address64 == *(uint64_t *) addr; |
| yamaguch | 3:48f7780963e2 | 61 | } |
| yamaguch | 3:48f7780963e2 | 62 | |
| yamaguch | 3:48f7780963e2 | 63 | bool XBeeAddress64::operator ==(uint64_t addr) { |
| yamaguch | 3:48f7780963e2 | 64 | return *(uint64_t *) address64 == addr; |
| yamaguch | 3:48f7780963e2 | 65 | } |
| yamaguch | 3:48f7780963e2 | 66 | |
| yamaguch | 3:48f7780963e2 | 67 | XBeeAddress64::operator char *() { |
| yamaguch | 3:48f7780963e2 | 68 | sprintf(buf, "%02X%02X%02X%02X %02X%02X%02X%02X", |
| yamaguch | 3:48f7780963e2 | 69 | address64[0], address64[1], address64[2], address64[3], |
| yamaguch | 3:48f7780963e2 | 70 | address64[4], address64[5], address64[6], address64[7]); |
| yamaguch | 3:48f7780963e2 | 71 | |
| yamaguch | 3:48f7780963e2 | 72 | return buf; |
| yamaguch | 3:48f7780963e2 | 73 | } |
| yamaguch | 3:48f7780963e2 | 74 | |
| yamaguch | 3:48f7780963e2 | 75 | XBeeAddress16::XBeeAddress16() : address16() { |
| yamaguch | 3:48f7780963e2 | 76 | } |
| yamaguch | 3:48f7780963e2 | 77 | |
| yamaguch | 3:48f7780963e2 | 78 | XBeeAddress16::XBeeAddress16(const char *address) { |
| yamaguch | 3:48f7780963e2 | 79 | memcpy(address16, address, 2); |
| yamaguch | 3:48f7780963e2 | 80 | } |
| yamaguch | 3:48f7780963e2 | 81 | |
| yamaguch | 3:48f7780963e2 | 82 | XBeeAddress16::XBeeAddress16(uint16_t address) { |
| yamaguch | 3:48f7780963e2 | 83 | address16[0] = (address >> 8) & 255; |
| yamaguch | 3:48f7780963e2 | 84 | address16[1] = address & 255; |
| yamaguch | 3:48f7780963e2 | 85 | } |
| yamaguch | 3:48f7780963e2 | 86 | |
| yamaguch | 3:48f7780963e2 | 87 | XBeeAddress16::XBeeAddress16(char high, char low) { |
| yamaguch | 3:48f7780963e2 | 88 | address16[0] = high; |
| yamaguch | 3:48f7780963e2 | 89 | address16[1] = low; |
| yamaguch | 3:48f7780963e2 | 90 | } |
| yamaguch | 3:48f7780963e2 | 91 | |
| yamaguch | 3:48f7780963e2 | 92 | XBeeAddress16::operator uint16_t() { |
| yamaguch | 3:48f7780963e2 | 93 | return uint16_t(address16[0]) << 8 | uint16_t(address16[0]); |
| yamaguch | 3:48f7780963e2 | 94 | } |
| yamaguch | 3:48f7780963e2 | 95 | |
| yamaguch | 3:48f7780963e2 | 96 | bool XBeeAddress16::operator ==(XBeeAddress16& addr) { |
| yamaguch | 3:48f7780963e2 | 97 | return *(uint16_t *) address16 == *(uint16_t *) (addr.address16); |
| yamaguch | 3:48f7780963e2 | 98 | } |
| yamaguch | 3:48f7780963e2 | 99 | |
| yamaguch | 3:48f7780963e2 | 100 | bool XBeeAddress16::operator ==(const char* addr) { |
| yamaguch | 3:48f7780963e2 | 101 | return *(uint16_t *) address16 == *(uint16_t *) addr; |
| yamaguch | 3:48f7780963e2 | 102 | } |
| yamaguch | 3:48f7780963e2 | 103 | |
| yamaguch | 3:48f7780963e2 | 104 | bool XBeeAddress16::operator ==(uint16_t addr) { |
| yamaguch | 3:48f7780963e2 | 105 | return *(uint16_t *) address16 == addr; |
| yamaguch | 3:48f7780963e2 | 106 | } |
| yamaguch | 3:48f7780963e2 | 107 | |
| yamaguch | 3:48f7780963e2 | 108 | XBeeAddress16::operator char *() { |
| yamaguch | 3:48f7780963e2 | 109 | sprintf(buf, "%02X%02X", address16[0], address16[1]); |
| yamaguch | 3:48f7780963e2 | 110 | return buf; |
| yamaguch | 3:48f7780963e2 | 111 | } |
| yamaguch | 3:48f7780963e2 | 112 | |
| yamaguch | 3:48f7780963e2 | 113 | XBeeData::XBeeData(int capacity) : capacity(capacity), size(0) { |
| yamaguch | 3:48f7780963e2 | 114 | data = new char[capacity](); |
| yamaguch | 3:48f7780963e2 | 115 | } |
| yamaguch | 3:48f7780963e2 | 116 | |
| yamaguch | 3:48f7780963e2 | 117 | XBeeData::~XBeeData() { |
| yamaguch | 3:48f7780963e2 | 118 | delete[] data; |
| yamaguch | 3:48f7780963e2 | 119 | } |
| yamaguch | 3:48f7780963e2 | 120 | |
| yamaguch | 3:48f7780963e2 | 121 | BitArray::BitArray(int mask, int values) : mask(mask), values(values) { |
| yamaguch | 3:48f7780963e2 | 122 | } |
| yamaguch | 3:48f7780963e2 | 123 | |
| yamaguch | 3:48f7780963e2 | 124 | int BitArray::operator [](int i) const { |
| yamaguch | 3:48f7780963e2 | 125 | return valueAt(i); |
| yamaguch | 3:48f7780963e2 | 126 | } |
| yamaguch | 3:48f7780963e2 | 127 | |
| yamaguch | 3:48f7780963e2 | 128 | int BitArray::valueAt(int i) const { |
| yamaguch | 3:48f7780963e2 | 129 | return (mask & 1 << i) ? (values >> i) & 1 : -1; |
| yamaguch | 3:48f7780963e2 | 130 | } |
| yamaguch | 3:48f7780963e2 | 131 | |
| yamaguch | 3:48f7780963e2 | 132 | IntArray::IntArray(int mask, const char *value) : mask(mask), values(values) { |
| yamaguch | 3:48f7780963e2 | 133 | } |
| yamaguch | 3:48f7780963e2 | 134 | |
| yamaguch | 3:48f7780963e2 | 135 | int IntArray::operator [](int i) const { |
| yamaguch | 3:48f7780963e2 | 136 | return valueAt(i); |
| yamaguch | 3:48f7780963e2 | 137 | } |
| yamaguch | 3:48f7780963e2 | 138 | |
| yamaguch | 3:48f7780963e2 | 139 | int IntArray::valueAt(int i) const { |
| yamaguch | 3:48f7780963e2 | 140 | if (mask & 1 << i) { |
| yamaguch | 3:48f7780963e2 | 141 | int count = 0; |
| yamaguch | 3:48f7780963e2 | 142 | for (int j = 0; j < i; j++) { |
| yamaguch | 3:48f7780963e2 | 143 | if (mask & 1 << j) count++; |
| yamaguch | 3:48f7780963e2 | 144 | } |
| yamaguch | 3:48f7780963e2 | 145 | |
| yamaguch | 3:48f7780963e2 | 146 | const char *p = values + 2 * count; |
| yamaguch | 3:48f7780963e2 | 147 | |
| yamaguch | 3:48f7780963e2 | 148 | return (p[0] << 8) | p[1]; |
| yamaguch | 3:48f7780963e2 | 149 | } else { |
| yamaguch | 3:48f7780963e2 | 150 | return -1; |
| yamaguch | 3:48f7780963e2 | 151 | } |
| yamaguch | 3:48f7780963e2 | 152 | } |
| yamaguch | 3:48f7780963e2 | 153 | |
| yamaguch | 3:48f7780963e2 | 154 | IOSample::IOSample(const char *data) |
| yamaguch | 3:48f7780963e2 | 155 | : dio((data[1] << 8 | data[2]), (data[1] << 8 | data[2]) ? (data[4] << 8 | data[5]) : 0), |
| yamaguch | 3:48f7780963e2 | 156 | ad(data[3], (data[1] << 8 | data[2]) ? data + 6 : data + 4) { |
| yamaguch | 3:48f7780963e2 | 157 | } |
| yamaguch | 3:48f7780963e2 | 158 | |
| yamaguch | 3:48f7780963e2 | 159 | IOSample::operator char *() { |
| yamaguch | 3:48f7780963e2 | 160 | bool first = true; |
| yamaguch | 3:48f7780963e2 | 161 | char *p = buf; |
| yamaguch | 3:48f7780963e2 | 162 | |
| yamaguch | 3:48f7780963e2 | 163 | for (int i = 0; i < 16; i++) { |
| yamaguch | 3:48f7780963e2 | 164 | if (dio[i] != -1) { |
| yamaguch | 3:48f7780963e2 | 165 | if (first) { |
| yamaguch | 3:48f7780963e2 | 166 | first = false; |
| yamaguch | 3:48f7780963e2 | 167 | p += sprintf(p, "DIO%d=%d", i, dio[i]); |
| yamaguch | 3:48f7780963e2 | 168 | } else { |
| yamaguch | 3:48f7780963e2 | 169 | p += sprintf(p, ", DIO%d=%d", i, dio[i]); |
| yamaguch | 3:48f7780963e2 | 170 | } |
| yamaguch | 3:48f7780963e2 | 171 | } |
| yamaguch | 3:48f7780963e2 | 172 | } |
| yamaguch | 3:48f7780963e2 | 173 | |
| yamaguch | 3:48f7780963e2 | 174 | for (int i = 0; i < 8; i++) { |
| yamaguch | 3:48f7780963e2 | 175 | if (ad[i] != -1) { |
| yamaguch | 3:48f7780963e2 | 176 | if (first) { |
| yamaguch | 3:48f7780963e2 | 177 | first = false; |
| yamaguch | 3:48f7780963e2 | 178 | p += sprintf(p, "AD%d=%d", i, ad[i]); |
| yamaguch | 3:48f7780963e2 | 179 | } else { |
| yamaguch | 3:48f7780963e2 | 180 | p += sprintf(p, ", AD%d=%d", i, ad[i]); |
| yamaguch | 3:48f7780963e2 | 181 | } |
| yamaguch | 3:48f7780963e2 | 182 | } |
| yamaguch | 3:48f7780963e2 | 183 | } |
| yamaguch | 3:48f7780963e2 | 184 | return buf; |
| yamaguch | 3:48f7780963e2 | 185 | } |