|
| 1 | +//OpenLRSng adaptive channel picker |
| 2 | + |
| 3 | +// development only... |
| 4 | +#define CHANNELS_TO_PICK 12 |
| 5 | + |
| 6 | +void isort(uint8_t *a, uint8_t n) |
| 7 | +{ |
| 8 | + for (uint8_t i=1; i<n; i++) { |
| 9 | + for (uint8_t j = i; j> 0 && a[j] < a[j-1]; j--) { |
| 10 | + uint8_t v = a[j]; |
| 11 | + a[j] = a[j-1]; |
| 12 | + a[j-1] = v; |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +uint8_t chooseChannelsPerRSSI() |
| 18 | +{ |
| 19 | + uint8_t chRSSImax[255]; |
| 20 | + uint8_t picked[CHANNELS_TO_PICK]; |
| 21 | + Serial.println("Entering adaptive channel selection"); |
| 22 | + init_rfm(0); |
| 23 | + rx_reset(); |
| 24 | + for (uint8_t ch=1; ch<255; ch++) { |
| 25 | + uint32_t start = millis(); |
| 26 | + rfmSetChannel(ch); |
| 27 | + delay(1); |
| 28 | + chRSSImax[ch]=0; |
| 29 | + while ((millis()-start) < 500) { |
| 30 | + uint8_t rssi = rfmGetRSSI(); |
| 31 | + if (rssi > chRSSImax[ch]) { |
| 32 | + chRSSImax[ch] = rssi; |
| 33 | + } |
| 34 | + } |
| 35 | + if (ch&1) { |
| 36 | + Green_LED_OFF |
| 37 | + Red_LED_ON |
| 38 | + } else { |
| 39 | + Green_LED_ON |
| 40 | + Red_LED_OFF |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + for (uint8_t i=0; i < CHANNELS_TO_PICK; i++) { |
| 45 | + uint8_t lowest, lowestRSSI=255; |
| 46 | + for (uint8_t ch=1; ch<255; ch++) { |
| 47 | + if (chRSSImax[ch] < lowestRSSI) { |
| 48 | + lowestRSSI = chRSSImax[ch]; |
| 49 | + lowest = ch; |
| 50 | + } |
| 51 | + } |
| 52 | + picked[i] = lowest; |
| 53 | + chRSSImax[lowest]=255; |
| 54 | + if (lowest>1) { |
| 55 | + chRSSImax[lowest-1]=255; |
| 56 | + } |
| 57 | + if (lowest>2) { |
| 58 | + chRSSImax[lowest-2]=200; |
| 59 | + } |
| 60 | + if (lowest<254) { |
| 61 | + chRSSImax[lowest+1]=255; |
| 62 | + } |
| 63 | + if (lowest<253) { |
| 64 | + chRSSImax[lowest+2]=200; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + isort(picked,CHANNELS_TO_PICK); |
| 69 | + |
| 70 | + for (uint8_t i=0; i < CHANNELS_TO_PICK; i++) { |
| 71 | + Serial.print(picked[i]); |
| 72 | + Serial.print(','); |
| 73 | + } |
| 74 | + Serial.println(); |
| 75 | + |
| 76 | + return 1; |
| 77 | +} |
0 commit comments