-
Notifications
You must be signed in to change notification settings - Fork 0
/
HapticBelt.cpp
57 lines (45 loc) · 1.03 KB
/
HapticBelt.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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "HapticBelt.h"
#include <Arduino.h>
HapticBelt::HapticBelt(int haptics[8])
{
for (int i = 0; i < 8; i++)
{
haptic_pins_[i] = haptics[i];
pinMode(haptic_pins_[i], OUTPUT);
}
}
bool HapticBelt::isDirectionValid(int direction)
{
return direction >= 0 && direction < 360 && direction % 45 == 0;
}
void HapticBelt::on(int direction, int power)
{
if (!isDirectionValid(direction))
{
// Error condition
return;
}
int position = direction / 45;
if (power >= 255){
digitalWrite(haptic_pins_[position], HIGH);
} else {
analogWrite(haptic_pins_[position], power);
}
}
void HapticBelt::off()
{
for (int i = 0; i < 8; i++)
{
digitalWrite(haptic_pins_[i], LOW);
}
}
void HapticBelt::off(int direction)
{
if (!isDirectionValid(direction))
{
// Error condition
return;
}
int position = direction / 45;
digitalWrite(haptic_pins_[position], LOW);
}