|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include <libtock-sync/display/screen.h> |
| 6 | + |
| 7 | +uint8_t* buffer; |
| 8 | + |
| 9 | +static void write_pixel(int x, int y, uint8_t v) { |
| 10 | + int vertical = y / 8; |
| 11 | + int byte_index = x + (vertical * 64); |
| 12 | + int vertical_pixel = y % 8; |
| 13 | + |
| 14 | + uint8_t original = buffer[byte_index]; |
| 15 | + uint8_t new = (original & ~(1 << vertical_pixel)) | ((v & 0x1) << vertical_pixel); |
| 16 | + buffer[byte_index] = new; |
| 17 | +} |
| 18 | + |
| 19 | +static void draw_black_square_middle(uint16_t x, uint16_t y, uint16_t width) { |
| 20 | + if (width < 3) return; |
| 21 | + |
| 22 | + uint16_t third = width / 3; |
| 23 | + |
| 24 | + // Write all pixels for the new square. |
| 25 | + for (int i = x + third; i < x + third + third; i++) { |
| 26 | + for (int j = y + third; j < y + third + third; j++) { |
| 27 | + write_pixel(i, j, 0); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Recurse for surrounding squares. |
| 32 | + for (int i = 0; i < 3; i++) { |
| 33 | + for (int j = 0; j < 3; j++) { |
| 34 | + if (i == 1 && j == 1) continue; |
| 35 | + draw_black_square_middle(x + (third * i), y + (third * j), third); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +int main(void) { |
| 41 | + returncode_t err; |
| 42 | + |
| 43 | + uint32_t width, height; |
| 44 | + |
| 45 | + err = libtock_screen_get_resolution(&width, &height); |
| 46 | + if (err != RETURNCODE_SUCCESS) return -1; |
| 47 | + |
| 48 | + // Make width smaller of width and height. |
| 49 | + if (width > height) width = height; |
| 50 | + |
| 51 | + // Allocate buffer. |
| 52 | + uint32_t buffer_size = width * width; |
| 53 | + buffer = malloc(sizeof(uint8_t) * buffer_size); |
| 54 | + |
| 55 | + // Fill in the display buffer. |
| 56 | + memset(buffer, 0xff, buffer_size); |
| 57 | + draw_black_square_middle(0, 0, width); |
| 58 | + libtocksync_screen_set_frame(0, 0, width, width); |
| 59 | + libtocksync_screen_write(buffer, buffer_size, buffer_size / 8); |
| 60 | + |
| 61 | + return 0; |
| 62 | +} |
0 commit comments