mirror of
https://github.com/usatiuk/EggbotWireless.git
synced 2025-10-26 08:47:49 +01:00
less magic numbers
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
#ifndef CONFIG_H
|
#ifndef CONFIG_H
|
||||||
#define CONFIG_H
|
#define CONFIG_H
|
||||||
|
|
||||||
constexpr int pin12v {5};
|
constexpr int pin12v{5};
|
||||||
|
constexpr int i2cTimeout{10}, i2cTimeoutTries{10};
|
||||||
|
constexpr int lastStsTTL{1};
|
||||||
|
constexpr unsigned long powerTimeout{20000}, powerStartupDelay{100};
|
||||||
|
|
||||||
#endif // CONFIG_H
|
#endif // CONFIG_H
|
||||||
6
Firmware/EggbotWireless/include/ConfigManager.h
Normal file
6
Firmware/EggbotWireless/include/ConfigManager.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef CONFIG_MANAGER_H
|
||||||
|
#define CONFIG_MANAGER_H
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // CONFIG_MANAGER_H
|
||||||
@@ -12,7 +12,6 @@ class Executor
|
|||||||
private:
|
private:
|
||||||
unsigned long lastStsTime;
|
unsigned long lastStsTime;
|
||||||
I2CStatusMsg lastSts;
|
I2CStatusMsg lastSts;
|
||||||
static constexpr int lastStsTTL = 1;
|
|
||||||
public:
|
public:
|
||||||
Executor(/* args */);
|
Executor(/* args */);
|
||||||
void execCommand(Command command);
|
void execCommand(Command command);
|
||||||
|
|||||||
@@ -1,36 +1,36 @@
|
|||||||
#include "Executor.h"
|
#include "Executor.h"
|
||||||
|
#include "Config.h"
|
||||||
|
#include "common/Commands.h"
|
||||||
|
|
||||||
Executor executor;
|
Executor executor;
|
||||||
|
|
||||||
Executor::Executor() {}
|
Executor::Executor() {}
|
||||||
|
|
||||||
void Executor::execCommand(Command command) {
|
void Executor::execCommand(Command command) {
|
||||||
if (command.type != CommandType::unk) {
|
if (command.type == CommandType::unk) {
|
||||||
Wire.beginTransmission(8);
|
|
||||||
byte buffer[7][sizeof(float)];
|
|
||||||
command.toBytes(buffer[0]);
|
|
||||||
for (int i = 0; i < 7; i++) {
|
|
||||||
Wire.write(buffer[i], sizeof(float));
|
|
||||||
}
|
|
||||||
Wire.endTransmission();
|
|
||||||
} else {
|
|
||||||
Serial.println("OK");
|
Serial.println("OK");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Wire.beginTransmission(8);
|
||||||
|
byte buffer[i2cCmdBytes];
|
||||||
|
command.toBytes(buffer);
|
||||||
|
Wire.write(buffer, i2cCmdBytes);
|
||||||
|
Wire.endTransmission();
|
||||||
|
|
||||||
if (command.type == CommandType::G01 || command.type == CommandType::G00) {
|
if (command.type == CommandType::G01 || command.type == CommandType::G00) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command.type == CommandType::M99) {
|
if (command.type == CommandType::M99) {
|
||||||
delay(10);
|
delay(10);
|
||||||
Wire.requestFrom(8, 5 * sizeof(float));
|
Wire.requestFrom(8, 5 * i2cFloatSize);
|
||||||
|
|
||||||
float resp[5];
|
float resp[5];
|
||||||
byte buffer[sizeof(float)];
|
byte buffer[i2cFloatSize];
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
for (unsigned int j = 0; j < sizeof(float); j++) {
|
for (unsigned int j = 0; j < i2cFloatSize; j++) {
|
||||||
while (!Wire.available()) {
|
while (!Wire.available()) {
|
||||||
}
|
}
|
||||||
buffer[j] = Wire.read();
|
buffer[j] = Wire.read();
|
||||||
@@ -68,7 +68,7 @@ I2CStatusMsg Executor::status() {
|
|||||||
|
|
||||||
Wire.requestFrom(8, 1);
|
Wire.requestFrom(8, 1);
|
||||||
while (!Wire.available()) {
|
while (!Wire.available()) {
|
||||||
if (millis() - reqTime > 10 && tries < 10) {
|
if (millis() - reqTime > i2cTimeout && tries < i2cTimeoutTries) {
|
||||||
Wire.requestFrom(8, 1);
|
Wire.requestFrom(8, 1);
|
||||||
tries++;
|
tries++;
|
||||||
reqTime = millis();
|
reqTime = millis();
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
#include "Power.h"
|
#include "Power.h"
|
||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
|
#include "Config.h"
|
||||||
|
|
||||||
Power power;
|
Power power;
|
||||||
|
|
||||||
constexpr unsigned long commandTimeout = 20000;
|
|
||||||
|
|
||||||
Power::Power() {
|
Power::Power() {
|
||||||
pinMode(pin12v, OUTPUT);
|
pinMode(pin12v, OUTPUT);
|
||||||
disable12v();
|
disable12v();
|
||||||
@@ -12,7 +11,7 @@ Power::Power() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Power::tickerRoutine() {
|
void Power::tickerRoutine() {
|
||||||
if (millis() - lastCmdTime > commandTimeout) {
|
if (millis() - lastCmdTime > powerTimeout) {
|
||||||
disable12v();
|
disable12v();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,6 +32,6 @@ void Power::commandHook() {
|
|||||||
lastCmdTime = millis();
|
lastCmdTime = millis();
|
||||||
if (!isEnabled12v()) {
|
if (!isEnabled12v()) {
|
||||||
enable12v();
|
enable12v();
|
||||||
delay(100);
|
delay(powerStartupDelay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,39 +41,34 @@ Command command;
|
|||||||
bool newCommand = false;
|
bool newCommand = false;
|
||||||
bool executing = false;
|
bool executing = false;
|
||||||
|
|
||||||
byte rxBuffer[7][sizeof(float)];
|
byte rxBuffer[i2cCmdBytes];
|
||||||
void receiveEvent(int howMany) {
|
void receiveEvent(int howMany) {
|
||||||
static int curByte = 0;
|
static int curByte = 0;
|
||||||
static int curFloat = 0;
|
|
||||||
while (Wire.available() > 0) {
|
while (Wire.available() > 0) {
|
||||||
if (!newCommand) {
|
if (!newCommand) {
|
||||||
char c = Wire.read();
|
char c = Wire.read();
|
||||||
rxBuffer[curFloat][curByte] = c;
|
rxBuffer[curByte] = c;
|
||||||
curByte++;
|
curByte++;
|
||||||
if (curByte == 4) {
|
if (curByte == i2cCmdBytes) {
|
||||||
curByte = 0;
|
curByte = 0;
|
||||||
curFloat++;
|
command.fromBytes(rxBuffer);
|
||||||
}
|
|
||||||
if (curFloat == 7) {
|
|
||||||
curFloat = 0;
|
|
||||||
command.fromBytes(rxBuffer[0]);
|
|
||||||
newCommand = true;
|
newCommand = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
byte txBuffer[5][sizeof(float)];
|
byte txBuffer[5 * i2cFloatSize];
|
||||||
void requestEvent() {
|
void requestEvent() {
|
||||||
if (command.type == CommandType::M99 && newCommand) {
|
if (command.type == CommandType::M99 && newCommand) {
|
||||||
floatToBytes(txBuffer[0], servoStepper.getPos());
|
floatToBytes(&txBuffer[0 * i2cFloatSize], servoStepper.getPos());
|
||||||
floatToBytes(txBuffer[1], eggStepper.getPos());
|
floatToBytes(&txBuffer[1 * i2cFloatSize], eggStepper.getPos());
|
||||||
|
|
||||||
floatToBytes(txBuffer[2], servoStepper.getPosMm());
|
floatToBytes(&txBuffer[2 * i2cFloatSize], servoStepper.getPosMm());
|
||||||
floatToBytes(txBuffer[3], eggStepper.getPosMm());
|
floatToBytes(&txBuffer[3 * i2cFloatSize], eggStepper.getPosMm());
|
||||||
|
|
||||||
floatToBytes(txBuffer[4], (float)pen.getEngaged());
|
floatToBytes(&txBuffer[4 * i2cFloatSize], (float)pen.getEngaged());
|
||||||
Wire.write(txBuffer[0], 5 * sizeof(float));
|
Wire.write(txBuffer, 5 * i2cFloatSize);
|
||||||
newCommand = false;
|
newCommand = false;
|
||||||
} else if (executing || newCommand) {
|
} else if (executing || newCommand) {
|
||||||
Wire.write(static_cast<int>(I2CStatusMsg::WAIT));
|
Wire.write(static_cast<int>(I2CStatusMsg::WAIT));
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
#ifndef COMMANDS_H
|
#ifndef COMMANDS_H
|
||||||
#define COMMANDS_H
|
#define COMMANDS_H
|
||||||
|
|
||||||
|
constexpr int i2cFloatSize{4}, i2cCmdFloats{7},
|
||||||
|
i2cCmdBytes{i2cFloatSize * i2cCmdFloats};
|
||||||
|
|
||||||
enum bcAxis {
|
enum bcAxis {
|
||||||
X = 1,
|
X = 1,
|
||||||
Y = 2,
|
Y = 2,
|
||||||
@@ -50,12 +53,12 @@ struct Command {
|
|||||||
arg6(arg6){};
|
arg6(arg6){};
|
||||||
|
|
||||||
Command(float *floats);
|
Command(float *floats);
|
||||||
int fromFloats(float *floats);
|
void fromFloats(float *floats);
|
||||||
int toFloats(float *floats);
|
void toFloats(float *floats);
|
||||||
|
|
||||||
Command(byte *bytes);
|
Command(byte *bytes);
|
||||||
int fromBytes(byte *bytes);
|
void fromBytes(byte *bytes);
|
||||||
int toBytes(byte *bytes);
|
void toBytes(byte *bytes);
|
||||||
};
|
};
|
||||||
|
|
||||||
void bytesToFloat(float *target, byte *val);
|
void bytesToFloat(float *target, byte *val);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
Command::Command(float *floats) { fromFloats(floats); }
|
Command::Command(float *floats) { fromFloats(floats); }
|
||||||
|
|
||||||
int Command::fromFloats(float *floats) {
|
void Command::fromFloats(float *floats) {
|
||||||
type = static_cast<CommandType>(floats[0]);
|
type = static_cast<CommandType>(floats[0]);
|
||||||
arg1 = floats[1];
|
arg1 = floats[1];
|
||||||
arg2 = floats[2];
|
arg2 = floats[2];
|
||||||
@@ -14,7 +14,7 @@ int Command::fromFloats(float *floats) {
|
|||||||
arg6 = floats[6];
|
arg6 = floats[6];
|
||||||
}
|
}
|
||||||
|
|
||||||
int Command::toFloats(float *floats) {
|
void Command::toFloats(float *floats) {
|
||||||
floats[0] = static_cast<float>(type);
|
floats[0] = static_cast<float>(type);
|
||||||
floats[1] = arg1;
|
floats[1] = arg1;
|
||||||
floats[2] = arg2;
|
floats[2] = arg2;
|
||||||
@@ -26,28 +26,28 @@ int Command::toFloats(float *floats) {
|
|||||||
|
|
||||||
Command::Command(byte *bytes) { fromBytes(bytes); }
|
Command::Command(byte *bytes) { fromBytes(bytes); }
|
||||||
|
|
||||||
int Command::fromBytes(byte *bytes) {
|
void Command::fromBytes(byte *bytes) {
|
||||||
float floats[7];
|
float floats[i2cCmdFloats];
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < i2cCmdFloats; i++) {
|
||||||
bytesToFloat(&floats[i], &bytes[i * sizeof(float)]);
|
bytesToFloat(&floats[i], &bytes[i * i2cFloatSize]);
|
||||||
}
|
}
|
||||||
fromFloats(floats);
|
fromFloats(floats);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Command::toBytes(byte *bytes) {
|
void Command::toBytes(byte *bytes) {
|
||||||
float floats[7];
|
float floats[i2cCmdFloats];
|
||||||
toFloats(floats);
|
toFloats(floats);
|
||||||
for (int i = 0; i < 7; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
floatToBytes(&bytes[i * sizeof(float)], floats[i]);
|
floatToBytes(&bytes[i * i2cFloatSize], floats[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void bytesToFloat(float *target, byte *val) {
|
void bytesToFloat(float *target, byte *val) {
|
||||||
memcpy(target, val, sizeof(float));
|
memcpy(target, val, i2cFloatSize);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void floatToBytes(byte *target, float val) {
|
void floatToBytes(byte *target, float val) {
|
||||||
memcpy(target, &val, sizeof(float));
|
memcpy(target, &val, i2cFloatSize);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user