----------------------------------------------------------------------------- Read a value from a variable length sequence of bytes -----------------------------------------------------------------------------
| 516 | // Read a value from a variable length sequence of bytes |
| 517 | //----------------------------------------------------------------------------- |
| 518 | string CommandClass::ExtractValue |
| 519 | ( |
| 520 | uint8 const* _data, |
| 521 | uint8* _scale, |
| 522 | uint8* _precision, |
| 523 | uint8 _valueOffset // = 1 |
| 524 | )const |
| 525 | { |
| 526 | uint8 const size = _data[0] & c_sizeMask; |
| 527 | uint8 const precision = (_data[0] & c_precisionMask) >> c_precisionShift; |
| 528 | |
| 529 | if( _scale ) |
| 530 | { |
| 531 | *_scale = (_data[0] & c_scaleMask) >> c_scaleShift; |
| 532 | } |
| 533 | |
| 534 | if( _precision ) |
| 535 | { |
| 536 | *_precision = precision; |
| 537 | } |
| 538 | |
| 539 | uint32 value = 0; |
| 540 | uint8 i; |
| 541 | for( i=0; i<size; ++i ) |
| 542 | { |
| 543 | value <<= 8; |
| 544 | value |= (uint32)_data[i+(uint32)_valueOffset]; |
| 545 | } |
| 546 | |
| 547 | // Deal with sign extension. All values are signed |
| 548 | string res; |
| 549 | if( _data[_valueOffset] & 0x80 ) |
| 550 | { |
| 551 | res = "-"; |
| 552 | |
| 553 | // MSB is signed |
| 554 | if( size == 1 ) |
| 555 | { |
| 556 | value |= 0xffffff00; |
| 557 | } |
| 558 | else if( size == 2 ) |
| 559 | { |
| 560 | value |= 0xffff0000; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // Convert the integer to a decimal string. We avoid |
| 565 | // using floats to prevent accuracy issues. |
| 566 | char numBuf[12] = {0}; |
| 567 | |
| 568 | if( precision == 0 ) |
| 569 | { |
| 570 | // The precision is zero, so we can just print the number directly into the string. |
| 571 | snprintf( numBuf, 12, "%d", (signed int)value ); |
| 572 | res = numBuf; |
| 573 | } |
| 574 | else |
| 575 | { |
nothing calls this directly
no outgoing calls
no test coverage detected