Skip to content

Commit 3202ea3

Browse files
committed
fix Button bug
1 parent 60135cd commit 3202ea3

File tree

6 files changed

+67
-17
lines changed

6 files changed

+67
-17
lines changed

examples/Blinker_Button/Button_BLE/Button_BLE.ino

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ void loop()
3030
}
3131

3232
if (Blinker.button(BUTTON_1)) {
33-
digitalWrite(LED_BUILTIN, HIGH);
34-
}
35-
else {
36-
digitalWrite(LED_BUILTIN, LOW);
33+
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
3734
}
3835
}

examples/Blinker_Button/Button_WiFi/Button_WiFi.ino

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ void loop()
3333
}
3434

3535
if (Blinker.button(BUTTON_1)) {
36-
digitalWrite(LED_BUILTIN, HIGH);
37-
}
38-
else {
39-
digitalWrite(LED_BUILTIN, LOW);
36+
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
4037
}
4138
}

src/Blinker/BlinkerApi.h

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ class BlinkerApi
8484
_fresh = false;
8585

8686
for (uint8_t bNum = 0; bNum < _bCount; bNum++) {
87-
button(_Button[bNum]->getName());
87+
buttonParse(_Button[bNum]->getName());
8888
}
8989
for (uint8_t sNum = 0; sNum < _sCount; sNum++) {
9090
slider(_Slider[sNum]->getName());
9191
}
92+
9293
joystick(J_Xaxis);
9394
ahrs(Yaw);
9495

@@ -145,7 +146,10 @@ class BlinkerApi
145146
return false;
146147
}
147148

148-
return _Button[num]->getState();;
149+
bool _state = _Button[num]->getState();
150+
_Button[num]->freshState(false);
151+
152+
return _state;
149153
}
150154
}
151155

@@ -294,6 +298,57 @@ class BlinkerApi
294298
uint8_t joyValue[2];
295299
int16_t ahrsValue[3];
296300
bool _fresh = false;
301+
302+
bool buttonParse(const String & _bName)
303+
{
304+
int8_t num = checkNum(_bName, _Button, _bCount);
305+
String state = STRING_find_string_value(static_cast<Proto*>(this)->dataParse(), _bName);
306+
307+
if (state == BLINKER_CMD_BUTTON_PRESSED) {
308+
if( num == BLINKER_OBJECT_NOT_AVAIL ) {
309+
if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) {
310+
_Button[_bCount] = new BlinkerButton();
311+
_Button[_bCount]->name(_bName);
312+
_Button[_bCount]->freshState(true);
313+
_bCount++;
314+
}
315+
}
316+
else {
317+
_Button[num]->freshState(true);
318+
}
319+
320+
_fresh = true;
321+
return true;
322+
}
323+
else if (state == BLINKER_CMD_BUTTON_RELEASED) {
324+
if( num == BLINKER_OBJECT_NOT_AVAIL ) {
325+
if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) {
326+
_Button[_bCount] = new BlinkerButton();
327+
_Button[_bCount]->name(_bName);
328+
_Button[_bCount]->freshState(false);
329+
_bCount++;
330+
}
331+
}
332+
else {
333+
_Button[num]->freshState(false);
334+
}
335+
336+
_fresh = true;
337+
return false;
338+
}
339+
else {
340+
if( num == BLINKER_OBJECT_NOT_AVAIL ) {
341+
if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) {
342+
_Button[_bCount] = new BlinkerButton();
343+
_Button[_bCount]->name(_bName);
344+
_bCount++;
345+
}
346+
return false;
347+
}
348+
349+
return _Button[num]->getState();;
350+
}
351+
}
297352
};
298353

299354
#endif

src/Blinker/BlinkerProtocol.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ class BlinkerProtocol
101101
void println(const char str[], unsigned long n) { print(str, n); }
102102
void println(const char str[], double n) { print(str, n); }
103103

104+
void flush() {
105+
isFresh = false;
106+
}
107+
104108
private :
105109
bool checkAvail()
106110
{
@@ -165,11 +169,9 @@ void BlinkerProtocol<Transp>::run()
165169
switch (state)
166170
{
167171
case CONNECTING :
168-
// while (state == CONNECTING) {
169-
if (conn.connect()) {
170-
state = CONNECTED;
171-
}
172-
// }
172+
if (conn.connect()) {
173+
state = CONNECTED;
174+
}
173175
break;
174176
case CONNECTED :
175177
if (conState) {

src/modules/Ultrasonic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Ultrasonic::Ultrasonic(uint8_t TP, uint8_t EP, uint32_t TO)
1212
pinMode(EP,INPUT);
1313
Trig_pin = TP;
1414
Echo_pin = EP;
15-
Time_out = TO;
15+
Time_out = TO; // 3000 µs = 50cm // 30000 µs = 5 m
1616
}
1717

1818
uint32_t Ultrasonic::cm()

src/modules/Ultrasonic.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Ultrasonic
1111
{
1212
public:
1313
Ultrasonic(uint8_t TP, uint8_t EP, uint32_t TO = 30000);
14-
// 3000 µs = 50cm // 30000 µs = 5 m
1514
uint32_t cm();
1615
uint32_t inch();
1716

0 commit comments

Comments
 (0)