forked from franckmarini/KnxDevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnxDevice.cpp
613 lines (525 loc) · 21.6 KB
/
KnxDevice.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// This file is part of Arduino Knx Bus Device library.
// The Arduino Knx Bus Device library allows to turn Arduino into "self-made" KNX bus device.
// Copyright (C) 2014 2015 2016 Franck MARINI ([email protected])
// The Arduino Knx Bus Device library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// File : KnxDevice.cpp
// Author : Franck Marini
// Description : KnxDevice Abstraction Layer
// Module dependencies : HardwareSerial, KnxTelegram, KnxComObject, KnxTpUart, ActionRingBuffer
#include "KnxDevice.h"
static inline word TimeDeltaWord(word now, word before) { return (word)(now - before); }
#ifdef KNXDEVICE_DEBUG_INFO
const char KnxDevice::_debugInfoText[] = "KNXDEVICE INFO: ";
#endif
// KnxDevice unique instance creation
KnxDevice KnxDevice::Knx;
KnxDevice& Knx = KnxDevice::Knx;
// Constructor
KnxDevice::KnxDevice()
{
_state = INIT;
_knxBus = NULL;
_txActionList= ActionRingBuffer<type_tx_action, ACTIONS_QUEUE_SIZE>();
_initCompleted = false;
_initIndex = 0;
_rxTelegram = NULL;
#if defined(KNXDEVICE_DEBUG_INFO)
_nbOfInits = 0;
_debugStrPtr = NULL;
#endif
_comObjectsNb = 0;
dynComObjects = 0;
}
#ifdef HAVE_TPUART
e_KnxDeviceStatus KnxDevice::begin(HardwareSerial& serial, word physicalAddr,
KnxComObject** dynComObjects_, byte numberObjects)
{
_knxBus = new KnxTpUart(serial ,physicalAddr, NORMAL);
return commonInit(dynComObjects_, numberObjects);
}
#endif
// Start the KNX Device
// return KNX_DEVICE_ERROR (255) if begin() failed
// else return KNX_DEVICE_OK
#ifdef HAVE_STKNX
e_KnxDeviceStatus KnxDevice::begin(type_TransmitCallbackFctPtr cb, word physicalAddr,
KnxComObject** dynComObjects_, byte numberObjects)
{
_knxBus = new StKnxCoupler(cb, physicalAddr, NORMAL);
return commonInit(dynComObjects_, numberObjects);
}
void KnxDevice::setReceivedTelegram(KnxTelegram &telegram)
{
_knxBus->SetReceivedTelegram(telegram);
}
#endif
e_KnxDeviceStatus KnxDevice::commonInit(KnxComObject** dynComObjects_, byte numberObjects)
{
_rxTelegram = &_knxBus->GetReceivedTelegram();
// delay(10000); // Workaround for init issue with bus-powered arduino
// the issue is reproduced on one (faulty?) TPUART device only, so remove it for the moment.
if(_knxBus->Reset()!= KNX_BUSCOUPLER_OK)
{
delete(_knxBus);
_knxBus = NULL;
_rxTelegram = NULL;
#if defined(KNXDEVICE_DEBUG_INFO)
DebugInfo("Init Error!\n");
#endif
return KNX_DEVICE_ERROR;
}
dynComObjects = dynComObjects_;
_comObjectsNb = numberObjects;
_knxBus->AttachComObjectsList(dynComObjects, _comObjectsNb);
_knxBus->SetEvtCallback(&KnxDevice::GetTpUartEvents);
_knxBus->SetAckCallback(&KnxDevice::TxTelegramAck);
_knxBus->Init();
_state = IDLE;
#if defined(KNXDEVICE_DEBUG_INFO)
DebugInfo("Init successful\n");
#endif
_lastInitTimeMillis = millis();
_lastTXTimeMicros = micros();
#if defined(KNXDEVICE_DEBUG_INFO)
_nbOfInits = 0;
#endif
return KNX_DEVICE_OK;
}
// Stop the KNX Device
void KnxDevice::end()
{
type_tx_action action;
_state = INIT;
while(_txActionList.Pop(action)); // empty ring buffer
_initCompleted = false;
_initIndex = 0;
_rxTelegram = NULL;
delete(_knxBus);
_knxBus = NULL;
}
// KNX device execution task
// This function call shall be placed in the "loop()" Arduino function
void KnxDevice::task(void)
{
type_tx_action action;
word nowTimeMillis, nowTimeMicros;
// STEP 1 : Initialize Com Objects having Init Read attribute
if(!_initCompleted)
{
nowTimeMillis = millis();
// To avoid EIB bus overloading, we wait for 500 ms between each Init read request
if (TimeDeltaWord(nowTimeMillis, _lastInitTimeMillis) > 500 )
{
while ( (_initIndex< _comObjectsNb) && (dynComObjects[_initIndex]->GetValidity() )) _initIndex++;
if (_initIndex == _comObjectsNb)
{
_initCompleted = true; // All the Com Object initialization have been performed
// DebugInfo(String("KNXDevice INFO: Com Object init completed, ")+ String( _nbOfInits) + String("objs initialized.\n"));
}
else
{ // Com Object to be initialised has been found
// Add a READ request in the TX action list
#if defined(KNXDEVICE_DEBUG_INFO) || defined(KNXDEVICE_DEBUG_INFO_VERBOSE)
_nbOfInits++;
#endif
// init read request
action.command = EIB_READ_REQUEST;
action.index = _initIndex;
_initIndex = _comObjectsNb;
_txActionList.Append(action);
_lastInitTimeMillis = millis(); // Update the timer
}
}
}
// STEP 2 : Get new received EIB messages from the TPUART
// The TPUART RX task is executed every 400 us
nowTimeMicros = micros();
if (TimeDeltaWord(nowTimeMicros, _lastRXTimeMicros) > 400)
{
_lastRXTimeMicros = nowTimeMicros;
_knxBus->RXTask();
}
// STEP 3 : Send KNX messages following TX actions
if(_state == IDLE)
{
if( _txActionList.Pop(action))
{ // Data to be transmitted
switch (action.command)
{
case EIB_READ_REQUEST: // a read operation of a Com Object on the EIB network is required
//_objectsList[action.index].CopyToTelegram(_txTelegram, KNX_COMMAND_VALUE_READ);
dynComObjects[action.index]->CopyAttributes(_txTelegram);
_txTelegram.ClearLongPayload(); _txTelegram.ClearFirstPayloadByte(); // Is it required to have a clean payload ??
_txTelegram.SetCommand(KNX_COMMAND_VALUE_READ);
_txTelegram.UpdateChecksum();
_knxBus->SendTelegram(_txTelegram);
_state = TX_ONGOING;
break;
case EIB_RESPONSE_REQUEST: // a response operation of a Com Object on the EIB network is required
dynComObjects[action.index]->CopyAttributes(_txTelegram);
dynComObjects[action.index]->CopyValue(_txTelegram);
_txTelegram.SetCommand(KNX_COMMAND_VALUE_RESPONSE);
_txTelegram.UpdateChecksum();
_knxBus->SendTelegram(_txTelegram);
_state = TX_ONGOING;
break;
case EIB_WRITE_REQUEST: // a write operation of a Com Object on the EIB network is required
// update the com obj value
if ((dynComObjects[action.index]->GetLength()) <= 2 )
dynComObjects[action.index]->UpdateValue(action.byteValue);
else
{
dynComObjects[action.index]->UpdateValue(action.valuePtr);
free(action.valuePtr);
}
// transmit the value through EIB network only if the Com Object has transmit attribute
if ( (dynComObjects[action.index]->GetIndicator()) & KNX_COM_OBJ_T_INDICATOR)
{
dynComObjects[action.index]->CopyAttributes(_txTelegram);
dynComObjects[action.index]->CopyValue(_txTelegram);
_txTelegram.SetCommand(KNX_COMMAND_VALUE_WRITE);
_txTelegram.UpdateChecksum();
_knxBus->SendTelegram(_txTelegram);
_state = TX_ONGOING;
}
break;
default : break;
}
}
}
// STEP 4 : LET THE TP-UART TRANSMIT EIB MESSAGES
// The TPUART TX task is executed every 800 us
nowTimeMicros = micros();
if (TimeDeltaWord(nowTimeMicros, _lastTXTimeMicros) > 800)
{
_lastTXTimeMicros = nowTimeMicros;
_knxBus->TXTask();
}
}
// Quick method to read a short (<=1 byte) com object
// NB : The returned value will be hazardous in case of use with long objects
byte KnxDevice::read(byte objectIndex)
{
return dynComObjects[objectIndex]->GetValue();
}
// Read an usual format com object
// Supported DPT formats are short com object, U16, V16, U32, V32, F16 and F32 (not implemented yet)
template <typename T> e_KnxDeviceStatus KnxDevice::read(byte objectIndex, T& returnedValue)
{
// Short com object case
if (dynComObjects[objectIndex]->GetLength()<=2)
{
returnedValue = (T) dynComObjects[objectIndex]->GetValue();
return KNX_DEVICE_OK;
}
else // long object case, let's see if we are able to translate the DPT value
{
byte dptValue[14]; // define temporary DPT value with max length
dynComObjects[objectIndex]->GetValue(dptValue);
return ConvertFromDpt(dptValue, returnedValue, pgm_read_byte(&KnxDPTIdToFormat[dynComObjects[objectIndex]->GetDptId()]));
}
}
template e_KnxDeviceStatus KnxDevice::read <boolean>(byte objectIndex, boolean& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <unsigned char>(byte objectIndex, unsigned char& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <char>(byte objectIndex, char& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <unsigned int>(byte objectIndex, unsigned int& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <int>(byte objectIndex, int& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <unsigned long>(byte objectIndex, unsigned long& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <long>(byte objectIndex, long& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <float>(byte objectIndex, float& returnedValue);
template e_KnxDeviceStatus KnxDevice::read <double>(byte objectIndex, double& returnedValue);
// Read any type of com object (DPT value provided as is)
e_KnxDeviceStatus KnxDevice::read(byte objectIndex, byte returnedValue[])
{
dynComObjects[objectIndex]->GetValue(returnedValue);
return KNX_DEVICE_OK;
}
// Update an usual format com object
// Supported DPT types are short com object, U16, V16, U32, V32, F16 and F32
// The Com Object value is updated locally
// And a telegram is sent on the EIB bus if the com object has communication & transmit attributes
template <typename T> e_KnxDeviceStatus KnxDevice::write(byte objectIndex, T value)
{
type_tx_action action;
byte *destValue;
byte length = dynComObjects[objectIndex]->GetLength();
if (length <= 2 ) action.byteValue = (byte) value; // short object case
else
{ // long object case, let's try to translate value to the com object DPT
destValue = (byte *) malloc(length-1); // allocate the memory for DPT
e_KnxDeviceStatus status = ConvertToDpt(value, destValue, pgm_read_byte(&KnxDPTIdToFormat[dynComObjects[objectIndex]->GetDptId()]));
if (status) // translation error
{
free(destValue);
return status; // we cannot convert, we stop here
}
else action.valuePtr = destValue;
}
// add WRITE action in the TX action queue
action.command = EIB_WRITE_REQUEST;
action.index = objectIndex;
_txActionList.Append(action);
return KNX_DEVICE_OK;
}
template e_KnxDeviceStatus KnxDevice::write <boolean>(byte objectIndex, boolean value);
template e_KnxDeviceStatus KnxDevice::write <unsigned char>(byte objectIndex, unsigned char value);
template e_KnxDeviceStatus KnxDevice::write <char>(byte objectIndex, char value);
template e_KnxDeviceStatus KnxDevice::write <unsigned int>(byte objectIndex, unsigned int value);
template e_KnxDeviceStatus KnxDevice::write <int>(byte objectIndex, int value);
template e_KnxDeviceStatus KnxDevice::write <unsigned long>(byte objectIndex, unsigned long value);
template e_KnxDeviceStatus KnxDevice::write <long>(byte objectIndex, long value);
template e_KnxDeviceStatus KnxDevice::write <float>(byte objectIndex, float value);
template e_KnxDeviceStatus KnxDevice::write <double>(byte objectIndex, double value);
// Update any type of com object (rough DPT value shall be provided)
// The Com Object value is updated locally
// And a telegram is sent on the EIB bus if the com object has communication & transmit attributes
e_KnxDeviceStatus KnxDevice::write(byte objectIndex, byte valuePtr[])
{
type_tx_action action;
byte *dptValue;
byte length = dynComObjects[objectIndex]->GetLength();
if (length>2) // check we are in long object case
{ // add WRITE action in the TX action queue
action.command = EIB_WRITE_REQUEST;
action.index = objectIndex;
dptValue = (byte *) malloc(length-1); // allocate the memory for long value
for (byte i=0; i<length-1; i++) dptValue[i] = valuePtr[i]; // copy value
action.valuePtr = (byte *) dptValue;
_txActionList.Append(action);
return KNX_DEVICE_OK;
}
return KNX_DEVICE_ERROR;
}
// Com Object EIB Bus Update request
// Request the local object to be updated with the value from the bus
// NB : the function is asynchroneous, the update completion is notified by the knxEvents() callback
void KnxDevice::update(byte objectIndex)
{
type_tx_action action;
action.command = EIB_READ_REQUEST;
action.index = objectIndex;
_txActionList.Append(action);
}
// The function returns true if there is rx/tx activity ongoing, else false
boolean KnxDevice::isActive(void) const
{
if (_knxBus->IsActive()) return true; // TPUART is active
if (_state == TX_ONGOING) return true; // the Device is sending a request
if(_txActionList.ElementsNb()) return true; // there is at least one tx action in the queue
return false;
}
// Static GetTpUartEvents() function called by the KnxTpUart layer (callback)
void KnxDevice::GetTpUartEvents(e_KnxBusCouplerEvent event)
{
type_tx_action action;
byte targetedComObjIndex; // index of the Com Object targeted by the event
// Manage RECEIVED MESSAGES
if (event == BUSCOUPLER_EVENT_RECEIVED_EIB_TELEGRAM)
{
Knx._state = IDLE;
targetedComObjIndex = Knx._knxBus->GetTargetedComObjectIndex();
switch(Knx._rxTelegram->GetCommand())
{
case KNX_COMMAND_VALUE_READ :
#if defined(KNXDEVICE_DEBUG_INFO)
Knx.DebugInfo("READ req.\n");
#endif
// READ command coming from the bus
// if the Com Object has read attribute, then add RESPONSE action in the TX action list
if ( (Knx.dynComObjects[targetedComObjIndex]->GetIndicator()) & KNX_COM_OBJ_R_INDICATOR)
{ // The targeted Com Object can indeed be read
action.command = EIB_RESPONSE_REQUEST;
action.index = targetedComObjIndex;
Knx._txActionList.Append(action);
}
break;
case KNX_COMMAND_VALUE_RESPONSE :
#if defined(KNXDEVICE_DEBUG_INFO)
Knx.DebugInfo("RESP req.\n");
#endif
// RESPONSE command coming from EIB network, we update the value of the corresponding Com Object.
// We 1st check that the corresponding Com Object has UPDATE attribute
if((Knx.dynComObjects[targetedComObjIndex]->GetIndicator()) & KNX_COM_OBJ_U_INDICATOR)
{
Knx.dynComObjects[targetedComObjIndex]->UpdateValue(*(Knx._rxTelegram));
//We notify the upper layer of the update
knxEvents(targetedComObjIndex);
}
break;
case KNX_COMMAND_VALUE_WRITE :
#if defined(KNXDEVICE_DEBUG_INFO)
Knx.DebugInfo("WRITE req.\n");
#endif
// WRITE command coming from EIB network, we update the value of the corresponding Com Object.
// We 1st check that the corresponding Com Object has WRITE attribute
if((Knx.dynComObjects[targetedComObjIndex]->GetIndicator()) & KNX_COM_OBJ_W_INDICATOR)
{
Knx.dynComObjects[targetedComObjIndex]->UpdateValue(*(Knx._rxTelegram));
//We notify the upper layer of the update
knxEvents(targetedComObjIndex);
}
break;
// case KNX_COMMAND_MEMORY_WRITE : break; // Memory Write not handled
default : break; // not supposed to happen
}
}
// Manage RESET events
if (event == BUSCOUPLER_EVENT_RESET)
{
while(Knx._knxBus->Reset()==KNX_BUSCOUPLER_ERROR);
Knx._knxBus->Init();
Knx._state = IDLE;
}
}
// Static TxTelegramAck() function called by the KnxTpUart layer (callback)
void KnxDevice::TxTelegramAck(e_BusCouplerTxAck value)
{
Knx._state = IDLE;
#ifdef KNXDevice_DEBUG
if(value != ACK_RESPONSE)
{
switch(value)
{
case NACK_RESPONSE: DebugInfo("NACK RESPONSE!!\n"); break;
case NO_ANSWER_TIMEOUT: DebugInfo("NO ANSWER TIMEOUT RESPONSE!!\n");; break;
case BUSCOUPLER_RESET_RESPONSE: DebugInfo("RESET RESPONSE!!\n");; break;
}
}
#endif // KNXDevice_DEBUG
}
// Functions to convert a standard C type to a DPT format
// NB : only the usual DPT formats are supported (U16, V16, U32, V32, F16 and F32 (not yet implemented)
template <typename T> e_KnxDeviceStatus ConvertFromDpt(const byte dptOriginValue[], T& resultValue, byte dptFormat)
{
switch (dptFormat)
{
case KNX_DPT_FORMAT_U16:
case KNX_DPT_FORMAT_V16:
resultValue = (T)((unsigned int)dptOriginValue[0] << 8);
resultValue += (T)(dptOriginValue[1]);
return KNX_DEVICE_OK;
break;
case KNX_DPT_FORMAT_U32:
case KNX_DPT_FORMAT_V32:
resultValue = (T)((unsigned long)dptOriginValue[0] << 24);
resultValue += (T)((unsigned long)dptOriginValue[1] << 16);
resultValue += (T)((unsigned long)dptOriginValue[2] << 8);
resultValue += (T)(dptOriginValue[3]);
return KNX_DEVICE_OK;
break;
case KNX_DPT_FORMAT_F16 :
{
// Get the DPT sign, mantissa and exponent
int signMultiplier = (dptOriginValue[0] & 0x80) ? -1 : 1;
word absoluteMantissa = dptOriginValue[1] + ((dptOriginValue[0]&0x07)<<8);
if (signMultiplier == -1)
{ // Calculate absolute mantissa value in case of negative mantissa
// Abs = 2's complement + 1
absoluteMantissa = ((~absoluteMantissa)& 0x07FF ) + 1;
}
byte exponent = (dptOriginValue[0]&0x78)>>3;
float resF = 0.01 * (((long)absoluteMantissa) << exponent) * signMultiplier;
resultValue = (T) resF;
return KNX_DEVICE_OK;
}
break;
// support 24 bit values (e.g. for 3 byte color rgb/hsl )
case KNX_DPT_FORMAT_B24: {
unsigned int resU = 0;
((byte*)&resU)[0] = dptOriginValue[0];
((byte*)&resU)[1] = dptOriginValue[1];
((byte*)&resU)[2] = dptOriginValue[2];
resultValue = resU;
return KNX_DEVICE_OK;
}
break;
case KNX_DPT_FORMAT_F32 :
return KNX_DEVICE_NOT_IMPLEMENTED;
break;
default :
return KNX_DEVICE_ERROR;
break;
}
}
template e_KnxDeviceStatus ConvertFromDpt <unsigned char>(const byte dptOriginValue[], unsigned char&, byte dptFormat);
template e_KnxDeviceStatus ConvertFromDpt <char>(const byte dptOriginValue[], char&, byte dptFormat);
template e_KnxDeviceStatus ConvertFromDpt <unsigned int>(const byte dptOriginValue[], unsigned int&, byte dptFormat);
template e_KnxDeviceStatus ConvertFromDpt <int>(const byte dptOriginValue[], int&, byte dptFormat);
template e_KnxDeviceStatus ConvertFromDpt <unsigned long>(const byte dptOriginValue[], unsigned long&, byte dptFormat);
template e_KnxDeviceStatus ConvertFromDpt <long>(const byte dptOriginValue[], long&, byte dptFormat);
template e_KnxDeviceStatus ConvertFromDpt <float>(const byte dptOriginValue[], float&, byte dptFormat);
template e_KnxDeviceStatus ConvertFromDpt <double>(const byte dptOriginValue[], double&, byte dptFormat);
// Functions to convert a standard C type to a DPT format
// NB : only the usual DPT formats are supported (U16, V16, U32, V32, F16 and F32 (not yet implemented)
template <typename T> e_KnxDeviceStatus ConvertToDpt(T originValue, byte dptDestValue[], byte dptFormat)
{
switch (dptFormat)
{
case KNX_DPT_FORMAT_U16:
case KNX_DPT_FORMAT_V16:
dptDestValue[0] = (byte)((unsigned int)originValue>>8);
dptDestValue[1] = (byte)(originValue);
return KNX_DEVICE_OK;
break;
case KNX_DPT_FORMAT_U32:
case KNX_DPT_FORMAT_V32:
dptDestValue[0] = (byte)((unsigned long)originValue>>24);
dptDestValue[1] = (byte)((unsigned long)originValue>>16);
dptDestValue[2] = (byte)((unsigned long)originValue>>8);
dptDestValue[3] = (byte)(originValue);
return KNX_DEVICE_OK;
break;
case KNX_DPT_FORMAT_F16 :
{
long longValuex100 = (long)(100.0 * originValue);
boolean negativeSign = (longValuex100 & 0x80000000)? true : false;
byte exponent = 0;
byte round = 0;
if (negativeSign)
{
while(longValuex100 < (long)(-2048))
{
exponent++; round = (byte)(longValuex100) & 1 ; longValuex100>>=1; longValuex100|=0x80000000;
}
}
else
{
while(longValuex100 > (long)(2047))
{
exponent++; round = (byte)(longValuex100) & 1 ; longValuex100>>=1;
}
}
if (round) longValuex100++;
dptDestValue[1] = (byte)longValuex100;
dptDestValue[0] = (byte)(longValuex100>>8) & 0x7 ;
dptDestValue[0] += exponent<<3;
if (negativeSign) dptDestValue[0] += 0x80;
return KNX_DEVICE_OK;
}
break;
case KNX_DPT_FORMAT_F32 :
return KNX_DEVICE_NOT_IMPLEMENTED;
break;
default :
return KNX_DEVICE_ERROR;
break;
}
}
template e_KnxDeviceStatus ConvertToDpt <unsigned char>(unsigned char, byte dptDestValue[], byte dptFormat);
template e_KnxDeviceStatus ConvertToDpt <char>(char, byte dptDestValue[], byte dptFormat);
template e_KnxDeviceStatus ConvertToDpt <unsigned int>(unsigned int, byte dptDestValue[], byte dptFormat);
template e_KnxDeviceStatus ConvertToDpt <int>(int, byte dptDestValue[], byte dptFormat);
template e_KnxDeviceStatus ConvertToDpt <unsigned long>(unsigned long, byte dptDestValue[], byte dptFormat);
template e_KnxDeviceStatus ConvertToDpt <long>(long, byte dptDestValue[], byte dptFormat);
template e_KnxDeviceStatus ConvertToDpt <float>(float, byte dptDestValue[], byte dptFormat);
template e_KnxDeviceStatus ConvertToDpt <double>(double, byte dptDestValue[], byte dptFormat);
// EOF