Last active
October 24, 2023 02:44
-
-
Save Hermann-SW2/4c71a01fd041c1551f542beb54c5919c to your computer and use it in GitHub Desktop.
Raspberry hello_pi dispmanx.c extended with DrawCircle() for crosshairs demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2012, Broadcom Europe Ltd | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
* Neither the name of the copyright holder nor the | |
names of its contributors may be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY | |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
// A simple demo using dispmanx to display an overlay | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdarg.h> | |
#include <assert.h> | |
#include <unistd.h> | |
#include <sys/time.h> | |
#include "bcm_host.h" | |
// from https://github.com/snaptoken/kilo-src/blob/isig/kilo.c | |
#include <termios.h> | |
struct termios orig_termios; | |
void disableRawMode() { | |
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios); | |
} | |
void enableRawMode() { | |
tcgetattr(STDIN_FILENO, &orig_termios); | |
atexit(disableRawMode); | |
struct termios raw = orig_termios; | |
raw.c_lflag &= ~(ECHO | ICANON | ISIG); | |
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); | |
} | |
#define WIDTH 400 | |
#define HEIGHT 400 | |
#ifndef ALIGN_UP | |
#define ALIGN_UP(x,y) ((x + (y)-1) & ~((y)-1)) | |
#endif | |
typedef struct | |
{ | |
DISPMANX_DISPLAY_HANDLE_T display; | |
DISPMANX_MODEINFO_T info; | |
void *image; | |
DISPMANX_UPDATE_HANDLE_T update; | |
DISPMANX_RESOURCE_HANDLE_T resource; | |
DISPMANX_ELEMENT_HANDLE_T element; | |
uint32_t vc_image_ptr; | |
} RECT_VARS_T; | |
static RECT_VARS_T gRectVars; | |
static void FillRect( VC_IMAGE_TYPE_T type, void *image, int pitch, int aligned_height, int x, int y, int w, int h, int val ) | |
{ | |
int row; | |
int col; | |
uint32_t *line = (uint32_t *)image + y * (pitch>>2) + x; | |
for ( row = 0; row < h; row++ ) | |
{ | |
for ( col = 0; col < w; col++ ) | |
{ | |
line[col] = val; | |
} | |
line += (pitch>>2); | |
} | |
} | |
static void DrawCirc( VC_IMAGE_TYPE_T type, void *image, int pitch, int aligned_height, int x, int y, int r, int val ) | |
{ | |
/* http://rosettacode.org/wiki/Bitmap/Midpoint_circle_algorithm#C | |
*/ | |
#define plot(x, y) (*((uint32_t *)image + (y) * (pitch>>2) + (x)) = val) | |
void raster_circle( | |
unsigned int x0, | |
unsigned int y0, | |
unsigned int radius) | |
{ | |
int f = 1 - radius; | |
int ddF_x = 0; | |
int ddF_y = -2 * radius; | |
int x = 0; | |
int y = radius; | |
plot(x0, y0 + radius); | |
plot(x0, y0 - radius); | |
plot(x0 + radius, y0); | |
plot(x0 - radius, y0); | |
while(x < y) | |
{ | |
if(f >= 0) | |
{ | |
y--; | |
ddF_y += 2; | |
f += ddF_y; | |
} | |
x++; | |
ddF_x += 2; | |
f += ddF_x + 1; | |
plot(x0 + x, y0 + y); | |
plot(x0 - x, y0 + y); | |
plot(x0 + x, y0 - y); | |
plot(x0 - x, y0 - y); | |
plot(x0 + y, y0 + x); | |
plot(x0 - y, y0 + x); | |
plot(x0 + y, y0 - x); | |
plot(x0 - y, y0 - x); | |
} | |
} | |
#undef plot | |
raster_circle(x, y, r); | |
} | |
int main(void) | |
{ | |
RECT_VARS_T *vars; | |
uint32_t screen = 0; | |
int ret; | |
VC_RECT_T src_rect; | |
VC_RECT_T dst_rect; | |
VC_IMAGE_TYPE_T type = VC_IMAGE_RGBA32; | |
int width = WIDTH, height = HEIGHT, X, Y; | |
int pitch = ALIGN_UP(width*4, 32); | |
int aligned_height = ALIGN_UP(height, 16); | |
VC_DISPMANX_ALPHA_T alpha = { DISPMANX_FLAGS_ALPHA_FROM_SOURCE, // | DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS, | |
120, /*alpha 0->255*/ | |
0 }; | |
vars = &gRectVars; | |
bcm_host_init(); | |
printf("Open display[%i]...\n", screen ); | |
vars->display = vc_dispmanx_display_open( screen ); | |
ret = vc_dispmanx_display_get_info( vars->display, &vars->info); | |
assert(ret == 0); | |
printf( "Display is %d x %d\n", vars->info.width, vars->info.height ); | |
vars->image = calloc( 1, pitch * height ); | |
assert(vars->image); | |
FillRect( type, vars->image, pitch, aligned_height, 0, 0, width, height, 0x00FFFFFF ); | |
DrawCirc( type, vars->image, pitch, aligned_height, width/2, height/2, width/4, 0xFFFFFFFF ); | |
DrawCirc( type, vars->image, pitch, aligned_height, width/2, height/2, width/4-1, 0xFF000000 ); | |
FillRect( type, vars->image, pitch, aligned_height, 0, height/2, width, 1, 0xFFFFFFFF ); | |
FillRect( type, vars->image, pitch, aligned_height, 0, 1+height/2, width, 1, 0xFF000000 ); | |
FillRect( type, vars->image, pitch, aligned_height, width/2, 0, 1, height, 0xFFFFFFFF ); | |
FillRect( type, vars->image, pitch, aligned_height, 1+width/2, 0, 1, height, 0xFF000000 ); | |
vars->resource = vc_dispmanx_resource_create( type, | |
width, | |
height, | |
&vars->vc_image_ptr ); | |
assert( vars->resource ); | |
vc_dispmanx_rect_set( &dst_rect, 0, 0, width, height); | |
ret = vc_dispmanx_resource_write_data( vars->resource, | |
type, | |
pitch, | |
vars->image, | |
&dst_rect ); | |
assert( ret == 0 ); | |
vars->update = vc_dispmanx_update_start( 10 ); | |
assert( vars->update ); | |
vc_dispmanx_rect_set( &src_rect, 0, 0, width << 16, height << 16 ); | |
vc_dispmanx_rect_set( &dst_rect, X = ( vars->info.width - width ) / 2, | |
Y = ( vars->info.height - height ) / 2, | |
width, | |
height ); | |
vars->element = vc_dispmanx_element_add( vars->update, | |
vars->display, | |
2000, // layer | |
&dst_rect, | |
vars->resource, | |
&src_rect, | |
DISPMANX_PROTECTION_NONE, | |
&alpha, | |
NULL, // clamp | |
VC_IMAGE_ROT0 ); | |
ret = vc_dispmanx_update_submit_sync( vars->update ); | |
assert( ret == 0 ); | |
{ | |
enableRawMode(); | |
char c=0, state=0; | |
while (c != 'q') { | |
if (read(STDIN_FILENO, &c, 1) == 1) { | |
switch (state) { | |
case 0: if (c==27) ++state; break; | |
case 1: if (c==91) ++state; else state=0; break; | |
case 2: if (c<65 || c>68) { state=0; break; } | |
switch (c) { | |
case 65: --Y; break; | |
case 66: ++Y; break; | |
case 67: ++X; break; | |
case 68: --X; break; | |
} | |
// Start a new update, DISPMANX_NO_HANDLE on error | |
vars->update = vc_dispmanx_update_start( 10 ); | |
assert( vars->update ); | |
assert( vars->update != DISPMANX_NO_HANDLE ); | |
vc_dispmanx_rect_set( &dst_rect, X, | |
Y, | |
width, | |
height ); | |
vc_dispmanx_element_change_attributes( vars->update, | |
vars->element, | |
0, //uint32_t change_flags, | |
3, | |
9, //uint8_t opacity, | |
&dst_rect, | |
&src_rect, | |
0, //DISPMANX_RESOURCE_HANDLE_T mask, | |
0); //DISPMANX_TRANSFORM_T transform ); | |
// End an update and wait for it to complete | |
ret = vc_dispmanx_update_submit_sync( vars->update ); | |
assert( ret == 0 ); | |
} | |
usleep(1000); | |
} | |
} | |
} | |
ret = vc_dispmanx_element_remove( vars->update, vars->element ); | |
assert( ret == 0 ); | |
ret = vc_dispmanx_resource_delete( vars->resource ); | |
assert( ret == 0 ); | |
ret = vc_dispmanx_display_close( vars->display ); | |
assert( ret == 0 ); | |
return 0; | |
} |
2nd revision, perfect raspivid/raspistill camera preview/aiming crosshairs (overlays camera preview window only on black/white circles/lines, like a mouse cursor):
https://www.raspberrypi.org/forums/viewtopic.php?f=33&t=318902&p=1909183#p1909183
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.raspberrypi.org/forums/viewtopic.php?f=33&p=1909146#p1909128
