| 1 | #include "../libs/ELECHOUSE_CC1101_SRC_DRV.h" |
| 2 | #include <Arduino.h> |
| 3 | #include <RF24.h> |
| 4 | #include <SPI.h> |
| 5 | #include <Wire.h> |
| 6 | |
| 7 | #include "config.h" |
| 8 | #include "ui/display.h" |
| 9 | #include "buttons.h" |
| 10 | |
| 11 | // ===== EXTERNALS ===== |
| 12 | extern RF24 radio1; |
| 13 | extern RF24 radio2; |
| 14 | extern SPIClass *RADIO_SPI; |
| 15 | extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; |
| 16 | |
| 17 | // ===== RESULTS ===== |
| 18 | struct DeviceStatus { |
| 19 | bool nrf1 = false; |
| 20 | bool nrf2 = false; |
| 21 | bool cc1101 = false; |
| 22 | bool oled = true; |
| 23 | }; |
| 24 | |
| 25 | // ===== NRF CHECK ===== |
| 26 | // bool checkNRF(RF24 &radio) |
| 27 | //{ |
| 28 | // // safer: only init if needed |
| 29 | // if (!radio.isChipConnected()) { |
| 30 | // if (!radio.begin(RADIO_SPI)) |
| 31 | // return false; |
| 32 | // } |
| 33 | // |
| 34 | // return radio.isChipConnected(); |
| 35 | //} |
| 36 | |
| 37 | bool checkNRF(RF24 &radio) { |
| 38 | radio.powerDown(); |
| 39 | delay(5); |
| 40 | |
| 41 | if (!radio.begin(RADIO_SPI)) |
| 42 | return false; |
| 43 | |
| 44 | delay(5); |
| 45 | |
| 46 | return radio.isChipConnected(); |
| 47 | } |
| 48 | |
| 49 | // ===== CC1101 CHECK ===== |
| 50 | bool checkCC1101(uint8_t csPin) { |
| 51 | ELECHOUSE_cc1101.setSpiPin(cc1101_SCK, cc1101_MISO, cc1101_MOSI, csPin); |
| 52 | |
| 53 | delay(5); |
| 54 | |
| 55 | return ELECHOUSE_cc1101.getCC1101(); |
| 56 | } |
| 57 | |
| 58 | // ===== DRAW ===== |
| 59 | #define MAX_ITEMS 4 |
| 60 | #define VISIBLE_ROWS 5 |
| 61 | |
| 62 | const char *labels[MAX_ITEMS] = {"NRF1", "NRF2", "CC1101", "OLED"}; |
| 63 | |
| 64 | bool values[MAX_ITEMS]; |
| 65 | |
| 66 | int selectedIndex = 0; |
| 67 | int offset = 0; |
| 68 | |
| 69 | void drawStatus(DeviceStatus &s) { |
| 70 | values[0] = s.nrf1; |
| 71 | values[1] = s.nrf2; |
| 72 | values[2] = s.cc1101; |
| 73 | values[3] = s.oled; |
| 74 | |
| 75 | u8g2.clearBuffer(); |
| 76 | u8g2.setFont(u8g2_font_6x10_tr); |
| 77 | |
| 78 | // scrolling logic |
| 79 | if (selectedIndex < offset) |
| 80 | offset = selectedIndex; |
| 81 | |
| 82 | if (selectedIndex >= offset + VISIBLE_ROWS) |
| 83 | offset = selectedIndex - VISIBLE_ROWS + 1; |
| 84 | |
| 85 | for (int i = 0; i < VISIBLE_ROWS; i++) { |
| 86 | int item = offset + i; |
| 87 | if (item >= MAX_ITEMS) |
| 88 | break; |
| 89 | |
| 90 | int y = 12 + i * 10; |
| 91 | |
| 92 | if (item == selectedIndex) { |
| 93 | u8g2.drawBox(0, y - 9, 128, 10); |
| 94 | u8g2.setDrawColor(0); |
| 95 | } |
| 96 | |
| 97 | u8g2.drawStr(2, y, labels[item]); |
| 98 | |
| 99 | if (values[item]) |
| 100 | u8g2.drawStr(80, y, "OK"); |
| 101 | else |
| 102 | u8g2.drawStr(80, y, "FAIL"); |
| 103 | |
| 104 | if (item == selectedIndex) |
| 105 | u8g2.setDrawColor(1); |
| 106 | } |
| 107 | |
| 108 | u8g2.sendBuffer(); |
| 109 | } |
| 110 | |
| 111 | // ===== MAIN ===== |
| 112 | void device_check_run() { |
| 113 | DeviceStatus status; |
| 114 | |
| 115 | Serial.println("Running device diagnostics..."); |
| 116 | |
| 117 | // NRF |
| 118 | status.nrf1 = checkNRF(radio1); |
| 119 | status.nrf2 = checkNRF(radio2); |
| 120 | |
| 121 | // CC1101 |
| 122 | status.cc1101 = checkCC1101(CC1101_CS); |
| 123 | // status.cc1101 = true; |
| 124 | |
| 125 | drawStatus(status); |
| 126 | |
| 127 | Serial.println("Diagnostics complete"); |
| 128 | |
| 129 | while (1) { |
| 130 | drawStatus(status); |
| 131 | |
| 132 | if (btnUp()) { |
| 133 | selectedIndex--; |
| 134 | if (selectedIndex < 0) |
| 135 | selectedIndex = MAX_ITEMS - 1; |
| 136 | delay(150); |
| 137 | } |
| 138 | |
| 139 | if (btnDown()) { |
| 140 | selectedIndex++; |
| 141 | if (selectedIndex >= MAX_ITEMS) |
| 142 | selectedIndex = 0; |
| 143 | delay(150); |
| 144 | } |
| 145 | |
| 146 | if (btnBack()) { |
| 147 | delay(150); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | } |