Skip to content

Commit

Permalink
⚡ 1.add prusaslicer print after uploaded 2.fix upload 0Bytes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fiber-punk committed Oct 19, 2022
1 parent ab034ba commit 4180775
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 13 deletions.
Binary file removed WifiNode/FiberPunk_SSD1306.cpp.esp32.bin
Binary file not shown.
44 changes: 44 additions & 0 deletions WifiNode/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ String current_temp = "";
String current_bed_temp = "";
String pc_ipaddress = "";
String current_file="";
String current_upload_file = "";

String user1_cmd_f_name = "user1";
String user2_cmd_f_name = "user2";
Expand All @@ -41,6 +42,7 @@ bool hasSD = false;
bool rst_usb = false;
bool paused_for_user = false;
bool paused_for_filament = false;
bool b_print_after_upload = false;

//printer sd type: 0==spi 1==sdio
#if MB(MARLIN_VER)
Expand All @@ -62,6 +64,9 @@ uint8_t cmd_length=0;
void sendCaptureImage(String);
void sendHttpMsg(String);
void writeLog(String);
String genRandomHeader(int length);
String renameRandom(String filename);
String convertToShortName(String filename);

void writeLog(String log_txt)
{
Expand Down Expand Up @@ -93,6 +98,45 @@ void sendHttpMsg(String url)
if(socket_client.connected())
socket_client.write(url.c_str());
}

String genRandomHeader(int length)
{
String result = "";
String charters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
int chater_len = charters.length();
int random_in = 0;
for(int i=0; i<length; i++)
{
random_in = random(0,61);
result.concat(charters.charAt(random_in));
}
return result;
}

String renameRandom(String filename)
{
filename = filename.substring(1,filename.length());
String header = "/"+genRandomHeader(3)+"-";
header = header + filename;
return header;
}

String convertToShortName(String filename)
{
filename = filename.substring(1,filename.length()-5);
String base_name = "";
if(filename.length()>=6)
{
base_name = filename.substring(0,6) + "~1";
}
else
{
base_name = filename + "~1";
}
String full_name = "/"+base_name+".gco";
return full_name;
}

