Skip to content

Commit 38b0776

Browse files
committed
combined repetitive dataclass
fixed a problem with circular dependency
1 parent 87bf7d5 commit 38b0776

File tree

5 files changed

+24
-93
lines changed

5 files changed

+24
-93
lines changed

src/edgepi/gpio/gpio_configs.py

Lines changed: 10 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from edgepi.gpio.gpio_constants import *
44

55
@dataclass(frozen = True)
6-
class GpioDACConfig:
6+
class GpioExpanderConfig:
77
''' Represents peripheral information for GPIOs used DAC
88
99
Attributes:
@@ -18,84 +18,18 @@ class GpioDACConfig:
1818
dev_path (str): device path to the peripheral device file
1919
'''
2020

21-
name: str = 'dac'
22-
device: str = 'i2c'
23-
num_pins: int = 9
24-
address: GpioExpanderAddress = GpioExpanderAddress
25-
dev_path: str = '/dev/i2c-10'
26-
27-
@dataclass(frozen = True)
28-
class GpioADCConfig:
29-
''' Represents peripheral information for GPIOs used ADC
30-
31-
Attributes:
32-
name (str): name of config
33-
34-
device (str): peripheral device
35-
36-
num_pins (int): number of pins used for this configuration
37-
38-
address (GpioExpanderAdreess): addresses of i2c device
39-
40-
dev_path (str): device path to the peripheral device file
41-
'''
42-
43-
name: str = 'adc'
44-
device: str = 'i2c'
45-
num_pins: int = 2
46-
address: GpioExpanderAddress = GpioExpanderAddress
47-
dev_path: str = '/dev/i2c-10'
48-
49-
@dataclass(frozen = True)
50-
class GpioRTDConfig:
51-
''' Represents peripheral information for GPIOs used RTD
52-
53-
Attributes:
54-
name (str): name of config
55-
56-
device (str): peripheral device
57-
58-
num_pins (int): number of pins used for this configuration
59-
60-
address (GpioExpanderAdreess): addresses of i2c device
61-
62-
dev_path (str): device path to the peripheral device file
63-
'''
64-
65-
name: str = 'rtd'
66-
device: str = 'i2c'
67-
num_pins: int = 1
68-
address: GpioExpanderAddress = GpioExpanderAddress
69-
dev_path: str = '/dev/i2c-10'
70-
71-
@dataclass(frozen = True)
72-
class GpioLEDConfig:
73-
''' Represents peripheral information for GPIOs used LED array
74-
75-
Attributes:
76-
name (str): name of config
77-
78-
device (str): peripheral device
79-
80-
num_pins (int): number of pins used for this configuration
81-
82-
address (GpioExpanderAdreess): addresses of i2c device
83-
84-
dev_path (str): device path to the peripheral device file
85-
'''
86-
87-
name: str = 'led'
88-
device: str = 'i2c'
89-
num_pins: int = 8
90-
address: GpioExpanderAddress = GpioExpanderAddress
91-
dev_path: str = '/dev/i2c-10'
21+
name: str = None
22+
device: str = None
23+
num_pins: int = None
24+
address: GpioExpanderAddress = None
25+
dev_path: str = None
9226

9327
@unique
9428
class GpioConfigs(Enum):
95-
DAC = GpioDACConfig
96-
ADC = GpioADCConfig
97-
RTD = GpioRTDConfig
98-
LED = GpioLEDConfig
29+
DAC = GpioExpanderConfig(name = 'dac', device='i2c', num_pins=9, address=GpioExpanderAddress.EXP_ONE, dev_path='/dev/i2c-10')
30+
ADC = GpioExpanderConfig(name = 'adc', device='i2c', num_pins=2, address=GpioExpanderAddress.EXP_TWO, dev_path='/dev/i2c-10')
31+
RTD = GpioExpanderConfig(name = 'rtd', device='i2c', num_pins=1, address=GpioExpanderAddress.EXP_TWO, dev_path='/dev/i2c-10')
32+
LED = GpioExpanderConfig(name = 'led', device='i2c', num_pins=8, address=GpioExpanderAddress.EXP_ONE, dev_path='/dev/i2c-10')
9933

10034
@dataclass
10135
class I2cPinInfo:

