-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompassBelt.cpp
78 lines (63 loc) · 1.86 KB
/
CompassBelt.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "CompassBelt.h"
const char* CompassBelt::Direction_names[8] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
CompassBelt::CompassBelt(HapticBelt* belt, unsigned long vibrationDuration, unsigned long vibrationInverval)
{
belt_ = belt;
onDuration_ = vibrationDuration;
vibrationInterval_ = vibrationInverval;
lastOn_ = 0;
lastDirection_ = -1;
}
void CompassBelt::setAlwaysOn(bool alwaysOn){
alwaysOn_ = alwaysOn;
}
bool CompassBelt::isAlwaysOn(){
return alwaysOn_;
}
void CompassBelt::off()
{
belt_->off();
}
void CompassBelt::update(double heading)
{
if (heading < 0 || heading >= 360)
{
// Contract violation
Serial.println("XXXX");
return;
}
const int direction_threshold = 5;
int direction = (int)(round(heading / 45) * 45) % 360;
// Add a buffer zone
if (lastDirection_ != -1 && abs(direction - heading) > direction_threshold)
{
direction = lastDirection_;
}
if (direction != lastDirection_)
{
if (lastDirection_ != -1){
belt_->off(lastDirection_);
}
lastDirection_ = direction;
belt_->on(direction, 255);
lastOn_ = millis();
}
if(shouldStartVibrating()) {
belt_->on(direction, 255);
lastOn_ = millis();
} else if (shouldStopVibrating())
{
belt_->off(direction);
}
//Serial.print(direction);
Serial.print(" A: ");
Serial.println(Direction_names[direction/45]);
//Serial.println(HapticBelt(Direction_names[direction]));
}
bool CompassBelt::shouldStopVibrating(){
return !alwaysOn_ && millis() - lastOn_ >= onDuration_;
}
bool CompassBelt::shouldStartVibrating(){
long timeSinceOn = millis() - lastOn_;
return alwaysOn_ || timeSinceOn >= onDuration_ + vibrationInterval_;
}