Skip to content

Commit a7c9596

Browse files
author
Scott Powell
committed
* companion: added Datastore methods: getStorageUsedKb(), getStorageTotalKb()
* companion: CMD_GET_BATTERY now changed to CMD_GET_BATT_AND_STORAGE
1 parent 85b164b commit a7c9596

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

examples/companion_radio/DataStore.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,36 @@ void DataStore::begin() {
4040
#include <SPIFFS.h>
4141
#elif defined(RP2040_PLATFORM)
4242
#include <LittleFS.h>
43+
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
44+
#include <InternalFileSystem.h>
4345
#endif
4446

47+
uint32_t DataStore::getStorageUsedKb() const {
48+
#if defined(ESP32)
49+
return SPIFFS.usedBytes() / 1024;
50+
#elif defined(RP2040_PLATFORM)
51+
FSInfo info;
52+
info.usedBytes = 0;
53+
_fs->info(info);
54+
return info.usedBytes / 1024;
55+
#else
56+
return 0; // TODO: InternalFS. method?
57+
#endif
58+
}
59+
60+
uint32_t DataStore::getStorageTotalKb() const {
61+
#if defined(ESP32)
62+
return SPIFFS.totalBytes() / 1024;
63+
#elif defined(RP2040_PLATFORM)
64+
FSInfo info;
65+
info.totalBytes = 0;
66+
_fs->info(info);
67+
return info.totalBytes / 1024;
68+
#else
69+
return 0; // TODO: InternalFS. method?
70+
#endif
71+
}
72+
4573
File DataStore::openRead(const char* filename) {
4674
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
4775
return _fs->open(filename, FILE_O_READ);

examples/companion_radio/DataStore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ class DataStore {
3939
bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len);
4040
File openRead(const char* filename);
4141
bool removeFile(const char* filename);
42+
uint32_t getStorageUsedKb() const;
43+
uint32_t getStorageTotalKb() const;
4244
};

examples/companion_radio/MyMesh.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define CMD_EXPORT_CONTACT 17
2323
#define CMD_IMPORT_CONTACT 18
2424
#define CMD_REBOOT 19
25-
#define CMD_GET_BATTERY_VOLTAGE 20
25+
#define CMD_GET_BATT_AND_STORAGE 20 // was CMD_GET_BATTERY_VOLTAGE
2626
#define CMD_SET_TUNING_PARAMS 21
2727
#define CMD_DEVICE_QEURY 22
2828
#define CMD_EXPORT_PRIVATE_KEY 23
@@ -58,7 +58,7 @@
5858
#define RESP_CODE_CURR_TIME 9 // a reply to CMD_GET_DEVICE_TIME
5959
#define RESP_CODE_NO_MORE_MESSAGES 10 // a reply to CMD_SYNC_NEXT_MESSAGE
6060
#define RESP_CODE_EXPORT_CONTACT 11
61-
#define RESP_CODE_BATTERY_VOLTAGE 12 // a reply to a CMD_GET_BATTERY_VOLTAGE
61+
#define RESP_CODE_BATT_AND_STORAGE 12 // a reply to a CMD_GET_BATT_AND_STORAGE
6262
#define RESP_CODE_DEVICE_INFO 13 // a reply to CMD_DEVICE_QEURY
6363
#define RESP_CODE_PRIVATE_KEY 14 // a reply to CMD_EXPORT_PRIVATE_KEY
6464
#define RESP_CODE_DISABLED 15
@@ -1016,12 +1016,17 @@ void MyMesh::handleCmdFrame(size_t len) {
10161016
saveContacts();
10171017
}
10181018
board.reboot();
1019-
} else if (cmd_frame[0] == CMD_GET_BATTERY_VOLTAGE) {
1020-
uint8_t reply[3];
1021-
reply[0] = RESP_CODE_BATTERY_VOLTAGE;
1019+
} else if (cmd_frame[0] == CMD_GET_BATT_AND_STORAGE) {
1020+
uint8_t reply[11];
1021+
int i = 0;
1022+
reply[i++] = RESP_CODE_BATT_AND_STORAGE;
10221023
uint16_t battery_millivolts = board.getBattMilliVolts();
1023-
memcpy(&reply[1], &battery_millivolts, 2);
1024-
_serial->writeFrame(reply, 3);
1024+
uint32_t used = _store->getStorageUsedKb();
1025+
uint32_t total = _store->getStorageTotalKb();
1026+
memcpy(&reply[i], &battery_millivolts, 2); i += 2;
1027+
memcpy(&reply[i], &used, 4); i += 4;
1028+
memcpy(&reply[i], &total, 4); i += 4;
1029+
_serial->writeFrame(reply, i);
10251030
} else if (cmd_frame[0] == CMD_EXPORT_PRIVATE_KEY) {
10261031
#if ENABLE_PRIVATE_KEY_EXPORT
10271032
uint8_t reply[65];

0 commit comments

Comments
 (0)