-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVibrator.cpp
45 lines (33 loc) · 1.01 KB
/
Vibrator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* (C) 2016 Onwrikbaar <onwrikbaar@hotmail.com>
*/
#include "Vibrator.h"
#define VIB_SPEED_MIN 70 // Must be large enough for the vib to start.
#define VIB_SPEED_MAX 110 // Keep below 128 when powering a 3V vib from a 6V supply.
// Constructor.
Vibrator::Vibrator(uint8_t pn) : OutputPin(pn)
{
resetSpeed();
}
void Vibrator::setSpeed(uint8_t speed)
{
// Note: For a 3V vibrator the on-time must be scaled down proportionally if the supply voltage exceeds 3V.
pwmWrite(speed <= 128 ? speed : 128);
}
void Vibrator::start()
{
setSpeed(speed);
}
void Vibrator::stop(int8_t bump)
{
setSpeed(0); // Switch off.
if (bump >= 0) { // TODO Check for overflow.
if (speed < VIB_SPEED_MAX) speed += bump;
} else { // TODO Check for underflow.
if (speed > VIB_SPEED_MIN) speed += bump;
}
}
void Vibrator::resetSpeed()
{
speed = VIB_SPEED_MIN;
}