-
Notifications
You must be signed in to change notification settings - Fork 49
/
esp-knx-ip-conversion.cpp
87 lines (73 loc) · 1.88 KB
/
esp-knx-ip-conversion.cpp
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
/**
* esp-knx-ip library for KNX/IP communication on an ESP8266
* Author: Nico Weichbrodt <envy>
* License: MIT
*/
#include "esp-knx-ip.h"
/**
* Conversion functions
*/
bool ESPKNXIP::data_to_bool(uint8_t *data)
{
return (data[0] & 0x01) == 1 ? true : false;
}
int8_t ESPKNXIP::data_to_1byte_int(uint8_t *data)
{
return (int8_t)data[1];
}
uint8_t ESPKNXIP::data_to_1byte_uint(uint8_t *data)
{
return data[1];
}
int16_t ESPKNXIP::data_to_2byte_int(uint8_t *data)
{
return (int16_t)((data[1] << 8) | data[2]);
}
uint16_t ESPKNXIP::data_to_2byte_uint(uint8_t *data)
{
return (uint16_t)((data[1] << 8) | data[2]);
}
float ESPKNXIP::data_to_2byte_float(uint8_t *data)
{
//uint8_t sign = (data[1] & 0b10000000) >> 7;
uint8_t expo = (data[1] & 0b01111000) >> 3;
int16_t mant = ((data[1] & 0b10000111) << 8) | data[2];
return 0.01f * mant * pow(2, expo);
}
time_of_day_t ESPKNXIP::data_to_3byte_time(uint8_t *data)
{
time_of_day_t time;
time.weekday = (weekday_t)((data[1] & 0b11100000) >> 5);
time.hours = (data[1] & 0b00011111);
time.minutes = (data[2] & 0b00111111);
time.seconds = (data[3] & 0b00111111);
return time;
}
date_t ESPKNXIP::data_to_3byte_data(uint8_t *data)
{
date_t date;
date.day = (data[1] & 0b00011111);
date.month = (data[2] & 0b00001111);
date.year = (data[3] & 0b01111111);
return date;
}
color_t ESPKNXIP::data_to_3byte_color(uint8_t *data)
{
color_t color;
color.red = data[1];
color.green = data[2];
color.blue = data[3];
return color;
}
int32_t ESPKNXIP::data_to_4byte_int(uint8_t *data)
{
return (int32_t)((data[1] << 24) | (data[2] << 16) | (data[3] << 8) | (data[4] << 0));
}
uint32_t ESPKNXIP::data_to_4byte_uint(uint8_t *data)
{
return (uint32_t)((data[1] << 24) | (data[2] << 16) | (data[3] << 8) | (data[4] << 0));
}
float ESPKNXIP::data_to_4byte_float(uint8_t *data)
{
return (float)((data[1] << 24) | (data[2] << 16) | (data[3] << 8) |data[4]);
}