Skip to content

Commit 91961fd

Browse files
committed
tests: screen: add sierpinski_carpet test
1 parent 4b3ef30 commit 91961fd

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../../..
5+
6+
# Which files to compile.
7+
C_SRCS := $(wildcard *.c)
8+
9+
APP_HEAP_SIZE := 5000
10+
11+
# Include userland master makefile. Contains rules and flags for actually
12+
# building the application.
13+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Sierpiński Carpet
2+
=================
3+
4+
This is a screen test app that draws a [Sierpiński
5+
carpet](https://en.wikipedia.org/wiki/Sierpi%C5%84ski_carpet) to the screen.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)