diff --git a/thermalprinter/index.html b/thermalprinter/index.html index 47e0027..4632128 100644 --- a/thermalprinter/index.html +++ b/thermalprinter/index.html @@ -1,21 +1,23 @@ -/* -Copyright (C) 2013 electric imp, inc. + -Permission is hereby granted, free of charge, to any person obtaining a copy of this software -and associated documentation files (the "Software"), to deal in the Software without restriction, -including without limitation the rights to use, copy, modify, merge, publish, distribute, -sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial -portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE -AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ @@ -101,7 +103,7 @@

Electric Imp Printer

// this is where you put your agent URL. You can find your URL under // "device settings" in the electric imp IDE - impURL = "http://agent.electricimp.com/YOUR_UNIQUE_URL"; + impURL = "https://agent.electricimp.com/xETq8-VuECEd"; // this takes the text from the form, prepares it, makes JSON of the // above printer settings table, and POSTs the whole thing to @@ -152,6 +154,7 @@

Electric Imp Printer

// print the imp logo by sending a generic request to /logo request.open("GET", impURL+"/logo", false); // make JSON from the settings table + var json = JSON.stringify(settings); request.send(json); console.log("Sent logo request to imp"); } @@ -204,4 +207,4 @@

Electric Imp Printer

} - + \ No newline at end of file diff --git a/thermalprinter/thermalprinter.agent.nut b/thermalprinter/thermalprinter.agent.nut index 8af2920..f1a1159 100644 --- a/thermalprinter/thermalprinter.agent.nut +++ b/thermalprinter/thermalprinter.agent.nut @@ -160,7 +160,26 @@ http.onrequest(function(request,res){ // send response res.send(200, "printed"); - } else { + } + else if (request.path == "/logo") { + server.log("Agent printing logo"); + printLogo(); + // send response + res.send(200, "printed"); + + } + else if (request.path == "/plain") { + server.log("Agent received plain text"); + local message = request.query["p"]; + server.log("Text of message: "+message);//.text); + // now feed the message down and print it + // the PHP script that feeds us text takes care of doing the word wrapping + device.send("printnolf", message); + // send response + res.send(200, "printed"); + + } + else { server.log("Agent got unknown request: "+request.body); res.send(400, "request error"); } @@ -168,4 +187,4 @@ http.onrequest(function(request,res){ device.on("logo", function(value) { printLogo(); -}); +}); \ No newline at end of file diff --git a/thermalprinter/thermalprinter.device.nut b/thermalprinter/thermalprinter.device.nut index 4987550..cf83860 100644 --- a/thermalprinter/thermalprinter.device.nut +++ b/thermalprinter/thermalprinter.device.nut @@ -152,7 +152,15 @@ class printer { // print the buffer uart.write(FF); } - + + // Load a buffer and print it immediately + function printnolf(printStr) { + // load the string into the buffer + uart.write(printStr); + // print the buffer + uart.write(FF); + } + // load buffer into the printer's buffer without printing function load(buffer) { uart.write(buffer); @@ -161,6 +169,7 @@ class printer { // this function pulls data from the agent down to the imp, which can then push it to the printer // part of the printer class because it eventually calls the "print downloaded image command" itself function pull() { + server.log("pull called " + this.loadedDataLength + "/" + this.imageDataLength); if(this.loadedDataLength < this.imageDataLength) { agent.send("pull", CHUNK_SIZE); } else { @@ -170,38 +179,42 @@ class printer { // tell the agent we're done and it should reset download pointers too agent.send("imageDone", 0); imp.sleep(0.5); + this.feed(1); this.reset(); server.log("Device: done loading image"); } } // this function writes a row of bitmap image data to the printer - function printImg(width, height, data) { + function printImgBuffer(data) { + server.log("printImgBuffer"); // round width up to next byte boundary - local rowBytes = (width + 7) / 8; - + local rowBytes = (this.imageWidth + 7) / 8; + server.log("rowbytes "+rowBytes); // enforce max width (384 pixels / 8 = 48 bytes) local rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // print up to 255 rows at a time - for (local rowStart = 0; rowStart < height; rowStart += 255) { - local chunkHeight = height - rowStart; - if (chunkHeight > 255) chunkHeight = 255; + //for (local rowStart = 0; rowStart < this.imageHeight; rowStart += 255) + { + local chunkHeight = CHUNK_SIZE / rowBytes;//this.imageHeight - rowStart; + //if (chunkHeight > 255) chunkHeight = 255; // put printer in print-bitmap mode with some nasty magic numbers uart.write(18); uart.write(42); uart.write(chunkHeight); uart.write(rowBytesClipped); - for (local row = 0; row < chunkHeight; row++) { + for (local row = 0; row < chunkHeight; row++) + { uart.write(data.readblob(rowBytes)); uart.flush(); - server.log("Printing row "+row+" of chunk "+(rowStart / 255)); + server.log("Printing row "+row); } } - server.log("Done Printing Image"); - this.feed(2); + server.log("Done Printing block"); + //this.feed(2); } // print the buffer and feed n lines @@ -364,19 +377,6 @@ class printer { // instatiate the printer object at global scope myPrinter <- printer(hardware.uart57, 19200); -/* -function ei() { - myPrinter.setJustify("center"); - myPrinter.setBold(true); - myPrinter.print("electric imp"); - myPrinter.feed(1); - myPrinter.reset(); -} - -imp.wakeup(0.5, ei); -// once this callback is done, we've printed the logo and "electric imp" and reset the printer -*/ - // Register some hooks for the agent to call, allowing the agent to push actions to the device // the most obvious: print a buffer of data agent.on("print", function(buffer) { @@ -384,13 +384,22 @@ agent.on("print", function(buffer) { myPrinter.print(buffer); }); +agent.on("printnolf", function(buffer) { + server.log("Device: printing new buffer from agent (no lf): "+buffer); + myPrinter.printnolf(buffer); +}); + // provides info on a bitmap to download and print -agent.on("image", function(image) { - printImgBuffer(image.data); +agent.on("downloadImage", function(imageParams) +{ + server.log("downloadImage called "+imageParams[1]+","+imageParams[2]); + myPrinter.imageWidth = imageParams[1]; + myPrinter.imageHeight = imageParams[2]; + myPrinter.imageDataLength = imageParams[0]; + myPrinter.pull(); }); // load chunks of an image as pulled from agent -/* agent.on("imgData", function(buffer) { myPrinter.printImgBuffer(buffer); myPrinter.loadedDataLength += buffer.len(); @@ -399,7 +408,6 @@ agent.on("imgData", function(buffer) { imp.sleep(0.01); myPrinter.pull(); }); -*/ // allow the agent to load a buffer without printing agent.on("load", function(buffer) {