Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

"""This module provides Python bindings for DGILib.""" 

 

from os import (path, getcwd, getenv) 

from ctypes import cdll 

 

from pydgilib.dgilib_exceptions import ( 

DLLError, DeviceIndexError, DeviceConnectionError) 

from pydgilib.dgilib_discovery import DGILibDiscovery 

from pydgilib.dgilib_housekeeping import DGILibHousekeeping 

from pydgilib.dgilib_interface_communication import ( 

DGILibInterfaceCommunication) 

from pydgilib.dgilib_auxiliary import DGILibAuxiliary 

 

 

class DGILib(object): 

"""Python bindings for DGILib. 

 

DGILib is a Dynamic-Link Library (DLL) to help software applications 

communicate with Data Gateway Interface (DGI) devices. See the Data Gateway 

Interface user guide for further details. DGILib handles the low-level USB 

communication and adds a level of buffering for minimizing the chance of 

overflows. 

The library helps parse data streams of high complexity. The timestamp 

interface is parsed and split into separate buffers for each data source. 

The power interface is optionally parsed and calibrated using an auxiliary 

API. 

 

:Example: 

 

>>> with DGILib() as dgilib: 

... dgilib.get_major_version() 

5 

""" 

 

# Add modules 

# Discovery 

# discovery = DGILibDiscovery 

discover = DGILibDiscovery.discover 

get_device_count = DGILibDiscovery.get_device_count 

get_device_name = DGILibDiscovery.get_device_name 

get_device_serial = DGILibDiscovery.get_device_serial 

is_msd_mode = DGILibDiscovery.is_msd_mode 

set_mode = DGILibDiscovery.set_mode 

 

# Housekeeping 

# housekeeping = DGILibHousekeeping 

connect = DGILibHousekeeping.connect 

disconnect = DGILibHousekeeping.disconnect 

connection_status = DGILibHousekeeping.connection_status 

get_major_version = DGILibHousekeeping.get_major_version 

get_minor_version = DGILibHousekeeping.get_minor_version 

get_build_number = DGILibHousekeeping.get_build_number 

get_fw_version = DGILibHousekeeping.get_fw_version 

start_polling = DGILibHousekeeping.start_polling 

stop_polling = DGILibHousekeeping.stop_polling 

target_reset = DGILibHousekeeping.target_reset 

 

# Interface Communication 

# interface_communication = DGILibInterfaceCommunication 

interface_list = DGILibInterfaceCommunication.interface_list 

interface_enable = DGILibInterfaceCommunication.interface_enable 

interface_disable = DGILibInterfaceCommunication.interface_disable 

interface_get_configuration = DGILibInterfaceCommunication.interface_get_configuration 

interface_set_configuration = DGILibInterfaceCommunication.interface_set_configuration 

interface_clear_buffer = DGILibInterfaceCommunication.interface_clear_buffer 

interface_read_data = DGILibInterfaceCommunication.interface_read_data 

interface_write_data = DGILibInterfaceCommunication.interface_write_data 

 

# Auxiliary 

# auxiliary = DGILibAuxiliary 

auxiliary_power_initialize = DGILibAuxiliary.auxiliary_power_initialize 

auxiliary_power_uninitialize = DGILibAuxiliary.auxiliary_power_uninitialize 

auxiliary_power_register_buffer_pointers = DGILibAuxiliary.auxiliary_power_register_buffer_pointers 

auxiliary_power_unregister_buffer_pointers = DGILibAuxiliary.auxiliary_power_unregister_buffer_pointers 

auxiliary_power_calibration_is_valid = DGILibAuxiliary.auxiliary_power_calibration_is_valid 

auxiliary_power_trigger_calibration = DGILibAuxiliary.auxiliary_power_trigger_calibration 

auxiliary_power_get_calibration = DGILibAuxiliary.auxiliary_power_get_calibration 

auxiliary_power_get_circuit_type = DGILibAuxiliary.auxiliary_power_get_circuit_type 

auxiliary_power_get_status = DGILibAuxiliary.auxiliary_power_get_status 

