FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ArduinoLED/ArduinoControl/src/ArduinoDevice.cpp at master · thagberg/ArduinoLED · GitHub
thagberg
/
ArduinoLED
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ArduinoLED
/
ArduinoControl
/
src
/
ArduinoDevice.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
109 lines (96 loc) · 2.71 KB
Breadcrumbs
ArduinoLED
/
ArduinoControl
/
src
/
ArduinoDevice.cpp
Copy path
File metadata and controls
109 lines (96 loc) · 2.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
ArduinoDevice.h
"
#
include
<
assert.h
>
namespace
hvk
{
namespace
control
{
ArduinoException::ArduinoException
(std::string message)
:
mMessage
(message)
{
}
const
char
*
ArduinoException::what
()
const
{
return
mMessage
.
c_str
();
}
ArduinoDevice::ArduinoDevice
()
:
mCommHandle
(
INVALID_HANDLE_VALUE
)
,
mDcb
()
,
mDeviceReady
(
false
)
{
ZeroMemory
(
mReadBuffer
,
_countof
(
mReadBuffer
));
mCommHandle
=
CreateFileA
(
"
COM4
"
,
GENERIC_READ
|
GENERIC_WRITE
,
NULL
,
NULL
,
OPEN_EXISTING
,
FILE_ATTRIBUTE_NORMAL
,
NULL
);
assert
(
mCommHandle
!=
INVALID_HANDLE_VALUE
);
if
(
mCommHandle
==
INVALID_HANDLE_VALUE
)
{
throw
ArduinoException
(
"
Failed to open COM4 to Arduino Device
"
);
}
memset
(&
mDcb
,
0
,
sizeof
(
DCB
));
bool
success =
GetCommState
(
mCommHandle
, &
mDcb
);
assert
(success);
if
(!success)
{
throw
ArduinoException
(
"
Failed to determine CommState with Arduino Device
"
);
}
mDcb
.
BaudRate
=
CBR_38400
;
mDcb
.
ByteSize
=
8
;
mDcb
.
Parity
=
NOPARITY
;
mDcb
.
StopBits
=
ONESTOPBIT
;
mDcb
.
fRtsControl
=
RTS_CONTROL_ENABLE
;
mDcb
.
fDtrControl
=
DTR_CONTROL_ENABLE
;
success =
SetCommState
(
mCommHandle
, &
mDcb
);
assert
(success);
if
(!success)
{
throw
ArduinoException
(
"
Failed to set CommState of Arduino Device
"
);
}
success =
GetCommState
(
mCommHandle
, &
mDcb
);
assert
(success);
if
(!success)
{
throw
ArduinoException
(
"
Failed to validate updated CommState of Arduino Device
"
);
}
COMMTIMEOUTS
timeouts = {};
timeouts.
ReadTotalTimeoutConstant
=
1000
;
timeouts.
ReadIntervalTimeout
=
0
;
timeouts.
ReadTotalTimeoutMultiplier
=
1
;
timeouts.
WriteTotalTimeoutConstant
=
1000
;
timeouts.
WriteTotalTimeoutMultiplier
=
1
;
success =
SetCommTimeouts
(
mCommHandle
, &timeouts);
assert
(success);
if
(!success)
{
throw
ArduinoException
(
"
Failed to set CommTimeouts with Arduino Device
"
);
}
mDeviceReady
=
true
;
}
ArduinoDevice::~ArduinoDevice
()
{
if
(
mDeviceReady
)
{
auto
success =
CancelIo
(
mCommHandle
);
//
not sure what the risk is of leaving the handle open if closing IO fails
if
(success)
{
CloseHandle
(
mCommHandle
);
}
}
}
size_t
ArduinoDevice::write
(Framebuffer& framebuffer)
{
DWORD
numBytesWritten;
bool
writeSuccess =
WriteFile
(
mCommHandle
, framebuffer.
data
(),
sizeof
(Color) * framebuffer.
size
(), &numBytesWritten,
nullptr
);
assert
(writeSuccess);
DWORD
numBytesRead;
bool
readSuccess =
ReadFile
(
mCommHandle
,
mReadBuffer
,
1
, &numBytesRead,
nullptr
);
assert
(readSuccess && numBytesRead ==
1
);
return
static_cast
<
size_t
>(numBytesWritten);
}
}
}
Back
|
FazBrowse Home
|
New Git URL