Skip to content

Commit

Permalink
FHSS: function to calculate nearest (larger or equal) prime number
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarkko Paso committed Jan 25, 2018
1 parent 5e70ec1 commit c8a9c7c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
19 changes: 19 additions & 0 deletions source/Service_Libs/fhss/channel_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@

static uint32_t global_seed = 1;


uint16_t tr51_calc_nearest_prime_number(uint16_t start_value)
{
if (start_value < 2) {
return 0;
}
uint16_t divider = start_value - 1;
while (start_value) {
if (start_value % divider--) {
if (divider == 1) {
return start_value;
}
} else {
divider = ++start_value - 1;
}
}
return 0;
}

void tr51_seed_rand(uint32_t seed)
{
if (!seed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ TEST(channel_functions, test_dh1cf_get_bc_channel_index)
{
CHECK(test_dh1cf_get_bc_channel_index());
}

TEST(channel_functions, test_tr51_calc_nearest_prime_number)
{
CHECK(test_tr51_calc_nearest_prime_number());
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const int32_t test_channel_table[131] = { 57,78,25,22,97,90,86,102,
56,71,4,29,52,82,120,113,
8,-1,-1 };

const int32_t test_HopSequenceTable [129] = { 4,93,31,84,55,3,116,123,
const int32_t test_HopSequenceTable[129] = { 4,93,31,84,55,3,116,123,
41,68,105,28,25,71,33,66,
122,20,112,125,118,11,69,89,
128,78,56,42,124,30,64,114,
Expand All @@ -54,6 +54,23 @@ const int32_t test_HopSequenceTable [129] = { 4,93,31,84,55,3,116,123,
77,48,100,51,14,21,94,23,
22 };

const uint16_t prime_number_table[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 1009 };

bool test_tr51_get_rand()
{
Expand Down Expand Up @@ -194,3 +211,21 @@ bool test_dh1cf_get_bc_channel_index()
}
return true;
}

bool test_tr51_calc_nearest_prime_number()
{
uint16_t prime_number;
int j;
for (int i=2; i<1000; i++) {
prime_number = tr51_calc_nearest_prime_number(i);
for (j=0; j<sizeof(prime_number_table); j++) {
if (prime_number == prime_number_table[j]) {
break;
}
}
if (j >= sizeof(prime_number_table)) {
return false;
}
}
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bool test_tr51_compute_cfd();
bool test_tr51_calculate_hopping_sequence();
bool test_dh1cf_get_uc_channel_index();
bool test_dh1cf_get_bc_channel_index();
bool test_tr51_calc_nearest_prime_number();

#ifdef __cplusplus
}
Expand Down

0 comments on commit c8a9c7c

Please sign in to comment.