@@ -27,6 +27,7 @@ class GPIOCubit extends Cubit<GPIOState> {
27
27
GPIOConfig (
28
28
direction: isInput ? GPIODirection .input : GPIODirection .output,
29
29
label: label,
30
+ chip: 0 , // Default to chip 0 if no config provided
30
31
);
31
32
32
33
final result = await setupPin (
@@ -40,6 +41,7 @@ class GPIOCubit extends Cubit<GPIOState> {
40
41
if (result) {
41
42
final newPin = GPIOPin (
42
43
pinNumber: pinNumber,
44
+ chipNumber: effectiveConfig.chip, // Include chip number in pin
43
45
isInput: isInput,
44
46
label: label,
45
47
value: false ,
@@ -52,26 +54,32 @@ class GPIOCubit extends Cubit<GPIOState> {
52
54
} else {
53
55
emit (state.copyWith (
54
56
isLoading: false ,
55
- error: 'Failed to setup pin $pinNumber ' ,
57
+ error: 'Failed to setup pin $pinNumber on chip ${ effectiveConfig . chip } ' ,
56
58
));
57
59
}
58
60
}
59
61
60
62
Future <void > togglePin (int pinNumber, bool value) async {
61
63
emit (state.copyWith (isLoading: true , error: null ));
62
64
65
+ final pin = state.pins.firstWhere (
66
+ (p) => p.pinNumber == pinNumber,
67
+ orElse: () => throw Exception ('Pin not found' ),
68
+ );
69
+
63
70
final result = await repository.writePin (pinNumber, value);
64
71
if (result) {
65
- final updatedPins = state.pins.map ((pin ) {
66
- if (pin .pinNumber == pinNumber) {
72
+ final updatedPins = state.pins.map ((p ) {
73
+ if (p .pinNumber == pinNumber) {
67
74
return GPIOPin (
68
- pinNumber: pin.pinNumber,
69
- isInput: pin.isInput,
70
- label: pin.label,
75
+ pinNumber: p.pinNumber,
76
+ chipNumber: pin.chipNumber,
77
+ isInput: p.isInput,
78
+ label: p.label,
71
79
value: value,
72
80
);
73
81
}
74
- return pin ;
82
+ return p ;
75
83
}).toList ();
76
84
77
85
emit (state.copyWith (
@@ -81,23 +89,29 @@ class GPIOCubit extends Cubit<GPIOState> {
81
89
} else {
82
90
emit (state.copyWith (
83
91
isLoading: false ,
84
- error: 'Failed to write to pin $pinNumber ' ,
92
+ error: 'Failed to write to pin $pinNumber on chip ${ pin . chipNumber } ' ,
85
93
));
86
94
}
87
95
}
88
96
89
97
Future <void > readPin (int pinNumber) async {
98
+ final pin = state.pins.firstWhere (
99
+ (p) => p.pinNumber == pinNumber,
100
+ orElse: () => throw Exception ('Pin not found' ),
101
+ );
102
+
90
103
final result = await repository.readPin (pinNumber);
91
- final updatedPins = state.pins.map ((pin ) {
92
- if (pin .pinNumber == pinNumber) {
104
+ final updatedPins = state.pins.map ((p ) {
105
+ if (p .pinNumber == pinNumber) {
93
106
return GPIOPin (
94
- pinNumber: pin.pinNumber,
95
- isInput: pin.isInput,
96
- label: pin.label,
107
+ pinNumber: p.pinNumber,
108
+ chipNumber: pin.chipNumber,
109
+ isInput: p.isInput,
110
+ label: p.label,
97
111
value: result,
98
112
);
99
113
}
100
- return pin ;
114
+ return p ;
101
115
}).toList ();
102
116
103
117
emit (state.copyWith (pins: updatedPins));
@@ -124,15 +138,16 @@ class GPIOCubit extends Cubit<GPIOState> {
124
138
if (! success) {
125
139
emit (state.copyWith (
126
140
isLoading: false ,
127
- error: 'Failed to close pin ${pin .pinNumber }' ,
141
+ error:
142
+ 'Failed to close pin ${pin .pinNumber } on chip ${pin .chipNumber }' ,
128
143
));
129
144
return ;
130
145
}
131
146
}
132
147
133
148
emit (state.copyWith (
134
149
isLoading: false ,
135
- pins: [], // Clear all pins after successful cleanup
150
+ pins: [],
136
151
));
137
152
} catch (e) {
138
153
emit (state.copyWith (
0 commit comments