Skip to content

Commit

Permalink
add std::string overload
Browse files Browse the repository at this point in the history
Signed-off-by: Brenton Poke <[email protected]>
  • Loading branch information
BrentonPoke committed Oct 22, 2024
1 parent 3e0847f commit 6c27d1a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
44 changes: 24 additions & 20 deletions src/Ducks/Duck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,41 @@ void Duck::logIfLowMemory() {
}
}

int Duck::setDeviceId(std::array<byte,8> id) {
if (id.size() != DUID_LENGTH) {
logerr_ln("ERROR device id too long rc = %d", DUCK_ERR_ID_TOO_LONG);
return DUCK_ERR_ID_TOO_LONG;
}
int Duck::setDeviceId(std::array<byte,8>& id) {
std::copy(id.begin(), id.end(),duid.begin());
loginfo_ln("setupDeviceId rc = %d",DUCK_ERR_NONE);
return DUCK_ERR_NONE;
}

[[deprecated("use setDeviceId(std::array<byte,8>& id) instead")]]
int Duck::setDeviceId(byte* id) {
if (id == NULL) {
logerr_ln("ERROR device id too long rc = %d", DUCK_ERR_SETUP);

return DUCK_ERR_SETUP;
}
int len = *(&id + 1) - id;
if (len > DUID_LENGTH) {
logerr_ln("ERROR device id too long rc = %d", DUCK_ERR_ID_TOO_LONG);
return DUCK_ERR_ID_TOO_LONG;
}
duid.assign(id, id + len);
loginfo_ln("setupDeviceId rc = %d",DUCK_ERR_NONE);
if (id == NULL) {
logerr_ln("ERROR device id too long rc = %d", DUCK_ERR_SETUP);
return DUCK_ERR_SETUP;
}
int len = *(&id + 1) - id;
if (len > DUID_LENGTH) {
logerr_ln("ERROR device id too long rc = %d", DUCK_ERR_ID_TOO_LONG);
return DUCK_ERR_ID_TOO_LONG;
}
std::copy(id, id + len, duid.begin());
loginfo_ln("setupDeviceId rc = %d",DUCK_ERR_NONE);
return DUCK_ERR_NONE;
}

return DUCK_ERR_NONE;
int Duck::setDeviceId(std::string& id) {
if (id.size() != DUID_LENGTH) {
logerr_ln("ERROR device id too long rc = %d", DUCK_ERR_ID_TOO_LONG);
return DUCK_ERR_ID_TOO_LONG;
}
std::copy(id.begin(), id.end(),duid.begin());
loginfo_ln("setupDeviceId rc = %d", DUCK_ERR_NONE);
return DUCK_ERR_NONE;
}

int Duck::setupSerial(int baudRate) {
// This gives us 10 seconds to do a hard reset if the board is in a bad state after power cycle
while (!Serial && millis() < 10000)
;
while (!Serial && millis() < 10000);

Serial.begin(baudRate);
loginfo_ln("setupSerial rc = %d",DUCK_ERR_NONE);
Expand Down
5 changes: 3 additions & 2 deletions src/include/Duck.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ class Duck {
* @param an 8 byte unique id
* @return DUCK_ERR_NONE if successful, an error code otherwise
*/
int setDeviceId(std::array<byte,8> id);
int setDeviceId(std::array<byte,8>& id);

/**
* @brief setup the duck unique ID
*
* @param an 8 byte unique id
* @return DUCK_ERR_NONE if successful, an error code otherwise
*/
int setDeviceId(byte* id);
int setDeviceId(std::string& id);

int Duck::setDeviceId(byte* id);
/**
* @brief Setup serial connection.
*
Expand Down

0 comments on commit 6c27d1a

Please sign in to comment.