Skip to content

Commit

Permalink
Merge pull request #52 from bdring/Devt
Browse files Browse the repository at this point in the history
Devt
  • Loading branch information
bdring authored Oct 14, 2021
2 parents e15a34e + 8c0defe commit 29314f2
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 27 deletions.
2 changes: 2 additions & 0 deletions FluidNC/src/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ std::map<Error, const char*> ErrorNames = {
{ Error::BadPinSpecification, "Bad Pin Specification" },
{ Error::JogCancelled, "Jog Cancelled" },
{ Error::ConfigurationInvalid, "Configuration is invalid. Check boot messages for ERR's." },
{ Error::UploadFailed, "File Upload Failed" },
{ Error::DownloadFailed, "File Download Failed" },
};
2 changes: 2 additions & 0 deletions FluidNC/src/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ enum class Error : uint8_t {
BadPinSpecification = 150,
BadRuntimeConfigSetting = 151,
ConfigurationInvalid = 152,
UploadFailed = 160,
DownloadFailed = 161,
};

const char* errorString(Error errorNumber);
Expand Down
27 changes: 24 additions & 3 deletions FluidNC/src/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
#include "Machine/MachineConfig.h" // config->
#include "SDCard.h"

int FileStream::available() {
return 1;
}
int FileStream::read() {
char data;
size_t res = fread(&data, 1, 1, _fd);
return res == 1 ? data : -1;
}
int FileStream::peek() {
return -1;
}
void FileStream::flush() {}

size_t FileStream::readBytes(char* buffer, size_t length) {
return fread(buffer, 1, length, _fd);
}

size_t FileStream::write(uint8_t c) {
return FileStream::write(&c, 1);
}
Expand All @@ -10,9 +27,13 @@ size_t FileStream::write(const uint8_t* buffer, size_t length) {
return fwrite(buffer, 1, length, _fd);
}

