Subversion Repositories HomeAutomation

Rev

Rev 1599 | Rev 1605 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1599 Rev 1600
Line 303... Line 303...
303
    bitset.Write(start_bit, bit_length, raw_bit_value);
303
    bitset.Write(start_bit, bit_length, raw_bit_value);
304
}
304
}
305
 
305
 
306
std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
306
std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
307
{
307
{
308
    float raw_value = 0;
308
    float float_value = boost::lexical_cast<float>(this->DecodeInt(bitset, start_bit, bit_length)) / 64.0f;
309
    bool negative = false;;
-
 
310
   
309
   
311
    if (bitset.Get(start_bit))
-
 
312
    {
-
 
313
        negative = true;
-
 
314
    }
-
 
315
   
-
 
316
    for (int c = bit_length - 1; c > 0; c--)
-
 
317
    {
-
 
318
        if (bitset.Get(start_bit + c) != negative)
-
 
319
        {
-
 
320
            raw_value += pow(2.0f, (int)(bit_length - 1 - c));
-
 
321
        }
-
 
322
    }
-
 
323
   
-
 
324
    raw_value /= 64.0f;
-
 
325
   
-
 
326
    if (negative)
-
 
327
    {
-
 
328
        raw_value = -raw_value;
-
 
329
    }
-
 
330
   
-
 
331
    return boost::lexical_cast<std::string>(raw_value);
310
    return boost::lexical_cast<std::string>(float_value);
332
}
311
}
333
 
312
 
334
void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
313
void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
335
{
314
{
-
 
315
    int int_value = boost::lexical_cast<float>(value) * 64.0f;
-
 
316
   
-
 
317
    this->EncodeInt(bitset, start_bit, bit_length, boost::lexical_cast<std::string>(int_value));
-
 
318
}
-
 
319
 
-
 
320
std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
-
 
321
{
-
 
322
    std::string value;
-
 
323
   
-
 
324
    for (unsigned int n = 0; n < bit_length; n += 8)
-
 
325
    {
-
 
326
        unsigned long raw_value = bitset.Read(start_bit + n, 8);
-
 
327
        value += (char)raw_value;
-
 
328
    }
-
 
329
   
336
    // TODO:
330
    return value;
-
 
331
}
-
 
332
 
-
 
333
void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
-
 
334
{
337
    LOG.Error("EncodeFloat() is not implemented yet!");
335
    for (unsigned int n = 0; n < bit_length; n += 8)
-
 
336
    {
-
 
337
        bitset.Write(start_bit + n, 8, value[n / 8]);
-
 
338
    }
338
}
339
}
339
 
340
 
340
std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
341
std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
341
{
342
{
342
    std::string value;
343
    std::string value;
343
    char c = 0;
344
    char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
344
   
345
   
345
    for (unsigned int p = 0; p < bit_length; p += 8)
346
    for (unsigned int n = 0; n < bit_length; n += 4)
346
    {
347
    {
347
        unsigned long raw_bit_value = bitset.Read(start_bit + p, 8);
348
        unsigned long raw_bit_value = bitset.Read(start_bit + n, 4);
-
 
349
       
348
        memcpy(&c, &raw_bit_value, sizeof(c));
350
        if (raw_bit_value >= sizeof(hex))
-
 
351
        {
349
        value += c;
352
            value += '-';
-
 
353
        }
-
 
354
        else
-
 
355
        {
-
 
356
            value += hex[raw_bit_value];
-
 
357
        }
350
    }
358
    }
351
   
359
   
352
    return value;
360
    return value;
353
}
361
}
354
 
362
 
355
void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
363
void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
356
{
-
 
357
    // TODO
-
 
358
    LOG.Error("EncodeAscii() is not implemented yet!");
-
 
359
}
-
 
360
 
-
 
361
std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
-
 
362
{
364
{
363
    std::string value;
365
    int ascii_value;
364
    char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
-
 
365
   
366
   
366
    for (unsigned int p = 0; p < bit_length; p += 4)
367
    for (unsigned int n = 0; n < bit_length; n += 4)
367
    {
368
    {
368
        unsigned long raw_bit_value = bitset.Read(start_bit + p, 4);
369
        ascii_value = boost::lexical_cast<int>((int)std::toupper(value[n / 4]));
369
       
370
       
370
        if (raw_bit_value >= sizeof(hex))
371
        if (48 <= ascii_value && ascii_value <= 57)
371
        {
372
        {
-
 
373
            bitset.Write(start_bit + n, 4, ascii_value - 48);
-
 
374
        }
372
            value += '-';
375
        else if (65 <= ascii_value && ascii_value <= 70)
-
 
376
        {
-
 
377
            bitset.Write(start_bit + n, 4, ascii_value - 65);
373
        }
378
        }
374
        else
379
        else
375
        {
380
        {
376
            value += hex[raw_bit_value];
381
            throw std::runtime_error("This is not an hex string, " + value);
377
        }
382
        }
378
    }
383
    }
379
   
-
 
380
    return value;
-
 
381
}
-
 
382
 
-
 
383
void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
-
 
384
{
-
 
385
    // TODO:
-
 
386
    LOG.Error("EncodeHexstring() is not implemented yet!");
-
 
387
}
384
}
388
 
385
 
389
}; // namespace can
386
}; // namespace can
390
}; // namespace atom
387
}; // namespace atom