Handling new print3d driver (using new api, printer states)

This commit is contained in:
peteruithoven 2013-10-11 12:39:05 +02:00
parent d417a7454f
commit abc9841bc7
6 changed files with 152 additions and 154 deletions

View File

@ -30,4 +30,14 @@
.agentInfoToggle {
display: block;
}
#debug_display {
position: absolute;
top: 0;
left: 0;
display: none;
background-color: #fff;
opacity: .7;
color: #000;
}

View File

@ -78,6 +78,8 @@
<div id="debug_textArea">
<textarea rows="5" cols="115" id="textdump"></textarea>
</div>
<div id="debug_display">
</div>
<div id="contentOverlay">
<div id="settings" class="popup">
<header>

View File

@ -11,37 +11,45 @@ function setPrintprogress(val) {
//*/
function Printer() {
this.temperature = 0;
this.targetTemperature = 0;
this.printing;
Printer.UNKNOWN_STATE = "unknown";
Printer.DISCONNECTED_STATE = "disconnected";
Printer.IDLE_STATE = "idle"; // printer found, but idle
Printer.BUFFERING_STATE = "buffering"; // printer is buffering (recieving) data, but not yet printing
Printer.PRINTING_STATE = "printing";
Printer.STOPPING_STATE = "stopping"; // when you stop (abort) a print it prints the endcode
this.temperature = 0;
this.targetTemperature = 0;
this.currentLine = 0;
this.totalLines = 0;
this.bufferedLines = 0;
this.state = Printer.UNKNOWN_STATE;
this.hasControl = true; // whether this client has control access
this.wifiboxURL;
this.maxTempLastMod = 7; // max time (seconds) since the last temp info modification before the printer connection is considered lost
this.checkTemperatureInterval = 6000;
this.checkTemperatureDelay;
this.checkProgressInterval = 6000;
this.checkProgressDelay;
this.timeoutTime = 3000;
this.checkStatusInterval = 3000;
this.checkStatusDelay;
this.timeoutTime = 3000;
this.sendPrintPartTimeoutTime = 5000;
this.gcode; // gcode to be printed
this.sendLength = 6000; // max amount of gcode lines per post (limited because WiFi box can't handle to much)
this.sendLength = 1500; // max amount of gcode lines per post (limited because WiFi box can't handle to much)
this.retryDelay = 2000; // retry setTimout delay
this.retrySendPrintPartDelay; // retry setTimout instance
this.retryCheckTemperatureDelay; // retry setTimout instance
this.retryCheckProgressDelay; // retry setTimout instance
this.retryCheckStatusDelay; // retry setTimout instance
this.retryStopDelay; // retry setTimout instance
this.retryPreheatDelay; // retry setTimout instance
this.maxGCodeSize = 10; // max size of gcode in MB's (estimation)
this.sendStopGCodeDelay = 1000;
this.maxGCodeSize = 10; // max size of gcode in MB's (estimation)
// Events
Printer.UPDATE = "update";
var self = this;
this.init = function() {
console.log("Printer:init");
//this.wifiboxURL = "http://" + window.location.host + "/cgi-bin/d3dapi";
@ -51,20 +59,17 @@ function Printer() {
console.log(" wifiboxURL: ",this.wifiboxURL);
if(autoUpdate) {
this.checkTemperature();
this.checkProgress();
this.checkStatus();
}
}
this.preheat = function() {
console.log("Printer:preheat");
var postData = { id: 0 };
var self = this;
if (communicateWithWifibox) {
$.ajax({
url: this.wifiboxURL + "/printer/heatup",
type: "POST",
data: postData,
dataType: 'json',
timeout: this.timeoutTime,
success: function(data){
@ -109,8 +114,6 @@ function Printer() {
this.targetTemperature = settings["printer.temperature"]; // slight hack
this.sendPrintPart(this.sendIndex, this.sendLength);
this.restartIntervals(); // slight hack
}
this.byteSize = function(s){
return~-encodeURI(s).split(/%..|./).length;
@ -127,7 +130,7 @@ function Printer() {
}
var gcodePart = this.gcode.slice(sendIndex, sendIndex+sendLength);
var postData = { id: 0, gcode: gcodePart.join("\n"), first: firstOne, last: lastOne};
var postData = { gcode: gcodePart.join("\n"), first: firstOne, last: lastOne};
var self = this;
if (communicateWithWifibox) {
$.ajax({
@ -135,7 +138,7 @@ function Printer() {
type: "POST",
data: postData,
dataType: 'json',
timeout: this.timeoutTime,
timeout: this.sendPrintPartTimeoutTime,
success: function(data){
console.log("Printer:sendPrintPart response: ",data);
@ -161,13 +164,11 @@ function Printer() {
this.stop = function() {
console.log("Printer:stop");
var postData = { id: 0 };
var self = this;
if (communicateWithWifibox) {
$.ajax({
url: this.wifiboxURL + "/printer/stop",
type: "POST",
data: postData,
dataType: 'json',
timeout: this.timeoutTime,
success: function(data){
@ -183,94 +184,60 @@ function Printer() {
} else {
console.log ("Printer >> f:communicateWithWifibox() >> communicateWithWifibox is false, so not executing this function");
}
this.restartIntervals(); // slight hack
}
this.checkTemperature = function() {
//console.log("Printer:checkTemperature");
var getData = { id: 0 };
this.checkStatus = function() {
//console.log("Printer:checkStatus");
var self = this;
if (communicateWithWifibox) {
$.ajax({
url: this.wifiboxURL + "/printer/temperature",
data: getData,
url: this.wifiboxURL + "/info/status",
dataType: 'json',
timeout: this.timeoutTime,
success: function(data){
//console.log("Printer:temperature response: ",data);
if(data.status == "success") {
//console.log("temp: ",response.data.hotend+"/"+response.data.hotend_target+" ("+response.data.last_mod+")");
self.temperature = data.data.hotend;
if(data.data.hotend_target != undefined) {
if(state == PRINTING_STATE) { // HACK
self.targetTemperature = settings["printer.temperature"];
} else {
self.targetTemperature = data.data.hotend_target;
}
}
self.alive = (data.data.last_mod < self.maxTempLastMod);
} else {
self.alive = false;
}
//console.log(" this.alive: ",self.alive);
$(document).trigger(Printer.UPDATE);
self.checkTemperatureDelay = setTimeout(function() { self.checkTemperature() }, self.checkTemperatureInterval);
success: function(response){
console.log("Printer:status: ",response.data.state," response: ",response);
self.handleStatusUpdate(response);
clearTimeout(self.checkStatusDelay);
clearTimeout(self.retryCheckStatusDelay);
self.checkStatusDelay = setTimeout(function() { self.checkStatus() }, self.checkStatusInterval);
}
}).fail(function() {
console.log("Printer:checkTemperature: failed");
clearTimeout(self.retryCheckTemperatureDelay);
self.retryCheckTemperatureDelay = setTimeout(function() { self.checkTemperature() },self.retryDelay); // retry after delay
console.log("Printer:checkStatus: failed");
self.state = Printer.UNKNOWN_STATE;
clearTimeout(self.checkStatusDelay);
clearTimeout(self.retryCheckStatusDelay);
self.retryCheckStatusDelay = setTimeout(function() { self.checkStatus() },self.retryDelay); // retry after delay
});
} else {
console.log ("Printer >> f:checkTemperature() >> communicateWithWifibox is false, so not executing this function");
console.log ("Printer >> f:checkStatus() >> communicateWithWifibox is false, so not executing this function");
}
}
this.checkProgress = function() {
//console.log("Printer:checkProgress");
var getData = { id: 0 };
var self = this;
if (communicateWithWifibox) {
$.ajax({
url: this.wifiboxURL + "/printer/progress",
data: getData,
dataType: 'json',
timeout: this.timeoutTime,
success: function(data){
if(data.status == "success") {
this.handleStatusUpdate = function(response) {
var data = response.data;
if(response.status != "success") {
self.state = Printer.UNKNOWN_STATE;
} else {
// state
self.state = data.state;
// temperature
self.temperature = data.hotend;
self.targetTemperature = data.hotend_target;
// progress
self.currentLine = data.current_line;
self.totalLines = data.total_lines;
self.bufferedLines = data.buffered_lines
self.printing = data.data.printing;
self.currentLine = data.data.current_line;
self.num_lines = data.data.num_lines;
if(self.printing) {
console.log("progress: ",data.data.current_line+"/"+data.data.num_lines+" ("+data.data.last_mod+")");
}
} else {
self.printing = false;
}
//console.log(" this.alive: ",self.alive);
$(document).trigger(Printer.UPDATE);
self.checkProgressDelay = setTimeout(function() { self.checkProgress() },self.checkProgressInterval);
}
}).fail(function() {
console.log("Printer:checkProgress: failed");
clearTimeout(self.retryCheckProgressDelay);
self.retryCheckProgressDelay = setTimeout(function() { self.checkProgress() },self.retryDelay); // retry after delay
});
} else {
console.log ("Printer >> f:checkProgress() >> communicateWithWifibox is false, so not executing this function");
}
}
this.restartIntervals = function() {
var self = this;
clearTimeout(self.checkProgressDelay);
self.checkProgressDelay = setTimeout(function() { self.checkProgress() },self.checkProgressInterval);
clearTimeout(self.checkTemperatureDelay);
self.checkTemperatureDelay = setTimeout(function() { self.checkTemperature() }, self.checkTemperatureInterval);
// access
self.hasControl = data.has_control;
if(self.state == Printer.PRINTING_STATE || self.state == Printer.STOPPING_STATE) {
console.log("progress: ",self.currentLine+"/"+self.totalLines+" ("+self.bufferedLines+") ("+self.state+")");
}
}
$(document).trigger(Printer.UPDATE);
}
}

View File

@ -1,5 +1,3 @@
var $displayThermometer;
function Thermometer() {
this.currentTemperature = 0; // default val
this.targetTemperature = 180; // default val
@ -13,9 +11,12 @@ function Thermometer() {
this.$canvas;
this.canvas;
this.context;
this.$container;
this.isInitted = false;
this.enabled = true;
this.thermoColors = [
[50, 200, 244], // 'cold'
[244, 190, 10], // 'warming up'
@ -25,7 +26,7 @@ function Thermometer() {
this.init = function(targCanvas, targCanvasContainer) {
console.log("Thermometer.init()");
$displayThermometer = targCanvasContainer;
this.$container = targCanvasContainer;
this.$canvas = targCanvas;
this.canvas = this.$canvas[0];
@ -45,6 +46,7 @@ function Thermometer() {
// console.log("Thermometer.update(" + curr + "," + targ + ")");
if (this.isInitted) {
if(!this.enabled) return;
if (curr == undefined) curr = 0;
if (targ== undefined) targ = 180; // prevent divide by zero
@ -85,7 +87,7 @@ function Thermometer() {
// rect will be clipped by the thermometer outlines
this.context.beginPath();
this.context.rect(20, h - paddingUnder - p, 60, p + paddingUnder);
console.log(" currColor: " + currColor);
//console.log(" currColor: " + currColor);
//todo Math.floor??
this.context.fillStyle = "rgb(" + currColor[0] + "," + currColor[1] + "," + currColor[2] + ")";
this.context.fill();
@ -116,4 +118,12 @@ function Thermometer() {
console.log("Thermometer.setTemperature() -> thermometer not initialized!");
}
}
this.show = function() {
this.$container.show();
this.enabled = true;
}
this.hide = function() {
this.$container.hide();
this.enabled = false;
}
}

View File

@ -12,15 +12,10 @@ var btnOops, btnStop, btnClear;
var btnMoveUp, btnMoveDown, btnTwistLeft, btnTwistRight;
var btnInfo, btnSettings;
var btnDebug; // debug
var displayTemp, displayProgress;
var displayProgress;
var displayTempEnabled = false;
var IDLE_STATE = "idle";
var PRINTING_STATE = "printing";
var state = IDLE_STATE;
var prevState = state;
var state;
var prevState;
function initButtonBehavior() {
console.log("f:initButtonBehavior >> btnNew = " + btnNew);
@ -36,7 +31,6 @@ function initButtonBehavior() {
btnNew = $("#btnNew");
btnPrint= $("#btnPrint");
btnStop = $("#btnStop");
displayTemp = $("#displayTemp");
displayProgress = $("#printProgressContainer");
// btnPrevious = $("#btnPrevious");
@ -192,7 +186,7 @@ function initButtonBehavior() {
function stopPrint() {
console.log("f:stopPrint() >> sendPrintCommands = " + sendPrintCommands);
if (sendPrintCommands) printer.stop();
setState(IDLE_STATE);
setState(Printer.STOPPING_STATE);
}
@ -210,7 +204,7 @@ function print(e) {
$("#textdump").text("");
if (_points.length > 2) {
setState(PRINTING_STATE);
setState(Printer.BUFFERING_STATE);
var gcode = generate_gcode();
//startPrint(gencode);
@ -296,52 +290,61 @@ function previewTwistRight(redrawLess) {
function update() {
if(!displayTempEnabled && printer.alive) {
displayTemp.show();
$displayThermometer.show();
displayTempEnabled = true;
} else if(displayTempEnabled && !printer.alive) {
displayTemp.hide();
$displayThermometer.hide();
displayTempEnabled = false;
}
setState(printer.state);
if(displayTempEnabled) {
displayTemp.text(printer.temperature+"/"+printer.targetTemperature);
thermometer.update(printer.temperature, printer.targetTemperature);
}
//setPrintprogress(printer.currentLine/printer.num_lines);
var btnPrint= $("#btnPrint");
setState(printer.printing? PRINTING_STATE : IDLE_STATE);
thermometer.update(printer.temperature, printer.targetTemperature);
//TODO: update progress
}
function setState(newState) {
function setState(newState) { //TODO add hasControl
if(newState == state) return;
console.log("setState: ",state," > ",newState);
setDebugText("State: "+newState);
// print button
switch(newState) {
case IDLE_STATE:
case Printer.IDLE_STATE:
btnPrint.removeClass("disabled"); // enable print button
btnStop.addClass("disabled"); // disable stop button
btnPrint.bind('touchstart mousedown',print);
displayProgress.hide();
break;
case PRINTING_STATE:
default:
btnPrint.addClass("disabled"); // disable print button
btnStop.removeClass("disabled"); // enable stop button
btnPrint.unbind('touchstart mousedown');
displayProgress.show();
break;
}
// stop button
switch(newState) {
case Printer.PRINTING_STATE:
case Printer.BUFFERING_STATE:
btnStop.removeClass("disabled");
break;
default:
btnStop.addClass("disabled");
break;
}
// thermometer
switch(newState) {
case Printer.UNKNOWN_STATE:
case Printer.DISCONNECTED_STATE:
thermometer.hide();
break;
default:
thermometer.show();
break;
}
// progress indicator
switch(newState) {
case Printer.PRINTING_STATE:
displayProgress.show(); // TODO: Show progress
break;
default:
displayProgress.hide(); // TODO: hide progress
break;
}
prevState = state;
state = newState;
}

View File

@ -8,7 +8,7 @@ var printer = new Printer();
var thermometer = new Thermometer();
var settingsWindow = new SettingsWindow();
var firstTimesettingsLoaded = true;
var firstTimeSettingsLoaded = true;
$(function() {
@ -54,7 +54,9 @@ $(function() {
$("body").css("overflow", "auto");
$("#debug_textArea").css("display", "block");
$("#preview_tmp").css("display", "block");
$("#debug_display").css("display", "block");
/* TEMP CODE!! -> artificially populates the startgcode and endgcode textareas in the settings window */
// todo remove this temporary code...
/*
@ -85,9 +87,13 @@ function settingsLoaded() {
console.log("settingsLoaded");
console.log("autoHeatup: ",settings["printer.heatup.enabled"]);
if(settings["printer.heatup.enabled"]) {
if(firstTimesettingsLoaded) {
if(firstTimeSettingsLoaded) {
printer.preheat();
firstTimesettingsLoaded = false;
firstTimeSettingsLoaded = false;
}
}
}
function setDebugText(text) {
$("#debug_display").text(text);
}