Krolyxon
Commit ef5352a

Branches

Tags

Refs →

orion-rffirmwaredevice_check.cpp

207 lines · 3.7 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 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 =====
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_1()
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
54bool checkCC1101_2()
55{
56 ELECHOUSE_cc1101.setSpiPin(
57 cc1101_SCK,
58 cc1101_MISO,
59 cc1101_MOSI,
60 CC1101_2_CS
61 );
62
63 delay(5); // important stabilization
64
65 return ELECHOUSE_cc1101.getCC1101();
66}
67
68
69// ===== BUTTON CHECK =====
70bool checkButtons()
71{
72 u8g2.clearBuffer();
73 u8g2.setFont(u8g2_font_6x10_tr);
74 u8g2.drawStr(0, 20, "Press any button...");
75 u8g2.sendBuffer();
76
77 unsigned long start = millis();
78
79 while (millis() - start < 2000)
80 {
81 if (!digitalRead(BTN_UP) ||
82 !digitalRead(BTN_DOWN) ||
83 !digitalRead(BTN_SELECT) ||
84 !digitalRead(BTN_BACK))
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
97const char* labels[MAX_ITEMS] = {
98 "NRF1",
99 "NRF2",
100 "CC1101_1",
101 "CC1101_2",
102 "BUTTONS",
103 "OLED"
104};
105
106bool values[MAX_ITEMS];
107
108int selectedIndex = 0;
109int offset = 0;
110
111void 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 u8g2.clearBuffer();
121 u8g2.setFont(u8g2_font_6x10_tr);
122
123 // scrolling logic
124 if (selectedIndex < offset)
125 offset = selectedIndex;
126
127 if (selectedIndex >= offset + VISIBLE_ROWS)
128 offset = selectedIndex - VISIBLE_ROWS + 1;
129
130 for (int i = 0; i < VISIBLE_ROWS; i++)
131 {
132 int item = offset + i;
133 if (item >= MAX_ITEMS) break;
134
135 int y = 12 + i * 10;
136
137 if (item == selectedIndex)
138 {
139 u8g2.drawBox(0, y - 9, 128, 10);
140 u8g2.setDrawColor(0);
141 }
142
143 u8g2.drawStr(2, y, labels[item]);
144
145 if (values[item])
146 u8g2.drawStr(80, y, "OK");
147 else
148 u8g2.drawStr(80, y, "FAIL");
149
150 if (item == selectedIndex)
151 u8g2.setDrawColor(1);
152 }
153
154 u8g2.sendBuffer();
155}
156
157
158// ===== MAIN =====
159void device_check_run()
160{
161 DeviceStatus status;
162
163 Serial.println("Running device diagnostics...");
164
165 // NRF
166 // NRF link test
167 status.nrf1 = checkNRF(radio1);
168 status.nrf2 = checkNRF(radio2);
169
170 // CC1101
171 status.cc1101_1 = checkCC1101_1();
172 status.cc1101_2 = checkCC1101_1();
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