Skip to content

Commit ce5f8cf

Browse files
committed
Merge pull request #6 from matthijskooijman/improvements
XBeeAddress64 improvements
2 parents 29177e8 + 289db61 commit ce5f8cf

File tree

2 files changed

+34
-48
lines changed

2 files changed

+34
-48
lines changed

XBee.cpp

-39
Original file line numberDiff line numberDiff line change
@@ -1051,45 +1051,6 @@ void PayloadRequest::setPayloadLength(uint8_t payloadLength) {
10511051
_payloadLength = payloadLength;
10521052
}
10531053

1054-
1055-
XBeeAddress::XBeeAddress() {
1056-
1057-
}
1058-
1059-
XBeeAddress64::XBeeAddress64() : XBeeAddress() {
1060-
1061-
}
1062-
1063-
XBeeAddress64::XBeeAddress64(uint32_t msb, uint32_t lsb) : XBeeAddress() {
1064-
_msb = msb;
1065-
_lsb = lsb;
1066-
}
1067-
1068-
uint32_t XBeeAddress64::getMsb() {
1069-
return _msb;
1070-
}
1071-
1072-
void XBeeAddress64::setMsb(uint32_t msb) {
1073-
_msb = msb;
1074-
}
1075-
1076-
uint32_t XBeeAddress64::getLsb() {
1077-
return _lsb;
1078-
}
1079-
1080-
void XBeeAddress64::setLsb(uint32_t lsb) {
1081-
_lsb = lsb;
1082-
}
1083-
1084-
// contributed by user repat123 on issue tracker
1085-
//bool XBeeAddress64::operator==(XBeeAddress64 addr) {
1086-
// return ((_lsb == addr.getLsb()) && (_msb == addr.getMsb()));
1087-
//}
1088-
1089-
//bool XBeeAddress64::operator!=(XBeeAddress64 addr) {
1090-
// return !(*this == addr);
1091-
//}
1092-
10931054
#ifdef SERIES_2
10941055

10951056
ZBTxRequest::ZBTxRequest() : PayloadRequest(ZB_TX_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {

XBee.h

+34-9
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@
151151
#define PACKET_EXCEEDS_BYTE_ARRAY_LENGTH 2
152152
#define UNEXPECTED_START_BYTE 3
153153

154+
/**
155+
* C++11 introduced the constexpr as a hint to the compiler that things
156+
* can be evaluated at compiletime. This can help to remove
157+
* startup code for global objects, or otherwise help the compiler to
158+
* optimize. Since the keyword is introduced in C++11, but supporting
159+
* older compilers is a matter of removing the keyword, we use a macro
160+
* for this.
161+
*/
162+
#if __cplusplus >= 201103L
163+
#define CONSTEXPR constexpr
164+
#else
165+
#define CONSTEXPR
166+
#endif
167+
154168
/**
155169
* The super class of all XBee responses (RX packets)
156170
* Users should never attempt to create an instance of this class; instead
@@ -301,23 +315,34 @@ class XBeeResponse {
301315

302316
class XBeeAddress {
303317
public:
304-
XBeeAddress();
318+
CONSTEXPR XBeeAddress() {};
305319
};
306320

307321
/**
308322
* Represents a 64-bit XBee Address
323+
*
324+
* Note that avr-gcc as of 4.9 doesn't optimize uint64_t very well, so
325+
* for the smallest and fastest code, use msb and lsb separately. See
326+
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66511
309327
*/
310328
class XBeeAddress64 : public XBeeAddress {
311329
public:
312-
XBeeAddress64(uint32_t msb, uint32_t lsb);
313-
XBeeAddress64();
314-
uint32_t getMsb();
315-
uint32_t getLsb();
316-
void setMsb(uint32_t msb);
317-
void setLsb(uint32_t lsb);
318-
//bool operator==(XBeeAddress64 addr);
319-
//bool operator!=(XBeeAddress64 addr);
330+
CONSTEXPR XBeeAddress64(uint64_t addr) : _msb(addr >> 32), _lsb(addr) {}
331+
CONSTEXPR XBeeAddress64(uint32_t msb, uint32_t lsb) : _msb(msb), _lsb(lsb) {}
332+
CONSTEXPR XBeeAddress64() : _msb(0), _lsb(0) {}
333+
uint32_t getMsb() {return _msb;}
334+
uint32_t getLsb() {return _lsb;}
335+
uint64_t get() {return (static_cast<uint64_t>(_msb) << 32) | _lsb;}
336+
operator uint64_t() {return get();}
337+
void setMsb(uint32_t msb) {_msb = msb;}
338+
void setLsb(uint32_t lsb) {_lsb = lsb;}
339+
void set(uint64_t addr) {
340+
_msb = addr >> 32;
341+
_lsb = addr;
342+
}
320343
private:
344+
// Once https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66511 is
345+
// fixed, it might make sense to merge these into a uint64_t.
321346
uint32_t _msb;
322347
uint32_t _lsb;
323348
};

0 commit comments

Comments
 (0)