Krolyxon
Commit bf0a032

Branches

Tags

Refs →

orion-rffirmwaredevice_check.cpp

242 lines · 4.5 KB · C++

1#include <Arduino.h>
2#include <SPI.h>
3#include <Wire.h>
4#include <RF24.h>
5#include "ELECHOUSE_CC1101_SRC_DRV.h"
6
7#include "display.h"
8#include "buttons.h"
9#include "config.h"
10
11// ===== EXTERNALS =====
12extern RF24 radio1;
13extern RF24 radio2;
14extern SPIClass *RADIO_SPI;
15extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2;
16
17// ===== RESULTS =====
18struct DeviceStatus {
19 bool nrf_link = false;
20 bool nrf1 = false;
21 bool nrf2 = false;
22 bool cc1101 = false;
23 bool oled = true;
24 bool buttons = false;
25};
26
27// ===== NRF CHECK =====
28bool 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// ===== CC1101 CHECK =====
40bool checkCC1101()
41{
42 ELECHOUSE_cc1101.setSpiPin(
43 cc1101_SCK,
44 cc1101_MISO,
45 cc1101_MOSI,
46 CC1101_CS
47 );
48
49 delay(5); // important stabilization
50
51 return ELECHOUSE_cc1101.getCC1101();
52}
53
54// ===== BUTTON CHECK =====
55bool checkButtons()
56{
57 u8g2.clearBuffer();
58 u8g2.setFont(u8g2_font_6x10_tr);
59 u8g2.drawStr(0, 20, "Press any button...");
60 u8g2.sendBuffer();
61
62 unsigned long start = millis();
63
64 while (millis() - start < 2000)
65 {
66 if (!digitalRead(BTN_UP) ||
67 !digitalRead(BTN_DOWN) ||
68 !digitalRead(BTN_SELECT) ||
69 !digitalRead(BTN_BACK))
70 {
71 return true;
72 }
73 }
74
75 return false;
76}
77
78// ===== DRAW =====
79#define MAX_ITEMS 6
80#define VISIBLE_ROWS 5
81
82const char* labels[MAX_ITEMS] = {
83 "NRF1",
84 "NRF2",
85 "LINK",
86 "CC1101",
87 "BUTTONS",
88 "OLED"
89};
90
91bool values[MAX_ITEMS];
92
93int selectedIndex = 0;
94int offset = 0;
95
96void drawStatus(DeviceStatus &s)
97{
98 values[0] = s.nrf1;
99 values[1] = s.nrf2;
100 values[2] = s.nrf_link;
101 values[3] = s.cc1101;
102 values[4] = s.buttons;
103 values[5] = s.oled;
104
105 u8g2.clearBuffer();
106 u8g2.setFont(u8g2_font_6x10_tr);
107
108 // scrolling logic
109 if (selectedIndex < offset)
110 offset = selectedIndex;
111
112 if (selectedIndex >= offset + VISIBLE_ROWS)
113 offset = selectedIndex - VISIBLE_ROWS + 1;
114
115 for (int i = 0; i < VISIBLE_ROWS; i++)
116 {
117 int item = offset + i;
118 if (item >= MAX_ITEMS) break;
119
120 int y = 12 + i * 10;
121
122 if (item == selectedIndex)
123 {
124 u8g2.drawBox(0, y - 9, 128, 10);
125 u8g2.setDrawColor(0);
126 }
127
128 u8g2.drawStr(2, y, labels[item]);
129
130 if (values[item])
131 u8g2.drawStr(80, y, "OK");
132 else
133 u8g2.drawStr(80, y, "FAIL");
134
135 if (item == selectedIndex)
136 u8g2.setDrawColor(1);
137 }
138
139 u8g2.sendBuffer();
140}
141
142bool testNRFLink()
143{
144 const byte address[6] = "00001";
145 uint8_t payload = 0xAB;
146 uint8_t received = 0;
147
148 // --- init radios ---
149 if (!radio1.begin(RADIO_SPI)) return false;
150 if (!radio2.begin(RADIO_SPI)) return false;
151
152 radio1.setPALevel(RF24_PA_LOW);
153 radio2.setPALevel(RF24_PA_LOW);
154
155 radio1.setDataRate(RF24_1MBPS);
156 radio2.setDataRate(RF24_1MBPS);
157
158 radio1.setChannel(100);
159 radio2.setChannel(100);
160
161 radio1.setAutoAck(false);
162 radio2.setAutoAck(false);
163
164 // --- configure pipes ---
165 radio1.openWritingPipe(address);
166 radio2.openReadingPipe(0, address);
167
168 radio2.startListening();
169
170 delay(50);
171
172 // --- send ---
173 radio1.stopListening();
174 bool sent = radio1.write(&payload, sizeof(payload));
175
176 if (!sent) return false;
177
178 // --- receive ---
179 unsigned long start = millis();
180
181 while (millis() - start < 200)
182 {
183 if (radio2.available())
184 {
185 radio2.read(&received, sizeof(received));
186 return (received == payload);
187 }
188 }
189
190 return false;
191}
192
193// ===== MAIN =====
194void device_check_run()
195{
196 DeviceStatus status;
197
198 Serial.println("Running device diagnostics...");
199
200 // NRF
201 // NRF link test
202 status.nrf_link = testNRFLink();
203 status.nrf1 = checkNRF(radio1);
204 status.nrf2 = checkNRF(radio2);
205
206 // CC1101
207 status.cc1101 = checkCC1101();
208
209 // Buttons
210 status.buttons = checkButtons();
211
212 drawStatus(status);
213
214 Serial.println("Diagnostics complete");
215
216 while (1)
217{
218 drawStatus(status);
219
220 if (btnUp())
221 {
222 selectedIndex--;
223 if (selectedIndex < 0) selectedIndex = MAX_ITEMS - 1;
224 delay(150);
225 }
226
227 if (btnDown())
228 {
229 selectedIndex++;
230 if (selectedIndex >= MAX_ITEMS) selectedIndex = 0;
231 delay(150);
232 }
233
234 if (btnBack())
235 {
236 delay(150);
237 break;
238 }
239}}
240
241
242