Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --osd-custom-message #32

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pthread_cond_t video_cond;
int video_zpos = 1;

bool mavlink_dvr_on_arm = false;
bool osd_custom_message = false;

VideoCodec codec = VideoCodec::H265;
Dvr *dvr = NULL;
Expand Down Expand Up @@ -483,6 +484,8 @@ void printHelp() {
"\n"
" --osd-refresh <rate> - Defines the delay between osd refresh (Default: 1000 ms)\n"
"\n"
" --osd-custom-message - Enables the display of /run/pixelpilot.msg\n"
"\n"
" --dvr-template <path> - Save the video feed (no osd) to the provided filename template.\n"
" DVR is toggled by SIGUSR1 signal\n"
" Supports placeholders %%Y - year, %%m - month, %%d - day,\n"
Expand Down Expand Up @@ -623,7 +626,12 @@ int main(int argc, char **argv)
osd_vars.telemetry_level = atoi(__ArgValue);
continue;
}


__OnArgument("--osd-custom-message") {
osd_custom_message = true;
continue;
}

__OnArgument("--screen-mode") {
char* mode = const_cast<char*>(__ArgValue);
mode_width = atoi(strtok(mode, "x"));
Expand Down
44 changes: 44 additions & 0 deletions src/osd.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ uint32_t stats_rx_bytes = 0;
struct timespec last_timestamp = {0, 0};
float rx_rate = 0;
int hours = 0 , minutes = 0 , seconds = 0 , milliseconds = 0;
char custom_msg[80];
u_int custom_msg_refresh_count = 0;


double getTimeInterval(struct timespec* timestamp, struct timespec* last_meansure_timestamp) {
return (timestamp->tv_sec - last_meansure_timestamp->tv_sec) +
Expand Down Expand Up @@ -268,6 +271,47 @@ void modeset_paint_buffer(struct modeset_buf *buf) {
minutes = 0;
}

//display custom message
if (osd_custom_message) {
FILE *file = fopen("/run/pixelpilot.msg", "r");
if (file != NULL) {

if (fgets(custom_msg, sizeof(custom_msg), file) == NULL) {
perror("Error reading from file");
fclose(file);
}
fclose(file);
if (unlink("/run/pixelpilot.msg") != 0) {
perror("Error deleting the file");
}
custom_msg_refresh_count = 1;
}
if (custom_msg_refresh_count > 0) {

if (custom_msg_refresh_count++ > 5) custom_msg_refresh_count=0;

size_t msg_length = strlen(custom_msg);

//remove any trailing newline that fgets may read
if (msg_length > 0 && custom_msg[msg_length - 1] == '\n') {
custom_msg[msg_length - 1] = '\0';
msg_length--; // Adjust length after removing newline
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a while loop instead? Smth like

while (msg_length > 0 && custom_msg[msg_length - 1] == '\n') {
				custom_msg[msg_length - 1] = '\0';
				msg_length--;  // Adjust length after removing newline
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plan was to ensure that the read message is null terminated at position 80.
We only read sizeof(custom_msg) so either custom_msg[80] (and maybe before) is aleardy null terminted or the read input is exactly 80 bytes long. In that case we should always null termintare at 80.
Will change this to always null termiate at 80.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henkwiedig I rather meant that it could be that the file contains

my msg\n
\n
\n

Wuite unlikely, but since we already decide to strip newlines at the end of the line we read, might make sense to ensure all the newlines are stripped. But up to you. Just a tip.


// Measure the text width
cairo_text_extents_t extents;
cairo_text_extents(cr, custom_msg, &extents);

// Calculate the position to center the text horizontally
double x = (buf->width / 2) - (extents.width / 2);
double y = (buf->height / 2);

// Set the position and draw the text
cairo_move_to(cr, x, y);
cairo_show_text(cr, custom_msg);
}
}

cairo_fill(cr);
}

Expand Down
1 change: 1 addition & 0 deletions src/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct osd_vars {

extern struct osd_vars osd_vars;
extern int osd_thread_signal;
extern bool osd_custom_message;
extern pthread_mutex_t osd_mutex;

typedef struct {
Expand Down