make command a struct

This commit is contained in:
2019-07-13 23:49:15 +03:00
parent f139333bba
commit e4e3e5e00a
9 changed files with 174 additions and 70 deletions

View File

@@ -0,0 +1,6 @@
#ifndef CONFIG_H
#define CONFIG_H
constexpr int pin12v {5};
#endif // CONFIG_H

View File

@@ -1,9 +1,9 @@
#include <Arduino.h>
#include "common/Commands.h"
#ifndef PARSER_H
#define PARSER_H
float *parseGCode(String gcode);
#include <Arduino.h>
#include "common/Commands.h"
#endif
Command parseGCode(String gcode);
#endif // PARSER_H

View File

@@ -0,0 +1,15 @@
#ifndef POWER_H
#define POWER_H
#include <Arduino.h>
#include "Config.h"
class Power {
public:
Power();
void disable12v();
void enable12v();
};
#endif // POWER_H

View File

@@ -1,13 +1,10 @@
#include "GCodeParser.h"
#include <Arduino.h>
float bytecode[4] = {-1, -1, -1, -1};
#include "GCodeParser.h"
float* parseGCode(String gcode) {
for (int i = 0; i < 4; i++) {
bytecode[i] = nanf("");
}
Command bufcmd;
Command parseGCode(String gcode) {
char commandStringIn[50];
char commandString[50];
@@ -55,28 +52,28 @@ float* parseGCode(String gcode) {
floatValue = atof(value);
if (strcmp(axis, "X") == 0) {
bytecode[X] = floatValue;
bufcmd.arg1 = floatValue;
} else if (strcmp(axis, "Y") == 0) {
bytecode[Y] = floatValue;
bufcmd.arg2 = floatValue;
} else if (strcmp(axis, "Z") == 0) {
bytecode[Z] = floatValue;
bufcmd.arg3 = floatValue;
}
}
if (strcmp(command, "G00") == 0) {
bytecode[0] = G00;
return bytecode;
bufcmd.type = CommandType::G00;
return bufcmd;
}
if (strcmp(command, "G01") == 0) {
bytecode[0] = G01;
return bytecode;
bufcmd.type = CommandType::G01;
return bufcmd;
}
}
if (strcmp(command, "M99") == 0) {
bytecode[0] = M99;
return bytecode;
bufcmd.type = CommandType::M99;
return bufcmd;
}
bytecode[0] = unk;
return bytecode;
bufcmd.type = CommandType::unk;
return bufcmd;
}

View File

@@ -0,0 +1,10 @@
#include "Power.h"
Power::Power() {
pinMode(pin12v, OUTPUT);
enable12v();
}
void Power::disable12v() { digitalWrite(pin12v, false); }
void Power::enable12v() { digitalWrite(pin12v, true); }

View File

@@ -8,13 +8,18 @@
String inString;
bool waitingForNext = false;
void sendCommand(float *command) {
if (command[0] != unk) {
void execCommand(Command command) {
if (command.type != CommandType::unk) {
Wire.beginTransmission(8);
byte txBuffer[4];
for (int i = 0; i < 4; i++) {
floatToBytes(txBuffer, command[i]);
Wire.write(txBuffer, 4);
byte buffer[7][sizeof(float)];
command.toBytes(buffer[0]);
for (int i = 0; i < 7; i++) {
/*
float dbg;
bytesToFloat(&dbg, buffer[i]);
Serial.println(dbg);
*/
Wire.write(buffer[i], sizeof(float));
}
Wire.endTransmission();
} else {
@@ -22,24 +27,23 @@ void sendCommand(float *command) {
return;
}
if (command[0] == G01 || command[0] == G00) {
if (command.type == CommandType::G01 || command.type == CommandType::G00) {
Wire.requestFrom(8, 1);
waitingForNext = true;
return;
}
if (command[0] == M99) {
if (command.type == CommandType::M99) {
Wire.requestFrom(8, 5 * sizeof(float));
float resp[5];
byte buffer[4];
byte buffer[sizeof(float)];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
for (int j = 0; j < sizeof(float); j++) {
while (!Wire.available()) {
}
char read = Wire.read();
buffer[j] = read;
buffer[j] = Wire.read();
}
bytesToFloat(&resp[i], buffer);
}
@@ -69,7 +73,7 @@ void loop() {
if (inChar == '\n') {
inString.trim();
sendCommand(parseGCode(inString));
execCommand(parseGCode(inString));
unsigned long reqTime = millis();
while (waitingForNext) {
while (!Wire.available()) {