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
Merge requests
!7
Add lcd.c
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Add lcd.c
qkrwlgus0622-main-patch-61830
into
main
Overview
0
Commits
1
Pipelines
0
Changes
1
Merged
지현 박
requested to merge
qkrwlgus0622-main-patch-61830
into
main
2 years ago
Overview
0
Commits
1
Pipelines
0
Changes
1
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
e68c90ed
1 commit,
2 years ago
1 file
+
199
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Actuator/lcd.c
0 → 100644
+
199
−
0
View file @ e68c90ed
Edit in single-file editor
Open in Web IDE
#include
<sys/stat.h>
#include
<sys/types.h>
#include
<fcntl.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<string.h>
#include
<pthread.h>
#include
<arpa/inet.h>
#include
<sys/socket.h>
#include
<wiringPi.h>
#include
<wiringPiI2C.h>
// Define some device parameters
#define I2C_ADDR 0x27 // I2C device address
#define LCD_CHR 1 // Mode - Sending data
#define LCD_CMD 0 // Mode - Sending command
#define LCD_BACKLIGHT 0x08 // On
#define ENABLE 0b00000100 // Enable bit
#define LINE1_HUMAN 0x80 // 1st line
#define LINE2_HP 0xC0 // 2nd line
//lcd 관련 함수
void
lcd_init
(
void
);
void
lcd_byte
(
int
bits
,
int
mode
);
void
lcd_toggle_enable
(
int
bits
);
void
lcdLoc
(
int
line
);
void
typeln
(
const
char
*
s
);
int
fd
;
// seen by all subroutines
//사람이 감지되면 센서로부터 '2'를 받음
#define HUMAN "2"
//소켓 설정
int
serv_sock
;
int
camera_sock
=
-
1
,
sensor_sock
=
-
1
;
struct
sockaddr_in
serv_addr
,
camera_addr
,
sensor_addr
;
socklen_t
clnt_addr_size
;
//lcd 표출 메시지 설정
char
text1
[
20
];
char
text2
[
20
];
char
human_detect_msg
[
16
]
=
"Human Detection"
;
char
not_dc_msg
[
17
]
=
"Not Disabled car"
;
//등록된 차량 지정
char
registed_car_num
[
3
][
9
]
=
{
"01가5568"
,
"01가5568"
,
""
};
//센서 수신
void
*
sensor_thd
(){
char
sensor_msg
[
2
];
while
(
1
)
{
read
(
sensor_sock
,
sensor_msg
,
sizeof
(
sensor_msg
));
printf
(
"%s
\n
"
,
sensor_msg
);
for
(
int
i
=
0
;
i
<
sizeof
(
text1
);
i
++
){
text1
[
i
]
=
'\0'
;
}
//사람 감지된 경우
if
(
strcmp
(
sensor_msg
,
"2"
)
==
0
){
strcpy
(
text1
,
"Human Detection"
);
}
//사람 감지되지 않은 경우
else
{
strcpy
(
text1
,
"No Human"
);
}
}
}
//카메라 수신
void
*
camera_thd
(){
char
buffer
[
16
];
while
(
1
){
read
(
camera_sock
,
buffer
,
sizeof
(
buffer
));
printf
(
"%s
\n
"
,
buffer
);
for
(
int
i
=
0
;
i
<
sizeof
(
text2
);
i
++
){
text2
[
i
]
=
'\0'
;
}
if
(
strncmp
(
"empty"
,
buffer
,
sizeof
(
buffer
))
==
0
){
strcpy
(
text2
,
"Empty"
);
}
//저장된 번호와 비교해서 일치하는 경우
else
if
(
strncmp
(
"39가2222"
,
buffer
,
sizeof
(
buffer
))
==
0
)
{
strcpy
(
text2
,
"Disabled Car"
);
}
//일치하지 않는 경우
else
{
strcpy
(
text2
,
"No Disabled Car"
);
}
}
}
//소켓 통신 에러 처리
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
){
printf
(
"Usage : %s <port>
\n
"
,
argv
[
0
]);
}
//소켓
serv_sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
if
(
serv_sock
==
-
1
)
error_handling
(
"socket() error"
);
memset
(
&
serv_addr
,
0
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
serv_addr
.
sin_port
=
htons
(
atoi
(
argv
[
1
]));
if
(
bind
(
serv_sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==
-
1
)
error_handling
(
"bind() error"
);
if
(
listen
(
serv_sock
,
5
)
==
-
1
)
error_handling
(
"listen() error"
);
clnt_addr_size
=
sizeof
(
camera_addr
);
camera_sock
=
accept
(
serv_sock
,
(
struct
sockaddr
*
)
&
camera_addr
,
&
clnt_addr_size
);
if
(
camera_sock
==
-
1
)
error_handling
(
"1_accept() error"
);
else
printf
(
"1 connect
\n
"
);
clnt_addr_size
=
sizeof
(
sensor_addr
);
sensor_sock
=
accept
(
serv_sock
,
(
struct
sockaddr
*
)
&
sensor_addr
,
&
clnt_addr_size
);
if
(
sensor_sock
==
-
1
)
error_handling
(
"2_accept() error"
);
else
printf
(
"2 connect
\n
"
);
if
(
wiringPiSetup
()
==
-
1
)
exit
(
1
);
fd
=
wiringPiI2CSetup
(
I2C_ADDR
);
lcd_init
();
// setup LCD
//스레드
pthread_t
camera
,
sensor
;
if
(
pthread_create
(
&
camera
,
NULL
,
camera_thd
,
NULL
)
<
0
)
{
perror
(
"thread create error: "
);
exit
(
0
);
}
if
(
pthread_create
(
&
camera
,
NULL
,
sensor_thd
,
NULL
)
<
0
)
{
perror
(
"thread create error: "
);
exit
(
0
);
}
while
(
1
)
{
lcd_init
();
printf
(
"1: %s
\n
"
,
text1
);
printf
(
"2: %s
\n
"
,
text2
);
lcd_write
(
LINE1_HUMAN
,
text1
);
lcd_write
(
LINE2_HP
,
text2
);
sleep
(
3
);
//3초마다 변경
}
}
void
lcd_write
(
int
line
,
const
char
*
s
){
//lcd line 지정
lcd_byte
(
line
,
LCD_CMD
);
//문자 출력
while
(
*
s
)
lcd_byte
(
*
(
s
++
),
LCD_CHR
);
}
//lcd에 문자열 또는 커서 이동 명령을 전송
void
lcd_byte
(
int
bits
,
int
mode
)
{
//상위 4비트, 하위 4비트로 나눠서
int
bits_high
;
int
bits_low
;
bits_high
=
mode
|
(
bits
&
0xF0
)
|
LCD_BACKLIGHT
;
bits_low
=
mode
|
((
bits
<<
4
)
&
0xF0
)
|
LCD_BACKLIGHT
;
wiringPiI2CReadReg8
(
fd
,
bits_high
);
lcd_toggle_enable
(
bits_high
);
wiringPiI2CReadReg8
(
fd
,
bits_low
);
lcd_toggle_enable
(
bits_low
);
}
//lcd enable pin 제어
void
lcd_toggle_enable
(
int
bits
)
{
delayMicroseconds
(
500
);
wiringPiI2CReadReg8
(
fd
,
(
bits
|
ENABLE
));
delayMicroseconds
(
500
);
wiringPiI2CReadReg8
(
fd
,
(
bits
&
~
ENABLE
));
delayMicroseconds
(
500
);
}
//lcd 초기화
void
lcd_init
()
{
lcd_byte
(
0x33
,
LCD_CMD
);
lcd_byte
(
0x32
,
LCD_CMD
);
lcd_byte
(
0x06
,
LCD_CMD
);
lcd_byte
(
0x0C
,
LCD_CMD
);
lcd_byte
(
0x28
,
LCD_CMD
);
lcd_byte
(
0x01
,
LCD_CMD
);
}
\ No newline at end of file
Loading