forked from jhoff/Split-Flap-Display
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitFlapDisplay.cpp
More file actions
320 lines (274 loc) · 11.3 KB
/
SplitFlapDisplay.cpp
File metadata and controls
320 lines (274 loc) · 11.3 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "SplitFlapDisplay.h"
#include "JsonSettings.h"
#include "SplitFlapModule.h"
#include "SplitFlapMqtt.h"
SplitFlapDisplay::SplitFlapDisplay(JsonSettings &settings) : settings(settings) {}
void SplitFlapDisplay::init() {
numModules = settings.getInt("moduleCount");
stepsPerRot = settings.getInt("stepsPerRot");
displayOffset = settings.getInt("displayOffset");
magnetPosition = settings.getInt("magnetPosition");
maxVel = settings.getFloat("maxVel");
charSetSize = settings.getInt("charset");
std::vector<int> settingAddresses = settings.getIntVector("moduleAddresses");
for (int i = 0; i < numModules; i++) {
moduleAddresses[i] = (uint8_t) settingAddresses[i];
}
std::vector<int> settingOffsets = settings.getIntVector("moduleOffsets");
for (int i = 0; i < numModules; i++) {
moduleOffsets[i] = settingOffsets[i];
}
Serial.print("Module Offsets: ");
for (int i = 0; i < numModules; i++) {
Serial.print(moduleOffsets[i]);
Serial.print(" ");
}
Serial.println();
for (uint8_t i = 0; i < numModules; i++) {
modules[i] = SplitFlapModule(
moduleAddresses[i], stepsPerRot, moduleOffsets[i] + displayOffset, magnetPosition, charSetSize
);
}
SDAPin = settings.getInt("sdaPin");
SCLPin = settings.getInt("sclPin");
Wire.begin(SDAPin, SCLPin);
Wire.setClock(400000);
for (uint8_t i = 0; i < numModules; i++) {
modules[i].init();
}
}
void SplitFlapDisplay::testAll() {
char testChars[37] = {' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int numChars = sizeof(testChars) / sizeof(testChars[0]);
int targetPositions[numModules];
int charPos;
for (int i = 0; i < numChars; i++) {
// Serial.print("Target Positions: [");
// fill array with same char
for (int j = 0; j < numModules; j++) {
targetPositions[j] = modules[j].getCharPosition(testChars[i]);
// Serial.print(targetPositions[j]);
// Serial.print(" , ");
}
// Serial.println("]");
moveTo(targetPositions);
delay(500);
}
}
void SplitFlapDisplay::testRandom(float speed) {
char testChars[37] = {' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int targetPositions[numModules];
char randChar;
Serial.print("Target: ");
for (int i = 0; i < numModules; i++) {
randChar = testChars[random(0, 37)];
targetPositions[i] = modules[i].getCharPosition(randChar);
Serial.print(randChar);
}
Serial.println(" ");
moveTo(targetPositions, speed);
}
void SplitFlapDisplay::testCount() {
int count = 0;
int maxCount = pow(10, numModules);
char targetChar;
int targetInteger;
int targetPositions[numModules];
for (int i = 0; i < maxCount; i++) {
// get each character in the count integer
for (int j = 0; j < numModules; j++) {
targetInteger = (i % (int) pow(10, j + 1)) / (int) pow(10, j);
targetChar = targetInteger + '0'; // convert to char
targetPositions[numModules - j - 1] = modules[j].getCharPosition(targetChar);
}
moveTo(targetPositions);
delay(250);
}
}
void SplitFlapDisplay::home(float speed) {
Serial.println("Homing");
int targetPositions[numModules];
for (int i = 0; i < numModules; i++) {
targetPositions[i] = (modules[i].getPosition() - 1 + stepsPerRot) % stepsPerRot;
}
startMotors();
moveTo(targetPositions, speed, false);
char homeChar = ' ';
int charPosition;
for (int i = 0; i < numModules; i++) {
targetPositions[i] = modules[i].getCharPosition(homeChar);
}
moveTo(targetPositions, speed);
}
void SplitFlapDisplay::homeToString(String homeString, float speed, bool centering) {
Serial.println("Homing");
int targetPositions[numModules];
for (int i = 0; i < numModules; i++) {
targetPositions[i] = (modules[i].getPosition() - 1 + stepsPerRot) % stepsPerRot;
}
startMotors();
moveTo(targetPositions, speed, false);
writeString(homeString, speed, centering);
}
void SplitFlapDisplay::homeToChar(char homeChar, float speed) {
Serial.println("Homing");
int targetPositions[numModules];
for (int i = 0; i < numModules; i++) {
targetPositions[i] = (modules[i].getPosition() - 1 + stepsPerRot) % stepsPerRot;
}
startMotors();
moveTo(targetPositions, speed, false);
for (int i = 0; i < numModules; i++) {
targetPositions[i] = modules[i].getCharPosition(homeChar);
}
moveTo(targetPositions, true, speed);
}
void SplitFlapDisplay::writeChar(char inputChar, float speed) {
int targetPositions[numModules];
// Iterate through the input string and process each character
for (int i = 0; i < numModules; i++) {
targetPositions[i] = modules[i].getCharPosition(inputChar);
}
moveTo(targetPositions, speed);
}
void SplitFlapDisplay::writeString(String inputString, float speed, bool centering) {
String displayString = inputString.substring(0, numModules);
if (centering) {
int totalPadding = numModules - displayString.length();
int paddingLeft = totalPadding / 2;
int paddingRight = totalPadding - paddingLeft;
// Add padding to the left
String result = "";
for (int i = 0; i < paddingLeft; i++) {
result += " ";
}
// Add the original string
result += displayString;
// Add padding to the right
for (int i = 0; i < paddingRight; i++) {
result += " ";
}
displayString = result;
} else { // pad blanks to end, if no centering
while (displayString.length() < numModules) { // Pad with spaces
displayString += " "; // Padding with space
}
}
int targetPositions[numModules];
// Iterate through the input string and process each character
for (int i = 0; i < displayString.length(); i++) {
char currentChar = displayString[i];
// Serial.println(currentChar);
targetPositions[i] = modules[i].getCharPosition(currentChar);
}
moveTo(targetPositions, speed);
if (mqtt && mqtt->isConnected()) {
mqtt->publishState(displayString);
}
}
void SplitFlapDisplay::moveTo(int targetPositions[], float speed, bool releaseMotors) {
// TODO check length of array and return if empty
speed = constrain(speed, 2, maxVel);
float stepsPerSecond = (speed / 60) * stepsPerRot;
float timePerStep = 1000000 / stepsPerSecond;
unsigned long currentTime = micros();
int checkIntervalUs = 20 * 1000; // How often to check each modules hall effect sensor, less
// than 20ms causes issues with bouncing
int startStopDelay = 200; // time to wait to let motor realign itself to
// magnetic field on stop and start
bool resetLatches[numModules] = {}; // Initialize to false //start with latch on to prevent case where the
// motion starts with the magnet over the sensor
bool needsStepping[numModules] = {}; // Initialize to false; //modules that still require moving
unsigned long lastStepTimes[numModules] = {}; // Initialize to false; //track when each module was last stepped
unsigned long lastSensorCheckTime = currentTime; // track when we last read all the hall effect sensors
for (int i = 0; i < numModules; i++) {
targetPositions[i] = constrain(
targetPositions[i],
0,
stepsPerRot - 1
); // Constrain to avoid errors with incorrect inputs
resetLatches[i] = true;
lastStepTimes[i] = currentTime;
if (modules[i].getPosition() != targetPositions[i]) {
needsStepping[i] = true;
} else {
needsStepping[i] = false;
}
}
startMotors(); // not sure if this helps or not, likely that it does not based
// on testing
delay(startStopDelay); // give the motor time to align to magnetic field
bool isFinished = checkAllFalse(needsStepping, numModules);
while (! isFinished) {
currentTime = micros();
for (int i = 0; i < numModules; i++) {
if (((currentTime - lastStepTimes[i]) > timePerStep) && needsStepping[i]) {
modules[i].step();
lastStepTimes[i] = micros();
if (modules[i].getPosition() == targetPositions[i]) { // this module is not in the correct position,
// requires stepping
needsStepping[i] = false;
}
}
}
if ((currentTime - lastSensorCheckTime) > checkIntervalUs) { // check hall effect sensor every checkIntervalMs
// check every modules sensor
for (int i = 0; i < numModules; i++) {
if (needsStepping[i] &&
(modules[i].readHallEffectSensor() == true
)) { // only check sensors where the module is still moving
if (! resetLatches[i]) {
// UNCOMMENTING THIS WILL PROBBALY MAKE THE MOTORS INACCURATE, DUE
// TO TIME TAKEN TO PRINT
// Serial.print("Module: ");
// Serial.print(i);
// Serial.print(" Magnet Position: ");
// Serial.print(modules[i].getMagnetPosition());
// Serial.print(" Actual Position: ");
// Serial.print(modules[i].getPosition());
// Serial.print(" Error: ");
// Serial.println((modules[i].getMagnetPosition() -
// modules[i].getPosition()));
modules[i].magnetDetected(); // update position to the modules
// magnet position
resetLatches[i] = true;
}
} else if (resetLatches[i] == true) {
resetLatches[i] = false;
}
}
isFinished = checkAllFalse(needsStepping, numModules);
lastSensorCheckTime = currentTime; // recall micros because for loop may
// take a moment to execute
}
}
if (releaseMotors) {
delay(startStopDelay); // allow all motors time to settle
stopMotors();
}
}
bool SplitFlapDisplay::checkAllFalse(bool array[], int size) {
for (int i = 0; i < size; i++) {
if (array[i] == true) {
return false; // As soon as a true value is found, return false
}
}
return true; // All values were false
}
void SplitFlapDisplay::startMotors() { // Probably broken somewhere, not sure
// why, haven't looked
for (int i = 0; i < numModules; i++) {
modules[i].start();
}
}
void SplitFlapDisplay::stopMotors() {
// Serial.println("Stopping Motors");
for (int i = 0; i < numModules; i++) {
modules[i].stop();
}
}
void SplitFlapDisplay::setMqtt(SplitFlapMqtt *mqttHandler) {
mqtt = mqttHandler;
}