2013-02-22 77 views
0

我想解析Arduino上的XBM位图,根本没有运气(期望< 16x16位图)。经过大量的搜索,研究和修补,我已经达到了这个功能。我确定我正确地阅读了字节(测试),但转换有问题。在Arduino上绘制XBM位图

void bitmap(int x, int y, uint16_t *bitmap, uint8_t w, uint8_t h) { 
    uint16_t dots, msb; 
    uint16_t col, row; 

    msb = 1 << (w - 1); 
    for (row = 0; row < h; row++) { 
    dots = pgm_read_word(bitmap + row); 
    //Serial.print(dots, HEX); 
    //Serial.println(" "); 
    for (col = 0; col < w; col++) { 
     if (dots & (msb >> col)) 
     Serial.print("#"); //toolbox.setPixel(x, y, 1, false); 
     else 
     Serial.print("'"); //toolbox.setPixel(x, y, 0, false); 
    } 
    Serial.println(""); 
    } 
} 

这是我试图展示的位图。它的大小为32x32像素。 16x16或更少的图像显示正确。

// 32x32 
uint16_t medium[] PROGMEM = { 
    0xffff, 0xffff, 0x0000, 0x8000, 0xffff, 0xffff, 0x0001, 0x0000, 0xffff, 
    0xffff, 0x0000, 0x8000, 0xffff, 0xffff, 0x0001, 0x0000, 0xffff, 0xffff, 
    0x0000, 0x8000, 0xffff, 0xffff, 0x0001, 0x0000, 0xffff, 0xffff, 0x0000, 
    0x8000, 0xffff, 0xffff, 0x0001, 0x0000, 0xffff, 0xffff, 0x0000, 0x8000, 
    0xffff, 0xffff, 0x0001, 0x0000, 0xffff, 0xffff, 0x0000, 0x8000, 0xffff, 
    0xffff, 0x0001, 0x0000, 0xffff, 0xffff, 0x0000, 0x8000, 0xffff, 0xffff, 
    0x0001, 0x0000, 0xffff, 0xffff, 0x0000, 0x8000, 0xffff, 0xffff, 0x0001, 
    0x0000 }; 

这是位图的PNG供参考:响应于评价:

回答

0
msb = 1 << (w - 1); 
[snip] 
if (dots & (msb >> col)) 

该逻辑仅当w小于16

EDIT工作。

其实,这是需要修复的部分。的确,您将使用uint16_t,但您需要设法将16位整数转换为位流。现在,它只适用于行数正好是16位宽的情况。

我会做点什么来获得流中的下一个位。我会使用一个变量来跟踪当前单词的位置,然后在需要时使用另一个变量来获取下一个单词。

void bitmap(int x, int y, uint16_t *bitmap, uint8_t w, uint8_t h) { 
    uint16_t dots = 0, current_mask = 0; 
    uint16_t col, row, next_word = 0; 

    for (row = 0; row < h; row++) { 
    for (col = 0; col < w; col++) { 
     current_mask >>= 1; 
     if (0 == current_mask) { 
      current_mask = 1 << 15; 
      dots = pgm_read_word(bitmap + next_word); 
      ++next_word; 
     } 

     if (dots & current_mask) 
     Serial.print("#"); //toolbox.setPixel(x, y, 1, false); 
     else 
     Serial.print("'"); //toolbox.setPixel(x, y, 0, false); 

    } 
    Serial.println(""); 
    } 
} 

我没有尝试过这一点,有一些细节与索引内存和位移位取的。祝你好运。