-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi_streamer.c
More file actions
120 lines (90 loc) · 2.12 KB
/
wifi_streamer.c
File metadata and controls
120 lines (90 loc) · 2.12 KB
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
#include <Wire.h>
#include <stdint.h>
/*
* com.hpp
*
* Created on: Sep 16, 2024
* Author: sachin
*/
#ifndef INC_COM_HPP_
#define INC_COM_HPP_
#include <stdint.h>
#include <string.h>
#define WIFI_BOARD 7
enum UserStatus {
Ok = 1,
Unhealthy = 2,
Critical = 3,
Unknown = 4,
};
typedef struct {
uint8_t buf[8];
} Ping;
union MessageType {
UserStatus status;
Ping ping;
};
enum MessageTypeIdx {
UserStatusIdx = 1,
PingIdx = 2
};
uint8_t TX_BUF[64] = { 0 };
uint8_t RX_BUF[64] = { 0 };
class Message {
public:
MessageTypeIdx type;
MessageType message;
Message(Ping ping)
: type(MessageTypeIdx::PingIdx), message(MessageType { .ping = ping }) {}
Message(UserStatus status)
: type(MessageTypeIdx::UserStatusIdx), message(MessageType { .status = status }) {}
static Message deserialize(uint8_t* buffer) {
uint16_t ptr = 0;
MessageType msg;
MessageTypeIdx command = (MessageTypeIdx)(buffer[ptr]); ptr++;
switch (command) {
case MessageTypeIdx::PingIdx:
memcpy(&msg.ping.buf, buffer + ptr, 8); ptr += 8;
return Message(msg.ping);
case MessageTypeIdx::UserStatusIdx:
msg.status = (UserStatus)(buffer[ptr]); ptr += 8;
return Message(msg.status);
}
msg.status = UserStatus::Unknown;
return Message(msg.status);
}
private:
uint16_t serialize() {
memset(TX_BUF, 0, 64);
uint16_t ptr = 0;
TX_BUF[ptr] = (uint8_t)(this->type); ptr++;
switch (this->type) {
case MessageTypeIdx::PingIdx:
memcpy(TX_BUF, this->message.ping.buf, 8); ptr += 8;
case MessageTypeIdx::UserStatusIdx:
TX_BUF[ptr] = (uint8_t)(this->message.status); ptr++;
}
return ptr;
}
};
#endif /* INC_COM_HPP_ */
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
Wire.begin();
Wire.onReceive(recvUserStatus);
}
void recvUserStatus(int bytes) {
uint8_t buf[bytes] = { 0 };
while (Wire.available()) {
Wire.readBytes(buf, bytes);
}
Message msg = Message::deserialize(buf);
Serial.println(msg.type);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("heartbeat");
delay(1000);
}