Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The algorithm for this took three main stages:<br> 1) Translate the Y from top left to bottom left.<br> 2) Break up the X into word:bit values.<br> 3) Use Bresenham's algorithm to draw lines between the points. And then figure out how to <a href="https://stackoverflow.com/questions/1222713/how-do-i-create-a-line-of-arbitrary-thickness-using-bresenham/18950950#18950950">make the line thicker</a>.</p> <p>For <a href="https://forum.sparkfun.com/viewtopic.php?f=14&amp;t=36254&amp;sid=091846753e1e51bb2dc126d3597d0bfc&amp;start=15#p164392" rel="nofollow noreferrer">my exact case</a>, the target bitmap is 384x384, so requires 19k of SRAM to store in memory. I had to ditch the "lame" Arduino Mega and upgrade to the <a href="http://www.adafruit.com/products/949" rel="nofollow noreferrer">ChipKIT uC32</a> to pull this off, 32k of RAM, 80 MHz cpu, &amp; twice the I/O!</p> <p>The way I figured out this was to base my logic on <a href="https://github.com/adafruit/Adafruit-Thermal-Printer-Library" rel="nofollow noreferrer">Adafruit's Thermal library</a> for Arduino. In their examples, they include how to convert a 1-bit bitmap into a static array for printing. I used their GFX library to implement the setXY function as well as their GFX Bresenham's algorithm to draw lines between (X,Y)s using my <code>setXY()</code>.</p> <p>It all boiled down to the code in this function I wrote:</p> <pre class="lang-c prettyprint-override"><code>// *bitmap is global or class member pointer to byte array of size 384/8*384 // bytesPerRow is 384/8 void setXY(int x, int y) { // integer divide by 8 (/8) because array size is byte or char int xByte = x/8; // modulus 8 (%8) to get the bit to set uint8_t shifty = x%8; // right shift because we start from the LEFT int xVal = 0x80 &gt;&gt; shifty; // inverts Y from bottom to start of array int yRow = yMax - y; // Get the actual byte in the array to manipulate int offset = yRow*bytesPerRow + xByte; // Use logical OR in case there is other data in the bitmap, // such as a frame or a grid *(bitmap+offset)|=xVal; } </code></pre> <p>The big point is to remember with an array, we are starting at the top left of the bitmap, going right across the row, then down one Y row and repeating. The <code>gotchya's</code> are in translating the X into the word:bit combo. You've got to shift from the left (sort-of like translating the Y backwards). Another gotchya is one-off error in bookkeeping for the Y. </p> <p>I put all of this in a class, which helped prevent me from making one big function to do it all and through better design made the implementation easier than I thought it would be.</p> <p>Pic of the printout:<br> <img src="https://i.stack.imgur.com/U7hVC.jpg" alt="Output from TPS test bench I made"><br> <a href="http://forums.adafruit.com/viewtopic.php?f=22&amp;t=44710" rel="nofollow noreferrer">Write-up of the project is here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload