Subversion Repositories HomeAutomation

Rev

Rev 1602 | Rev 1647 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1602 Rev 1642
Line 23... Line 23...
23
 
23
 
24
#include <iostream>
24
#include <iostream>
25
#include <string>
25
#include <string>
26
#include <vector>
26
#include <vector>
27
#include <map>
27
#include <map>
-
 
28
#include <stdexcept>
28
 
29
 
29
#include <stdio.h>
30
#include <stdio.h>
30
 
31
 
31
#include <boost/shared_ptr.hpp>
32
#include <boost/shared_ptr.hpp>
-
 
33
#include <boost/lexical_cast.hpp>
32
 
34
 
33
namespace atom {
35
namespace atom {
34
namespace type {
36
namespace common {
35
 
37
 
36
typedef boost::shared_ptr<unsigned char> BytePointer;
38
typedef boost::shared_ptr<unsigned char> BytePointer;
37
typedef std::vector<std::string> StringList;
39
typedef std::vector<std::string> StringList;
38
typedef std::map<std::string, std::string> StringMap;
40
typedef std::map<std::string, std::string> StringMap;
-
 
41
 
-
 
42
#define SWAP_BYTE_ORDER_16(x) (((x << 8) | (x >> 8)) & 0xFFFF)
-
 
43
#define GET_HIGH_BYTE_16(x)   ((x >> 8) & 0xFF);
-
 
44
#define GET_LOW_BYTE_16(x)    (x & 0xFF)
39
/*
45
 
40
template <typename T>
46
template <typename ElemT>
41
struct HexTo
47
struct HexTo {
-
 
48
    ElemT value;
-
 
49
    operator ElemT() const {return value;}
-
 
50
    friend std::istream& operator>>(std::istream& in, HexTo& out) {
-
 
51
        in >> std::hex >> out.value;
-
 
52
        return in;
-
 
53
    }
-
 
54
};
-
 
55
 
-
 
56
inline unsigned int FromHex(std::string hex_chars)
42
{
57
{
43
    T value;
58
    int value = 0;
-
 
59
    int ascii_value;
-
 
60
   
-
 
61
    for (int n = hex_chars.length() - 1; n >= 0; n--)
-
 
62
    {
-
 
63
        ascii_value = (int)hex_chars.data()[n];
44
   
64
   
-
 
65
        if (48 <= ascii_value && ascii_value <= 57)
-
 
66
        {
-
 
67
            value |= (ascii_value - 48) << n;
45
    operator T() const
68
        }
-
 
69
        else if (65 <= ascii_value && ascii_value <= 70)
46
    {
70
        {
-
 
71
            value |= (ascii_value - 65) << n;
-
 
72
        }
-
 
73
        else
-
 
74
        {
-
 
75
            throw std::runtime_error("This is not an hex string, hex_chars = " + hex_chars + ", ascii_value = " + boost::lexical_cast<std::string>(ascii_value) + ", n = " + boost::lexical_cast<std::string>(n) + ", length = " + boost::lexical_cast<std::string>(hex_chars.length()));
-
 
76
        }
-
 
77
    }
-
 
78
   
47
        return value;
79
    return value;
48
    }
80
}
49
   
81
 
50
    friend std::istream& operator>>(std::istream& in, HexTo& out)
-
 
51
    {
-
 
52
        in >> std::hex >> out.value;
-
 
53
        return in;
-
 
54
    }
-
 
55
};
-
 
56
*/
-
 
57
inline std::string ToHex(unsigned int value)
82
inline std::string ToHex(unsigned int value)
58
{
83
{
59
    char hex_string[11];
84
    char hex_string[11];
60
   
85
   
61
    snprintf(hex_string, sizeof(hex_string), "0x%08X", value);
86
    snprintf(hex_string, sizeof(hex_string), "0x%08X", value);