FileStream::FileStream(const char* filename, const char* defaultFs) {
FileStream::FileStream(const char* filename, const char* mode, const char* defaultFs) {
String path;

if (!filename || !*filename) {
throw Error::FsFailedCreateFile;
}

// Insert the default file system prefix if a file system name is not present
if (*filename != '/') {
path = "/";
Expand All @@ -33,9 +54,9 @@ FileStream::FileStream(const char* filename, const char* defaultFs) {
_isSD = true;
}

_fd = fopen(path.c_str(), "w");
_fd = fopen(path.c_str(), mode);
if (!_fd) {
throw Error::FsFailedCreateFile;
throw strcmp(mode, "w") ? Error::FsFailedOpenFile : Error::FsFailedCreateFile;
}
}

Expand Down
13 changes: 10 additions & 3 deletions FluidNC/src/FileStream.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#pragma once

#include <Print.h>
#include <Stream.h>

extern "C" {
#include <stdio.h>
}

class FileStream : public Print {
class FileStream : public Stream {
bool _isSD;
FILE* _fd;

public:
FileStream(const char* filename, const char* defaultFs);
FileStream(const char* filename, const char* mode, const char* defaultFs = "localfs");

int available() override;
int read() override;
int peek() override;
void flush() override;
size_t readBytes(char* buffer, size_t length) override; // read chars from stream into buffer

size_t write(uint8_t c) override;
size_t write(const uint8_t* buffer, size_t length) override;

~FileStream();
};
6 changes: 3 additions & 3 deletions FluidNC/src/Motors/TrinamicBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
namespace MotorDrivers {

enum TrinamicMode {
StealthChop = 1, // very quiet
CoolStep = 2, // cooler so higher current possible
StallGuard = 3, // coolstep plus stall indication
StealthChop = 0, // very quiet
CoolStep = 1, // cooler so higher current possible
StallGuard = 2, // coolstep plus stall indication
};

extern EnumItem trinamicModes[];
Expand Down
12 changes: 6 additions & 6 deletions FluidNC/src/Motors/TrinamicSpiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,20 @@ namespace MotorDrivers {
if (tmc2130) {
switch (_mode) {
case TrinamicMode ::StealthChop:
//log_info("StealthChop");
log_debug("StealthChop");
tmc2130->en_pwm_mode(true);
tmc2130->pwm_autoscale(true);
tmc2130->diag1_stall(false);
break;
case TrinamicMode ::CoolStep:
//log_info("Coolstep");
log_debug("Coolstep");
tmc2130->en_pwm_mode(false);
tmc2130->pwm_autoscale(false);
tmc2130->TCOOLTHRS(NORMAL_TCOOLTHRS); // when to turn on coolstep
tmc2130->THIGH(NORMAL_THIGH);
break;
case TrinamicMode ::StallGuard:
//log_info("Stallguard");
log_debug("Stallguard");
{
auto feedrate = config->_axes->_axis[axis_index()]->_homing->_feedRate;

Expand All @@ -222,20 +222,20 @@ namespace MotorDrivers {
} else {
switch (_mode) {
case TrinamicMode ::StealthChop:
//log_info("StealthChop");
log_debug("StealthChop");
tmc5160->en_pwm_mode(true);
tmc5160->pwm_autoscale(true);
tmc5160->diag1_stall(false);
break;
case TrinamicMode ::CoolStep:
//log_info("Coolstep");
log_debug("Coolstep");
tmc5160->en_pwm_mode(false);
tmc5160->pwm_autoscale(false);
tmc5160->TCOOLTHRS(NORMAL_TCOOLTHRS); // when to turn on coolstep
tmc5160->THIGH(NORMAL_THIGH);
break;
case TrinamicMode ::StallGuard:
//log_info("Stallguard");
log_debug("Stallguard");
{
auto feedrate = config->_axes->_axis[axis_index()]->_homing->_feedRate;

Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Motors/TrinamicUartDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace MotorDrivers {

static bool _uart_started;

TMC2208Stepper* tmc2208;
TMC2209Stepper* tmc2209;
TMC2208Stepper* tmc2208 = nullptr;
TMC2209Stepper* tmc2209 = nullptr;

bool test();
void set_mode(bool isHoming);
Expand Down
49 changes: 48 additions & 1 deletion FluidNC/src/ProcessSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Protocol.h" // LINE_BUFFER_SIZE
#include "Uart.h" // Uart0.write()
#include "FileStream.h" // FileStream()
#include "xmodem.h" // xmodemReceive(), xmodemTransmit()

#include <cstring>
#include <map>
Expand Down Expand Up @@ -462,12 +463,56 @@ static Error motor_disable(const char* value, WebUI::AuthenticationLevel auth_le
return Error::Ok;
}

static Error xmodem_receive(const char* value, WebUI::AuthenticationLevel auth_level, Print& out) {
if (!value || !*value) {
value = "uploaded";
}
Print* outfile;
try {
outfile = new FileStream(value, "w");
} catch (...) {
log_info("Cannot open " << value);
return Error::UploadFailed;
}
log_info("Receiving " << value << " via XModem");
int size = xmodemReceive(&Uart0, outfile);
delete outfile;
if (size >= 0) {
log_info("Received " << size << " bytes");
} else {
log_info("Reception failed or was canceled");
}
return size < 0 ? Error::UploadFailed : Error::Ok;
}

static Error xmodem_send(const char* value, WebUI::AuthenticationLevel auth_level, Print& out) {
if (!value || !*value) {
value = "config.yaml";
}
Stream* infile;
try {
infile = new FileStream(value, "r");
} catch (...) {
log_info("Cannot open " << value);
return Error::DownloadFailed;
}
log_info("Sending " << value << " via XModem");
int size = xmodemTransmit(&Uart0, infile);
delete infile;
if (size >= 0) {
log_info("Sent " << size << " bytes");
} else {
log_info("Sending failed or was canceled");
}
return size < 0 ? Error::DownloadFailed : Error::Ok;
}

static Error dump_config(const char* value, WebUI::AuthenticationLevel auth_level, Print& out) {
Print* ss;
try {
if (value) {
// Use a file on the local file system unless there is an explicit prefix like /sd/
ss = new FileStream(value, "localfs");
ss = new FileStream(value, "w");
} else {
ss = &out;
}
Expand Down Expand Up @@ -501,6 +546,8 @@ static Error fakeLaserMode(const char* value, WebUI::AuthenticationLevel auth_le
// to performing some system state change. Each command is responsible
// for decoding its own value string, if it needs one.
void make_user_commands() {
new UserCommand("XR", "Xmodem/Receive", xmodem_receive, notIdleOrAlarm);
new UserCommand("XS", "Xmodem/Send", xmodem_send, notIdleOrJog);
new UserCommand("CD", "Config/Dump", dump_config, anyState);
new UserCommand("", "Help", show_help, anyState);
new UserCommand("T", "State", showState, anyState);
Expand Down
10 changes: 4 additions & 6 deletions FluidNC/src/SettingsDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ void make_settings() {
make_coordinate(CoordIndex::G28, "G28");
make_coordinate(CoordIndex::G30, "G30");

// GRBL Numbered Settings
build_info = new StringSetting(EXTENDED, WG, NULL, "Firmware/Build", "");

status_mask = new IntSetting(GRBL, WG, "10", "Report/Status", 1, 0, 3);
message_level = new EnumSetting("Which Messages", EXTENDED, WG, NULL, "Message/Level", MsgLevelInfo, &messageLevels, NULL);

message_level = new EnumSetting(NULL, EXTENDED, WG, NULL, "Message/Level", MsgLevelInfo, &messageLevels, NULL);
config_filename = new StringSetting("Name of Configuration File", EXTENDED, WG, NULL, "Config/Filename", "config.yaml", 1, 50, NULL);

config_filename = new StringSetting(EXTENDED, WG, NULL, "Config/Filename", "config.yaml");
// GRBL Numbered Settings
status_mask = new IntSetting(NULL, GRBL, WG, "10", "Report/Status", 1, 0, 3, NULL);
}
4 changes: 1 addition & 3 deletions FluidNC/src/WebUI/WebSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,7 @@ namespace WebUI {

// NVS settings
for (Setting* js = Setting::List; js; js = js->next()) {
if (js->getType() == WEBSET) {
js->addWebui(&j);
}
js->addWebui(&j);
}

// Configuration tree
Expand Down
Loading

0 comments on commit 29314f2

Please sign in to comment.