src/edgepi/gpio/gpio_constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from enum import Enum, unique
22
from edgepi.reg_helper.reg_helper import OpCode
3-
from edgepi.gpio.gpio_configs import *
43

5-
@unique
64
class GpioExpanderAddress(Enum):
75
EXP_ONE = 32
86
EXP_TWO = 33

src/test_edgepi/test_gpio/test_edgepi_gpio.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def fixture_test_edgepi_tc(mocker):
1313
mocker.patch('edgepi.peripherals.gpio.GPIO')
1414

1515
@pytest.mark.parametrize("mock_expect,config, result",
16-
[(['/dev/i2c-10'],'dac',[GpioDACConfig]),
17-
(['/dev/i2c-10'],'adc',[GpioADCConfig]),
18-
(['/dev/i2c-10'],'rtd',[GpioRTDConfig]),
19-
(['/dev/i2c-10'],'led',[GpioLEDConfig]),
16+
[(['/dev/i2c-10'],'dac',[GpioConfigs.DAC.value]),
17+
(['/dev/i2c-10'],'adc',[GpioConfigs.ADC.value]),
18+
(['/dev/i2c-10'],'rtd',[GpioConfigs.RTD.value]),
19+
(['/dev/i2c-10'],'led',[GpioConfigs.LED.value]),
2020
])
2121
@patch('edgepi.peripherals.i2c.I2CDevice')
2222
def test_edgepi_gpio_init(i2c_mock, mock_expect, config, result):

src/test_edgepi/test_gpio/test_gpio_command.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from edgepi.gpio.gpio_configs import *
44

55
@pytest.mark.parametrize('config, result',
6-
[('dac', GpioDACConfig),
7-
('adc', GpioADCConfig),
8-
('rtd', GpioRTDConfig),
6+
[('dac', GpioConfigs.DAC.value),
7+
('adc', GpioConfigs.ADC.value),
8+
('rtd', GpioConfigs.RTD.value),
99
('din', None),
1010
('dout', None),
11-
('led', GpioLEDConfig),
11+
('led', GpioConfigs.LED.value),
1212
( None, None)])
1313
def test_getPeriphConfig(config, result):
1414
assert getPeriphConfig(config) == result

src/test_edgepi/test_gpio/test_gpio_config.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55

66

77
@pytest.mark.parametrize('config, result',
8-
[(GpioDACConfig, ['dac', 'i2c', 9, 32, 33, '/dev/i2c-10']),
9-
(GpioADCConfig, ['adc', 'i2c', 2, 32, 33, '/dev/i2c-10']),
10-
(GpioRTDConfig, ['rtd', 'i2c', 1, 32, 33, '/dev/i2c-10']),
11-
(GpioLEDConfig, ['led', 'i2c', 8, 32, 33, '/dev/i2c-10'])
8+
[(GpioConfigs.DAC.value, ['dac', 'i2c', 9, 32, '/dev/i2c-10']),
9+
(GpioConfigs.ADC.value, ['adc', 'i2c', 2, 33, '/dev/i2c-10']),
10+
(GpioConfigs.RTD.value, ['rtd', 'i2c', 1, 33, '/dev/i2c-10']),
11+
(GpioConfigs.LED.value, ['led', 'i2c', 8, 32, '/dev/i2c-10'])
1212
])
1313
def test_GpioModuleConfig(config, result):
1414
assert config.name == result[0]
1515
assert config.device == result[1]
1616
assert config.num_pins == result[2]
17-
assert config.address.EXP_ONE.value == result[3]
18-
assert config.address.EXP_TWO.value == result[4]
19-
assert config.dev_path == result[5]
17+
assert config.address.value == result[3]
18+
assert config.dev_path == result[4]
2019

2120
@pytest.mark.parametrize('name, setoutput, clearoutput, address, result',
2221
[('AO_EN1', GpioAOutputSet.SET_OUTPUT_1.value, GpioAOutputClear.CLEAR_OUTPUT_1.value, GpioExpanderAddress.EXP_ONE.value, ['AO_EN1', OpCode(1, 2, 0xFE), OpCode(0, 2, 0xFE), 32]),

0 commit comments

Comments
 (0)