forked from raspberrypi/pico-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pattern.c
102 lines (81 loc) · 3.24 KB
/
test_pattern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico.h"
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/scanvideo.h"
#include "pico/scanvideo/composable_scanline.h"
#include "pico/sync.h"
#define vga_mode vga_mode_320x240_60
void core1_func();
// Simple color bar program, which draws 7 colored bars: red, green, yellow, blow, magenta, cyan, white
// Can be used to check resister DAC correctness.
//
// Note this program also demonstrates running video on core 1, leaving core 0 free. It supports
// user input over USB or UART stdin, although all it does with it is invert the colors when you press SPACE
static semaphore_t video_initted;
static bool invert;
int main(void) {
stdio_init_all();
// create a semaphore to be posted when video init is complete
sem_init(&video_initted, 0, 1);
// launch all the video on core 1, so it isn't affected by USB handling on core 0
multicore_launch_core1(core1_func);
// wait for initialization of video to be complete
sem_acquire_blocking(&video_initted);
puts("Color bars ready, press SPACE to invert...");
while (true) {
// prevent tearing when we invert - if you're astute you'll notice this actually causes
// a fixed tear a number of scanlines from the top. this is caused by pre-buffering of scanlines
// and is too detailed a topic to fix here.
scanvideo_wait_for_vblank();
int c = getchar_timeout_us(0);
switch (c) {
case ' ':
invert = !invert;
printf("Inverted: %d\n", invert);
break;
}
}
}
void draw_color_bar(scanvideo_scanline_buffer_t *buffer) {
// figure out 1/32 of the color value
uint line_num = scanvideo_scanline_number(buffer->scanline_id);
uint32_t primary_color = 1u + (line_num * 7 / vga_mode.height);
uint32_t color_mask = PICO_SCANVIDEO_PIXEL_FROM_RGB5(0x1f * (primary_color & 1u), 0x1f * ((primary_color >> 1u) & 1u), 0x1f * ((primary_color >> 2u) & 1u));
uint bar_width = vga_mode.width / 32;
uint16_t *p = (uint16_t *) buffer->data;
uint32_t invert_bits = invert ? PICO_SCANVIDEO_PIXEL_FROM_RGB5(0x1f,0x1f,0x1f) : 0;
for (uint bar = 0; bar < 32; bar++) {
*p++ = COMPOSABLE_COLOR_RUN;
uint32_t color = PICO_SCANVIDEO_PIXEL_FROM_RGB5(bar, bar, bar);
*p++ = (color & color_mask) ^ invert_bits;
*p++ = bar_width - 3;
}
// 32 * 3, so we should be word aligned
assert(!(3u & (uintptr_t) p));
// black pixel to end line
*p++ = COMPOSABLE_RAW_1P;
*p++ = 0;
// end of line with alignment padding
*p++ = COMPOSABLE_EOL_SKIP_ALIGN;
*p++ = 0;
buffer->data_used = ((uint32_t *) p) - buffer->data;
assert(buffer->data_used < buffer->data_max);
buffer->status = SCANLINE_OK;
}
void core1_func() {
// initialize video and interrupts on core 1
scanvideo_setup(&vga_mode);
scanvideo_timing_enable(true);
sem_release(&video_initted);
while (true) {
scanvideo_scanline_buffer_t *scanline_buffer = scanvideo_begin_scanline_generation(true);
draw_color_bar(scanline_buffer);
scanvideo_end_scanline_generation(scanline_buffer);
}
}