Subversion Repositories HomeAutomation

Rev

Rev 1227 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1227 Rev 1380
Line 430... Line 430...
430
    makeNMTDataValid(commandName, data);
430
    makeNMTDataValid(commandName, data);
431
 
431
 
432
    return translateValidDataToHex(data);
432
    return translateValidDataToHex(data);
433
}
433
}
434
 
434
 
-
 
435
 
-
 
436
template <class T>
-
 
437
bool from_string(T& t,
-
 
438
                 const std::string& s,
-
 
439
                 std::ios_base& (*f)(std::ios_base&))
-
 
440
{
-
 
441
  std::istringstream iss(s);
-
 
442
  return !(iss >> f >> t).fail();
-
 
443
}
-
 
444
 
-
 
445
 
435
string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data)
446
string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data)
436
{
447
{
437
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
448
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
438
    int highestBit = 0;
449
    int highestBit = 0;
439
 
450
 
440
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
451
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
Line 443... Line 454...
443
        {
454
        {
444
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
455
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
445
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
456
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
446
 
457
 
447
            unsigned int a = stou(iter->second.getValue());
458
            unsigned int a = stou(iter->second.getValue());
448
 
-
 
449
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
459
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
450
        }
460
        }
-
 
461
        else if (iter->second.getType() == "int")
-
 
462
        {
-
 
463
if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
-
 
464
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
-
 
465
 
-
 
466
            unsigned int a = stou(iter->second.getValue());
-
 
467
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
-
 
468
            /*if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
-
 
469
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
-
 
470
cout << iter->second.getValue() << endl;
-
 
471
 
-
 
472
            int a = stoi(iter->second.getValue());
-
 
473
cout << a << endl;
-
 
474
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));*/
-
 
475
        }
451
        else if (iter->second.getType() == "float")
476
        else if (iter->second.getType() == "float")
452
        {
477
        {
453
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
478
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
454
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
479
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
455
 
-
 
456
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
480
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
-
 
481
           
457
        }
482
        }
458
        else if (iter->second.getType() == "enum")
483
    else if (iter->second.getType() == "enum")
459
        {
484
        {
460
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
485
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
461
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
486
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
Line 475... Line 500...
475
        {
500
        {
476
            for (int n = 0; n < iter->second.getValue().length(); n++)
501
            for (int n = 0; n < iter->second.getValue().length(); n++)
477
            {
502
            {
478
                if (iter->second.getStartBit()+n*8 + 8 > highestBit)
503
                if (iter->second.getStartBit()+n*8 + 8 > highestBit)
479
                    highestBit = iter->second.getStartBit()+n*8 + 8;
504
                    highestBit = iter->second.getStartBit()+n*8 + 8;
480
 
505
 
481
                //bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8));
506
                //bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8));
482
                unsigned int temp = iter->second.getValue()[n];
507
                unsigned int temp = iter->second.getValue()[n];
483
                if (temp > 0xff)
508
                if (temp > 0xff)
484
                {
509
                {
485
                    temp &= 0xff;
510
                    temp &= 0xff;
Line 505... Line 530...
505
    try
530
    try
506
    {
531
    {
507
        while (highestBit%8 != 0)
532
        while (highestBit%8 != 0)
508
        {
533
        {
509
            highestBit++;
534
            highestBit++;
510
        }
535
        }
511
        bin = bin.substr(0, highestBit);
536
        bin = bin.substr(0, highestBit);
512
    }
537
    }
513
    catch (std::out_of_range& e)
538
    catch (std::out_of_range& e)
514
    {
539
    {
515
        cout << "DEBUG: translateValidDataToHex exception: " << e.what() << "\n";
540
        cout << "DEBUG: translateValidDataToHex exception: " << e.what() << "\n";
Line 551... Line 576...
551
        variable.setBitLength(bitLength);
576
        variable.setBitLength(bitLength);
552
 
577
 
553
        if (attributes["type"] == "uint")
578
        if (attributes["type"] == "uint")
554
        {
579
        {
555
            variable.setValue(utos(bin2uint(bits)));
580
            variable.setValue(utos(bin2uint(bits)));
-
 
581
        }
-
 
582
        else if (attributes["type"] == "int")
-
 
583
        {
-
 
584
            variable.setValue(itos(bin2int(bits)));
556
        }
585
        }
557
        else if (attributes["type"] == "float")
586
        else if (attributes["type"] == "float")
558
        {
587
        {
559
            variable.setValue(ftos(bin2float(bits)));
588
            variable.setValue(ftos(bin2float(bits)));
560
        }
589
        }