Skip to content

Commit 3b813c0

Browse files
committed
update
1 parent 705e80b commit 3b813c0

File tree

13 files changed

+140
-134
lines changed

13 files changed

+140
-134
lines changed

sketchbooks/enr_message_board/include/message.h

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <M5EPD.h>
2+
3+
void clear_canvas(M5EPD_Canvas &canvas) {
4+
canvas.clear();
5+
canvas.setCursor(0, 0);
6+
}
7+
8+
void init_epd(M5EPD_Canvas &canvas_title, M5EPD_Canvas &canvas_status, M5EPD_Canvas &canvas_message) {
9+
canvas_title.createCanvas(540, 100);
10+
canvas_status.createCanvas(540, 60);
11+
canvas_message.createCanvas(540, 800);
12+
canvas_title.setTextSize(3);
13+
canvas_status.setTextSize(2);
14+
canvas_message.setTextSize(2);
15+
clear_canvas(canvas_title);
16+
clear_canvas(canvas_status);
17+
clear_canvas(canvas_message);
18+
}
19+
20+
void update_epd(M5EPD_Canvas &canvas_title, M5EPD_Canvas &canvas_status, M5EPD_Canvas &canvas_message) {
21+
canvas_title.pushCanvas(0, 0, UPDATE_MODE_DU4);
22+
canvas_status.pushCanvas(0, 100, UPDATE_MODE_DU4);
23+
canvas_message.pushCanvas(0, 160, UPDATE_MODE_DU4);
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <smart_device_protocol/Packet.h>
2+
3+
#include <variant>
4+
#include <vector>
5+
6+
#include "sdp/packet_creator.h"
7+
#include "sdp/packet_parser.h"
8+
9+
class Message {
10+
public:
11+
inline static std::string packet_description_write = "Message Board to write";
12+
inline static std::string serialization_format_write = "siS";
13+
inline static std::string packet_description_message = "Message Board message";
14+
15+
char message[64];
16+
char source_name[64];
17+
unsigned long deadline;
18+
19+
Message(char *source_name, char *message, int32_t timeout_duration) {
20+
strncpy(this->source_name, source_name, 16);
21+
strncpy(this->message, message, 64);
22+
this->deadline = (int32_t)millis() + timeout_duration;
23+
}
24+
25+
void to_v2_packet(uint8_t *data) {
26+
std::vector<std::variant<int32_t, float, std::string, bool>> data_vector;
27+
data_vector.push_back(std::variant<int32_t, float, std::string, bool>(std::string(source_name)));
28+
data_vector.push_back(std::variant<int32_t, float, std::string, bool>((int32_t)(this->deadline - millis())));
29+
data_vector.push_back(std::variant<int32_t, float, std::string, bool>(std::string(message)));
30+
generate_data_frame(data, packet_description_message.c_str(), data_vector);
31+
}
32+
33+
static SDPInterfaceDescription get_interface_description() {
34+
return std::make_tuple(packet_description_message, serialization_format_write);
35+
}
36+
};

sketchbooks/enr_message_board/src/main.cpp renamed to sketchbooks/sdp_message_board/src/main.cpp

Lines changed: 80 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
#include <vector>
99

1010
#include "devices/uwb_module_util.h"
11+
#include "epd.h"
1112
#include "m5stack_utils/m5paper.h"
1213
#include "message.h"
1314
#include "sdp/sdp.h"
1415
#include "utils/config_loader.h"
1516

1617
// CONFIG
17-
String device_name = "msg_board";
18+
String device_name;
1819

1920
// CANVAS
2021
M5EPD_Canvas canvas_title(&M5.EPD);
@@ -25,6 +26,7 @@ M5EPD_Canvas canvas_message(&M5.EPD);
2526
int uwb_id = -1;
2627
std::string packet_description_uwb = "UWB Station";
2728
std::string serialization_format_uwb = "i";
29+
SDPInterfaceDescription interface_description_uwb = std::make_tuple(packet_description_uwb, serialization_format_uwb);
2830
std::vector<SDPData> data_for_uwb_data_packet;
2931

3032
// SDP
@@ -36,15 +38,6 @@ std::vector<Message> message_board;
3638
// Others
3739
int loop_counter = 0;
3840

39-
void OnDataRecvV1(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
40-
uint8_t packet_type = get_packet_type(data);
41-
if (packet_type == smart_device_protocol::Packet::PACKET_TYPE_DEVICE_MESSAGE_BOARD_DATA) {
42-
auto m = Message(data);
43-
message_board.push_back(m);
44-
Serial.printf("Push message from V1 Data\n");
45-
}
46-
}
47-
4841
void callback_for_v2(const uint8_t *mac_addr, const std::vector<SDPData> &body) {
4942
std::string source_name = std::get<std::string>(body[0]);
5043
int32_t duration_until_deletion = std::get<int32_t>(body[1]);
@@ -55,43 +48,23 @@ void callback_for_v2(const uint8_t *mac_addr, const std::vector<SDPData> &body)
5548
Serial.printf("Push message from V2 Data\n");
5649
}
5750

58-
void load_config() {
51+
bool load_config(fs::FS &fs, const String &filename) {
5952
StaticJsonDocument<1024> doc;
60-
if (not load_json_from_FS<1024>(SD, "/config.json", doc)) {
61-
return;
53+
if (not load_json_from_FS<1024>(fs, filename, doc)) {
54+
return false;
55+
}
56+
if (not doc.containsKey("device_name") or
57+
not doc.containsKey("uwb_id")) {
58+
return false;
6259
}
63-
if (doc.containsKey("device_name"))
64-
device_name = doc["device_name"].as<String>();
65-
if (doc.containsKey("uwb_id"))
66-
uwb_id = doc["uwb_id"].as<int>();
67-
}
68-
69-
void clear_canvas(M5EPD_Canvas &canvas) {
70-
canvas.clear();
71-
canvas.setCursor(0, 0);
72-
}
7360

74-
void init_epd(M5EPD_Canvas &canvas_title, M5EPD_Canvas &canvas_status, M5EPD_Canvas &canvas_message) {
75-
canvas_title.createCanvas(540, 100);
76-
canvas_status.createCanvas(540, 60);
77-
canvas_message.createCanvas(540, 800);
78-
canvas_title.setTextSize(3);
79-
canvas_status.setTextSize(2);
80-
canvas_message.setTextSize(2);
81-
clear_canvas(canvas_title);
82-
clear_canvas(canvas_status);
83-
clear_canvas(canvas_message);
84-
}
61+
device_name = doc["device_name"].as<String>();
62+
uwb_id = doc["uwb_id"].as<int>();
8563

86-
void update_epd(M5EPD_Canvas &canvas_title, M5EPD_Canvas &canvas_status, M5EPD_Canvas &canvas_message) {
87-
canvas_title.pushCanvas(0, 0, UPDATE_MODE_DU4);
88-
canvas_status.pushCanvas(0, 100, UPDATE_MODE_DU4);
89-
canvas_message.pushCanvas(0, 160, UPDATE_MODE_DU4);
64+
return true;
9065
}
9166

9267
void setup() {
93-
esp_read_mac(mac_address, ESP_MAC_WIFI_STA);
94-
9568
// Init M5Paper
9669
M5.begin(false, true, true, true, false);
9770
M5.EPD.SetRotation(90);
@@ -101,51 +74,90 @@ void setup() {
10174
Serial.println("Start init");
10275
Serial1.begin(115200, SERIAL_8N1, M5StackSerialPortInfoList[PORT_C].rx, M5StackSerialPortInfoList[PORT_C].tx);
10376

77+
// EPD
78+
canvas_title.printf("SDP MESSAGE BOARD\n");
79+
update_epd(canvas_title, canvas_status, canvas_message);
80+
10481
// Load config
105-
load_config();
82+
if (not load_config(SD, "/config.json")) {
83+
Serial.println("Failed to load config");
84+
canvas_status.printf("Failed to load config\n");
85+
update_epd(canvas_title, canvas_status, canvas_message);
86+
while (true) {
87+
delay(1000);
88+
}
89+
}
90+
Serial.println("Config loaded");
91+
clear_canvas(canvas_status);
92+
canvas_status.printf("Config loaded\n");
93+
update_epd(canvas_title, canvas_status, canvas_message);
94+
95+
// Show device name
96+
canvas_title.printf("Name: %s\n", device_name.c_str());
97+
update_epd(canvas_title, canvas_status, canvas_message);
10698

10799
// Init SDP
108-
init_sdp(mac_address, device_name);
109-
register_sdp_esp_now_recv_callback(OnDataRecvV1);
110-
register_sdp_interface_callback(Message::get_interface_description(), callback_for_v2);
100+
if (not init_sdp(mac_address, device_name)) {
101+
Serial.println("Failed to initialize SDP");
102+
canvas_status.printf("Failed to initialize SDP\n");
103+
update_epd(canvas_title, canvas_status, canvas_message);
104+
while (true) {
105+
delay(1000);
106+
}
107+
}
108+
if (not register_sdp_interface_callback(Message::get_interface_description(), callback_for_v2)) {
109+
Serial.println("Failed to register callback for V2");
110+
canvas_status.printf("Failed to register callback for V2\n");
111+
update_epd(canvas_title, canvas_status, canvas_message);
112+
while (true) {
113+
delay(1000);
114+
}
115+
}
111116
Serial.println("SDP Initialized!");
117+
clear_canvas(canvas_status);
118+
canvas_status.printf("SDP Initialized!\n");
119+
update_epd(canvas_title, canvas_status, canvas_message);
120+
121+
// Show Address
122+
canvas_title.printf("ADDR: %2x:%2x:%2x:%2x:%2x:%2x\n",
123+
mac_address[0], mac_address[1],
124+
mac_address[2], mac_address[3],
125+
mac_address[4], mac_address[5]);
126+
Serial.printf("ADDR: %2x:%2x:%2x:%2x:%2x:%2x\n",
127+
mac_address[0], mac_address[1],
128+
mac_address[2], mac_address[3],
129+
mac_address[4], mac_address[5]);
130+
update_epd(canvas_title, canvas_status, canvas_message);
112131

113132
// UWB Initialized
114-
if (uwb_id >= 0 ) {
133+
if (uwb_id >= 0) {
115134
bool result = initUWB(false, uwb_id, Serial1);
116135
data_for_uwb_data_packet.clear();
117136
data_for_uwb_data_packet.push_back(SDPData(uwb_id));
118137
if (result) {
119138
Serial.println("Success for initialization of UWB");
139+
Serial.printf("UWB_ID: %d\n", uwb_id);
140+
canvas_title.printf("UWB_ID: %d\n", uwb_id);
120141
} else {
121142
uwb_id = -1;
122143
resetUWB(Serial1);
123144
Serial.println("Failed to initialize UWB");
145+
canvas_title.printf("Failed to initialize UWB\n");
124146
}
125147
} else {
126148
resetUWB(Serial1);
127149
Serial.println("UWB is not used");
150+
canvas_title.printf("UWB is not used\n");
128151
}
129-
130-
// Show device info
131-
canvas_title.printf("ENR & SDP MESSAGE BOARD\n");
132-
canvas_title.printf("Name: %s\n", device_name.c_str());
133-
canvas_title.printf("ADDR: %2x:%2x:%2x:%2x:%2x:%2x\n",
134-
mac_address[0], mac_address[1],
135-
mac_address[2], mac_address[3],
136-
mac_address[4], mac_address[5]);
137-
canvas_title.printf("UWB_ID: %d\n", uwb_id);
138152
update_epd(canvas_title, canvas_status, canvas_message);
153+
154+
delay(3000);
139155
}
140156

141157
void loop() {
142158
Serial.printf("Loop %d\n", loop_counter);
143159
uint8_t buf[250];
144160

145-
// Manually Send V1 Meta Packet
146-
create_device_message_board_meta_packet(buf, device_name.c_str());
147-
broadcast_sdp_esp_now_packet((uint8_t *)buf, sizeof(buf));
148-
149161
// Show Battery voltage
150162
uint32_t battery_voltage = M5.getBatteryVoltage();
151163
clear_canvas(canvas_status);
@@ -155,7 +167,7 @@ void loop() {
155167
canvas_status.printf("x Battery: %u\n", battery_voltage);
156168
}
157169

158-
// Shoe messages
170+
// Show messages and send SDP packet
159171
clear_canvas(canvas_message);
160172
for (auto m = message_board.begin(); m != message_board.end();) {
161173
if (millis() > m->deadline) {
@@ -170,15 +182,21 @@ void loop() {
170182
canvas_message.printf("Duration until deletion(sec): %d\n", (int)((m->deadline - millis()) / 1000));
171183
canvas_message.printf("Message: %s\n\n", m->message);
172184

173-
m->to_v1_packet(buf);
174-
broadcast_sdp_esp_now_packet((uint8_t *)buf, sizeof(buf));
175-
delay(10);
176185
m->to_v2_packet(buf);
177186
broadcast_sdp_esp_now_packet((uint8_t *)buf, sizeof(buf));
178187
delay(10);
179188
}
180189
update_epd(canvas_title, canvas_status, canvas_message);
181190

191+
// Send UWB packet
192+
if (uwb_id >= 0) {
193+
if (not send_sdp_data_packet(interface_description_uwb, data_for_uwb_data_packet)) {
194+
Serial.println("Failed to send UWB packet");
195+
canvas_status.printf("Failed to send UWB packet\n");
196+
update_epd(canvas_title, canvas_status, canvas_message);
197+
}
198+
}
199+
182200
delay(100);
183201
loop_counter++;
184202
}

0 commit comments

Comments
 (0)