targets/typemapping.py
changeset 1740 b789b695b5c6
parent 1739 ec153828ded2
child 1741 dd94b9a68c61
equal deleted inserted replaced
1739:ec153828ded2 1740:b789b695b5c6
    45     """
    45     """
    46     _fields_ = [("s", c_long),   # tv_sec
    46     _fields_ = [("s", c_long),   # tv_sec
    47                 ("ns", c_long)]  # tv_nsec
    47                 ("ns", c_long)]  # tv_nsec
    48 
    48 
    49 
    49 
    50 def _t(t, u=lambda x:x.value, p=lambda t,x:t(x)): return (t, u, p)
    50 def _t(t, u=lambda x: x.value, p=lambda t, x: t(x)): return (t, u, p)
    51 
    51 
    52 
    52 
    53 def _ttime(): return (IEC_TIME,
    53 def _ttime(): return (IEC_TIME,
    54                       lambda x:td(0, x.s, x.ns/1000),
    54                       lambda x: td(0, x.s, x.ns/1000),
    55                       lambda t,x:t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000))
    55                       lambda t, x: t(x.days * 24 * 3600 + x.seconds, x.microseconds*1000))
    56 
    56 
    57 SameEndianessTypeTranslator = {
    57 SameEndianessTypeTranslator = {
    58     "BOOL":       _t(c_uint8,  lambda x:x.value!=0),
    58     "BOOL":       _t(c_uint8,  lambda x: x.value!=0),
    59     "STEP":       _t(c_uint8),
    59     "STEP":       _t(c_uint8),
    60     "TRANSITION": _t(c_uint8),
    60     "TRANSITION": _t(c_uint8),
    61     "ACTION":     _t(c_uint8),
    61     "ACTION":     _t(c_uint8),
    62     "SINT":       _t(c_int8),
    62     "SINT":       _t(c_int8),
    63     "USINT":      _t(c_uint8),
    63     "USINT":      _t(c_uint8),
    64     "BYTE":       _t(c_uint8),
    64     "BYTE":       _t(c_uint8),
    65     "STRING":     (IEC_STRING,
    65     "STRING":     (IEC_STRING,
    66                    lambda x:x.body[:x.len],
    66                    lambda x: x.body[:x.len],
    67                    lambda t,x:t(len(x),x)),
    67                    lambda t, x: t(len(x), x)),
    68     "INT":        _t(c_int16),
    68     "INT":        _t(c_int16),
    69     "UINT":       _t(c_uint16),
    69     "UINT":       _t(c_uint16),
    70     "WORD":       _t(c_uint16),
    70     "WORD":       _t(c_uint16),
    71     "DINT":       _t(c_int32),
    71     "DINT":       _t(c_int32),
    72     "UDINT":      _t(c_uint32),
    72     "UDINT":      _t(c_uint32),
    87     }
    87     }
    88 
    88 
    89 TypeTranslator=SameEndianessTypeTranslator
    89 TypeTranslator=SameEndianessTypeTranslator
    90 
    90 
    91 # Construct debugger natively supported types
    91 # Construct debugger natively supported types
    92 DebugTypesSize =  dict([(key,sizeof(t)) for key,(t,p,u) in SameEndianessTypeTranslator.iteritems() if t is not None])
    92 DebugTypesSize =  dict([(key, sizeof(t)) for key, (t, p, u) in SameEndianessTypeTranslator.iteritems() if t is not None])
    93 
    93 
    94 
    94 
    95 def UnpackDebugBuffer(buff, indexes):
    95 def UnpackDebugBuffer(buff, indexes):
    96     res =  []
    96     res =  []
    97     buffoffset = 0
    97     buffoffset = 0
    98     buffsize = len(buff)
    98     buffsize = len(buff)
    99     buffptr = cast(ctypes.pythonapi.PyString_AsString(id(buff)),c_void_p).value
    99     buffptr = cast(ctypes.pythonapi.PyString_AsString(id(buff)), c_void_p).value
   100     for iectype in indexes:
   100     for iectype in indexes:
   101         c_type,unpack_func, pack_func = \
   101         c_type, unpack_func, pack_func = \
   102             TypeTranslator.get(iectype,
   102             TypeTranslator.get(iectype,
   103                                     (None,None,None))
   103                                     (None, None, None))
   104         if c_type is not None and buffoffset < buffsize:
   104         if c_type is not None and buffoffset < buffsize:
   105             cursor = c_void_p( buffptr + buffoffset)
   105             cursor = c_void_p( buffptr + buffoffset)
   106             value = unpack_func( cast(cursor,
   106             value = unpack_func( cast(cursor,
   107                          POINTER(c_type)).contents)
   107                          POINTER(c_type)).contents)
   108             buffoffset += sizeof(c_type) if iectype != "STRING" else len(value)+1
   108             buffoffset += sizeof(c_type) if iectype != "STRING" else len(value)+1
   113         return res
   113         return res
   114     return None
   114     return None
   115 
   115 
   116 
   116 
   117 
   117 
   118 LogLevels = ["CRITICAL","WARNING","INFO","DEBUG"]
   118 LogLevels = ["CRITICAL", "WARNING", "INFO", "DEBUG"]
   119 LogLevelsCount = len(LogLevels)
   119 LogLevelsCount = len(LogLevels)
   120 LogLevelsDict = dict(zip(LogLevels,range(LogLevelsCount)))
   120 LogLevelsDict = dict(zip(LogLevels, range(LogLevelsCount)))
   121 LogLevelsDefault = LogLevelsDict["DEBUG"]
   121 LogLevelsDefault = LogLevelsDict["DEBUG"]
   122 
   122