该算法经历了三个主要阶段:
1)将Y从左上角平移到左下角。
2) 将 X 分解为 word:bit 值。
3) 使用 Bresenham 算法在点之间画线。然后想办法让线条变粗。
对于我的确切情况,目标位图是 384x384,因此需要 19k 的 SRAM 存储在内存中。我不得不放弃“蹩脚”的 Arduino Mega 并升级到ChipKIT uC32以实现这一目标,32k 的 RAM、80 MHz 的 cpu 和两倍的 I/O!
我想出这一点的方法是将我的逻辑基于Adafruit 的Arduino 热库。在他们的示例中,它们包括如何将 1 位位图转换为静态数组以进行打印。我使用他们的 GFX 库来实现 setXY 函数以及他们的 GFX Bresenham 算法,使用我的setXY()
.
这一切都归结为我编写的这个函数中的代码:
// *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 >> 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;
}
重要的是要记住一个数组,我们从位图的左上角开始,向右穿过行,然后向下一个 Y 行并重复。他们gotchya's
将 X 翻译成 word:bit 组合。您必须从左侧移动(有点像将 Y 向后平移)。另一个问题是 Y 的簿记中的一次性错误。
我将所有这些都放在一个类中,这有助于防止我创建一个大函数来完成这一切,并且通过更好的设计使实现比我想象的更容易。
打印输出的图片:
![我制作的 TPS 测试台的输出](https://i.stack.imgur.com/U7hVC.jpg)
项目的记录在这里。