-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcam_reader.c
More file actions
105 lines (94 loc) · 2.13 KB
/
cam_reader.c
File metadata and controls
105 lines (94 loc) · 2.13 KB
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
103
104
105
#include "cam_reader.h"
static struct cam_position* cam_pos;
int cam_init(){
int fd;
/*cam_pos - mmapped array of structs */
/* Open a file for writing.
* - Creating the file if it doesn't exist.
* - Truncating it to 0 size if it already exists. (not really needed)
*
* Note: "O_WRONLY" mode is not sufficient when mmapping.
*/
fd = open(FILEPATH, O_RDWR, S_IRUSR);
if (fd == -1)
{
// perror("Error opening file for writing");
// exit(EXIT_FAILURE);
return -1;
}
/* Now the file is ready to be mmapped. */
cam_pos = (struct cam_position *)mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (cam_pos == MAP_FAILED)
{
close(fd);
// perror("Error mmapping the file");
// exit(EXIT_FAILURE);
return -1;
}
memset(cam_pos, '\0', sizeof(struct cam_position));
/* Retrieve cam pid */
FILE *pid_process = fopen(PID_FILEPATH, "r");
if(pid_process==NULL){
// fprintf(stderr, "Opening file pid_process failed!\n");
return -1;
}
fscanf(pid_process, "%d\n", &pid);
fflush(pid_process);
fclose(pid_process);
return 0;
}
int cam_read(struct cam_position* cam_pos_main){
/* try lock mutex, if free than lock */
if(pthread_mutex_trylock(&cam_pos->pmutex)){
return -1;
}
else{
cam_pos_main->x = cam_pos->x;
cam_pos_main->y = cam_pos->y;
/* Unlock mutex */
if(pthread_mutex_unlock(&cam_pos->pmutex)){
return -1;
}
}
return 0;
}
/*int cam_start_white(){
if(kill(pid, START_WHITE_CAM)){
return -1;
}
else{
return 0;
}
}
int cam_start_black(){
if(kill(pid, START_BLACK_CAM)){
return -1;
}
else{
return 0;
}
}
int cam_pause(){
if(kill(pid, PAUSE_CAM)){
return -1;
}
else{
return 0;
}
}
int cam_close(){
if(kill(pid, CLOSE_CAM)){
return -1;
}
else{
return 0;
}
}
int cam_is_runing(){
if(!kill(pid, IS_RUNING)){
return -1;
}
else{
return 0;
}
}*/