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
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
Module developed by redfast00.

View file

@ -52,12 +52,23 @@ void loop() {
}
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++) {
uint8_t generated = random(26 + 10);
if (generated < 26)
serial_number[i] = 'A' + generated;
else
serial_number[i] = '0' + (generated - 26);
if (i == location_of_digit) {
serial_number[i] = '0' + random(10);
} else if (i == location_of_letter) {
serial_number[i] = 'A' + random(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);
render_now = true;