Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
SystemProgramming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
서의수
SystemProgramming
Commits
fc45f333
Commit
fc45f333
authored
2 years ago
by
minsu Hyun
Committed by
서의수
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Minsu
parent
b78cf522
No related branches found
No related tags found
1 merge request
!2
Minsu
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ParkingLot.C
+309
-0
309 additions, 0 deletions
ParkingLot.C
with
309 additions
and
0 deletions
ParkingLot.C
0 → 100644
+
309
−
0
View file @
fc45f333
#include
<sys/stat.h>
#include
<sys/types.h>
#include
<fcntl.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<unistd.h>
#include
<time.h>
#include
<arpa/inet.h>
#include
<sys/socket.h>
#define BUFFER_MAX 3
#define DIRECTION_MAX 45
#define VALUE_MAX 256
#define INTERVAL 1000000
#define IN 0
#define OUT 1
#define PWM 0
#define LOW 0
#define HIGH 1
#define POUT 23
#define PIN 24
#define PIR 17
static
int
GPIOExport
(
int
pin
)
{
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/export"
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open export for writing!
\n
"
);
return
(
-
1
);
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pin
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
return
(
0
);
}
static
int
GPIOUnexport
(
int
pin
){
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/unexport"
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open unexport for writing!
\n
"
);
return
(
-
1
);
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pin
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
return
(
0
);
}
static
int
GPIODirection
(
int
pin
,
int
dir
){
static
const
char
s_directions_str
[]
=
"in
\0
out"
;
char
path
[
DIRECTION_MAX
]
=
"/sys/class/gpio/gpio%d/direction"
;
int
fd
;
snprintf
(
path
,
DIRECTION_MAX
,
"/sys/class/gpio/gpio%d/direction"
,
pin
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open gpio direction for writing!
\n
"
);
return
(
-
1
);
}
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
)){
fprintf
(
stderr
,
"Faile to set direction!
\n
"
);
return
(
-
1
);
}
close
(
fd
);
return
(
0
);
}
static
int
GPIORead
(
int
pin
){
char
path
[
VALUE_MAX
];
char
value_str
[
3
];
int
fd
;
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/gpio/gpio%d/value"
,
pin
);
fd
=
open
(
path
,
O_RDONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open gpio value for reading!
\n
"
);
return
(
-
1
);
}
if
(
-
1
==
read
(
fd
,
value_str
,
3
)){
fprintf
(
stderr
,
"Failed to read value
\n
"
);
return
(
-
1
);
}
close
(
fd
);
return
(
atoi
(
value_str
));
}
static
int
GPIOWrite
(
int
pin
,
int
value
){
static
const
char
s_values_str
[]
=
"01"
;
char
path
[
VALUE_MAX
];
int
fd
;
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/gpio/gpio%d/value"
,
pin
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
){
fprintf
(
stderr
,
"Failed to open gpio value for writing!
\n
"
);
return
(
-
1
);
}
if
(
1
!=
write
(
fd
,
&
s_values_str
[
LOW
==
value
?
0
:
1
],
1
)){
fprintf
(
stderr
,
"Failed to write value!
\n
"
);
return
(
-
1
);
close
(
fd
);
return
(
0
);
}
}
double
ultrawave_thd
(
void
){
clock_t
start_t
,
end_t
;
double
distance
=
0
;
double
time
;
if
(
-
1
==
GPIOExport
(
POUT
)
||
-
1
==
GPIOExport
(
PIN
)){
printf
(
"gpio export err
\n
"
);
exit
(
0
);
}
usleep
(
100000
);
if
(
-
1
==
GPIODirection
(
POUT
,
OUT
)
||
-
1
==
GPIODirection
(
PIN
,
IN
)){
printf
(
"gpio direction err
\n
"
);
exit
(
0
);
}
GPIOWrite
(
POUT
,
0
);
usleep
(
10000
);
while
(
1
){
if
(
-
1
==
GPIOWrite
(
POUT
,
1
)){
printf
(
"gpio write/trigger err
\n
"
);
exit
(
0
);
}
usleep
(
10
);
GPIOWrite
(
POUT
,
0
);
while
(
GPIORead
(
PIN
)
==
0
){
start_t
=
clock
();
}
while
(
GPIORead
(
PIN
)
==
1
){
end_t
=
clock
();
}
time
=
(
double
)(
end_t
-
start_t
)
/
CLOCKS_PER_SEC
;
distance
=
time
/
2
*
34000
;
if
(
distance
>
900
)
distance
=
900
;
usleep
(
500000
);
return
distance
;
}
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
int
main
(
int
argc
,
char
*
argv
[]){
int
sock_1
;
int
sock_2
;
int
sock_3
;
int
serv_sock
,
clnt_sock
=-
1
;
char
PARK
[
2
]
=
"0"
;
char
CAR
[
2
]
=
"1"
;
char
HUMAN
[
2
]
=
"2"
;
double
start
=
0
;
double
end
=
0
;
struct
sockaddr_in
serv_addr1
;
struct
sockaddr_in
serv_addr2
;
struct
sockaddr_in
serv_addr3
;
sock_1
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
sock_2
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
sock_3
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
if
(
sock_1
==
-
1
)
error_handling
(
"socket() error"
);
if
(
sock_2
==
-
1
)
error_handling
(
"socket() error"
);
if
(
sock_3
==
-
1
)
error_handling
(
"socket() error"
);
memset
(
&
serv_addr1
,
0
,
sizeof
(
serv_addr1
));
memset
(
&
serv_addr2
,
0
,
sizeof
(
serv_addr2
));
memset
(
&
serv_addr3
,
0
,
sizeof
(
serv_addr3
));
serv_addr1
.
sin_family
=
AF_INET
;
serv_addr1
.
sin_addr
.
s_addr
=
inet_addr
(
argv
[
2
]);
serv_addr1
.
sin_port
=
htons
(
atoi
(
argv
[
1
]));
serv_addr2
.
sin_family
=
AF_INET
;
serv_addr2
.
sin_addr
.
s_addr
=
inet_addr
(
argv
[
3
]);
serv_addr2
.
sin_port
=
htons
(
atoi
(
argv
[
1
]));
serv_addr3
.
sin_family
=
AF_INET
;
serv_addr3
.
sin_addr
.
s_addr
=
inet_addr
(
argv
[
4
]);
serv_addr3
.
sin_port
=
htons
(
atoi
(
argv
[
1
]));
if
(
connect
(
sock_1
,
(
struct
sockaddr
*
)
&
serv_addr1
,
sizeof
(
serv_addr1
))
==
-
1
)
error_handling
(
"connect() error"
);
else
{
printf
(
"connect1
\n
"
);
}
if
(
connect
(
sock_2
,
(
struct
sockaddr
*
)
&
serv_addr2
,
sizeof
(
serv_addr2
))
==
-
1
)
error_handling
(
"connect() error"
);
else
{
printf
(
"connect2
\n
"
);
}
if
(
connect
(
sock_3
,
(
struct
sockaddr
*
)
&
serv_addr3
,
sizeof
(
serv_addr3
))
==
-
1
)
error_handling
(
"connect() error"
);
else
{
printf
(
"connect3
\n
"
);
}
double
distance
;
usleep
(
50000
);
if
(
-
1
==
GPIOExport
(
PIR
))
{
return
(
1
);
}
GPIODirection
(
PIR
,
IN
);
while
(
1
){
usleep
(
INTERVAL
);
if
(
GPIORead
(
PIR
)
==
HIGH
)
//pir sensor
{
int
cnt
=
0
;
start
=
ultrawave_thd
();
printf
(
"motion detected : %d
\n
"
,
GPIORead
(
PIR
));
end
=
ultrawave_thd
();
while
(
abs
(
end
-
start
)
<
5
)
{
printf
(
"There's a person : Start : %lf End %lf
\n
"
,
start
,
end
);
usleep
(
INTERVAL
);
end
=
ultrawave_thd
();
cnt
++
;
//return human
if
(
cnt
>
6
)
{
write
(
sock_1
,
HUMAN
,
2
);
write
(
sock_2
,
HUMAN
,
2
);
write
(
sock_3
,
HUMAN
,
2
);
}
}
printf
(
"Probelm solved
\n
"
);
usleep
(
INTERVAL
);
//return park
write
(
sock_1
,
PARK
,
2
);
write
(
sock_2
,
PARK
,
2
);
write
(
sock_3
,
PARK
,
2
);
}
else
{
distance
=
ultrawave_thd
();
if
(
80
<
distance
&&
distance
<
110
)
{
printf
(
"Car existed : %lf
\n
"
,
distance
);
usleep
(
INTERVAL
);
//return car
write
(
sock_1
,
CAR
,
2
);
write
(
sock_2
,
CAR
,
2
);
write
(
sock_3
,
CAR
,
2
);
while
(
distance
<=
80
||
distance
>=
110
)
{
distance
=
ultrawave_thd
();
usleep
(
INTERVAL
);
printf
(
"Car existed "
);
}
}
else
{
printf
(
"Nothing : %lf
\n
"
,
distance
);
usleep
(
INTERVAL
);
//return park
write
(
sock_1
,
PARK
,
sizeof
(
PARK
));
write
(
sock_2
,
PARK
,
sizeof
(
PARK
));
write
(
sock_3
,
PARK
,
sizeof
(
PARK
));
}
}
}
printf
(
"end
\n
"
);
close
(
sock_1
);
close
(
sock_2
);
close
(
sock_3
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment