-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnearby.c
218 lines (177 loc) · 6.62 KB
/
nearby.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// Created by hamster on 8/2/18.
//
#include "common.h"
#include "nearby.h"
#define BLOCK_HEIGHT 50
#define MAX_ROWS 2
static BADGE_ADV badges[NUM_BADGES_TO_STORE];
static uint8_t numBadges = 0;
/**
* Draw a caret at the start of the y value given
* @param y where to start the row
*/
static void drawMarker(uint16_t oldY, uint16_t newY){
util_gfx_draw_line(GFX_WIDTH - 1, oldY + 3, GFX_WIDTH - 1, oldY + BLOCK_HEIGHT - 8, COLOR_BLACK);
util_gfx_draw_line(0, oldY + 3, 0, oldY + BLOCK_HEIGHT - 8, COLOR_BLACK);
util_gfx_draw_line(GFX_WIDTH - 1, newY + 3, GFX_WIDTH - 1, newY + BLOCK_HEIGHT - 8, COLOR_LIGHTBLUE);
util_gfx_draw_line(0, newY + 3, 0, newY + BLOCK_HEIGHT - 8, COLOR_LIGHTBLUE);
}
/**
* Draw a few rows of nearby
* @param menu
* @param startY
* @param numItems
* @param startRow
*/
static void drawItems(BADGE_ADV *badge, uint8_t numItems, uint8_t startRow){
char row[25];
util_gfx_fill_rect(0, 15, GFX_WIDTH, GFX_HEIGHT - 15, COLOR_BLACK);
for(int i = 0; i < numItems; i++){
// Line at the top
util_gfx_draw_line(10, (BLOCK_HEIGHT * i) + 17, GFX_WIDTH - 10, (BLOCK_HEIGHT * i) + 17, COLOR_WHITE);
util_gfx_set_cursor(2, (BLOCK_HEIGHT * i) + 20);
util_gfx_set_font(FONT_VERAMONO_5PT);
// First row
snprintf(row, 25, "%d %02X%02X%02X%02X%02X%02X DC%d", i + startRow + 1,
badge[i + startRow].mac[0], badge[i + startRow].mac[1], badge[i + startRow].mac[2],
badge[i + startRow].mac[3], badge[i + startRow].mac[4], badge[i + startRow].mac[5],
badge[i + startRow].year == badgeYear_25 ? 25 : 26);
util_gfx_print(row);
// Second row
util_gfx_set_cursor(2, (BLOCK_HEIGHT * i) + 30);
snprintf(row, 25, "%s", badge[i + startRow].name);
util_gfx_print(row);
util_gfx_set_cursor(83, (BLOCK_HEIGHT * i) + 30);
snprintf(row, 25, "% 4ddB", badge[i + startRow].rssi);
util_gfx_print(row);
// Third row
util_gfx_set_cursor(2, (BLOCK_HEIGHT * i) + 40);
snprintf(row, 25, "%s", getBadgeGroupName(badge[i + startRow].group));
util_gfx_print(row);
util_gfx_set_cursor(95, (BLOCK_HEIGHT * i) + 40);
snprintf(row, 25, "%04X", badge[i + startRow].appearance);
util_gfx_print(row);
memset(row, 0, sizeof(row));
if(badge[i + startRow].year == badgeYear_26) {
if (badge[i + startRow].group == badge_dc801) {
// Show the score
uint8_t scoreBytes[4];
scoreBytes[0] = badge[i + startRow].data[1];
scoreBytes[1] = badge[i + startRow].data[6];
scoreBytes[2] = badge[i + startRow].data[4];
scoreBytes[3] = badge[i + startRow].data[2];
// check the CRC on the received score
uint16_t crc = (badge[i + startRow].data[3] << 8) | badge[i + startRow].data[5];
uint32_t score = 0;
if(calcCRC(scoreBytes, 4) == crc) {
score = (scoreBytes[3] << 24) |
(scoreBytes[2] << 16) |
(scoreBytes[1] << 8) |
(scoreBytes[0]);
}
snprintf(row, 25, "Score %u", score);
}
if (badge[i + startRow].group == badge_dcfurs) {
if (badge[i + startRow].data[0] == 0x35) {
snprintf(row, 25, "Has Rabies - RUN!");
} else {
snprintf(row, 25, "Friendly");
}
}
}
util_gfx_set_cursor(2, (BLOCK_HEIGHT * i) + 50);
util_gfx_print(row);
}
}
/**
* Draw the nearby items
*/
void drawNearby(void){
numBadges = getBadges(&badges[0]);
// Setup the main screen
util_gfx_fill_screen(COLOR_BLACK);
// Show header
util_gfx_set_font(FONT_VERAMONO_5PT);
util_gfx_set_color(COLOR_WHITE);
char header[15];
if(numBadges == 1){
snprintf(header, 15, "I see 1 badge");
}
else {
snprintf(header, 15, "I see %u badges", numBadges);
}
uint16_t w, h;
util_gfx_get_text_bounds(header, 0, 0, &w, &h);
util_gfx_set_cursor(64 - (w / 2), 0);
util_gfx_print(header);
drawItems(badges, MIN(numBadges, MAX_ROWS), 0);
}
/**
* Draw the nearby menu
*/
void nearby(void){
bool keepWaiting = true;
uint8_t selected = 0;
uint8_t cursor = 0;
uint8_t startRow = 0;
drawNearby();
if(numBadges > 0) {
drawMarker(15, 15);
}
while(keepWaiting) {
switch (getButton(false)) {
case USER_BUTTON_UP:
// Are we at the top of the list of items?
if(selected > 0){
// Do we need to scroll up?
if(cursor == 0){
// Need to scroll
drawItems(badges, MIN(numBadges, MAX_ROWS), --startRow);
drawMarker((cursor * BLOCK_HEIGHT) + 15, (cursor * BLOCK_HEIGHT) + 15);
}
else {
// No scroll, just move up
drawMarker((cursor * BLOCK_HEIGHT) + 15, ((cursor - 1) * BLOCK_HEIGHT) + 15);
cursor--;
}
selected--;
}
while(getButton(false) == USER_BUTTON_UP);
break;
case USER_BUTTON_DOWN:
if(selected < numBadges - 1){
// Do we need to scroll down?
if(cursor == 1){
// Need to scroll
drawItems(badges, MIN(numBadges, MAX_ROWS), ++startRow);
drawMarker((cursor * BLOCK_HEIGHT) + 15, (cursor * BLOCK_HEIGHT) + 15);
}
else {
// No scroll, just move down
drawMarker((cursor * BLOCK_HEIGHT) + 15, ((cursor + 1) * BLOCK_HEIGHT) + 15);
cursor++;
}
selected++;
}
while(getButton(false) == USER_BUTTON_DOWN);
break;
case USER_BUTTON_B:
keepWaiting = false;
break;
case USER_BUTTON_A:
selected = 0;
cursor = 0;
startRow = 0;
drawNearby();
if(numBadges > 0) {
drawMarker(15, 15);
}
break;
default:
break;
}
nrf_delay_ms(25);
}
while(getButton(false) == USER_BUTTON_B){ }
}