-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwingPov.cpp
126 lines (108 loc) · 3.79 KB
/
SwingPov.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "SwingPov.h"
/* Defines */
#define SWING_TIME_MIN 150
#define SWING_TIME_MAX 250
#define SWING_TIME_AVE ((SWING_TIME_MIN + SWING_TIME_MAX) / 2)
#define SWING_TIME_HISTORIES 16
#define SWING_PHASE_ADJUST 38
/* Local Functions */
static void initSwingTimeHistory(void);
static void tuneSwingTime(uint8_t swingTime);
static void controlDirection(uint8_t currentTiltState);
/* Local Variables */
static uint8_t enablePin, statusPin;
static CONFIG_T config;
static uint8_t totalWidth, lastTiltState;
static uint32_t baseTime, targetTime;
static int8_t direction, position, positionMin, positionMax;
static uint16_t updateCounter;
static bool isInitialzed = false, isTiltChanged;
static uint16_t swingTimeHistory[SWING_TIME_HISTORIES];
static uint8_t swingTimeHistoryIndex;
static uint16_t swingTimeHistorySum, adjustedSwingTime;
/*---------------------------------------------------------------------------*/
void initSwingPov(uint8_t enablePin_, uint8_t statusPin_)
{
enablePin = enablePin_;
statusPin = statusPin_;
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
pinMode(statusPin, INPUT);
isInitialzed = true;
}
void configSwingPov(CONFIG_T &config_)
{
if (!isInitialzed) return;
config = config_;
totalWidth = config.windowWidth + config.marginWidth * 2;
positionMin = -config.marginWidth;
positionMax = positionMin + totalWidth - 1;
if (config.init) config.init();
initSwingTimeHistory();
baseTime = millis();
targetTime = millis();
uint8_t currentTiltState = digitalRead(statusPin);
lastTiltState = !currentTiltState;
controlDirection(currentTiltState);
}
void updateSwingPov(void)
{
if (!isInitialzed) return;
controlDirection(digitalRead(statusPin));
if (position >= 0 && position < config.windowWidth) {
if (config.displayColumn) config.displayColumn(position);
} else {
if (config.displayMargin) config.displayMargin();
}
position += direction;
position = constrain(position, positionMin, positionMax);
updateCounter++;
}
uint32_t getNextTime(void)
{
if (!isInitialzed) return -1;
return baseTime + adjustedSwingTime * updateCounter / totalWidth;
}
/*---------------------------------------------------------------------------*/
static void initSwingTimeHistory(void)
{
for (uint8_t i = 0; i < SWING_TIME_HISTORIES; i++) {
swingTimeHistory[i] = SWING_TIME_AVE;
}
swingTimeHistoryIndex = 0;
swingTimeHistorySum = SWING_TIME_AVE * SWING_TIME_HISTORIES;
adjustedSwingTime = SWING_TIME_AVE;
}
static void tuneSwingTime(uint8_t swingTime)
{
if (swingTime >= SWING_TIME_MIN && swingTime <= SWING_TIME_MAX) {
swingTimeHistorySum -= swingTimeHistory[swingTimeHistoryIndex];
swingTimeHistorySum += swingTime;
adjustedSwingTime = (swingTimeHistorySum + SWING_TIME_HISTORIES / 2) / SWING_TIME_HISTORIES;
swingTimeHistory[swingTimeHistoryIndex] = swingTime;
swingTimeHistoryIndex = (swingTimeHistoryIndex + 1) % SWING_TIME_HISTORIES;
}
}
static void controlDirection(uint8_t currentTiltState)
{
uint32_t currentTime = millis();
if (lastTiltState != currentTiltState) {
lastTiltState = currentTiltState;
tuneSwingTime(currentTime - baseTime);
baseTime = currentTime;
updateCounter = 0;
targetTime = currentTime + adjustedSwingTime * SWING_PHASE_ADJUST / 100;
isTiltChanged = true;
}
if (isTiltChanged && currentTime >= targetTime) {
if (currentTiltState == HIGH) {
direction = -1;
position = positionMax;
} else {
direction = 1;
position = positionMin;
}
if (config.changeDirection) config.changeDirection(direction);
isTiltChanged = false;
}
}