firefly / wallet

The (original) Firefly hardware wallet base on the ATmega processors.
https://firefly.city
MIT License
173 stars 40 forks source link

Support for drawing Bitmaps on the oled. #11

Closed eulphean closed 6 years ago

eulphean commented 6 years ago

Hi Richard, I'm trying to draw a Bitmap on the OLED with this wallet. From the code, it looks like you have developed a custom display driver to account for performance of the oled. My bitmap is made up of a collection of hexadecimal codes like the following.

static unsigned char PROGMEM const bitmap[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, ..... }

Should I be using display_chunks or display_bigChars to draw the Bitmap? Any pointers from you would be useful.

eulphean commented 6 years ago

Hey Richard, any insights on this?

ricmoo commented 6 years ago

Heya! Sorry for the delay in responding...

We are working on an animatable bitmap format for the upcoming release and scripts to generate these bitmaps. It's a tight fit, but we think we can squeeze it into the final kilobyte of program space.

This will allow for better co-branding, for people who want their logo, with maybe a small spin or glimmer.

If you want to hack something in the meantime, you will want to use display_chunk, which writes a single column of a single page to the screen. Since there is not enough memory to store a back-buffer, we compute each page (16 columns, with 17 bytes, the first byte is 0x40 to indicate "draw the next page") on-the-fly.

It's a bit complicated, but I will try to give a quick overview of the drawing order. The screen is broken into pages, each page is 16 bytes, and each call to display_chunk will add the byte to the 17 byte buffer (includes a command byte of 0x40), and after 16 calls to display_chunk, will flush the page to the display.

There are 8 pages across, and 8 pages down for a total of 64 pages. Pages are drawn, left-to-right, top-to-bottom. Each page represents a 16x8 pixel portion of the screen, where each byte is one of the 16 columns. So, for example, to draw a vertical line from (3, 0) to (3, 64) you would need something like:

for pageRow in (0, 8):
    for pageColumn in (0, 8):
        for column in (0, 16):
            if pageColumn == 1 && column == 3:
                display_chunk(0xff);
            else:
                display_chunk(0x00); 

It's quite complicated, which is why we are working on making the API easier to deal with, and also compact, so that a fully animated bitmap can be sent over the BLECast payload, so it has to fit in about 700 bytes.

We have code right now for displaying images, but it is not ready for humans yet, but basically, we use a loop similar to the above and compute the virtual pixel for real pixel for each page and flush it at once. So, we draw 64 viewports, rendering the relevant portion of the image in each page.

Make sense? No worries if not... ;)

eulphean commented 6 years ago

thanks

Thanks for explaining that, Richard. That's exactly what I was looking for :) Just a small correction. I think you probably meant (pageColumn == 0 && column == 3). Here's how I got it working. Not the cleanest solution but works smoothly.

static void display_bitmap(uint8_t address, const uint8_t *bitmap) {
    DisplayContext context;
    display_begin(&context, address, 0);

    for (int8_t pageRow = 0; pageRow < 8; pageRow++) {
        for (int8_t pageCol = 0; pageCol < 8; pageCol++) {
            display_bitmapSection(&context, bitmap, pageRow, pageCol);
        }
    }
}

void display_bitmapSection(DisplayContext *context, const uint8_t *bitmap, int8_t pageRow, int8_t pageColumn) {
    // Image matrix width and height.
    int8_t w = 16; int8_t h = 64;

    // 8-bit chunk that will be flushed to the display.
    uint8_t chunk = 0x0;

    // Draw the 16x8 pixel page at pageRow and pageColumn
    // Calculate the starting position based on pageColumn and pageRow variables. 
    int8_t startColumn = pageColumn * 2;
    int8_t startRow = pageRow * 8; 
    for (int8_t col = startColumn; col < startColumn + 2; col++) {
         for (int8_t bit = 0; bit <= 7; bit++) { // 8 columns in each column (2 HEX characters)
            for (int8_t row = startRow + 7; row >= startRow; row--) { // Start creating a chunk from the last bit in this column. That's how the OLED likes it. 
                int32_t idx = w * row + col;
                chunk = (chunk << 1) | (pgm_read_byte(&bitmap[idx]) >> (7 - bit)) & 0x01; // Bitwise operation to get the bit I need in the current column.
            }
            // Flush the 8 bit chunk.
            display_chunks(context, chunk, 1);
            // Reset chunk
            chunk = 0x0;
       }
    }
}
ricmoo commented 6 years ago

Oh yes! I did. :)

Glad it worked for you, that’s awesome. :)