less magic numbers

This commit is contained in:
2019-07-23 18:27:05 +03:00
parent 9fbdbda4f1
commit 665bc4b63f
9 changed files with 56 additions and 51 deletions

View File

@@ -1,6 +1,9 @@
#ifndef 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

View File

@@ -0,0 +1,6 @@
#ifndef CONFIG_MANAGER_H
#define CONFIG_MANAGER_H
#endif // CONFIG_MANAGER_H

View File

@@ -12,7 +12,6 @@ class Executor
private:
unsigned long lastStsTime;
I2CStatusMsg lastSts;
static constexpr int lastStsTTL = 1;
public:
Executor(/* args */);
void execCommand(Command command);

View File

@@ -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();

View File

@@ -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);
}
}