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

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

"""This module holds the automated tests for DGILib Interface Communication.""" 

 

from pydgilib.dgilib import DGILib 

from pydgilib.dgilib_config import ( 

NUM_INTERFACES, INTERFACE_TIMESTAMP, INTERFACE_SPI, INTERFACE_USART, 

INTERFACE_I2C, INTERFACE_GPIO, INTERFACE_POWER_DATA, INTERFACE_POWER_SYNC, 

INTERFACE_RESERVED) 

 

from time import sleep 

 

import pytest 

 

verbosity = (0, 99) 

 

# Number of seconds to log data for in read and clear tests 

polling_duration = 1 

 

INTERFACES = [INTERFACE_TIMESTAMP, 

INTERFACE_SPI, 

INTERFACE_USART, 

INTERFACE_I2C, 

INTERFACE_GPIO, 

INTERFACE_POWER_DATA, 

INTERFACE_POWER_SYNC, 

80, # Not in documentation 

INTERFACE_RESERVED] 

 

INTERFACES_ENABLE = [INTERFACE_SPI, 

INTERFACE_USART, 

INTERFACE_I2C, 

INTERFACE_GPIO, 

INTERFACE_POWER_SYNC, 

80, # Not in documentation 

INTERFACE_RESERVED] 

 

INTERFACES_SET_CONFIG = [INTERFACE_TIMESTAMP, 

INTERFACE_SPI, 

INTERFACE_USART, 

INTERFACE_I2C, 

INTERFACE_GPIO, 

INTERFACE_POWER_SYNC, 

80, # Not in documentation 

INTERFACE_RESERVED] 

 

INTERFACES_WRITE = [INTERFACE_USART, 

INTERFACE_I2C, 

INTERFACE_GPIO, 

INTERFACE_RESERVED] 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_list(verbose): 

"""test_interface_list. 

 

DGILibInterfaceCommunication.interface_list 

""" 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

assert isinstance(interfaces, list) 

assert len(interfaces) < NUM_INTERFACES 

for interface in interfaces: 

assert interface in INTERFACES 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_enable(verbose): 

"""test_interface_enable. 

 

DGILibInterfaceCommunication.interface_enable 

""" 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_ENABLE: 

if interface_id in interfaces: 

assert dgilib.interface_enable(interface_id) is None 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_disable(verbose): 

"""test_interface_disable. 

 

DGILibInterfaceCommunication.interface_disable 

""" 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES: 

if interface_id in interfaces: 

assert dgilib.interface_disable(interface_id) is None 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_get_configuration(verbose): 

"""test_interface_get_configuration. 

 

DGILibInterfaceCommunication.interface_get_configuration 

""" 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES: 

if interface_id in interfaces: 

config = dgilib.interface_get_configuration(interface_id) 

assert isinstance(config, tuple) 

assert len(config) == 2 

assert isinstance(config[0], list) 

assert isinstance(config[1], list) 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_set_configuration(verbose): 

"""test_interface_set_configuration. 

 

DGILibInterfaceCommunication.interface_set_configuration 

 

Gets the configuration and sets it to the same values. 

""" 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_SET_CONFIG: 

if interface_id in interfaces: 

config = dgilib.interface_get_configuration(interface_id) 

assert dgilib.interface_set_configuration( 

interface_id, *config) is None 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_clear_buffer(verbose): 

"""test_interface_clear_buffer. 

 

DGILibInterfaceCommunication.interface_clear_buffer 

""" 

# When not enabled 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES: 

if interface_id in interfaces: 

assert dgilib.interface_clear_buffer(interface_id) is None 

# When enabled 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_ENABLE: 

if interface_id in interfaces: 

dgilib.interface_enable(interface_id) 

assert dgilib.interface_clear_buffer(interface_id) is None 

dgilib.interface_disable(interface_id) 

# When enabled and polling 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_ENABLE: 

if interface_id in interfaces: 

dgilib.interface_enable(interface_id) 

dgilib.start_polling() 

sleep(polling_duration) 

assert dgilib.interface_clear_buffer(interface_id) is None 

dgilib.stop_polling() 

dgilib.interface_disable(interface_id) 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_read_data(verbose): 

"""test_interface_read_data. 

 

DGILibInterfaceCommunication.interface_read_data 

""" 

# When not enabled 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_ENABLE: 

if interface_id in interfaces: 

data = dgilib.interface_read_data(interface_id) 

assert isinstance(data, tuple) 

assert len(data) == 2 

assert isinstance(data[0], list) 

assert isinstance(data[1], list) 

assert len(data[0]) == len(data[1]) 

# When enabled 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_ENABLE: 

if interface_id in interfaces: 

dgilib.interface_enable(interface_id) 

data = dgilib.interface_read_data(interface_id) 

assert isinstance(data, tuple) 

assert len(data) == 2 

assert isinstance(data[0], list) 

assert isinstance(data[1], list) 

assert len(data[0]) == len(data[1]) 

dgilib.interface_disable(interface_id) 

# When enabled and polling 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_ENABLE: 

if interface_id in interfaces: 

dgilib.interface_enable(interface_id) 

dgilib.start_polling() 

sleep(polling_duration) 

data = dgilib.interface_read_data(interface_id) 

assert isinstance(data, tuple) 

assert len(data) == 2 

assert isinstance(data[0], list) 

assert isinstance(data[1], list) 

assert len(data[0]) == len(data[1]) 

dgilib.stop_polling() 

dgilib.interface_disable(interface_id) 

 

 

@pytest.mark.parametrize("verbose", verbosity) 

def test_interface_write_data(verbose): 

"""test_interface_write_data. 

 

DGILibInterfaceCommunication.interface_write_data 

""" 

# When not enabled 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_WRITE: 

if interface_id in interfaces: 

assert dgilib.interface_write_data(interface_id, [0]) is None 

# When enabled 

with DGILib(verbose=verbose) as dgilib: 

interfaces = dgilib.interface_list() 

for interface_id in INTERFACES_WRITE: 

if interface_id in interfaces: 

dgilib.interface_enable(interface_id) 

assert dgilib.interface_write_data(interface_id, [0]) is None 

dgilib.interface_disable(interface_id)