Skip to content

Commit 001d948

Browse files
Merge pull request #1 from GoWired/dev
Dev
2 parents 4f116a1 + 038f0ca commit 001d948

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=GoWired-lib
2-
version=1.0.0
2+
version=1.1.0
33
author=GoWired
44
maintainer=GoWired <hello@gowired.dev>
55
sentence=Common libraries used by GoWired software.

src/core/CommonIO.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ void CommonIO::SetValues(bool RelayOFF, bool Invert, uint8_t Type, uint8_t Pin1,
6565
break;
6666
// Touch button
6767
case 5:
68-
_SensorPin = Pin1; ReadReference();
68+
_SensorPin = Pin1; _ReadReference(_SensorPin);
6969
break;
7070
// Touch button + digital button + output
7171
case 6:
72-
_SensorPin = Pin1; ReadReference();
72+
_SensorPin = Pin1; _ReadReference(_SensorPin);
7373
_SensorPin2 = Pin2; pinMode(_SensorPin2, INPUT_PULLUP);
7474
_RelayPin = Pin3; pinMode(_RelayPin, OUTPUT);
7575
digitalWrite(_RelayPin, _RelayOFF);
7676
break;
7777
// Touch button + output
7878
case 7:
79-
_SensorPin = Pin1; ReadReference();
79+
_SensorPin = Pin1; _ReadReference(_SensorPin);
8080
_RelayPin = Pin2; pinMode(_RelayPin, OUTPUT);
8181
digitalWrite(_RelayPin, _RelayOFF);
8282
break;
@@ -86,12 +86,25 @@ void CommonIO::SetValues(bool RelayOFF, bool Invert, uint8_t Type, uint8_t Pin1,
8686
}
8787

8888
/**
89-
* @brief reads reference value for touch buttons
89+
* @brief public function to call _ReadReference()
9090
*
9191
*/
9292
void CommonIO::ReadReference() {
9393

94-
_TouchReference = ADCTouch.read(_SensorPin, 500); // ADCTouch.read(pin, number of samples)
94+
uint8_t InputPin;
95+
96+
if(SensorType == 5 || SensorType == 6 || SensorType == 7) InputPin = _SensorPin;
97+
98+
_ReadReference(InputPin);
99+
}
100+
101+
/**
102+
* @brief reads reference value for touch buttons
103+
*
104+
*/
105+
void CommonIO::_ReadReference(uint8_t InputPin) {
106+
107+
_TouchReference = ADCTouch.read(InputPin, 512); // ADCTouch.read(pin, number of samples)
95108
}
96109

97110
/**
@@ -119,7 +132,7 @@ void CommonIO::CheckInput(uint16_t LongpressDuration, uint8_t DebounceValue) {
119132
uint32_t StartTime = millis();
120133

121134
do {
122-
Reading = _ReadDigital(DebounceValue);
135+
Reading = _ReadDigital(DebounceValue, _SensorPin);
123136

124137
if(SensorType == 0 || SensorType == 1) {
125138
if(State != Reading) {
@@ -167,10 +180,10 @@ void CommonIO::CheckInput2(uint8_t Threshold, uint16_t LongpressDuration, uint8_
167180
uint32_t StartTime = millis();
168181

169182
do {
170-
Reading = _ReadAnalog(Threshold);
183+
Reading = _ReadAnalog(Threshold, _SensorPin);
171184

172185
if(SensorType == 6 && !Reading) {
173-
Reading = _ReadDigital(DebounceValue);
186+
Reading = _ReadDigital(DebounceValue, _SensorPin2);
174187
}
175188

176189
if(!Shortpress && Reading) {
@@ -195,10 +208,10 @@ void CommonIO::CheckInput3(uint8_t Threshold, uint8_t DebounceValue, bool Monost
195208
bool Shortpress = false;
196209

197210
do {
198-
Reading = _ReadAnalog(Threshold);
211+
Reading = _ReadAnalog(Threshold, _SensorPin);
199212

200213
if(SensorType == 6 && !Reading) {
201-
Reading = _ReadDigital(DebounceValue);
214+
Reading = _ReadDigital(DebounceValue, _SensorPin2);
202215
}
203216

204217
if(Monostable) {
@@ -227,15 +240,15 @@ void CommonIO::CheckInput3(uint8_t Threshold, uint8_t DebounceValue, bool Monost
227240
* @return true if input state is true
228241
* @return false if input state is false
229242
*/
230-
bool CommonIO::_ReadDigital(uint8_t DebounceValue) {
243+
bool CommonIO::_ReadDigital(uint8_t DebounceValue, uint8_t InputPin) {
231244

232245
bool DigitalReading;
233246
bool PreviousReading = false;
234247
bool InputState = false;
235248
uint32_t StartTime = millis();
236249

237250
do {
238-
DigitalReading = (_Invert ? digitalRead(_SensorPin) : !digitalRead(_SensorPin));
251+
DigitalReading = (_Invert ? digitalRead(InputPin) : !digitalRead(InputPin));
239252

240253
if(DigitalReading && !PreviousReading) {
241254
StartTime = millis();
@@ -267,12 +280,12 @@ bool CommonIO::_ReadDigital(uint8_t DebounceValue) {
267280
* @return true if touch was sensed
268281
* @return false if touch was not sensed
269282
*/
270-
bool CommonIO::_ReadAnalog(uint8_t Threshold) {
283+
bool CommonIO::_ReadAnalog(uint8_t Threshold, uint8_t InputPin) {
271284

272285
int TouchValue;
273286
bool ButtonState = false;
274287

275-
TouchValue = ADCTouch.read(_SensorPin, 64);
288+
TouchValue = ADCTouch.read(InputPin, 64);
276289
TouchValue -= _TouchReference;
277290
TouchDiagnosisValue = TouchValue;
278291

src/core/CommonIO.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ class CommonIO
4444
bool _HighStateDetected;
4545
bool _RelayOFF;
4646

47-
bool _ReadAnalog(uint8_t Threshold);
48-
bool _ReadDigital(uint8_t DebounceValue);
47+
bool _ReadAnalog(uint8_t Threshold, uint8_t InputPin);
48+
bool _ReadDigital(uint8_t DebounceValue, uint8_t InputPin);
49+
void _ReadReference(uint8_t InputPin);
4950

5051
};
5152

src/core/Shutters.cpp

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,11 @@ uint32_t Shutters::ReadMessage(uint8_t Order) {
7777

7878
uint32_t MovementTime;
7979

80-
if(State == 2) {
81-
NewState = Order;
80+
NewState = Order;
8281

83-
if(Order == 0) {
84-
MovementTime = _UpTime;
85-
}
86-
else if(Order == 1) {
87-
MovementTime = _DownTime;
88-
}
89-
else if(Order == 2) {
90-
MovementTime = 0;
91-
}
92-
}
93-
else {
94-
NewState = 2;
95-
MovementTime = 0;
96-
}
82+
if(Order == 0) MovementTime = _UpTime;
83+
else if(Order == 1) MovementTime = _DownTime;
84+
else MovementTime = 0;
9785

9886
return MovementTime;
9987
}
@@ -108,20 +96,15 @@ uint32_t Shutters::ReadButtons(uint8_t Button) {
10896

10997
uint32_t MovementTime;
11098

111-
// Roller shutter is stopped -> start moving
112-
if(State == 2) {
113-
NewState = Button;
114-
if(Button == 0) {
115-
MovementTime = _UpTime;
116-
}
117-
else if(Button == 1) {
118-
MovementTime = _DownTime;
119-
}
99+
NewState = Button;
100+
101+
// Stop
102+
if(State != 2 && State == NewState) {
103+
NewState = 2; MovementTime = 0;
120104
}
121-
// Roller shutter is moving -> stop
105+
// Start moving or change direction
122106
else {
123-
NewState = 2;
124-
MovementTime = 0;
107+
MovementTime = NewState == 0 ? _UpTime : _DownTime;
125108
}
126109

127110
return MovementTime;
@@ -165,14 +148,18 @@ uint8_t Shutters::Movement() {
165148
}
166149
// Move upward
167150
else if(NewState == 0) {
168-
digitalWrite(_DownPin, _RelayOff);
169-
delay(20);
151+
if(digitalRead(_DownPin) != _RelayOff) {
152+
digitalWrite(_DownPin, _RelayOff);
153+
delay(50);
154+
}
170155
digitalWrite(_UpPin, _RelayOn);
171156
}
172157
// Move downward
173158
else if(NewState == 1) {
174-
digitalWrite(_UpPin, _RelayOff);
175-
delay(20);
159+
if(digitalRead(_UpPin) != _RelayOff) {
160+
digitalWrite(_UpPin, _RelayOff);
161+
delay(50);
162+
}
176163
digitalWrite(_DownPin, _RelayOn);
177164
}
178165

0 commit comments

Comments
 (0)