Skip to content

Commit 11cac89

Browse files
committed
feat: caught, fled and spin counter
1 parent c2f6923 commit 11cac89

File tree

5 files changed

+71
-85
lines changed

5 files changed

+71
-85
lines changed

pgpemu-esp32/main/pgp_led_handler.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pgp_autobutton.h"
1010
#include "pgp_autosetting.h"
1111
#include "settings.h"
12+
#include "stats.h"
1213
#include "uart.h"
1314

1415
const int retoggle_delay = 300000;
@@ -148,11 +149,13 @@ void handle_led_notify_from_app(esp_gatt_if_t gatts_if, uint16_t conn_id, const
148149
}
149150
} else if (count_ballshake) {
150151
if (count_blue && count_green) {
152+
increment_caught(conn_id);
151153
ESP_LOGI(LEDHANDLER_TAG,
152154
"[%d] Caught Pokemon after %d ball shakes.",
153155
conn_id,
154156
count_ballshake);
155157
} else if (count_red) {
158+
increment_fled(conn_id);
156159
ESP_LOGW(LEDHANDLER_TAG,
157160
"[%d] Pokemon fled after %d ball shakes.",
158161
conn_id,
@@ -165,6 +168,7 @@ void handle_led_notify_from_app(esp_gatt_if_t gatts_if, uint16_t conn_id, const
165168
}
166169
} else if (count_red && count_green && count_blue && !count_off) {
167170
// blinking grb-grb...
171+
increment_spin(conn_id);
168172
ESP_LOGI(LEDHANDLER_TAG, "[%d] Got items from Pokestop.", conn_id);
169173
} else {
170174
if (get_setting(&settings.autospin) || get_setting(&settings.autocatch)) {

pgpemu-esp32/main/pgpemu.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
#include "pgp_autobutton.h"
88
#include "pgp_autosetting.h"
99
#include "pgp_bluetooth.h"
10-
#include "pgp_gap.h"
11-
#include "pgp_gatts.h"
1210
#include "secrets.h"
1311
#include "settings.h"
1412
#include "setup_button.h"
15-
#include "stats.h"
1613
#include "uart.h"
1714

1815
void app_main() {
@@ -76,9 +73,6 @@ void app_main() {
7673
ESP_LOGI(PGPEMU_TAG, "input button disabled");
7774
}
7875

79-
// runtime counter
80-
init_stats();
81-
8276
// start autosetting task
8377
if (!init_autosetting()) {
8478
ESP_LOGI(PGPEMU_TAG, "creating setting task failed");

pgpemu-esp32/main/stats.c

Lines changed: 51 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,99 +6,74 @@
66
#include "log_tags.h"
77
#include "nvs.h"
88

9-
static void stats_task(void* pvParameters);
9+
#include <stdint.h>
1010

11-
static const char* KEY_RUNTIME_10MIN = "runtime10min";
11+
#define MAX_CONNECTIONS CONFIG_BT_ACL_CONNECTIONS
1212

13-
static uint32_t runtime = 0;
14-
static const uint32_t runtime_max = 500; // only count until about 3 days to avoid flash wear
13+
static StatsForConn stats[MAX_CONNECTIONS];
14+
static size_t stats_len = 0;
1515

16-
void init_stats() {
17-
xTaskCreate(stats_task, "stats_task", 2048, NULL, 9, NULL);
18-
}
19-
20-
uint32_t stats_get_runtime() {
21-
return 10 * runtime;
22-
}
23-
24-
static uint32_t read_runtime() {
25-
nvs_handle_t handle;
26-
esp_err_t err = nvs_open("stats", NVS_READONLY, &handle);
27-
if (err != ESP_OK) {
28-
if (err == ESP_ERR_NVS_NOT_FOUND) {
29-
ESP_LOGW(STATS_TAG, "runtime count initialized with 0");
30-
return 0;
16+
StatsForConn* get_conn_entry(uint16_t conn_id) {
17+
for (size_t i = 0; i < stats_len; i++) {
18+
if (stats[i].conn_id == conn_id) {
19+
return &stats[i];
3120
}
32-
ESP_LOGW(STATS_TAG, "%s nvs open failed: %s", __func__, esp_err_to_name(err));
33-
return UINT32_MAX; // error
3421
}
22+
if (stats_len < MAX_CONNECTIONS) {
23+
stats[stats_len].conn_id = conn_id;
24+
stats[stats_len].stats = (Stats){ 0 };
25+
stats_len++;
3526

36-
uint32_t runtime = 0;
37-
err = nvs_get_u32(handle, KEY_RUNTIME_10MIN, &runtime);
38-
if (err != ESP_OK) {
39-
ESP_LOGW(STATS_TAG,
40-
"%s nvs read %s failed: %s",
41-
__func__,
42-
KEY_RUNTIME_10MIN,
43-
esp_err_to_name(err));
27+
ESP_LOGI(STATS_TAG, "new entry for conn_id %d added, current len %d", conn_id, stats_len);
4428

45-
nvs_close(handle);
46-
return 0;
29+
return &stats[stats_len];
4730
}
4831

49-
nvs_close(handle);
50-
return runtime;
32+
ESP_LOGE(STATS_TAG, "no stat found for conn_id %d or impossible to store a new entry", conn_id);
33+
34+
return NULL;
5135
}
5236

53-
static void write_runtime(uint32_t runtime) {
54-
nvs_handle_t handle;
55-
esp_err_t err = nvs_open("stats", NVS_READWRITE, &handle);
56-
if (err != ESP_OK) {
57-
ESP_LOGE(STATS_TAG, "%s nvs open failed: %s", __func__, esp_err_to_name(err));
58-
return;
37+
void delete_conn_entry(uint16_t conn_id) {
38+
for (size_t i = 0; i < stats_len; i++) {
39+
if (stats[i].conn_id == conn_id) {
40+
stats[i] = stats[stats_len - 1];
41+
stats_len--;
42+
}
5943
}
44+
}
6045

61-
err = nvs_set_u32(handle, KEY_RUNTIME_10MIN, runtime);
62-
if (err != ESP_OK) {
63-
ESP_LOGE(STATS_TAG,
64-
"%s nvs write %s failed: %s",
65-
__func__,
66-
KEY_RUNTIME_10MIN,
67-
esp_err_to_name(err));
68-
nvs_close(handle);
69-
return;
46+
void increment_caught(uint16_t conn_id) {
47+
StatsForConn* s = get_conn_entry(conn_id);
48+
if (s) {
49+
s->stats.caught++;
7050
}
71-
72-
nvs_commit(handle);
73-
nvs_close(handle);
7451
}
7552

76-
static void stats_task(void* pvParameters) {
77-
TickType_t previousWakeTime = xTaskGetTickCount();
78-
runtime = read_runtime();
79-
80-
if (runtime == UINT32_MAX) {
81-
ESP_LOGE(STATS_TAG, "failed starting task");
82-
vTaskDelete(NULL);
83-
return;
53+
void increment_fled(uint16_t conn_id) {
54+
StatsForConn* s = get_conn_entry(conn_id);
55+
if (s) {
56+
s->stats.fled++;
8457
}
58+
}
8559

86-
ESP_LOGI(STATS_TAG, "task start");
87-
88-
while (true) {
89-
if (runtime > runtime_max) {
90-
ESP_LOGI(
91-
STATS_TAG, "stopping runtime counting to avoid flash wear at count %lu", runtime);
92-
break;
93-
}
94-
95-
// every 10 minutes
96-
vTaskDelayUntil(&previousWakeTime, (10 * 60 * 1000) / portTICK_PERIOD_MS);
97-
98-
runtime++;
99-
write_runtime(runtime);
100-
ESP_LOGD(STATS_TAG, "runtime counter now: %lu min", 10 * runtime);
60+
void increment_spin(uint16_t conn_id) {
61+
StatsForConn* s = get_conn_entry(conn_id);
62+
if (s) {
63+
s->stats.spin++;
10164
}
65+
}
10266

103-
vTaskDelete(NULL);
104-
}
67+
void stats_get_runtime() {
68+
for (size_t i = 0; i < stats_len; i++) {
69+
ESP_LOGI(STATS_TAG,
70+
"---STATS %d---\n"
71+
"Caught: %d\n"
72+
"Fled: %d\n"
73+
"Spin: %d",
74+
stats[i].conn_id,
75+
stats[i].stats.caught,
76+
stats[i].stats.fled,
77+
stats[i].stats.spin);
78+
}
79+
}

pgpemu-esp32/main/stats.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,21 @@
33

44
#include <stdint.h>
55

6-
void init_stats();
6+
void stats_get_runtime();
77

8-
uint32_t stats_get_runtime();
8+
void increment_caught(uint16_t conn_id);
9+
void increment_fled(uint16_t conn_id);
10+
void increment_spin(uint16_t conn_id);
11+
12+
typedef struct {
13+
uint16_t caught;
14+
uint16_t fled;
15+
uint16_t spin;
16+
} Stats;
17+
18+
typedef struct {
19+
uint16_t conn_id;
20+
Stats stats;
21+
} StatsForConn;
922

1023
#endif /* STATS_H */

pgpemu-esp32/main/uart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void process_char(uint8_t c) {
151151
}
152152
break;
153153
case 'r':
154-
ESP_LOGI(UART_TAG, "runtime: %lu min", stats_get_runtime());
154+
stats_get_runtime();
155155
break;
156156
case 'R':
157157
uart_restart_command();

0 commit comments

Comments
 (0)