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