-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.rs
228 lines (211 loc) · 9.57 KB
/
serial.rs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use image::{ImageBuffer, ImageFormat, Rgb};
use log::{debug, error, info};
use serialport::{self, SerialPort};
use std::{
io::Read,
sync::mpsc::{Receiver, Sender},
thread,
time::{self, Duration},
};
use crate::serial::SendToGui::*;
use crate::serial::SendToSerial::*;
use crate::api::{SendToGui, SendToSerial};
fn find_subsequence(vector: &[u8], subsequence: &[u8]) -> Option<usize> {
if subsequence.len() as isize > vector.len() as isize - subsequence.len() as isize {
return None;
}
for i in 0..=(vector.len() - subsequence.len()) {
if vector[i..].starts_with(subsequence) {
//debug!("Found subsequence of: {} in vec, it looks like: {}", String::from_utf8_lossy(subsequence), String::from_utf8_lossy(&vector[i..i + subsequence.len()]));
//debug!("It's at position: {}", i);
//debug!("Pure dump of this vector: {:?}", String::from_utf8_lossy(&vector));
return Some(i);
}
}
None
}
pub fn main(tx_gui: Sender<SendToGui>, rx_serial: Receiver<SendToSerial>) {
let mut port: Option<Box<dyn SerialPort>> = None;
let mut serial_buf: Vec<u8> = Vec::with_capacity(16000); // 15000 is screen size
let mut synced = false;
let packets_length = 16;
// thisisastartpacket
let start_packet: Vec<u8> = vec![
116, 104, 105, 115, 105, 115, 97, 115, 116, 97, 114, 116, 112, 97, 99, 107,
];
// thisisaendddpacket
let end_packet: Vec<u8> = vec![
116, 104, 105, 115, 105, 115, 97, 101, 110, 100, 100, 100, 112, 97, 99, 107,
];
loop {
match rx_serial.recv_timeout(Duration::from_millis(40)) {
Ok(x) => match x {
AskForPorts() => {
debug!("Received ask for ports");
match serialport::available_ports() {
Ok(x) => {
let mut serials = Vec::new();
serials.push(String::from("None"));
for serial in x {
serials.push(serial.port_name);
}
if tx_gui.send(Ports(serials)).is_err() {
error!("Failed to send Ports");
}
}
Err(x) => {
if tx_gui
.send(LogToShow(format!("{}", x.to_string())))
.is_err()
{
error!("Failed to send LogToShow");
}
}
}
}
SelectPort(port_name, baud_rate) => {
debug!("Received select port: {}", port_name);
if let Some(ref mut rport) = port {
debug!("Currently used port name: {:?}", rport.name());
if port_name == rport.name().unwrap() {
debug!("The same port is already selected, skipping");
continue;
}
}
let res = serialport::new(port_name, baud_rate as u32)
.timeout(Duration::from_millis(9999999))
.open();
if res.is_err() {
error!("Failed to open port, reason: {:?}", res);
std::process::exit(0);
}
port = Some(res.unwrap());
thread::sleep(time::Duration::from_millis(100));
if let Some(ref mut rport) = port {
if rport.write_all("screen:".as_bytes()).is_err() {
error!("Failed to write screen message");
}
if rport.flush().is_err() {
error!("Failed to flush");
};
}
}
SendMessage(x) => {
if let Some(ref mut rport) = port {
debug!("Writing to serial port: {}", x);
if rport.write_all(x.as_bytes()).is_err() {
error!("Failed to write message: {}", x);
}
if rport.flush().is_err() {
error!("Failed to flush");
};
} else {
error!("Failed to get rport");
}
}
},
Err(_x) => {
/*
if _x.to_string() != "receiving on an empty channel" {
error!("Failed to recv in serial: {}", _x);
}
*/
}
}
if let Some(ref mut rport) = port {
//debug!("Reading from port...");
let mut serial_buf_tmp: Vec<u8> = vec![0; 7000];
if let Ok(btr) = rport.bytes_to_read() {
if btr == 0 {
continue;
}
}
let _readed = rport.read(serial_buf_tmp.as_mut_slice()).unwrap();
//debug!("Readed bytes: {}", _readed);
//debug!("Pure dump: {}", String::from_utf8_lossy(&serial_buf_tmp));
let real_serial_buf_tmp = &serial_buf_tmp[0.._readed].to_owned();
serial_buf.extend(real_serial_buf_tmp);
if let Some(end_pos) = find_subsequence(&serial_buf, &end_packet) {
if !synced {
synced = true;
serial_buf.clear();
debug!("SYNCED!");
if rport.write_all("screen:".as_bytes()).is_err() {
error!("Failed to write screen message");
}
if rport.flush().is_err() {
error!("Failed to flush");
};
continue;
}
if let Some(start_pos) = find_subsequence(&serial_buf, &start_packet) {
debug!("it contains both packets!");
debug!("start_pos :{}", start_pos);
debug!("end_pos :{}", end_pos);
debug!("serial_buf.len(): {}", serial_buf.len());
if end_pos < start_pos {
error!("End pos is above start pos, how? skipping...");
serial_buf.clear();
continue;
}
//debug!("serial_buf len: {}", serial_buf.len());
let logs = serial_buf[0..start_pos].to_owned();
let screen = serial_buf[start_pos + packets_length..end_pos].to_owned();
let rest = serial_buf[end_pos + packets_length..serial_buf.len()].to_owned();
let real_logs = String::from_utf8_lossy(&logs);
//debug!("Real logs: {}", real_logs);
if !real_logs.is_empty() {
if tx_gui.send(LogToShow(real_logs.to_string())).is_err() {
error!("Failed to send logs to gui");
}
}
if screen.len() != 5000 {
error!("Screen len is: {}", screen.len());
}
info!("Screen succesfully readed");
//debug!("Real screen utf8: {}", String::from_utf8_lossy(&screen));
//debug!("Real screen bytes: {:?}", screen);
info!("Creating the image");
let mut img = ImageBuffer::<Rgb<u8>, _>::new(200, 200);
for y in 0..200 {
for x in 0..200 {
let index = (y * 200 + x) / 8;
let bit_offset = 7 - ((y * 200 + x) % 8);
let i_option = screen.get(index);
if let Some(i) = i_option {
let bit = (i >> bit_offset) & 1;
let color = if bit == 0 {
Rgb([0, 0, 0])
} else {
Rgb([255, 255, 255])
};
img.put_pixel(x as u32, y as u32, color);
} else {
//error!("Error creating image, pixels missing");
img.put_pixel(x as u32, y as u32, Rgb([255, 0, 0]));
}
}
}
let mut buffer = std::io::Cursor::new(Vec::new());
img.write_to(&mut buffer, ImageFormat::Png).unwrap();
if tx_gui.send(ShowPng(buffer.into_inner())).is_err() {
error!("Failed to send png to gui");
}
serial_buf.clear();
serial_buf.extend(rest);
} else {
debug!("Found only end packet");
let logs = &serial_buf[0..end_pos];
let real_logs = String::from_utf8_lossy(&logs);
if !real_logs.is_empty() {
//debug!("Real logs: {}", real_logs);
if tx_gui.send(LogToShow(real_logs.to_string())).is_err() {
error!("Failed to send logs to gui");
}
}
serial_buf.clear();
}
}
}
}
}