Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c78eb6
commit d3bf30e
Showing
8 changed files
with
109 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "devicename.h" | ||
|
||
#include "Arduino.h" | ||
|
||
#include "debug.h" | ||
|
||
DeviceName::DeviceName() : DeviceName("FLYBRIX") { | ||
} | ||
|
||
DeviceName::DeviceName(const String& name) { | ||
std::size_t l{name.length()}; | ||
if (l > 8) { | ||
l = 8; | ||
} | ||
for (std::size_t i{0}; i < l; ++i) { | ||
value[i] = name.charAt(i); | ||
} | ||
for (std::size_t i{l}; i < MAX_NAME_LENGTH + 1; ++i) { | ||
value[i] = 0; | ||
} | ||
} | ||
|
||
bool badChar(char c); | ||
|
||
bool DeviceName::verify() const { | ||
if (!value[0]) { | ||
DebugPrint("Name cannot be empty"); | ||
} | ||
for (char c : value) { | ||
if (badChar(c)) { | ||
DebugPrint( | ||
"Illegal character in name!" | ||
" " | ||
"Names are limited to 0-9, a-z, A-Z, '_', '-'!"); | ||
return false; | ||
} | ||
if (!c) { | ||
return true; | ||
} | ||
} | ||
DebugPrint("Given device name is too long (max 8 characters)!"); | ||
return false; | ||
} | ||
|
||
bool badChar(char c) { | ||
if (!c) { | ||
return false; | ||
} | ||
if (c >= 'a' && c <= 'z') { | ||
return false; | ||
} | ||
if (c >= 'A' && c <= 'Z') { | ||
return false; | ||
} | ||
if (c >= '0' && c <= '9') { | ||
return false; | ||
} | ||
for (char c_legal : "_-") { | ||
if (c == c_legal) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef DEVICENAME_H | ||
#define DEVICENAME_H | ||
|
||
#include <cstdint> | ||
|
||
class String; | ||
|
||
constexpr uint8_t MAX_NAME_LENGTH = 8; | ||
|
||
struct __attribute__((packed)) DeviceName { | ||
DeviceName(); | ||
DeviceName(const String& name); | ||
|
||
bool verify() const; | ||
|
||
char value[MAX_NAME_LENGTH + 1]; | ||
}; | ||
|
||
static_assert(sizeof(DeviceName) == sizeof(char) * 9, "Data is not packed"); | ||
|
||
#endif /* DEVICENAME_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters