Skip to content

Commit

Permalink
Reimplement Dpt15
Browse files Browse the repository at this point in the history
  • Loading branch information
thelsing committed Sep 21, 2024
1 parent 9ef7f12 commit bf573d8
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 91 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ set(SOURCES
./knx/group_object/dpt/dpt13.h
./knx/group_object/dpt/dpt14.cpp
./knx/group_object/dpt/dpt14.h
./knx/group_object/dpt/dpt15.cpp
./knx/group_object/dpt/dpt15.h
./knx/group_object/dpt/dptconvert.cpp
./knx/group_object/dpt/dptconvert.h
./knx/group_object/group_object.cpp
Expand Down
1 change: 0 additions & 1 deletion src/knx/group_object/dpt/dpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "../group_object.h"
namespace Knx
{
#define DPT_Access_Data Dpt(15, 0)
#define DPT_String_ASCII Dpt(16, 0)
#define DPT_String_8859_1 Dpt(16, 1)
#define DPT_SceneNumber Dpt(17, 1)
Expand Down
110 changes: 110 additions & 0 deletions src/knx/group_object/dpt/dpt15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "dpt15.h"

#include "dptconvert.h"

Knx::Go_SizeCode Knx::Dpt15::size() const
{
return Go_4_Octets;
}

void Knx::Dpt15::encode(uint8_t* data) const
{

for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10)
bcdToPayload(data, n, (_accessCode / factor) % 10);

bitToPayload(data, 24, _detectionError);
bitToPayload(data, 25, _permission);
bitToPayload(data, 26, _readDirection == RightToLeft);
bitToPayload(data, 27, _encrypted);
bcdToPayload(data, 7, _index);
}

bool Knx::Dpt15::decode(uint8_t* data)
{
int32_t digits = 0;

for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10)
{
unsigned char digit = bcdFromPayload(data, n);

if (digit > 9)
return false;

digits += digit * factor;
}

_accessCode = digits;
_detectionError = bitFromPayload(data, 24);
_permission = bitFromPayload(data, 25);
_readDirection = bitFromPayload(data, 26) ? RightToLeft : LeftToRight;
_encrypted = bitFromPayload(data, 27);
_index = bcdFromPayload(data, 7);
return true;
}

uint32_t Knx::Dpt15::accessCode() const
{
return _accessCode;
}

void Knx::Dpt15::accessCode(const uint32_t value)
{
if (value > 999999)
return;

_accessCode = value;
}

bool Knx::Dpt15::detectionError() const
{
return _detectionError;
}

void Knx::Dpt15::detetionError(const bool value)
{
_detectionError = value;
}

bool Knx::Dpt15::permission() const
{
return _permission;
}

void Knx::Dpt15::permission(const bool value)
{
_permission = value;
}

Knx::Dpt15::ReadDirectionValue Knx::Dpt15::readDirection() const
{
return _readDirection;
}

void Knx::Dpt15::readDirection(const ReadDirectionValue value)
{
_readDirection = value;
}

bool Knx::Dpt15::encrypted() const
{
return _encrypted;
}

void Knx::Dpt15::encrypted(const bool value)
{
_encrypted = value;
}

uint8_t Knx::Dpt15::index() const
{
return _index;
}

void Knx::Dpt15::index(const uint8_t value)
{
if (value > 15)
return;

_index = value;
}
37 changes: 37 additions & 0 deletions src/knx/group_object/dpt/dpt15.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include "dpt.h"
namespace Knx
{
class Dpt15: public Dpt
{
enum ReadDirectionValue { LeftToRight = 0, RightToLeft = 1};
public:
Dpt15() {};
Go_SizeCode size() const override;

void encode(uint8_t* data) const override;
bool decode(uint8_t* data) override;

uint32_t accessCode() const;
void accessCode(const uint32_t value);
bool detectionError() const;
void detetionError(const bool value);
bool permission() const;
void permission(const bool value);
ReadDirectionValue readDirection() const;
void readDirection(const ReadDirectionValue value);
bool encrypted() const;
void encrypted(const bool value);
uint8_t index() const;
void index(const uint8_t value);
private:
uint32_t _accessCode;
bool _detectionError;
bool _permission;
ReadDirectionValue _readDirection;
bool _encrypted;
uint8_t _index;
};

typedef Dpt15 DPT_Access_Data;
}
87 changes: 0 additions & 87 deletions src/knx/group_object/dpt/dptconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ namespace Knx
{
if (payload_length > 0)
{
// DPT 15.* - Access Data
if (datatype.mainGroup == 15 && !datatype.subGroup && datatype.index <= 5)
return busValueToAccess(payload, payload_length, datatype, value);

// DPT 16.* - String
if (datatype.mainGroup == 16 && datatype.subGroup <= 1 && !datatype.index)
return busValueToString(payload, payload_length, datatype, value);
Expand Down Expand Up @@ -108,10 +104,6 @@ namespace Knx

int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
// DPT 15.* - Access Data
if (datatype.mainGroup == 15 && !datatype.subGroup && datatype.index <= 5)
return valueToBusValueAccess(value, payload, payload_length, datatype);

// DPT 16.* - String
if (datatype.mainGroup == 16 && datatype.subGroup <= 1 && !datatype.index)
return valueToBusValueString(value, payload, payload_length, datatype);
Expand Down Expand Up @@ -202,45 +194,6 @@ namespace Knx
return true;
}

int busValueToAccess(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(4);

switch (datatype.index)
{
case 0:
{
int32_t digits = 0;

for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10)
{
unsigned char digit = bcdFromPayload(payload, n);

if (digit > 9)
return false;

digits += digit * factor;
}

value = digits;
return true;
}

case 1:
case 2:
case 3:
case 4:
value = bitFromPayload(payload, 23 + datatype.index);
return true;

case 5:
value = bcdFromPayload(payload, 7);
return true;
}

return false;
}

int busValueToString(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(14);
Expand Down Expand Up @@ -645,46 +598,6 @@ namespace Knx
return true;
}

int valueToBusValueAccess(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
switch (datatype.index)
{
case 0:
{
if ((int64_t)value < INT64_C(0) || (int64_t)value > INT64_C(999999))
return false;

ENSURE_PAYLOAD(4);

for (int n = 0, factor = 100000; n < 6; ++n, factor /= 10)
bcdToPayload(payload, n, ((uint64_t)value / factor) % 10);

break;
}

case 1:
case 2:
case 3:
case 4:
bitToPayload(payload, 23 + datatype.index, value);
break;

case 5:
{
if ((uint64_t)value > INT64_C(15))
return false;

bcdToPayload(payload, 7, (uint64_t)value);
break;
}

default:
return false;
}

return true;
}

int valueToBusValueString(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
const char* strValue = value;
Expand Down
2 changes: 0 additions & 2 deletions src/knx/group_object/dpt/dptconvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ namespace Knx
//KNX to internal
int busValueToUnsigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToSigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToAccess(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToString(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToScene(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToSceneControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
Expand All @@ -72,7 +71,6 @@ namespace Knx
//Internal to KNX
int valueToBusValueUnsigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueSigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueAccess(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueString(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueScene(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueSceneControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
Expand Down
3 changes: 2 additions & 1 deletion src/knx/group_object/dpt/dpts.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
#include "dpt11.h"
#include "dpt12.h"
#include "dpt13.h"
#include "dpt14.h"
#include "dpt14.h"
#include "dpt15.h"

0 comments on commit bf573d8

Please sign in to comment.