-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.c
More file actions
executable file
·127 lines (109 loc) · 3.43 KB
/
packet.c
File metadata and controls
executable file
·127 lines (109 loc) · 3.43 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
121
122
123
124
125
126
127
#include "packet.h"
#include "network.h"
#include "espmissingincludes.h"
#include "ssid.h"
#define RED_COMPONENT(x) (x) >> 12 & 0xF
#define GREEN_COMPONENT(x) (x) >> 8 & 0xF
#define BLUE_COMPONENT(x) (x) >> 4 & 0xF
#define COLOR(r,g,b) ((((r) & 0xF) << 12) | (((g) & 0xF) << 8) | (((b) & 0xF) << 4))
#define DISTANCE_FLAG_BIT 0
#define SOUND_FLAG_BIT 1
struct qsy_packet_p {
char signature[3];
uint8_t type;
uint16_t id;
uint16_t color;
uint32_t delay;
uint16_t step;
uint16_t config;
} __attribute__ ((packed));
void ICACHE_FLASH_ATTR packet_init(struct qsy_packet *packet)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
ets_memset(p, 0, QSY_PACKET_SIZE);
p->signature[0] = 'Q';
p->signature[1] = 'S';
p->signature[2] = 'Y';
p->id = htons(NODE_ID);
}
enum packet_type ICACHE_FLASH_ATTR packet_get_type(struct qsy_packet *packet)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
return (enum packet_type) p->type;
}
struct color ICACHE_FLASH_ATTR packet_get_color(const struct qsy_packet *packet)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
struct color res;
uint16_t hcolor = ntohs(p->color);
res.red = RED_COMPONENT(hcolor);
res.green = GREEN_COMPONENT(hcolor);
res.blue = BLUE_COMPONENT(hcolor);
return res;
}
uint32_t ICACHE_FLASH_ATTR packet_get_delay(const struct qsy_packet *packet)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
return ntohl(p->delay);
}
bool ICACHE_FLASH_ATTR packet_get_sound(const struct qsy_packet *packet)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
return (1 << SOUND_FLAG_BIT) & ntohs(p->config);
}
bool ICACHE_FLASH_ATTR packet_get_distance(const struct qsy_packet *packet)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
return (1 << DISTANCE_FLAG_BIT) & ntohs(p->config);
}
void ICACHE_FLASH_ATTR packet_set_sound(struct qsy_packet *packet, bool value)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
uint16_t config = ntohs(p->config);
if (value)
config |= 1 << SOUND_FLAG_BIT;
else
config &= ~(1 << SOUND_FLAG_BIT);
p->config = htons(p->config);
}
void ICACHE_FLASH_ATTR packet_set_distance(struct qsy_packet *packet, bool value)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
uint16_t config = ntohs(p->config);
if (value)
config |= 1 << DISTANCE_FLAG_BIT;
else
config &= ~(1 << DISTANCE_FLAG_BIT);
p->config = htons(p->config);
}
void ICACHE_FLASH_ATTR packet_set_type(struct qsy_packet *packet, enum packet_type type)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
p->type = (uint8_t) type;
}
void ICACHE_FLASH_ATTR packet_set_color(struct qsy_packet *packet, struct color c)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
uint16_t hcolor = COLOR(c.red, c.green, c.blue);
p->color = htons(hcolor);
}
void ICACHE_FLASH_ATTR packet_set_delay(struct qsy_packet *packet, uint32_t delay)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
p->delay = htonl(delay);
}
bool ICACHE_FLASH_ATTR packet_is_valid(const struct qsy_packet *packet)
{
return packet->private[0] == 'Q' && packet->private[1] == 'S' &&
packet->private[2] == 'Y';
}
uint16_t ICACHE_FLASH_ATTR packet_get_step(const struct qsy_packet *packet)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
return ntohs(p->step);
}
void ICACHE_FLASH_ATTR packet_set_step(struct qsy_packet *packet, uint16_t step)
{
struct qsy_packet_p *p = (struct qsy_packet_p *) packet;
p->step = htons(step);
}