Skip to content

Commit 384b02b

Browse files
author
Scott Powell
committed
* GenericVibration: code style refactor
1 parent b3e9fd7 commit 384b02b

File tree

5 files changed

+73
-79
lines changed

5 files changed

+73
-79
lines changed

examples/companion_radio/ui-new/UITask.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <helpers/ui/buzzer.h>
1313
#endif
1414
#ifdef PIN_VIBRATION
15-
#include <helpers/ui/vibration.h>
15+
#include <helpers/ui/GenericVibration.h>
1616
#endif
1717

1818
#include "../AbstractUITask.h"
@@ -25,7 +25,7 @@ class UITask : public AbstractUITask {
2525
genericBuzzer buzzer;
2626
#endif
2727
#ifdef PIN_VIBRATION
28-
genericVibration vibration;
28+
GenericVibration vibration;
2929
#endif
3030
unsigned long _next_refresh, _auto_off;
3131
NodePrefs* _node_prefs;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifdef PIN_VIBRATION
2+
#include "GenericVibration.h"
3+
4+
void GenericVibration::begin() {
5+
pinMode(PIN_VIBRATION, OUTPUT);
6+
digitalWrite(PIN_VIBRATION, LOW);
7+
duration = 0;
8+
}
9+
10+
void GenericVibration::trigger() {
11+
duration = millis();
12+
digitalWrite(PIN_VIBRATION, HIGH);
13+
}
14+
15+
void GenericVibration::loop() {
16+
if (isVibrating()) {
17+
if ((millis() / 1000) % 2 == 0) {
18+
digitalWrite(PIN_VIBRATION, LOW);
19+
} else {
20+
digitalWrite(PIN_VIBRATION, HIGH);
21+
}
22+
23+
if (millis() - duration > VIBRATION_TIMEOUT) {
24+
stop();
25+
}
26+
}
27+
}
28+
29+
bool GenericVibration::isVibrating() {
30+
return duration > 0;
31+
}
32+
33+
void GenericVibration::stop() {
34+
duration = 0;
35+
digitalWrite(PIN_VIBRATION, LOW);
36+
}
37+
38+
#endif // ifdef PIN_VIBRATION

src/helpers/ui/GenericVibration.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#ifdef PIN_VIBRATION
4+
5+
#include <Arduino.h>
6+
7+
/*
8+
* Vibration motor control class
9+
*
10+
* Provides vibration feedback for events like new messages and new contacts
11+
* Features:
12+
* - 1-second vibration pulse
13+
* - 5-second nag timeout (cooldown between vibrations)
14+
* - Non-blocking operation
15+
*/
16+
17+
#ifndef VIBRATION_TIMEOUT
18+
#define VIBRATION_TIMEOUT 5000 // 5 seconds default
19+
#endif
20+
21+
class GenericVibration {
22+
public:
23+
void begin(); // set up vibration pin
24+
void trigger(); // trigger vibration if cooldown has passed
25+
void loop(); // non-blocking timer handling
26+
bool isVibrating(); // returns true if currently vibrating
27+
void stop(); // stop vibration immediately
28+
29+
private:
30+
unsigned long duration;
31+
};
32+
33+
#endif // ifdef PIN_VIBRATION

src/helpers/ui/vibration.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/helpers/ui/vibration.h

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)