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

"""This module provides user friendly way to interact with the DGILib API.""" 

 

from time import sleep 

 

from pydgilib.dgilib import DGILib 

 

from pydgilib.dgilib_config import ( 

INTERFACE_GPIO, INTERFACE_TIMESTAMP, INTERFACE_POWER_DATA) 

from pydgilib_extra.dgilib_extra_config import (INTERFACE_POWER, LOGGER_CSV) 

from pydgilib_extra.dgilib_logger import DGILibLogger 

from pydgilib_extra.dgilib_data import LoggerData 

from pydgilib_extra.dgilib_interface import DGILibInterface 

from pydgilib_extra.dgilib_interface_gpio import DGILibInterfaceGPIO 

from pydgilib_extra.dgilib_interface_power import DGILibInterfacePower 

 

 

class DGILibExtra(DGILib): 

"""A user friendly way to interact with the DGILib API.""" 

 

default_enabled_interfaces = [INTERFACE_GPIO, INTERFACE_POWER] 

default_loggers = [LOGGER_CSV] 

 

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

"""Instantiate DGILibExtra object.""" 

# Add modules as classes (will be replaced by objects when used) 

self.logger = DGILibLogger 

self.interfaces = {INTERFACE_GPIO: DGILibInterfaceGPIO, 

INTERFACE_POWER: DGILibInterfacePower} 

# Set default values for attributes 

self.available_interfaces = [] 

self.enabled_interfaces = [] 

self.timer_factor = None 

self.data = None 

# Instantiate base class 

DGILib.__init__(self, *args, **kwargs) 

# Store arguments 

self.args = args 

self.kwargs = kwargs 

 

# Instantiate logger 

if self.kwargs.get("loggers", self.default_loggers): 

self.logger = self.logger(self, *self.args, **self.kwargs) 

 

if self.verbose >= 2: 

print("args: ", args) 

print("kwargs: ", kwargs) 

 

def __enter__(self): 

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

DGILib.__enter__(self) 

self.available_interfaces = self.interface_list() 

if INTERFACE_POWER_DATA in self.available_interfaces: 

self.available_interfaces.append(INTERFACE_POWER) 

 

# Instantiate interface objects and enable the interfaces 

for interface_id in self.kwargs.get( 

"interfaces", self.default_enabled_interfaces): 

if interface_id in self.interfaces: 

self.interfaces[interface_id] = self.interfaces[interface_id]( 

self, *self.args, **self.kwargs) 

else: 

self.interfaces[interface_id] = DGILibInterface( 

self, *self.args, **self.kwargs) 

self.interfaces[interface_id].enable() 

return self 

 

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

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

for interface in self.interfaces.values(): 

interface.disable() 

 

DGILib.__exit__(self, exc_type, exc_value, traceback) 

 

def info(self): 

"""Get the build information of DGILib. 

 

Returns 

------- 

tuple(str, str, str, str, str, str) 

Version information of DGILib: 

 

- major_version: the major_version of DGILib 

 

- minor_version: the minor_version of DGILib 

 

- build_number: the build number of DGILib. 0 if not supported 

 

- major_fw: the major firmware version of the connected DGI device 

 

- minor_fw: the minor firmware version of the connected DGI device 

""" 

major_version = self.get_major_version() 

minor_version = self.get_minor_version() 

build_number = self.get_build_number() 

major_fw, minor_fw = self.get_fw_version() 

 

return major_version, minor_version, build_number, major_fw, minor_fw 

 

def device_reset(self, duration=1): 

"""device_reset 

 

Set the device reset line for duration seconds.""" 

self.target_reset(True) 

sleep(duration) 

self.target_reset(False) 

 

def get_time_factor(self): 

"""get_time_factor 

 

Get the factor to multiply timestamps by to get seconds. 

 

Returns 

------- 

float 

Timer factor 

""" 

_, config_value = self.interface_get_configuration(INTERFACE_TIMESTAMP) 

timer_prescaler = config_value[0] 

timer_frequency = config_value[1] 

 

if self.verbose: 

print( 

f"timer_factor: {timer_prescaler / timer_frequency}, " 

f"timer_prescaler: {timer_prescaler}, timer_frequency: " 

f"{timer_frequency}") 

 

return timer_prescaler / timer_frequency 

 

def empty_data(self, interfaces=None): 

"""empty_data 

 

Populate self.data with an empty data structure (of type  

:class:`LoggerData`). 

 

Parameters 

---------- 

interfaces : list(int, int, ...) 

List of interface ids. (default: `self.enabled_interfaces`) 

""" 

if interfaces is None: 

interfaces = self.enabled_interfaces 

self.data = LoggerData(interfaces)