-
Notifications
You must be signed in to change notification settings - Fork 7
/
awoxl_protocol.c
63 lines (54 loc) · 2.17 KB
/
awoxl_protocol.c
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
#include "awoxl_protocol.h"
#include <stdlib.h>
#include <string.h>
static unsigned char SEQUENCE_ON[12] = {0xAA, 0x0A, 0xFC, 0x3A, 0x86, 0x01, 0x0A, 0x01, 0x01, 0x00, 0x28, 0x0D};
static unsigned char SEQUENCE_OFF[12] = {0xAA, 0x0A, 0xFC, 0x3A, 0x86, 0x01, 0x0A, 0x01, 0x00, 0x01, 0x28, 0x0D};
static unsigned char SEQUENCE_BRIGHNTESS[12] = {0xAA, 0x0A, 0xFC, 0x3A, 0x86, 0x01, 0x0C, 0x01, 0x00, 0xEC, 0x15, 0x0D};
static unsigned char SEQUENCE_WHITE[12] = {0xAA, 0x0A, 0xFC, 0x3A, 0x86, 0x01, 0x0E, 0x01, 0x00, 0x14, 0x3F, 0x0D};
static unsigned char SEQUENCE_RGB[17] = {0xAA, 0x0A, 0xFC, 0x3A, 0x86, 0x01, 0x0D, 0x06, 0x01, 0x00, 0x00, 0x00, 0x20, 0x30, 0xF8, 0x78, 0x0D};
static unsigned char checksum(unsigned char* buffer, unsigned int len) {
unsigned char sum = 0;
for (unsigned int i = 1; i+2 < len; i++)
sum += buffer[i];
return sum + 85;
}
unsigned int awoxl_protocol_on(unsigned char** buffer) {
*buffer = (unsigned char*) malloc(12);
memcpy(*buffer, SEQUENCE_ON, 12);
return 12;
}
unsigned int awoxl_protocol_off(unsigned char** buffer) {
*buffer = (unsigned char*) malloc(12);
memcpy(*buffer, SEQUENCE_OFF, 12);
return 12;
}
unsigned int awoxl_protocol_brightness(unsigned char** buffer,
unsigned char brightness) {
*buffer = (unsigned char*) malloc(12);
memcpy(*buffer, SEQUENCE_BRIGHNTESS, 12);
(*buffer)[8] = brightness;
(*buffer)[9] = rand() % 256;
(*buffer)[10] = checksum(*buffer, 12);
return 12;
}
unsigned int awoxl_protocol_white(unsigned char** buffer,
unsigned char temperature) {
*buffer = (unsigned char*) malloc(12);
memcpy(*buffer, SEQUENCE_WHITE, 12);
(*buffer)[8] = temperature;
(*buffer)[9] = rand() % 256;
(*buffer)[10] = checksum(*buffer, 12);
return 12;
}
unsigned int awoxl_protocol_rgb(unsigned char** buffer,
unsigned char r, unsigned char g, unsigned char b, int special) {
*buffer = (unsigned char*) malloc(17);
memcpy(*buffer, SEQUENCE_RGB, 17);
(*buffer)[8] = special ? 0x02 : 0x01;
(*buffer)[9] = r;
(*buffer)[10] = g;
(*buffer)[11] = b;
(*buffer)[14] = rand() % 256;
(*buffer)[15] = checksum(*buffer, 17);
return 17;
}