Skip to content

Re-worked some support for printing the Imp logo #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions thermalprinter/index.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
/*
Copyright (C) 2013 electric imp, inc.
<!--
-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.
-->

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.
*/
<!DOCTYPE html>
<html lang="en">
<html>
Expand Down Expand Up @@ -101,7 +103,7 @@ <h2>Electric Imp Printer</h2>

// 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
Expand Down Expand Up @@ -152,6 +154,7 @@ <h2>Electric Imp Printer</h2>
// 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");
}
Expand Down Expand Up @@ -204,4 +207,4 @@ <h2>Electric Imp Printer</h2>
}
</script>
</body>
</html>
</html>
23 changes: 21 additions & 2 deletions thermalprinter/thermalprinter.agent.nut
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,31 @@ 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");
}
});

device.on("logo", function(value) {
printLogo();
});
});
64 changes: 36 additions & 28 deletions thermalprinter/thermalprinter.device.nut
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -364,33 +377,29 @@ 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) {
server.log("Device: printing new buffer from agent: "+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();
Expand All @@ -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) {
Expand Down