paul@41 | 1 | /* |
paul@41 | 2 | * Common display-related functions. |
paul@41 | 3 | * |
paul@41 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@41 | 5 | * |
paul@41 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@41 | 7 | * it under the terms of the GNU General Public License as published by |
paul@41 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@41 | 9 | * (at your option) any later version. |
paul@41 | 10 | * |
paul@41 | 11 | * This program is distributed in the hope that it will be useful, |
paul@41 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@41 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@41 | 14 | * GNU General Public License for more details. |
paul@41 | 15 | * |
paul@41 | 16 | * You should have received a copy of the GNU General Public License |
paul@41 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@41 | 18 | */ |
paul@41 | 19 | |
paul@41 | 20 | #include "display.h" |
paul@41 | 21 | |
paul@41 | 22 | |
paul@41 | 23 | |
paul@46 | 24 | /* Return the line data position for the given pixel. */ |
paul@46 | 25 | |
paul@49 | 26 | int get_position(display_config_t *cfg, int x) |
paul@46 | 27 | { |
paul@49 | 28 | int cell = x / cfg->cell_size, offset = x % cfg->cell_size; |
paul@49 | 29 | int pos = (cell / 2) * cfg->cell_size + offset; |
paul@46 | 30 | |
paul@49 | 31 | return cell % 2 ? pos + cfg->line_length / 2 : pos; |
paul@46 | 32 | } |
paul@46 | 33 | |
paul@41 | 34 | /* Provide a pattern to test the line data. */ |
paul@41 | 35 | |
paul@49 | 36 | void test_linedata(display_config_t *cfg) |
paul@41 | 37 | { |
paul@41 | 38 | int x, y; |
paul@49 | 39 | uint8_t *linedata = cfg->framebuffer; |
paul@41 | 40 | |
paul@49 | 41 | for (y = 0; y < cfg->line_count; y++) |
paul@41 | 42 | { |
paul@49 | 43 | for (x = 0; x < cfg->line_length; x++) |
paul@41 | 44 | { |
paul@41 | 45 | /* Pixel: I0RRGGBB = Y0YYYYXX */ |
paul@41 | 46 | |
paul@49 | 47 | linedata[get_position(cfg, x)] = (x % 2) ? |
paul@49 | 48 | (((y / (cfg->line_count / 32)) & 0b1) << 7) | |
paul@49 | 49 | (((y / (cfg->line_count / 16)) & 0b1111) << 2) | |
paul@49 | 50 | ((x / (cfg->line_length / 4)) & 0b11) : |
paul@46 | 51 | 0x00; |
paul@41 | 52 | } |
paul@41 | 53 | |
paul@49 | 54 | linedata += cfg->line_length; |
paul@41 | 55 | } |
paul@41 | 56 | } |
paul@69 | 57 | |
paul@69 | 58 | /* Copy to framebuffer. */ |
paul@69 | 59 | |
paul@69 | 60 | void copy_to_framebuffer(display_config_t *cfg, uint8_t *image, int width, int height) |
paul@69 | 61 | { |
paul@69 | 62 | int sx, sy, tx, ty; |
paul@69 | 63 | uint8_t *targetline = cfg->framebuffer, *sourceline = image; |
paul@69 | 64 | |
paul@69 | 65 | for (sy = 0, ty = 0; (sy < height) && (ty < cfg->line_count); sy++, ty++) |
paul@69 | 66 | { |
paul@69 | 67 | for (sx = 0, tx = 0; (tx < cfg->line_length); sx++, tx++) |
paul@69 | 68 | { |
paul@69 | 69 | targetline[get_position(cfg, tx)] = (sx < width) ? sourceline[sx] : 0; |
paul@69 | 70 | } |
paul@69 | 71 | |
paul@69 | 72 | sourceline += width; |
paul@69 | 73 | targetline += cfg->line_length; |
paul@69 | 74 | } |
paul@69 | 75 | } |