NodeConfig::NodeConfig()
{

Expand Down
15 changes: 11 additions & 4 deletions WifiNode/nodeconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

#define MARLIN_VER 0x01
#define PRUSA_VER 0x02
#define FMTYPE MARLIN_VER
// #define FMTYPE PRUSA_VER
// #define FMTYPE MARLIN_VER
#define FMTYPE PRUSA_VER
#define MB(V) (V==FMTYPE)

#if MB(MARLIN_VER)
#define VERSION "2022-9-29-2007"
#define VERSION "2022-10-19-2008"
#elif MB(PRUSA_VER)
#define VERSION "2022-9-29-2007-PRUSA-MK3S"
#define VERSION "2022-10-19-2008-PRUSA-MK3S"
#endif

#define DBG_OUTPUT_PORT Serial
Expand Down Expand Up @@ -80,9 +80,11 @@ extern String current_temp;
extern String current_bed_temp;
extern String pc_ipaddress;
extern String current_file;
extern String current_upload_file;
extern OP_STATUS g_status;
extern ERROR_CODE g_error;


extern String user1_cmd_f_name;
extern String user2_cmd_f_name;
extern String user3_cmd_f_name;
Expand All @@ -102,13 +104,15 @@ extern bool recvl_ok;
extern bool rst_usb;
extern bool paused_for_user;
extern bool paused_for_filament;
extern bool b_print_after_upload;

extern uint8_t printer_sd_type;
extern uint8_t last_power_status;
extern uint8_t cmd_length;
extern uint8_t reset_sd_usb;
extern uint8_t print_start_flag;


extern unsigned char current_usb_status;
extern unsigned char pre_usb_status;

Expand All @@ -117,6 +121,9 @@ extern void sendHttpMsg(String);
extern void writeLog(String);
extern void saveCurrentPrintStatus(String status_str);
extern String getValue(String data, char separator, int index);
extern String genRandomHeader(int length);
extern String renameRandom(String filename);
extern String convertToShortName(String filename);
extern void writeString(int a,int b,String str);
extern uint8_t lastPowerOffPrinting();
class NodeConfig
Expand Down
91 changes: 84 additions & 7 deletions WifiNode/serverprocess.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "serverprocess.h"
#include "printerprocess.h"
#include "nodeconfig.h"
#include "ArduinoJson.h"

const char* host = "Fiberpunk";
void hardwareReleaseSD();
Expand Down Expand Up @@ -81,8 +82,18 @@ void handleFileUpload(AsyncWebServerRequest *request, String filename, size_t in
if (request->url() != "/edit") {
return;
}

if(g_status==PRINTING){
request->send(500, "text/plain", "UPLOAD OPEN SF FAILED");
return;
}

//start
if (!index) {
espGetSDCard();
if(uploadFile){
uploadFile.close();
}
if(printer_sd_type==0)
{
if (SD.exists((char *)filename.c_str()))
Expand All @@ -100,14 +111,25 @@ void handleFileUpload(AsyncWebServerRequest *request, String filename, size_t in
uploadFile = SD_MMC.open(filename.c_str(), FILE_WRITE);
}



DBG_OUTPUT_PORT.print("Upload: START, filename: "); DBG_OUTPUT_PORT.println(filename);
}

//write
if (len) {
if (uploadFile) {
uploadFile.write(t_data, len);
int written_len = uploadFile.write(t_data, len);
if(written_len != len){
DBG_OUTPUT_PORT.print("Faile written: WRITE, Bytes: ");
DBG_OUTPUT_PORT.println(written_len);
}
}
else
{
request->send(500, "text/plain", "UPLOAD OPEN SF FAILED");
}

DBG_OUTPUT_PORT.print("Upload: WRITE, Bytes: "); DBG_OUTPUT_PORT.println(len);
}

Expand Down Expand Up @@ -955,16 +977,55 @@ void espRestart(AsyncWebServerRequest *request)


//for compatible octoprint API
void octoPrinter(AsyncWebServerRequest *request)
{
String result = "{";
result += "\"sd\":{\"ready\": true},";
result += "\"state\":{\"flags\":{";

result +="\"operational\": false,\"sdReady\": true,\"ready\": true,";
// result += "\"paused\":false,";
// result += "\"printing\":false,";
// result += "\"cancelling\":false,";
// result += "\"pausing\":false,";
// result += "\"error\":false,";
// result += "\"closedOrError\":false";
result += "}}}";

request->send(200, "text/json", result);
}
void octoVersion(AsyncWebServerRequest *request)
{
// {"api":"0.1","server":"1.7.3","text":"OctoPrint 1.7.3"}
String version_octo_api = "{\"api\":\"0.1\",\"server\":\"2.0.6\",\"text\":\"OctoPrint Node 2.0.6\"}";
request->send(200, "text/plain", version_octo_api);
}
void octoFileUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *t_data, size_t len, bool final){

void octoFileUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *t_data, size_t len, bool final)
{
filename = "/"+filename;
if(g_status==PRINTING)
{
request->send(500, "text/plain", "PRINTER BUSY");
return;
}
//start
if (!index) {
if (!index)
{
espGetSDCard();
filename = renameRandom(filename);
if(request->hasArg("print"))
{
DBG_OUTPUT_PORT.println("get print value");
AsyncWebParameter* b_print_result = request->getParam("print", true, false);
DBG_OUTPUT_PORT.println(b_print_result->value());
if(b_print_result->value()=="true")
{
b_print_after_upload = true;
current_upload_file = filename;
}
}

if(printer_sd_type==0)
{
if (SD.exists((char *)filename.c_str()))
Expand All @@ -982,7 +1043,10 @@ void octoFileUpload(AsyncWebServerRequest *request, String filename, size_t inde
uploadFile = SD_MMC.open(filename.c_str(), FILE_WRITE);
}

DBG_OUTPUT_PORT.print("Upload: START, filename: "); DBG_OUTPUT_PORT.println(filename);
// size_t headers = request->headers();
// DBG_OUTPUT_PORT.println(headers);

// DBG_OUTPUT_PORT.print("Upload: START, filename: "); DBG_OUTPUT_PORT.println(filename);
}

//write
Expand All @@ -992,7 +1056,7 @@ void octoFileUpload(AsyncWebServerRequest *request, String filename, size_t inde
{
uploadFile.write(t_data, len);
}
DBG_OUTPUT_PORT.print("Upload: WRITE, Bytes: "); DBG_OUTPUT_PORT.println(len);

}

//finish
Expand All @@ -1002,8 +1066,20 @@ void octoFileUpload(AsyncWebServerRequest *request, String filename, size_t inde
{
uploadFile.close();
}
// read_header_flag = 0;
String logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
DBG_OUTPUT_PORT.print(logmessage);
// DBG_OUTPUT_PORT.print(logmessage);
if(b_print_after_upload)
{
String path = current_upload_file;
path.toLowerCase();
current_file = convertToShortName(path);
if((g_status==P_IDEL)&&current_usb_status)
{
print_start_flag = 1;
}
b_print_after_upload = false;
}
}
}

Expand Down Expand Up @@ -1069,6 +1145,7 @@ void ServerProcess::serverInit()
server.begin();

//for compatible octoprint API
octo_server.on("/api/printer", HTTP_GET, octoPrinter);
octo_server.on("/api/version", HTTP_GET, octoVersion); //{"api":"0.1","server":"1.7.3","text":"OctoPrint 1.7.3"}
octo_server.on("/api/files", HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200);
Expand Down Expand Up @@ -1124,7 +1201,7 @@ void espReleaseSD()
delay(50);

//2.send gcode to marlin, init and reload the sd card
sendCmdByPackage("M21\n");
sendCmdByPackageNow("M21\n");
delay(50);

}
Expand Down
4 changes: 2 additions & 2 deletions WifiNode/wifinode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ void WifiNode::process()
if(reset_sd_usb)
{
resetUsbHostInstance();
espReleaseSD();
delay(50);
// espReleaseSD();
// delay(50);
espGetSDCard();
reset_sd_usb = 0;
}
Expand Down

0 comments on commit 4180775

Please sign in to comment.