forked from ScaryTea/STM32F103
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic_rotation_PC.c
194 lines (170 loc) · 4.92 KB
/
pic_rotation_PC.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
/*
convert car.jpg -rotate 90 -resize 84x48 -background white -gravity center -extent 84x48 -colorspace gray -monochrome -depth 1 carsize.bmp
*/
void kill_old();
char* wait_angle();
void serial_config();
void convert_pic(char* picture, char* angle);
void format_pic();
void send_pic();
void send_pic2();
int main() {
//kill_old();
printf("Enter picture name or full path:\n");
char picture[100];
scanf("%99s",picture);
char* angle = "0";
int pid = fork();
if(pid) { // write child's pid to file
FILE* f = fopen("tmp/pic_prev_pid","w");
fwrite(&pid, sizeof(int), 1, f);
fclose(f);
}
if(!pid) {
while(1) {
convert_pic(picture, angle);
format_pic();
send_pic2();
angle = wait_angle();
// printf("Received angle: [%s]\n",angle);
}
}
return 0;
}
void kill_old() {
printf("YYYYYYYYYYYYY");
FILE* f = fopen("/tmp/pic_prev_pid","r");
if(f < 0) {
printf("Unable to open tmp file\n");
}
int prev_pid = -1;
if(fread(&prev_pid, sizeof(int), 1, f))
printf("prev_pid: %d, kill result: %d\n",prev_pid,kill(prev_pid, SIGKILL));
fclose(f);
}
char* wait_angle() {
int file;
file = open("/dev/ttyUSB0", O_RDONLY | O_NOCTTY );
if(file == -1) {
perror("Unable to open the serial port\n");
}
// printf("Serial port opened successfully\n");
struct termios options;
tcgetattr(file, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag |= PARENB; //No parity
//options.c_cflag |= PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; //8 bits
cfmakeraw(&options);
tcsetattr(file, TCSANOW, &options);
int bytes = -1;
char data[3];
// printf("Reading serial port ...\n\n");
bytes = read(file, data, 2);
if (bytes < 0)
printf("Read error[%d]: %s\n",errno,strerror(errno));
char* angle;
// printf("DATA = [%s]\n",data);
switch (data[0]) {
case '0': angle = "0"; break;
case '1': angle = "90"; break;
case '2': angle = "180"; break;
case '3': angle = "270"; break;
default: angle = "270"; break;
}
close(file);
return angle;
}
void convert_pic(char* picture, char* angle) {
char path[] = "/usr/bin/convert";
char* args_convert[20] = {"convert",picture,"-rotate",angle,"-resize","84x48",
"-background", "white", "-gravity", "center", "-extent", "84x48", "-colorspace",
"gray", "-monochrome", "-depth", "1", "pictmp.bmp"};
int pid_convert = fork();
if(!pid_convert) {
// printf("Started converting...\n");
execve("/usr/bin/convert",args_convert, NULL);
// printf("Error converting :[\n");
return;
}
int status;
do {
waitpid(pid_convert, &status, 0);
} while(!WIFEXITED(status));
// printf("Converting finished!\n");
}
void format_pic() {
char* args_format[20] = {"format","pictmp.bmp",NULL};
int pid_format = fork();
if(!pid_format) {
// printf("Started formatting...\n");
execve("/home/vlad_ko/Documents/Embedded/STM32F103/format",args_format, NULL);
// printf("Error formatting :[\n");
return;
}
int status;
do {
waitpid(pid_format, &status, 0);
} while(!WIFEXITED(status));
// printf("Formatting finished!\n");
}
void send_pic() {
/* socat OPEN:/home/vlad_ko/Documents/Embedded/STM32F103/result /dev/ttyUSB0,b9600,raw,echo=0 */
int pid_socat = fork();
char* args_socat[100] = {"socat",
"OPEN:/home/vlad_ko/Documents/Embedded/STM32F103/result", "/dev/ttyUSB0,b9600,raw",NULL};
if(!pid_socat) {
// printf("Started sending...\n");
execve("/usr/bin/socat",args_socat, NULL);
// printf("Error sending :[\n");
return;
}
int status;
do {
waitpid(pid_socat, &status, 0);
} while(!WIFEXITED(status));
// printf("Sent successfully!\n");
}
void send_pic2() {
int file;
file = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY );
if(file == -1) {
perror("Unable to open the serial port\n");
}
struct termios options;
tcgetattr(file, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag |= PARENB; //No parity
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; //8 bits
cfmakeraw(&options);
tcsetattr(file, TCSANOW, &options);
FILE* f = fopen("/home/vlad_ko/Documents/Embedded/STM32F103/result","r");
char data[505];
//printf("[%u] bytes read\n\n",fread(data, sizeof(char), 505, f));
//printf("data read from result: [[%s]]\n",data);
int bytes = write(file, data, 504);
//printf("bytes written: [%d]\n",bytes);
//printf("error(%d): %s\n",errno, strerror(errno));
fclose(f);
close(file);
printf("Sent successfully!\n");
}