Skip to content

Commit 5963bea

Browse files
Split XBeeWithCallbacks::loop() into top and bottom
This allows reusing both parts, while inserting a bit of code in between, in subsequent commits.
1 parent 1d21079 commit 5963bea

File tree

2 files changed

+79
-59
lines changed

2 files changed

+79
-59
lines changed

XBee.cpp

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,69 +1599,75 @@ void XBee::sendByte(uint8_t b, bool escape) {
15991599

16001600

16011601
void XBeeWithCallbacks::loop() {
1602+
if (loopTop())
1603+
loopBottom();
1604+
}
1605+
1606+
bool XBeeWithCallbacks::loopTop() {
16021607
readPacket();
16031608
if (getResponse().isAvailable()) {
16041609
_onResponse.call(getResponse());
1605-
1606-
bool called = false;
1607-
1608-
uint8_t id = getResponse().getApiId();
1609-
1610-
if (id == ZB_TX_STATUS_RESPONSE) {
1611-
ZBTxStatusResponse response;
1612-
getResponse().getZBTxStatusResponse(response);
1613-
called = _onZBTxStatusResponse.call(response);
1614-
} else if (id == ZB_RX_RESPONSE) {
1615-
ZBRxResponse response;
1616-
getResponse().getZBRxResponse(response);
1617-
called = _onZBRxResponse.call(response);
1618-
} else if (id == ZB_EXPLICIT_RX_RESPONSE) {
1619-
ZBExplicitRxResponse response;
1620-
getResponse().getZBExplicitRxResponse(response);
1621-
called = _onZBExplicitRxResponse.call(response);
1622-
} else if (id == ZB_IO_SAMPLE_RESPONSE) {
1623-
ZBRxIoSampleResponse response;
1624-
getResponse().getZBRxIoSampleResponse(response);
1625-
called = _onZBRxIoSampleResponse.call(response);
1626-
} else if (id == TX_STATUS_RESPONSE) {
1627-
TxStatusResponse response;
1628-
getResponse().getTxStatusResponse(response);
1629-
called = _onTxStatusResponse.call(response);
1630-
} else if (id == RX_16_RESPONSE) {
1631-
Rx16Response response;
1632-
getResponse().getRx16Response(response);
1633-
called = _onRx16Response.call(response);
1634-
} else if (id == RX_64_RESPONSE) {
1635-
Rx64Response response;
1636-
getResponse().getRx64Response(response);
1637-
called = _onRx64Response.call(response);
1638-
} else if (id == RX_16_IO_RESPONSE) {
1639-
Rx16IoSampleResponse response;
1640-
getResponse().getRx16IoSampleResponse(response);
1641-
called = _onRx16IoSampleResponse.call(response);
1642-
} else if (id == RX_64_IO_RESPONSE) {
1643-
Rx64IoSampleResponse response;
1644-
getResponse().getRx64IoSampleResponse(response);
1645-
called = _onRx64IoSampleResponse.call(response);
1646-
} else if (id == MODEM_STATUS_RESPONSE) {
1647-
ModemStatusResponse response;
1648-
getResponse().getModemStatusResponse(response);
1649-
called = _onModemStatusResponse.call(response);
1650-
} else if (id == AT_COMMAND_RESPONSE) {
1651-
AtCommandResponse response;
1652-
getResponse().getAtCommandResponse(response);
1653-
called = _onAtCommandResponse.call(response);
1654-
} else if (id == REMOTE_AT_COMMAND_RESPONSE) {
1655-
RemoteAtCommandResponse response;
1656-
getResponse().getRemoteAtCommandResponse(response);
1657-
called = _onRemoteAtCommandResponse.call(response);
1658-
}
1659-
1660-
if (!called)
1661-
_onOtherResponse.call(getResponse());
1662-
1663-
1610+
return true;
16641611
} else if (getResponse().isError()) {
16651612
_onPacketError.call(getResponse().getErrorCode());
16661613
}
1614+
return false;
1615+
}
1616+
1617+
void XBeeWithCallbacks::loopBottom() {
1618+
bool called = false;
1619+
uint8_t id = getResponse().getApiId();
1620+
1621+
if (id == ZB_TX_STATUS_RESPONSE) {
1622+
ZBTxStatusResponse response;
1623+
getResponse().getZBTxStatusResponse(response);
1624+
called = _onZBTxStatusResponse.call(response);
1625+
} else if (id == ZB_RX_RESPONSE) {
1626+
ZBRxResponse response;
1627+
getResponse().getZBRxResponse(response);
1628+
called = _onZBRxResponse.call(response);
1629+
} else if (id == ZB_EXPLICIT_RX_RESPONSE) {
1630+
ZBExplicitRxResponse response;
1631+
getResponse().getZBExplicitRxResponse(response);
1632+
called = _onZBExplicitRxResponse.call(response);
1633+
} else if (id == ZB_IO_SAMPLE_RESPONSE) {
1634+
ZBRxIoSampleResponse response;
1635+
getResponse().getZBRxIoSampleResponse(response);
1636+
called = _onZBRxIoSampleResponse.call(response);
1637+
} else if (id == TX_STATUS_RESPONSE) {
1638+
TxStatusResponse response;
1639+
getResponse().getTxStatusResponse(response);
1640+
called = _onTxStatusResponse.call(response);
1641+
} else if (id == RX_16_RESPONSE) {
1642+
Rx16Response response;
1643+
getResponse().getRx16Response(response);
1644+
called = _onRx16Response.call(response);
1645+
} else if (id == RX_64_RESPONSE) {
1646+
Rx64Response response;
1647+
getResponse().getRx64Response(response);
1648+
called = _onRx64Response.call(response);
1649+
} else if (id == RX_16_IO_RESPONSE) {
1650+
Rx16IoSampleResponse response;
1651+
getResponse().getRx16IoSampleResponse(response);
1652+
called = _onRx16IoSampleResponse.call(response);
1653+
} else if (id == RX_64_IO_RESPONSE) {
1654+
Rx64IoSampleResponse response;
1655+
getResponse().getRx64IoSampleResponse(response);
1656+
called = _onRx64IoSampleResponse.call(response);
1657+
} else if (id == MODEM_STATUS_RESPONSE) {
1658+
ModemStatusResponse response;
1659+
getResponse().getModemStatusResponse(response);
1660+
called = _onModemStatusResponse.call(response);
1661+
} else if (id == AT_COMMAND_RESPONSE) {
1662+
AtCommandResponse response;
1663+
getResponse().getAtCommandResponse(response);
1664+
called = _onAtCommandResponse.call(response);
1665+
} else if (id == REMOTE_AT_COMMAND_RESPONSE) {
1666+
RemoteAtCommandResponse response;
1667+
getResponse().getRemoteAtCommandResponse(response);
1668+
called = _onRemoteAtCommandResponse.call(response);
1669+
}
1670+
1671+
if (!called)
1672+
_onOtherResponse.call(getResponse());
16671673
}

XBee.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,20 @@ class XBeeWithCallbacks : public XBee {
885885
*/
886886
void loop();
887887
private:
888+
/**
889+
* Top half of a typical loop(). Calls readPacket(), calls
890+
* onPacketError on error, calls onResponse when a response is
891+
* available. Returns in the true in the latter case, after
892+
* which a caller should typically call loopBottom().
893+
*/
894+
bool loopTop();
895+
896+
/**
897+
* Bottom half of a typical loop. Call only when a valid
898+
* response was read, will call all response-specific callbacks.
899+
*/
900+
void loopBottom();
901+
888902
template <typename Arg> struct Callback {
889903
void (*func)(Arg, uintptr_t);
890904
uintptr_t data;

0 commit comments

Comments
 (0)