| 1 | #include <Arduino.h> |
| 2 | #include <SPI.h> |
| 3 | #include <Wire.h> |
| 4 | #include <RF24.h> |
| 5 | #include "../libs/ELECHOUSE_CC1101_SRC_DRV.h" |
| 6 | |
| 7 | #include "../ui/display.h" |
| 8 | #include "buttons.h" |
| 9 | #include "../config.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_1 = false; |
| 22 | bool cc1101_2 = false; |
| 23 | bool oled = true; |
| 24 | bool buttons = false; |
| 25 | }; |
| 26 | |
| 27 | // ===== NRF CHECK ===== |
| 28 | //bool checkNRF(RF24 &radio) |
| 29 | //{ |
| 30 | // // safer: only init if needed |
| 31 | // if (!radio.isChipConnected()) { |
| 32 | // if (!radio.begin(RADIO_SPI)) |
| 33 | // return false; |
| 34 | // } |
| 35 | // |
| 36 | // return radio.isChipConnected(); |
| 37 | //} |
| 38 | |
| 39 | bool checkNRF(RF24 &radio) |
| 40 | { |
| 41 | radio.powerDown(); |
| 42 | delay(5); |
| 43 | |
| 44 | if (!radio.begin(RADIO_SPI)) |
| 45 | return false; |
| 46 | |
| 47 | delay(5); |
| 48 | |
| 49 | return radio.isChipConnected(); |
| 50 | } |
| 51 | |
| 52 | // ===== CC1101 CHECK ===== |
| 53 | bool checkCC1101(uint8_t csPin) |
| 54 | { |
| 55 | ELECHOUSE_cc1101.setSpiPin( |
| 56 | cc1101_SCK, |
| 57 | cc1101_MISO, |
| 58 | cc1101_MOSI, |
| 59 | csPin |
| 60 | ); |
| 61 | |
| 62 | delay(5); |
| 63 | |
| 64 | return ELECHOUSE_cc1101.getCC1101(); |
| 65 | } |
| 66 | |
| 67 | // ===== BUTTON CHECK ===== |
| 68 | bool checkButtons() |
| 69 | { |
| 70 | u8g2.clearBuffer(); |
| 71 | u8g2.setFont(u8g2_font_6x10_tr); |
| 72 | u8g2.drawStr(0, 20, "Press any button..."); |
| 73 | u8g2.sendBuffer(); |
| 74 | |
| 75 | unsigned long start = millis(); |
| 76 | |
| 77 | while (millis() - start < 2000) |
| 78 | { |
| 79 | if (!digitalRead(BTN_UP) || |
| 80 | !digitalRead(BTN_DOWN) || |
| 81 | !digitalRead(BTN_SELECT) || |
| 82 | !digitalRead(BTN_BACK) || |
| 83 | !digitalRead(BTN_RIGHT) || |
| 84 | !digitalRead(BTN_LEFT)) |
| 85 | { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // ===== DRAW ===== |
| 94 | #define MAX_ITEMS 6 |
| 95 | #define VISIBLE_ROWS 5 |
| 96 | |
| 97 | const char* labels[MAX_ITEMS] = { |
| 98 | "NRF1", |
| 99 | "NRF2", |
| 100 | "CC1101-1", |
| 101 | "CC1101-2", |
| 102 | "BUTTONS", |
| 103 | "OLED" |
| 104 | }; |
| 105 | |
| 106 | bool values[MAX_ITEMS]; |
| 107 | |
| 108 | int selectedIndex = 0; |
| 109 | int offset = 0; |
| 110 | |
| 111 | void drawStatus(DeviceStatus &s) |
| 112 | { |
| 113 | values[0] = s.nrf1; |
| 114 | values[1] = s.nrf2; |
| 115 | values[2] = s.cc1101_1; |
| 116 | values[3] = s.cc1101_2; |
| 117 | values[4] = s.buttons; |
| 118 | values[5] = s.oled; |
| 119 | |
| 120 | |
| 121 | u8g2.clearBuffer(); |
| 122 | u8g2.setFont(u8g2_font_6x10_tr); |
| 123 | |
| 124 | // scrolling logic |
| 125 | if (selectedIndex < offset) |
| 126 | offset = selectedIndex; |
| 127 | |
| 128 | if (selectedIndex >= offset + VISIBLE_ROWS) |
| 129 | offset = selectedIndex - VISIBLE_ROWS + 1; |
| 130 | |
| 131 | for (int i = 0; i < VISIBLE_ROWS; i++) |
| 132 | { |
| 133 | int item = offset + i; |
| 134 | if (item >= MAX_ITEMS) break; |
| 135 | |
| 136 | int y = 12 + i * 10; |
| 137 | |
| 138 | if (item == selectedIndex) |
| 139 | { |
| 140 | u8g2.drawBox(0, y - 9, 128, 10); |
| 141 | u8g2.setDrawColor(0); |
| 142 | } |
| 143 | |
| 144 | u8g2.drawStr(2, y, labels[item]); |
| 145 | |
| 146 | if (values[item]) |
| 147 | u8g2.drawStr(80, y, "OK"); |
| 148 | else |
| 149 | u8g2.drawStr(80, y, "FAIL"); |
| 150 | |
| 151 | if (item == selectedIndex) |
| 152 | u8g2.setDrawColor(1); |
| 153 | } |
| 154 | |
| 155 | u8g2.sendBuffer(); |
| 156 | } |
| 157 | |
| 158 | // ===== MAIN ===== |
| 159 | void device_check_run() |
| 160 | { |
| 161 | DeviceStatus status; |
| 162 | |
| 163 | Serial.println("Running device diagnostics..."); |
| 164 | |
| 165 | // NRF |
| 166 | status.nrf1 = checkNRF(radio1); |
| 167 | status.nrf2 = checkNRF(radio2); |
| 168 | |
| 169 | // CC1101 |
| 170 | status.cc1101_1 = checkCC1101(CC1101_CS); |
| 171 | status.cc1101_2 = checkCC1101(CC1101_2_CS); |
| 172 | //status.cc1101_1 = status.cc1101_2 = false; |
| 173 | |
| 174 | // Buttons |
| 175 | status.buttons = checkButtons(); |
| 176 | |
| 177 | drawStatus(status); |
| 178 | |
| 179 | Serial.println("Diagnostics complete"); |
| 180 | |
| 181 | while (1) |
| 182 | { |
| 183 | drawStatus(status); |
| 184 | |
| 185 | if (btnUp()) |
| 186 | { |
| 187 | selectedIndex--; |
| 188 | if (selectedIndex < 0) selectedIndex = MAX_ITEMS - 1; |
| 189 | delay(150); |
| 190 | } |
| 191 | |
| 192 | if (btnDown()) |
| 193 | { |
| 194 | selectedIndex++; |
| 195 | if (selectedIndex >= MAX_ITEMS) selectedIndex = 0; |
| 196 | delay(150); |
| 197 | } |
| 198 | |
| 199 | if (btnBack()) |
| 200 | { |
| 201 | delay(150); |
| 202 | break; |
| 203 | } |
| 204 | }} |
| 205 | |
| 206 | |
| 207 | |