-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialFucker.m
75 lines (58 loc) · 2.22 KB
/
SerialFucker.m
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
classdef SerialFucker
properties
Figure
SerialPort %serial
SerialReader
end
methods
function self = SerialFucker(port)
ports = getAvailableComPort;
if isempty(ports)
error('There are not serial ports to fuck.');
end
if nargin < 1
port = ports{1};
else
assert(ismember(port,ports),'You can not fuck a port that does not exist.');
end
self.SerialPort = serial(port,'BaudRate',115200);
fopen(self.SerialPort);
self.SerialReader = timer('BusyMode','drop','ExecutionMode','fixedSpacing','Period',0.1,'TimerFcn',@self.printSerialBuffer);
start(self.SerialReader);
self.Figure = figure;
set(self.Figure,'KeyPressFcn',@self.sendKey);
set(self.Figure,'KeyReleaseFcn',@self.sendTheWordStop);
end
function delete(self)
delete(self.Figure);
if strcmp(self.SerialReader.Running,'on')
stop(self.SerialReader);
end
delete(self.SerialReader);
if isa(self.SerialPort,'serial') && ischar(get(self.SerialPort,'Status')) && strcmp(get(self.SerialPort,'Status'),'open')
fclose(self.SerialPort);
end
end
function printSerialBuffer(self,varargin)
disp('Polling...');
while self.SerialPort.BytesAvailable > 0
disp(fgetl(self.SerialPort));
end
end
function sendKey(self,~,eventData)
if numel(eventData.Key) > 1
return
end
keycode = uint16(eventData.Key);
if numel(keycode) > 1 || keycode > 255
return
end
disp(['SENT: ' eventData.Key]);
fwrite(self.SerialPort,uint8(keycode));
end
function sendTheWordStop(self,varargin)
disp('SENT: STOP');
fwrite(self.SerialPort,0);
end
end
end