knx
ETS configurable knx-stack
datapoint_types.cpp
Go to the documentation of this file.
1 /*
2  * datapoint_types.h - Conversion functions for datapoint types.
3  *
4  * Copyright (c) 2014 Stefan Taferner <stefan.taferner@gmx.at>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include "datapoint_types.h"
12 #include <stdint.h>
13 
14 // Sign for a negative DPT9 float value
15 #define DPT_FLOAT_NEG_SIGN 0x8000
16 
17 
18 uint16_t dptToFloat(int32_t value)
19 {
20  uint16_t exp = 0;
21 
22  if (value < -67108864 || value > 67076096)
23  return 0x7fff;
24 
25  if (value < 0)
26  {
27  while (value < -2048)
28  {
29  value >>= 1;
30  ++exp;
31  }
32 
33  return DPT_FLOAT_NEG_SIGN | (((int32_t) value) & 2047) | (exp << 11);
34  }
35  else
36  {
37  while (value > 2047)
38  {
39  value >>= 1;
40  ++exp;
41  }
42 
43  return value | (exp << 11);
44  }
45 }
46 
47 int32_t dptFromFloat(uint16_t dptValue)
48 {
49  uint16_t exp = (dptValue >> 11) & 15;
50  int32_t value;
51 
52  if (dptValue == 0x7fff)
53  return INVALID_DPT_FLOAT;
54 
55  if (dptValue >= 0x8000)
56  value = dptValue | (-1L & ~2047);
57  else
58  value = dptValue & 2047;
59 
60  for (; exp; --exp)
61  value <<= 1;
62 
63  return value;
64 }
int32_t dptFromFloat(uint16_t dptValue)
Convert a value from 2 uint8_t float (DPT9/EIS5) to integer.
uint16_t dptToFloat(int32_t value)
Convert a value from uint32_t to 2 uint8_t float (DPT9/EIS5).