auxiliary_power_start = DGILibAuxiliary.auxiliary_power_start 

auxiliary_power_stop = DGILibAuxiliary.auxiliary_power_stop 

auxiliary_power_lock_data_for_reading = DGILibAuxiliary.auxiliary_power_lock_data_for_reading 

auxiliary_power_copy_data = DGILibAuxiliary.auxiliary_power_copy_data 

auxiliary_power_free_data = DGILibAuxiliary.auxiliary_power_free_data 

 

def __init__(self, *args, **kwargs): 

"""__init__. 

 

Parameters 

---------- 

dgilib_path : str 

Path to dgilib.dll (More info at: 

https://www.microchip.com/developmenttools/ProductDetailsATPOWERDEBUGGER) 

 

device_index : int or None 

Index of the device to use, only useful if multiple devices are 

connected (default is `None`, will resolve to `0`) 

 

device_sn : str or None 

The serial number of the device to use. Has higher 

priority than device_index (default is `None`, will resolve to 

serial number of device `0`) 

 

verbose : int 

Set to a positive number to print more status messages 

(default is `0`) 

 

Raises 

------- 

DLLError 

TODO: `Not documented yet.` 

 

""" 

# Load the dgilib.dll 

dgilib_path = kwargs.get( 

"dgilib_path", args[0] if args else "dgilib.dll") 

for location in (dgilib_path, [dgilib_path], [getcwd(), "dgilib.dll"], 

[getcwd(), dgilib_path], [getenv( 

"programfiles(x86)"), "Atmel", "Studio", "7.0", 

"Extensions", "Application", "dgilib.dll"]): 

try: 

self.dgilib = cdll.LoadLibrary(path.join(*location)) 

except OSError: 

continue 

break 

else: 

raise DLLError( 

f"Could not find dgilib.dll. If you install Atmel Studio in " + 

f"the default location (" + path.join( 

getenv("programfiles(x86)"), "Atmel", "Studio", "7.0") + 

f") it should get loaded automatically. Alternatively you " + 

f"can download it from https://www.microchip.com/mplab/avr-" + 

f"support/data-visualizer (download DGIlib dll, unzip the " + 

f"files and put the dll in {getcwd()}) or specify the path " + 

f"as the first argument or as a keyword argument " + 

f"(dgilib_path). Got dgilib_path={dgilib_path}.") 

 

# Argument parsing 

self.device_index = kwargs.get("device_index", None) 

self.device_sn = kwargs.get("device_sn", None) 

self.verbose = kwargs.get("verbose", 0) 

 

self.dgi_hndl = None 

self.power_hndl = None 

 

# Instantiate modules 

# self.discovery(self) 

# self.housekeeping(self) 

# self.interface_communication(self) 

# self.auxiliary(self) 

 

def __enter__(self): 

"""__enter__ 

 

For usage in ``with DGILib() as dgilib:`` syntax. 

 

""" 

# Discovery 

self.discover() 

device_count = self.get_device_count() 

 

if self.device_sn is None: 

if self.device_index is None: 

self.device_index = 0 

elif self.device_index > device_count - 1: 

raise DeviceIndexError( 

f"Discovered {device_count} devices so could not select " 

f"device with index {self.device_index}." 

) 

self.device_sn = self.get_device_serial(self.device_index) 

 

# UNTESTED: 

# if self.is_msd_mode(self.device_sn): 

# res = self.set_mode(self.device_sn, 1) 

# print(f"\t{res} set_mode 1") 

 

# Housekeeping 

self.dgi_hndl = self.connect(self.device_sn) 

c_status = self.connection_status() 

if c_status: 

raise DeviceConnectionError( 

f"Could not connect to device. Connection status: {c_status}.") 

 

# Interface communication 

 

# Auxiliary 

 

return self 

 

def __exit__(self, exc_type, exc_value, traceback): 

"""__exit__ 

 

For usage in ``with DGILib() as dgilib:`` syntax. 

 

""" 

# Discovery 

 

# Housekeeping 

self.disconnect() 

 

# Interface communication 

 

# Auxiliary