| 1 | /* |
| 2 | |
| 3 | u8x8_capture.c |
| 4 | |
| 5 | Screen capture funcion |
| 6 | |
| 7 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 8 | |
| 9 | Copyright (c) 2016, olikraus@gmail.com |
| 10 | All rights reserved. |
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without modification, |
| 13 | are permitted provided that the following conditions are met: |
| 14 | |
| 15 | * Redistributions of source code must retain the above copyright notice, this list |
| 16 | of conditions and the following disclaimer. |
| 17 | |
| 18 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 19 | list of conditions and the following disclaimer in the documentation and/or other |
| 20 | materials provided with the distribution. |
| 21 | |
| 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 23 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 24 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 28 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 32 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 34 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | |
| 36 | |
| 37 | */ |
| 38 | |
| 39 | #include "u8x8.h" |
| 40 | |
| 41 | /*========================================================*/ |
| 42 | |
| 43 | |
| 44 | /* vertical top lsb memory architecture */ |
| 45 | uint8_t u8x8_capture_get_pixel_1(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width) |
| 46 | { |
| 47 | //uint8_t *dest_ptr = capture->buffer; |
| 48 | //if ( dest_ptr == NULL ) |
| 49 | //return 0; |
| 50 | //dest_ptr += (y/8)*capture->tile_width*8; |
| 51 | dest_ptr += (y/8)*tile_width*8; |
| 52 | y &= 7; |
| 53 | dest_ptr += x; |
| 54 | if ( (*dest_ptr & (1<<y)) == 0 ) |
| 55 | return 0; |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | /* horizontal right lsb memory architecture */ |
| 60 | /* SH1122, LD7032, ST7920, ST7986, LC7981, T6963, SED1330, RA8835, MAX7219, LS0 */ |
| 61 | uint8_t u8x8_capture_get_pixel_2(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width) |
| 62 | { |
| 63 | //uint8_t *dest_ptr = capture->buffer; |
| 64 | //if ( dest_ptr == NULL ) |
| 65 | // return 0; |
| 66 | //dest_ptr += y*capture->tile_width; |
| 67 | y *= tile_width; |
| 68 | dest_ptr += y; |
| 69 | dest_ptr += x>>3; |
| 70 | if ( (*dest_ptr & (128>>(x&7))) == 0 ) |
| 71 | return 0; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | void u8x8_capture_write_pbm_pre(uint8_t tile_width, uint8_t tile_height, void (*out)(const char *s)) |
| 76 | { |
| 77 | out("P1\n"); |
| 78 | out(u8x8_utoa((uint16_t)tile_width*8)); |
| 79 | out("\n"); |
| 80 | out(u8x8_utoa((uint16_t)tile_height*8)); |
| 81 | out("\n"); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void u8x8_capture_write_pbm_buffer(uint8_t *buffer, uint8_t tile_width, uint8_t tile_height, uint8_t (*get_pixel)(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width), void (*out)(const char *s)) |
| 86 | { |
| 87 | uint16_t x, y; |
| 88 | uint16_t w, h; |
| 89 | |
| 90 | w = tile_width; |
| 91 | w *= 8; |
| 92 | h = tile_height; |
| 93 | h *= 8; |
| 94 | |
| 95 | for( y = 0; y < h; y++) |
| 96 | { |
| 97 | for( x = 0; x < w; x++) |
| 98 | { |
| 99 | if ( get_pixel(x, y, buffer, tile_width) ) |
| 100 | out("1"); |
| 101 | else |
| 102 | out("0"); |
| 103 | } |
| 104 | out("\n"); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | void u8x8_capture_write_xbm_pre(uint8_t tile_width, uint8_t tile_height, void (*out)(const char *s)) |
| 112 | { |
| 113 | out("#define xbm_width "); |
| 114 | out(u8x8_utoa((uint16_t)tile_width*8)); |
| 115 | out("\n"); |
| 116 | out("#define xbm_height "); |
| 117 | out(u8x8_utoa((uint16_t)tile_height*8)); |
| 118 | out("\n"); |
| 119 | out("static unsigned char xbm_bits[] = {\n"); |
| 120 | } |
| 121 | |
| 122 | void u8x8_capture_write_xbm_buffer(uint8_t *buffer, uint8_t tile_width, uint8_t tile_height, uint8_t (*get_pixel)(uint16_t x, uint16_t y, uint8_t *dest_ptr, uint8_t tile_width), void (*out)(const char *s)) |
| 123 | { |
| 124 | uint16_t x, y; |
| 125 | uint16_t w, h; |
| 126 | uint8_t v, b; |
| 127 | char s[2]; |
| 128 | s[1] = '\0'; |
| 129 | |
| 130 | w = tile_width; |
| 131 | w *= 8; |
| 132 | h = tile_height; |
| 133 | h *= 8; |
| 134 | |
| 135 | y = 0; |
| 136 | for(;;) |
| 137 | { |
| 138 | x = 0; |
| 139 | for(;;) |
| 140 | { |
| 141 | v = 0; |
| 142 | for( b = 0; b < 8; b++ ) |
| 143 | { |
| 144 | v <<= 1; |
| 145 | if ( get_pixel(x+7-b, y, buffer, tile_width) ) |
| 146 | v |= 1; |
| 147 | } |
| 148 | out("0x"); |
| 149 | s[0] = (v>>4); |
| 150 | if ( s[0] <= 9 ) |
| 151 | s[0] += '0'; |
| 152 | else |
| 153 | s[0] += 'a'-10; |
| 154 | out(s); |
| 155 | s[0] = (v&15); |
| 156 | if ( s[0] <= 9 ) |
| 157 | s[0] += '0'; |
| 158 | else |
| 159 | s[0] += 'a'-10; |
| 160 | out(s); |
| 161 | x += 8; |
| 162 | if ( x >= w ) |
| 163 | break; |
| 164 | out(","); |
| 165 | } |
| 166 | y++; |
| 167 | if ( y >= h ) |
| 168 | break; |
| 169 | out(","); |
| 170 | out("\n"); |
| 171 | } |
| 172 | out("};\n"); |
| 173 | |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | /*========================================================*/ |
| 179 | |
| 180 | #ifdef NOT_YET_IMPLEMENTED_U8X8_SCREEN_CAPTURE |
| 181 | |
| 182 | struct _u8x8_capture_struct |
| 183 | { |
| 184 | u8x8_msg_cb old_cb; |
| 185 | uint8_t *buffer; /* tile_width*tile_height*8 bytes */ |
| 186 | uint8_t tile_width; |
| 187 | uint8_t tile_height; |
| 188 | }; |
| 189 | typedef struct _u8x8_capture_struct u8x8_capture_t; |
| 190 | |
| 191 | |
| 192 | u8x8_capture_t u8x8_capture; |
| 193 | |
| 194 | |
| 195 | static void u8x8_capture_memory_copy(uint8_t *dest, uint8_t *src, uint16_t cnt) |
| 196 | { |
| 197 | while( cnt > 0 ) |
| 198 | { |
| 199 | *dest++ = *src++; |
| 200 | cnt--; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | static void u8x8_capture_DrawTiles(u8x8_capture_t *capture, uint8_t tx, uint8_t ty, uint8_t tile_cnt, uint8_t *tile_ptr) |
| 205 | { |
| 206 | uint8_t *dest_ptr = capture->buffer; |
| 207 | //printf("tile pos: %d %d, cnt=%d\n", tx, ty, tile_cnt); |
| 208 | if ( dest_ptr == NULL ) |
| 209 | return; |
| 210 | dest_ptr += (uint16_t)ty*capture->tile_width*8; |
| 211 | dest_ptr += (uint16_t)tx*8; |
| 212 | u8x8_capture_memory_copy(dest_ptr, tile_ptr, tile_cnt*8); |
| 213 | } |
| 214 | |
| 215 | uint8_t u8x8_d_capture(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) |
| 216 | { |
| 217 | if ( msg == U8X8_MSG_DISPLAY_DRAW_TILE ) |
| 218 | { |
| 219 | uint8_t x, y, c; |
| 220 | uint8_t *ptr; |
| 221 | x = ((u8x8_tile_t *)arg_ptr)->x_pos; |
| 222 | y = ((u8x8_tile_t *)arg_ptr)->y_pos; |
| 223 | c = ((u8x8_tile_t *)arg_ptr)->cnt; |
| 224 | ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr; |
| 225 | do |
| 226 | { |
| 227 | u8x8_capture_DrawTiles(&u8x8_capture, x, y, c, ptr); |
| 228 | x += c; |
| 229 | arg_int--; |
| 230 | } while( arg_int > 0 ); |
| 231 | } |
| 232 | return u8x8_capture.old_cb(u8x8, msg, arg_int, arg_ptr); |
| 233 | } |
| 234 | |
| 235 | uint8_t u8x8_GetCaptureMemoryPixel(u8x8_t *u8x8, uint16_t x, uint16_t y) |
| 236 | { |
| 237 | return u8x8_capture_GetPixel(&u8x8_capture, x, y); |
| 238 | } |
| 239 | |
| 240 | /* memory: tile_width*tile_height*8 bytes */ |
| 241 | void u8x8_ConnectCapture(u8x8_t *u8x8, uint8_t tile_width, uint8_t tile_height, uint8_t *memory) |
| 242 | { |
| 243 | if ( u8x8->display_cb == u8x8_d_capture ) |
| 244 | return; /* do nothing, capture already installed */ |
| 245 | |
| 246 | u8x8_capture.buffer = memory; /* tile_width*tile_height*8 bytes */ |
| 247 | u8x8_capture.tile_width = tile_width; |
| 248 | u8x8_capture.tile_height = tile_height; |
| 249 | u8x8_capture.old_cb = u8x8->display_cb; |
| 250 | u8x8->display_cb = u8x8_d_capture; |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | #endif |