Make sure serial number consists of at least one digit and letter

This commit is contained in:
redfast00 2021-07-31 22:30:52 +02:00
parent 124e0bb43a
commit 93230c3736
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C
2 changed files with 18 additions and 6 deletions

View file

@ -1,7 +1,8 @@
## Serial number ## Serial number
This module provides a serial number for other modules to use. It consists of This module provides a serial number for other modules to use. It consists of
7 random letters/digits that are shown on a LCD display. 7 random letters/digits that are shown on a LCD display. The serial number has
at least one digit and one letter.
### Credits ### Credits
Module developed by redfast00. Module developed by redfast00.

View file

@ -52,12 +52,23 @@ void loop() {
} }
int info_generator(uint8_t* buffer) { int info_generator(uint8_t* buffer) {
uint8_t location_of_digit = random(SERIAL_NUMBER_SIZE);
uint8_t location_of_letter = random(SERIAL_NUMBER_SIZE);
if (location_of_digit == location_of_letter) {
location_of_letter = (location_of_letter + 1) % SERIAL_NUMBER_SIZE;
}
for (int i = 0; i < SERIAL_NUMBER_SIZE; i++) { for (int i = 0; i < SERIAL_NUMBER_SIZE; i++) {
uint8_t generated = random(26 + 10); if (i == location_of_digit) {
if (generated < 26) serial_number[i] = '0' + random(10);
serial_number[i] = 'A' + generated; } else if (i == location_of_letter) {
else serial_number[i] = 'A' + random(26);
serial_number[i] = '0' + (generated - 26); } else {
uint8_t generated = random(26 + 10);
if (generated < 26)
serial_number[i] = 'A' + generated;
else
serial_number[i] = '0' + (generated - 26);
}
} }
memcpy(buffer, serial_number, SERIAL_NUMBER_SIZE); memcpy(buffer, serial_number, SERIAL_NUMBER_SIZE);
render_now = true; render_now = true;