Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
F047_1_1997_BinaryStreet
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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
강한결
F047_1_1997_BinaryStreet
Commits
729c6d82
Commit
729c6d82
authored
3 years ago
by
Yoon Jieun
Browse files
Options
Downloads
Patches
Plain Diff
Fix typo
parent
41e6f8c6
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/client.c
+197
-161
197 additions, 161 deletions
src/client.c
src/traffic_system.c
+165
-149
165 additions, 149 deletions
src/traffic_system.c
with
362 additions
and
310 deletions
src/client.c
+
197
−
161
View file @
729c6d82
...
...
@@ -16,7 +16,6 @@
#include
<arpa/inet.h>
#include
<sys/socket.h>
#define IN 0
#define OUT 1
#define LOW 0
...
...
@@ -30,10 +29,8 @@
#define BUTTON 19
#define BUTTON2 26
#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0])
static
const
char
*
DEVICE
=
"/dev/spidev0.0"
;
static
u_int8_t
MODE
=
SPI_MODE_0
;
static
u_int8_t
BITS
=
8
;
...
...
@@ -42,27 +39,28 @@ static u_int16_t DELAY = 5;
int
mode_flag
=
0
;
int
alarm_flag
=
0
;
static
int
PWMExport
(
int
pwmnum
)
{
#define BUFFER_MAX 3
char
buffer
[
BUFFER_MAX
];
int
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/pwm/pwmchip0/unexport"
,
O_WRONLY
);
if
(
-
1
==
fd
){
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in unexport!
\n
"
);
return
-
1
;
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pwmnum
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
sleep
(
1
);
fd
=
open
(
"/sys/class/pwm/pwmchip0/export"
,
O_WRONLY
);
if
(
-
1
==
fd
){
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in export!
\n
"
);
return
-
1
;
}
...
...
@@ -80,23 +78,25 @@ static int PWMEnable(int pwmnum)
char
path
[
DIRECTION_MAX
];
int
fd
;
snprintf
(
path
,
DIRECTION_MAX
,
"/sys/class/pwm/pwmchip0/pwm%d/enable"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
return
-
1
;
}
write
(
fd
,
s_unenable_str
,
strlen
(
s_unenable_str
));
close
(
fd
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
return
-
1
;
}
write
(
fd
,
s_enable_str
,
strlen
(
s_enable_str
));
close
(
fd
);
return
0
;
...
...
@@ -107,22 +107,24 @@ static int PWMWritePeriod(int pwmnum, int value)
char
s_values_str
[
VALUE_MAX
];
char
path
[
VALUE_MAX
];
int
fd
,
byte
;
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm%d/period"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
){
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in period!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
10
,
"%d"
,
value
);
if
(
-
1
==
write
(
fd
,
s_values_str
,
byte
))
{
if
(
-
1
==
write
(
fd
,
s_values_str
,
byte
))
{
fprintf
(
stderr
,
"Failed to write value in period!
\n
"
);
close
(
fd
);
return
-
1
;
}
close
(
fd
);
return
0
;
}
...
...
@@ -132,277 +134,300 @@ static int PWMWriteDutyCycle(int pwmnum, int value)
char
path
[
VALUE_MAX
];
char
s_values_str
[
VALUE_MAX
];
int
fd
,
byte
;
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm%d/duty_cycle"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in duty_cycle!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
VALUE_MAX
,
"%d"
,
value
);
if
(
-
1
==
write
(
fd
,
s_values_str
,
byte
))
{
if
(
-
1
==
write
(
fd
,
s_values_str
,
byte
))
{
fprintf
(
stderr
,
"Failed to write value! in duty_cycle
\n
"
);
close
(
fd
);
return
-
1
;
}
close
(
fd
);
return
0
;
}
static
int
prepare
(
int
fd
)
{
ioctl
(
fd
,
SPI_IOC_WR_MODE
,
&
MODE
);
ioctl
(
fd
,
SPI_IOC_WR_BITS_PER_WORD
,
&
BITS
);
ioctl
(
fd
,
SPI_IOC_WR_MAX_SPEED_HZ
,
&
CLOCK
);
ioctl
(
fd
,
SPI_IOC_RD_MAX_SPEED_HZ
,
&
CLOCK
);
return
0
;
static
int
prepare
(
int
fd
)
{
ioctl
(
fd
,
SPI_IOC_WR_MODE
,
&
MODE
);
ioctl
(
fd
,
SPI_IOC_WR_BITS_PER_WORD
,
&
BITS
);
ioctl
(
fd
,
SPI_IOC_WR_MAX_SPEED_HZ
,
&
CLOCK
);
ioctl
(
fd
,
SPI_IOC_RD_MAX_SPEED_HZ
,
&
CLOCK
);
return
0
;
}
static
int
GPIOExport
(
int
pin
)
{
static
int
GPIOExport
(
int
pin
)
{
#define BUFFER_MAX 3
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/export"
,
O_WRONLY
);
if
(
-
1
==
fd
){
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"failed to open export for writing!
\n
"
);
return
(
-
1
);
return
(
-
1
);
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pin
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
return
(
0
);
return
(
0
);
}
static
int
GPIOUnexport
(
int
pin
)
{
static
int
GPIOUnexport
(
int
pin
)
{
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/unexport"
,
O_WRONLY
);
if
(
fd
==
-
1
)
{
if
(
fd
==
-
1
)
{
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
int
GPIODirection
(
int
pin
,
int
dir
)
{
static
const
char
s_directions_str
[]
=
"in
\0
out"
;
//char path[DIRECTION_MAX]="/sys/class/gpio/gpio24/direction";
char
path
[
DIRECTION_MAX
]
=
"/sys/class/gpio/gpio%d/direction"
;
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
(
fd
==
-
1
){
if
(
fd
==
-
1
)
{
fprintf
(
stderr
,
"Failed to open gpio direction for writing!
\n
"
);
return
(
-
1
);
return
(
-
1
);
}
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
fprintf
(
stderr
,
"Failed to set direction!
\n
"
);
return
(
-
1
);
return
(
-
1
);
}
close
(
fd
);
return
(
0
);
return
(
0
);
}
static
int
GPIOWrite
(
int
pin
,
int
value
)
{
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
(
fd
==
-
1
){
if
(
fd
==
-
1
)
{
fprintf
(
stderr
,
"Failed to open gpio value for writing!
\n
"
);
return
-
1
;
}
if
(
write
(
fd
,
&
s_values_str
[
LOW
==
value
?
0
:
1
],
1
)
==
-
1
){
if
(
write
(
fd
,
&
s_values_str
[
LOW
==
value
?
0
:
1
],
1
)
==
-
1
)
{
fprintf
(
stderr
,
"Failed to write balue!
\n
"
);
return
-
1
;
}
close
(
fd
);
return
0
;
}
static
int
GPIORead
(
int
pin
)
{
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
(
fd
==
-
1
){
if
(
fd
==
-
1
)
{
fprintf
(
stderr
,
"Failed to open gpio value for reading!
\n
"
);
return
-
1
;
}
if
(
read
(
fd
,
value_str
,
3
)
==
-
1
){
if
(
read
(
fd
,
value_str
,
3
)
==
-
1
)
{
fprintf
(
stderr
,
"Failed to read value!
\n
"
);
return
-
1
;
}
close
(
fd
);
return
(
atoi
(
value_str
));
}
close
(
fd
);
u_int8_t
control_bits_differential
(
u_int8_t
channel
){
return
(
channel
&
7
)
<<
4
;
return
(
atoi
(
value_str
));
}
u_int8_t
control_bits
(
u_int8_t
channel
)
{
return
0x8
|
control_bits_differential
(
channel
)
;
u_int8_t
control_bits_differential
(
u_int8_t
channel
)
{
return
(
channel
&
7
)
<<
4
;
}
int
readadc
(
int
fd
,
u_int8_t
channel
){
u_int8_t
tx
[]
=
{
1
,
control_bits
(
channel
),
0
};
u_int8_t
rx
[
3
];
struct
spi_ioc_transfer
tr
=
{
.
tx_buf
=
(
unsigned
long
)
tx
,
.
rx_buf
=
(
unsigned
long
)
rx
,
.
len
=
ARRAY_SIZE
(
tx
),
.
delay_usecs
=
DELAY
,
.
speed_hz
=
CLOCK
,
.
bits_per_word
=
BITS
,
};
ioctl
(
fd
,
SPI_IOC_MESSAGE
(
1
),
&
tr
);
return
((
rx
[
1
]
<<
8
)
&
0x300
)
|
(
rx
[
2
]
&
0xFF
);
u_int8_t
control_bits
(
u_int8_t
channel
)
{
return
0x8
|
control_bits_differential
(
channel
);
}
int
readadc
(
int
fd
,
u_int8_t
channel
)
{
u_int8_t
tx
[]
=
{
1
,
control_bits
(
channel
),
0
};
u_int8_t
rx
[
3
];
struct
spi_ioc_transfer
tr
=
{
.
tx_buf
=
(
unsigned
long
)
tx
,
.
rx_buf
=
(
unsigned
long
)
rx
,
.
len
=
ARRAY_SIZE
(
tx
),
.
delay_usecs
=
DELAY
,
.
speed_hz
=
CLOCK
,
.
bits_per_word
=
BITS
,
};
ioctl
(
fd
,
SPI_IOC_MESSAGE
(
1
),
&
tr
);
return
((
rx
[
1
]
<<
8
)
&
0x300
)
|
(
rx
[
2
]
&
0xFF
);
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
void
error_handling
(
char
*
message
)
{
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
void
signalingHandler
(
int
signo
)
{
void
signalingHandler
(
int
signo
)
{
exit
(
1
);
}
void
(
*
breakCapture
)(
int
);
void
*
socket_thread
(
void
*
data
){
char
**
argv
=
(
char
**
)
data
;
void
*
socket_thread
(
void
*
data
)
{
char
**
argv
=
(
char
**
)
data
;
int
sock
;
struct
sockaddr_in
serv_addr
;
char
msg
[
10
];
int
str_len
;
sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
if
(
sock
==
-
1
)
if
(
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
=
inet_addr
(
argv
[
1
]);
serv_addr
.
sin_port
=
htons
(
atoi
(
argv
[
2
]));
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==-
1
)
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==
-
1
)
error_handling
(
"connect() error"
);
while
(
1
){
while
(
1
)
{
snprintf
(
msg
,
10
,
"%d"
,
alarm_flag
);
write
(
sock
,
msg
,
sizeof
(
msg
));
write
(
sock
,
msg
,
sizeof
(
msg
));
usleep
(
500000
);
}
close
(
sock
);
}
void
led_init
()
{
void
led_init
()
{
//Enable GPIO pins
if
(
-
1
==
GPIOExport
(
LED_ONLY
)
||
-
1
==
GPIOExport
(
LED_RGB_R
)
||
-
1
==
GPIOExport
(
LED_RGB_B
)
||
-
1
==
GPIOExport
(
LED_RGB_G
))
if
(
-
1
==
GPIOExport
(
LED_ONLY
)
||
-
1
==
GPIOExport
(
LED_RGB_R
)
||
-
1
==
GPIOExport
(
LED_RGB_B
)
||
-
1
==
GPIOExport
(
LED_RGB_G
))
exit
(
0
);
//wait for writing to export file
usleep
(
10000
);
//Set GPIO directions
if
(
-
1
==
GPIODirection
(
LED_ONLY
,
OUT
)
||
-
1
==
GPIODirection
(
LED_RGB_R
,
OUT
)
||
-
1
==
GPIODirection
(
LED_RGB_B
,
OUT
)
||
-
1
==
GPIODirection
(
LED_RGB_G
,
OUT
))
if
(
-
1
==
GPIODirection
(
LED_ONLY
,
OUT
)
||
-
1
==
GPIODirection
(
LED_RGB_R
,
OUT
)
||
-
1
==
GPIODirection
(
LED_RGB_B
,
OUT
)
||
-
1
==
GPIODirection
(
LED_RGB_G
,
OUT
))
exit
(
0
);
}
void
button_init
()
{
void
button_init
()
{
//Enable GPIO pins
if
(
-
1
==
GPIOExport
(
BUTTON
)
||
-
1
==
GPIOExport
(
BUTTON2
))
if
(
-
1
==
GPIOExport
(
BUTTON
)
||
-
1
==
GPIOExport
(
BUTTON2
))
exit
(
1
);
//Set GPIO directions
if
(
-
1
==
GPIODirection
(
BUTTON
,
OUT
)
||
-
1
==
GPIODirection
(
BUTTON2
,
IN
))
if
(
-
1
==
GPIODirection
(
BUTTON
,
OUT
)
||
-
1
==
GPIODirection
(
BUTTON2
,
IN
))
exit
(
1
);
}
void
PWM_init
()
{
void
PWM_init
()
{
PWMExport
(
0
);
//pwm0 is gpio18
PWMWritePeriod
(
0
,
20000000
);
PWMWriteDutyCycle
(
0
,
0
);
PWMEnable
(
0
);
}
int
spi_init
()
{
int
spi_init
()
{
int
fd
=
open
(
DEVICE
,
O_RDWR
);
if
(
fd
<=
0
){
printf
(
"Device %s not found
\n
"
,
DEVICE
);
return
-
1
;
}
if
(
prepare
(
fd
)
==
-
1
)
{
return
-
1
;
}
if
(
fd
<=
0
)
{
printf
(
"Device %s not found
\n
"
,
DEVICE
);
return
-
1
;
}
if
(
prepare
(
fd
)
==
-
1
)
{
return
-
1
;
}
return
fd
;
}
void
return_resource
()
{
void
return_resource
()
{
//Disable GPIO pins
if
(
-
1
==
GPIOUnexport
(
BUTTON
)
||
-
1
==
GPIOUnexport
(
BUTTON2
))
if
(
-
1
==
GPIOUnexport
(
BUTTON
)
||
-
1
==
GPIOUnexport
(
BUTTON2
))
exit
(
1
);
if
(
-
1
==
GPIOUnexport
(
LED_RGB_B
)
||
-
1
==
GPIOUnexport
(
LED_RGB_R
)
||
-
1
==
GPIOUnexport
(
LED_RGB_G
)
||
-
1
==
GPIOUnexport
(
LED_ONLY
))
if
(
-
1
==
GPIOUnexport
(
LED_RGB_B
)
||
-
1
==
GPIOUnexport
(
LED_RGB_R
)
||
-
1
==
GPIOUnexport
(
LED_RGB_G
)
||
-
1
==
GPIOUnexport
(
LED_ONLY
))
exit
(
1
);
}
void
control_led
(
int
led
)
{
void
control_led
(
int
led
)
{
//init ultrawave trigger
GPIOWrite
(
LED_RGB_B
,
led
);
GPIOWrite
(
LED_RGB_R
,
led
);
GPIOWrite
(
LED_RGB_G
,
led
);
GPIOWrite
(
LED_ONLY
,
led
);
GPIOWrite
(
LED_ONLY
,
led
);
usleep
(
10000
);
}
void
*
safety_thread
(
void
*
data
)
{
void
*
safety_thread
(
void
*
data
)
{
int
fd
=
spi_init
();
int
moist
;
led_init
();
while
(
1
)
{
while
(
1
)
{
moist
=
readadc
(
fd
,
0
);
printf
(
"moist : %d
\n
"
,
moist
);
alarm_flag
=
moist
>
350
?
1
:
0
;
...
...
@@ -413,71 +438,82 @@ void * safety_thread(void*data) {
exit
(
0
);
}
void
*
button_thread
(
void
*
data
){
void
*
button_thread
(
void
*
data
)
{
int
state
=
0
;
int
button_state
=
1
;
int
light
=
0
;
button_init
();
while
(
1
)
{
while
(
1
)
{
if
(
-
1
==
GPIOWrite
(
BUTTON
,
1
))
exit
(
1
);
while
(
GPIORead
(
BUTTON2
)
==
button_state
)
continue
;
while
(
GPIORead
(
BUTTON2
)
==
button_state
)
continue
;
button_state
^=
1
;
if
(
button_state
)
{
if
(
button_state
)
{
printf
(
"fliped!
\n
"
);
mode_flag
^=
1
;
}
usleep
(
10000
);
}
exit
(
0
);
}
void
*
control_pwm_thread
(){
void
*
control_pwm_thread
()
{
PWM_init
();
while
(
1
)
{
if
(
mode_flag
){
for
(
int
i
=
1
;
i
<=
10
;
i
++
){
PWMWriteDutyCycle
(
0
,
i
*
100000
+
1000000
);
usleep
(
100000
*
(
2
.
0
/
i
));
if
(
!
mode_flag
)
{
while
(
1
)
{
if
(
mode_flag
)
{
for
(
int
i
=
1
;
i
<=
10
;
i
++
)
{
PWMWriteDutyCycle
(
0
,
i
*
100000
+
1000000
);
usleep
(
100000
*
(
2
.
0
/
i
));
if
(
!
mode_flag
)
{
PWMWriteDutyCycle
(
0
,
1500000
);
break
;
}
}
for
(
int
i
=
10
;
i
>
0
;
i
--
){
PWMWriteDutyCycle
(
0
,
i
*
100000
+
1000000
);
usleep
(
100000
*
(
2
.
0
/
i
));
if
(
!
mode_flag
)
{
for
(
int
i
=
10
;
i
>
0
;
i
--
)
{
PWMWriteDutyCycle
(
0
,
i
*
100000
+
1000000
);
usleep
(
100000
*
(
2
.
0
/
i
));
if
(
!
mode_flag
)
{
PWMWriteDutyCycle
(
0
,
1500000
);
break
;
}
}
}
PWMWriteDutyCycle
(
0
,
0
);
}
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
main
(
int
argc
,
char
*
argv
[])
{
// thread init
pthread_t
p_thread
[
5
];
int
thr_id
,
status
;
char
p1
[]
=
"spi"
;
char
p3
[]
=
"button"
;
char
p4
[]
=
"pwm"
;
if
(
argc
!=
3
){
printf
(
"Usage : %s <IP> <port>
\n
"
,
argv
[
0
]);
if
(
argc
!=
3
)
{
printf
(
"Usage : %s <IP> <port>
\n
"
,
argv
[
0
]);
exit
(
1
);
}
thr_id
=
pthread_create
(
&
p_thread
[
0
],
NULL
,
safety_thread
,
NULL
);
thr_id
=
pthread_create
(
&
p_thread
[
1
],
NULL
,
socket_thread
,
(
void
*
)
argv
);
thr_id
=
pthread_create
(
&
p_thread
[
1
],
NULL
,
socket_thread
,
(
void
*
)
argv
);
thr_id
=
pthread_create
(
&
p_thread
[
2
],
NULL
,
button_thread
,
NULL
);
thr_id
=
pthread_create
(
&
p_thread
[
3
],
NULL
,
control_pwm_thread
,
NULL
);
...
...
This diff is collapsed.
Click to expand it.
src/traffic_system.c
+
165
−
149
View file @
729c6d82
...
...
@@ -16,7 +16,6 @@
#include
<arpa/inet.h>
#include
<sys/socket.h>
#define IN 0
#define OUT 1
#define LOW 0
...
...
@@ -27,281 +26,298 @@
#define VALUE_MAX 256
#define DIRECTION_MAX 45
#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0])
static
const
char
*
DEVICE
=
"/dev/spidev0.0"
;
static
u_int8_t
MODE
=
SPI_MODE_0
;
static
u_int8_t
BITS
=
8
;
static
u_int32_t
CLOCK
=
1000000
;
static
u_int16_t
DELAY
=
5
;
int
mode_flag
=
0
;
int
op
;
static
int
prepare
(
int
fd
)
{
ioctl
(
fd
,
SPI_IOC_WR_MODE
,
&
MODE
);
ioctl
(
fd
,
SPI_IOC_WR_BITS_PER_WORD
,
&
BITS
);
ioctl
(
fd
,
SPI_IOC_WR_MAX_SPEED_HZ
,
&
CLOCK
);
ioctl
(
fd
,
SPI_IOC_RD_MAX_SPEED_HZ
,
&
CLOCK
);
return
0
;
static
int
prepare
(
int
fd
)
{
ioctl
(
fd
,
SPI_IOC_WR_MODE
,
&
MODE
);
ioctl
(
fd
,
SPI_IOC_WR_BITS_PER_WORD
,
&
BITS
);
ioctl
(
fd
,
SPI_IOC_WR_MAX_SPEED_HZ
,
&
CLOCK
);
ioctl
(
fd
,
SPI_IOC_RD_MAX_SPEED_HZ
,
&
CLOCK
);
return
0
;
}
static
int
GPIOExport
(
int
pin
)
{
static
int
GPIOExport
(
int
pin
)
{
#define BUFFER_MAX 3
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/export"
,
O_WRONLY
);
if
(
-
1
==
fd
){
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"failed to open export for writing!
\n
"
);
return
(
-
1
);
return
(
-
1
);
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pin
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
return
(
0
);
return
(
0
);
}
static
int
GPIOUnexport
(
int
pin
)
{
static
int
GPIOUnexport
(
int
pin
)
{
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/unexport"
,
O_WRONLY
);
if
(
fd
==
-
1
)
{
if
(
fd
==
-
1
)
{
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
int
GPIODirection
(
int
pin
,
int
dir
)
{
static
const
char
s_directions_str
[]
=
"in
\0
out"
;
//char path[DIRECTION_MAX]="/sys/class/gpio/gpio24/direction";
char
path
[
DIRECTION_MAX
]
=
"/sys/class/gpio/gpio%d/direction"
;
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
(
fd
==
-
1
){
if
(
fd
==
-
1
)
{
fprintf
(
stderr
,
"Failed to open gpio direction for writing!
\n
"
);
return
(
-
1
);
return
(
-
1
);
}
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
fprintf
(
stderr
,
"Failed to set direction!
\n
"
);
return
(
-
1
);
return
(
-
1
);
}
close
(
fd
);
return
(
0
);
return
(
0
);
}
static
int
GPIOWrite
(
int
pin
,
int
value
)
{
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
(
fd
==
-
1
){
if
(
fd
==
-
1
)
{
fprintf
(
stderr
,
"Failed to open gpio value for writing!
\n
"
);
return
-
1
;
}
if
(
write
(
fd
,
&
s_values_str
[
LOW
==
value
?
0
:
1
],
1
)
==
-
1
){
if
(
write
(
fd
,
&
s_values_str
[
LOW
==
value
?
0
:
1
],
1
)
==
-
1
)
{
fprintf
(
stderr
,
"Failed to write balue!
\n
"
);
return
-
1
;
}
close
(
fd
);
return
0
;
}
static
int
GPIORead
(
int
pin
)
{
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
(
fd
==
-
1
){
if
(
fd
==
-
1
)
{
fprintf
(
stderr
,
"Failed to open gpio value for reading!
\n
"
);
return
-
1
;
}
if
(
read
(
fd
,
value_str
,
3
)
==
-
1
){
if
(
read
(
fd
,
value_str
,
3
)
==
-
1
)
{
fprintf
(
stderr
,
"Failed to read value!
\n
"
);
return
-
1
;
}
close
(
fd
);
return
(
atoi
(
value_str
));
}
close
(
fd
);
u_int8_t
control_bits_differential
(
u_int8_t
channel
){
return
(
channel
&
7
)
<<
4
;
return
(
atoi
(
value_str
));
}
u_int8_t
control_bits
(
u_int8_t
channel
)
{
return
0x8
|
control_bits_differential
(
channel
)
;
u_int8_t
control_bits_differential
(
u_int8_t
channel
)
{
return
(
channel
&
7
)
<<
4
;
}
int
readadc
(
int
fd
,
u_int8_t
channel
){
u_int8_t
tx
[]
=
{
1
,
control_bits
(
channel
),
0
};
u_int8_t
rx
[
3
];
struct
spi_ioc_transfer
tr
=
{
.
tx_buf
=
(
unsigned
long
)
tx
,
.
rx_buf
=
(
unsigned
long
)
rx
,
.
len
=
ARRAY_SIZE
(
tx
),
.
delay_usecs
=
DELAY
,
.
speed_hz
=
CLOCK
,
.
bits_per_word
=
BITS
,
};
ioctl
(
fd
,
SPI_IOC_MESSAGE
(
1
),
&
tr
);
return
((
rx
[
1
]
<<
8
)
&
0x300
)
|
(
rx
[
2
]
&
0xFF
);
u_int8_t
control_bits
(
u_int8_t
channel
)
{
return
0x8
|
control_bits_differential
(
channel
);
}
int
readadc
(
int
fd
,
u_int8_t
channel
)
{
u_int8_t
tx
[]
=
{
1
,
control_bits
(
channel
),
0
};
u_int8_t
rx
[
3
];
struct
spi_ioc_transfer
tr
=
{
.
tx_buf
=
(
unsigned
long
)
tx
,
.
rx_buf
=
(
unsigned
long
)
rx
,
.
len
=
ARRAY_SIZE
(
tx
),
.
delay_usecs
=
DELAY
,
.
speed_hz
=
CLOCK
,
.
bits_per_word
=
BITS
,
};
ioctl
(
fd
,
SPI_IOC_MESSAGE
(
1
),
&
tr
);
return
((
rx
[
1
]
<<
8
)
&
0x300
)
|
(
rx
[
2
]
&
0xFF
);
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
void
error_handling
(
char
*
message
)
{
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
void
signalingHandler
(
int
signo
)
{
void
signalingHandler
(
int
signo
)
{
exit
(
1
);
}
void
(
*
breakCapture
)(
int
);
void
traffic_init
()
{
//Enable GPIO pins
if
(
-
1
==
GPIOExport
(
POUTR
)
||
-
1
==
GPIOExport
(
POUTY
)
||
-
1
==
GPIOExport
(
POUTG
))
void
traffic_init
()
{
//Enable GPIO pins
if
(
-
1
==
GPIOExport
(
POUTR
)
||
-
1
==
GPIOExport
(
POUTY
)
||
-
1
==
GPIOExport
(
POUTG
))
exit
(
0
);
//wait for writing to export file
usleep
(
100000
);
//Set GPIO directions
if
(
-
1
==
GPIODirection
(
POUTR
,
OUT
)
||
-
1
==
GPIODirection
(
POUTY
,
OUT
)
||
-
1
==
GPIODirection
(
POUTG
,
OUT
))
if
(
-
1
==
GPIODirection
(
POUTR
,
OUT
)
||
-
1
==
GPIODirection
(
POUTY
,
OUT
)
||
-
1
==
GPIODirection
(
POUTG
,
OUT
))
exit
(
0
);
}
void
return_resource
()
{
void
return_resource
()
{
//Disable GPIO pins
if
(
-
1
==
GPIOUnexport
(
POUTR
)
||
-
1
==
GPIOUnexport
(
POUTY
)
||
-
1
==
GPIOUnexport
(
POUTG
))
if
(
-
1
==
GPIOUnexport
(
POUTR
)
||
-
1
==
GPIOUnexport
(
POUTY
)
||
-
1
==
GPIOUnexport
(
POUTG
))
exit
(
1
);
}
void
*
control_traffic_thread
(
void
*
args
)
{
void
*
control_traffic_thread
(
void
*
args
)
{
//init ultrawave trigger
GPIOWrite
(
POUTR
,
0
);
GPIOWrite
(
POUTY
,
0
);
GPIOWrite
(
POUTG
,
0
);
usleep
(
10000
);
//start
int
green
=
3000000
;
int
yellow
=
800000
;
int
red
=
3000000
;
if
(
op
==
1
){
green
=
10000000
;
}
if
(
op
==
2
){
red
=
10000000
;
}
int
green
=
3000000
;
int
yellow
=
800000
;
int
red
=
3000000
;
if
(
op
==
1
)
{
green
=
10000000
;
}
if
(
op
==
2
)
{
red
=
10000000
;
}
printf
(
"data: %d
\n
"
,
op
);
GPIOWrite
(
POUTG
,
1
);
GPIOWrite
(
POUTR
,
0
);
printf
(
"CarGreen
\n
"
);
usleep
(
green
);
GPIOWrite
(
POUTG
,
0
);
GPIOWrite
(
POUTY
,
1
);
printf
(
"CarYellow
\n
"
);
usleep
(
yellow
);
GPIOWrite
(
POUTY
,
0
);
GPIOWrite
(
POUTR
,
1
);
printf
(
"CarRed
\n
"
);
usleep
(
red
);
pthread_exit
(
NULL
);
GPIOWrite
(
POUTG
,
1
);
GPIOWrite
(
POUTR
,
0
);
printf
(
"CarGreen
\n
"
);
usleep
(
green
);
GPIOWrite
(
POUTG
,
0
);
GPIOWrite
(
POUTY
,
1
);
printf
(
"CarYellow
\n
"
);
usleep
(
yellow
);
GPIOWrite
(
POUTY
,
0
);
GPIOWrite
(
POUTR
,
1
);
printf
(
"CarRed
\n
"
);
usleep
(
red
);
pthread_exit
(
NULL
);
}
int
spi_init
()
{
int
spi_init
()
{
int
fd
=
open
(
DEVICE
,
O_RDWR
);
if
(
fd
<=
0
){
printf
(
"Device %s not found
\n
"
,
DEVICE
);
return
-
1
;
}
if
(
prepare
(
fd
)
==
-
1
)
{
return
-
1
;
}
if
(
fd
<=
0
)
{
printf
(
"Device %s not found
\n
"
,
DEVICE
);
return
-
1
;
}
if
(
prepare
(
fd
)
==
-
1
)
{
return
-
1
;
}
return
fd
;
}
int
main
(
void
)
{
int
car_pressure
=
0
;
int
pedestrian_pressure
=
0
;
int
main
(
void
)
{
// adc read code in
traffic_init
();
int
fd
=
spi_init
();
// adc read code in
traffic_init
();
int
fd
=
spi_init
();
pthread_t
p_thread
[
2
];
pthread_t
p_thread
[
2
];
int
thr_id
;
int
status
;
int
count
=
6
;
int
diff
,
car
,
prede
;
int
count
=
6
;
int
diff
,
car
,
pede
;
while
(
1
)
{
while
(
1
){
thr_id
=
pthread_create
(
&
p_thread
[
0
],
NULL
,
control_traffic_thread
,
NULL
);
car
=
prede
=
0
;
for
(
int
i
=
0
;
i
<
count
;
i
++
){
diff
=
readadc
(
fd
,
1
)
-
readadc
(
fd
,
0
);
printf
(
"diff : %d
\n
"
,
diff
);
if
(
diff
>
300
)
car
++
;
else
if
(
diff
<
-
300
)
prede
++
;
usleep
(
1000000
);
}
if
((
car
>=
2
||
prede
>=
2
)
&&
!
(
car
>=
2
&&
prede
>=
2
)){
printf
(
"in
\n
"
);
op
=
car
>
prede
?
1
:
2
;
count
=
13
;
}
else
{
car
=
pede
=
0
;
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
diff
=
readadc
(
fd
,
1
)
-
readadc
(
fd
,
0
);
printf
(
"diff : %d
\n
"
,
diff
);
if
(
diff
>
300
)
car
++
;
else
if
(
diff
<
-
300
)
pede
++
;
usleep
(
1000000
);
}
if
((
car
>=
2
||
pede
>=
2
)
&&
!
(
car
>=
2
&&
pede
>=
2
))
{
printf
(
"in
\n
"
);
op
=
car
>
pede
?
1
:
2
;
count
=
13
;
}
else
{
op
=
0
;
count
=
6
;
count
=
6
;
}
pthread_join
(
p_thread
[
0
],
(
void
**
)
&
status
);
}
close
(
fd
);
return_resource
();
close
(
fd
);
return_resource
();
return
0
;
}
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