Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
protect-my-parents-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
Lee
protect-my-parents-systemprogramming
Commits
cb521268
Commit
cb521268
authored
3 years ago
by
qbinee
Browse files
Options
Downloads
Patches
Plain Diff
init
parent
40120210
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
client2.c
+347
-0
347 additions, 0 deletions
client2.c
project_client.c
+545
-0
545 additions, 0 deletions
project_client.c
server.c
+474
-0
474 additions, 0 deletions
server.c
with
1366 additions
and
0 deletions
client2.c
0 → 100644
+
347
−
0
View file @
cb521268
#include
<sys/stat.h>
#include
<sys/types.h>
#include
<fcntl.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<string.h>
#include
<arpa/inet.h>
#include
<time.h>
#include
<sys/socket.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define POUT 17
#define PIN1 24
#define POUT1 23
#define BTNOUT 21
#define BTN 20
#define VALUE_MAX 45
#define BUFFER_MAX 45
#define DIRECTION_MAX 45
#define LED 0
static
int
PWMExport
(
int
pwmnum
)
{
char
buffer
[
BUFFER_MAX
];
int
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/pwm/pwmchip0/unexport"
,
O_WRONLY
);
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
)
{
fprintf
(
stderr
,
"Failed to open in export!
\n
"
);
return
-
1
;
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pwmnum
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
sleep
(
1
);
return
0
;
}
static
int
PWMEnable
(
int
pwmnum
)
{
static
const
char
s_unenable_str
[]
=
"0"
;
static
const
char
s_enable_str
[]
=
"1"
;
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
)
{
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
)
{
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
return
-
1
;
}
write
(
fd
,
s_enable_str
,
strlen
(
s_enable_str
));
close
(
fd
);
return
0
;
}
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
)
{
fprintf
(
stderr
,
"Faild to open in period!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
VALUE_MAX
,
"%d"
,
value
);
//fprintf(stderr, "byte: %s\n", s_values_str);
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
;
}
static
int
PWMWriteDutyCycle
(
int
pwmnum
,
int
value
)
{
char
s_values_str
[
VALUE_MAX
];
char
path
[
VALUE_MAX
];
int
fd
,
byte
;
#define VALUE_MAX 45
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm%d/duty_cycle"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Faild to open in duty_cycle!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
VALUE_MAX
,
"%d"
,
value
);
//fprintf(stderr, "byte: %s\n", s_values_str);
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
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
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!, pin: %d\n", pin);
return
(
-
1
);
}
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
fprintf
(
stderr
,
"Failed to set direction!
\n
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
0
;
}
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
"
);
close
(
fd
);
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
);
//fprintf(stderr,path);
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
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
(
atoi
(
value_str
));
}
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
);
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
static
void
initLED
(
int
pwmnum
)
{
PWMExport
(
pwmnum
);
PWMWritePeriod
(
pwmnum
,
20000000
);
PWMWriteDutyCycle
(
pwmnum
,
0
);
PWMEnable
(
pwmnum
);
}
static
void
TurnOnLed
(
int
pwmbtnnum
,
int
bright
)
{
PWMWriteDutyCycle
(
pwmbtnnum
,
bright
);
}
float
distance
;
int
sock
;
struct
sockaddr_in
serv_addr
;
char
buffer
[
1025
]
=
{
0
,};
char
btnEvent
[
15
]
=
"101000"
;
int
str_len
;
int
bright
;
void
setLED
()
{
if
(
sizeof
(
buffer
)
>
1
&&
buffer
[
1
]
>=
'0'
)
bright
=
(
buffer
[
1
]
-
'0'
)
*
750000
;
if
(
buffer
[
0
]
==
'4'
)
bright
=
1300000
;
if
(
bright
>
1400000
)
bright
=
0
;
TurnOnLed
(
LED
,
bright
);
printf
(
"led bright: %d
\n
"
,
bright
);
}
int
initConnection
(){
if
(
-
1
==
GPIOExport
(
POUT
))
return
(
2
);
if
(
-
1
==
GPIODirection
(
POUT
,
OUT
))
return
(
3
);
sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
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
(
"192.168.0.38"
);
serv_addr
.
sin_port
=
htons
(
8888
);
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==-
1
){
printf
(
"cant connect...
\n
"
);
return
-
1
;
}
return
1
;
}
void
clearBuffer
()
{
for
(
int
i
=
0
;
i
<
1025
;
i
++
)
buffer
[
i
]
=
0
;
}
int
main
()
{
initLED
(
LED
);
TurnOnLed
(
LED
,
bright
);
if
(
-
1
==
GPIOExport
(
BTNOUT
)
||
-
1
==
GPIOExport
(
BTN
))
return
(
2
);
if
(
-
1
==
GPIODirection
(
BTNOUT
,
OUT
)
||
-
1
==
GPIODirection
(
BTN
,
IN
))
return
(
2
);
while
(
1
)
{
usleep
(
1000000
);
if
(
initConnection
()
==
1
)
break
;
printf
(
"trying reconnect...
\n
"
);
}
printf
(
"socket connected
\n
"
);
int
prev
=
1
;
while
(
1
)
{
usleep
(
20000
);
if
(
-
1
==
GPIOWrite
(
BTNOUT
,
1
))
return
(
3
);
if
(
prev
==
1
&&
GPIORead
(
BTN
)
==
0
)
{
write
(
sock
,
btnEvent
,
sizeof
(
1024
));
}
prev
=
GPIORead
(
BTN
);
clearBuffer
();
int
ret
=
recv
(
sock
,
buffer
,
sizeof
(
buffer
),
MSG_DONTWAIT
);
if
(
ret
<
0
)
{}
else
{
setLED
();
printf
(
"buffer = %s, len = %d
\n
"
,
buffer
,
sizeof
(
buffer
));
}
}
printf
(
"Client ended
\n
"
);
close
(
sock
);
if
(
-
1
==
GPIOUnexport
(
POUT
))
return
(
4
);
return
(
0
);
}
This diff is collapsed.
Click to expand it.
project_client.c
0 → 100644
+
545
−
0
View file @
cb521268
#include
<sys/stat.h>
#include
<sys/types.h>
#include
<fcntl.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<string.h>
#include
<arpa/inet.h>
#include
<time.h>
#include
<sys/socket.h>
#include
<linux/spi/spidev.h>
#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0])
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define POUT 17
#define PIN1 24
#define POUT1 23
#define BTNOUT 21
#define BTN 20
#define VALUE_MAX 45
#define BUFFER_MAX 45
#define DIRECTION_MAX 45
#define LED 0
static
int
file_i2c
=
-
1
;
static
int
length
=
1
;
static
const
char
*
DEVICE
=
"/dev/spidev0.0"
;
static
uint8_t
MODE
=
SPI_MODE_0
;
static
uint8_t
BITS
=
8
;
static
uint32_t
CLOCK
=
1000000
;
static
uint16_t
DELAY
=
5
;
static
int
PWMExport
(
int
pwmnum
)
{
char
buffer
[
BUFFER_MAX
];
int
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/pwm/pwmchip0/unexport"
,
O_WRONLY
);
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
)
{
fprintf
(
stderr
,
"Failed to open in export!
\n
"
);
return
-
1
;
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pwmnum
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
sleep
(
1
);
return
0
;
}
static
int
PWMEnable
(
int
pwmnum
)
{
static
const
char
s_unenable_str
[]
=
"0"
;
static
const
char
s_enable_str
[]
=
"1"
;
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
)
{
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
)
{
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
return
-
1
;
}
write
(
fd
,
s_enable_str
,
strlen
(
s_enable_str
));
close
(
fd
);
return
0
;
}
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
)
{
fprintf
(
stderr
,
"Faild to open in period!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
VALUE_MAX
,
"%d"
,
value
);
//fprintf(stderr, "byte: %s\n", s_values_str);
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
;
}
static
int
PWMWriteDutyCycle
(
int
pwmnum
,
int
value
)
{
char
s_values_str
[
VALUE_MAX
];
char
path
[
VALUE_MAX
];
int
fd
,
byte
;
#define VALUE_MAX 45
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm%d/duty_cycle"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Faild to open in duty_cycle!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
VALUE_MAX
,
"%d"
,
value
);
//fprintf(stderr, "byte: %s\n", s_values_str);
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
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
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!, pin: %d\n", pin);
return
(
-
1
);
}
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
fprintf
(
stderr
,
"Failed to set direction!
\n
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
0
;
}
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
"
);
close
(
fd
);
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
);
//fprintf(stderr,path);
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
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
(
atoi
(
value_str
));
}
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
);
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
static
void
initLED
(
int
pwmnum
)
{
PWMExport
(
pwmnum
);
PWMWritePeriod
(
pwmnum
,
20000000
);
PWMWriteDutyCycle
(
pwmnum
,
0
);
PWMEnable
(
pwmnum
);
}
static
void
TurnOnLed
(
int
pwmbtnnum
,
int
bright
)
{
PWMWriteDutyCycle
(
pwmbtnnum
,
bright
);
}
float
distance
;
int
databus
=
-
1
;
void
*
light_led
(
void
*
data
)
{
char
*
thread_name
=
(
char
*
)
data
;
printf
(
"led: %s
\n
"
,
thread_name
);
int
bright
=
0
;
while
(
1
)
{
usleep
(
12347
);
if
(
databus
<
-
1
){
bright
=
0
;
TurnOnLed
(
LED
,
bright
);
continue
;
}
if
(
databus
<
0
){
continue
;
}
databus
+=
2
;
float
tmp
=
((
float
)
1
/
databus
);
bright
=
(
int
)(
tmp
*
22000
*
600
);
printf
(
"bright: %d
\n
"
,
bright
);
TurnOnLed
(
LED
,
bright
);
databus
=
-
1
;
}
}
clock_t
start_t
,
end_t
;
double
time2
;
void
initUltrawave
()
{
if
(
-
1
==
GPIOExport
(
POUT1
)
||
-
1
==
GPIOExport
(
PIN1
))
{
printf
(
"gpio export err
\n
"
);
exit
(
0
);
}
if
(
-
1
==
GPIODirection
(
POUT1
,
OUT
)
||
-
1
==
GPIODirection
(
PIN1
,
IN
))
{
printf
(
"gpio direction err
\n
"
);
exit
(
0
);
}
GPIOWrite
(
POUT1
,
0
);
}
float
distance
;
float
logicUltrawave
()
{
// working phase
if
(
-
1
==
GPIOWrite
(
POUT1
,
1
))
{
printf
(
"gpio write/trigger err
\n
"
);
exit
(
0
);
}
GPIOWrite
(
POUT1
,
0
);
while
(
GPIORead
(
PIN1
)
==
0
)
{
start_t
=
clock
();
}
while
(
GPIORead
(
PIN1
)
==
1
)
{
end_t
=
clock
();
}
time2
=
(
double
)(
end_t
-
start_t
)
/
CLOCKS_PER_SEC
;
distance
=
time2
*
17000
;
if
(
distance
>
300
)
distance
=
300
;
//printf("distance: %.2lfcm\n", distance);
return
distance
;
}
int
sock
;
struct
sockaddr_in
serv_addr
;
char
buffer
[
1025
]
=
{
0
,};
char
btnEvent
[
15
]
=
"110000"
;
int
str_len
;
int
initConnection
(){
if
(
-
1
==
GPIOExport
(
POUT
))
return
(
2
);
if
(
-
1
==
GPIODirection
(
POUT
,
OUT
))
return
(
3
);
sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
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
(
"192.168.0.38"
);
serv_addr
.
sin_port
=
htons
(
8888
);
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==-
1
){
printf
(
"cant connect...
\n
"
);
return
-
1
;
}
return
1
;
}
void
logic
()
{
str_len
=
read
(
sock
,
buffer
,
sizeof
(
buffer
));
if
(
str_len
==
-
1
)
error_handling
(
"read() error"
);
int
data
=
atoi
(
buffer
);
databus
=
data
;
printf
(
"received data: %d
\n
"
,
data
);
}
void
pushBtnEvent
()
{
btnEvent
[
1
]
=
'1'
-
btnEvent
[
1
]
+
'0'
;
}
void
makeNumPakcet
(
int
code
,
int
data
,
char
*
res
)
{
char
tmp
[
15
]
=
{
0
,};
int
l
=
0
;
while
(
data
>
0
)
{
int
tmpN
=
data
%
10
;
data
/=
10
;
tmp
[
l
++
]
=
tmpN
+
'0'
;
}
for
(
int
i
=
0
;
i
<
l
/
2
;
i
++
)
{
char
ttmp
=
tmp
[
l
-
i
-
1
];
tmp
[
l
-
i
-
1
]
=
tmp
[
i
];
tmp
[
i
]
=
ttmp
;
}
res
[
0
]
=
code
+
'0'
;
for
(
int
i
=
1
;
i
<=
l
;
i
++
)
{
res
[
i
]
=
tmp
[
i
-
1
];
}
//printf("tmpres = %s\n",res);
return
res
;
}
void
clearBuffer
()
{
for
(
int
i
=
0
;
i
<
1025
;
i
++
)
buffer
[
i
]
=
0
;
}
static
void
Write_Bytes
(
int
output
){
if
(
write
(
file_i2c
,
&
output
,
length
)
!=
length
){
printf
(
"Failed to write to the i2c bus.
\n
"
);
return
;
}
}
//SPI
static
int
prepare
(
int
fd
)
{
if
(
ioctl
(
fd
,
SPI_IOC_WR_MODE
,
&
MODE
)
==
-
1
)
{
perror
(
"Can`t set MODE"
);
return
-
1
;
}
if
(
ioctl
(
fd
,
SPI_IOC_WR_BITS_PER_WORD
,
&
BITS
)
==
-
1
)
{
perror
(
"Can`t set number of BITS"
);
return
-
1
;
}
if
(
ioctl
(
fd
,
SPI_IOC_WR_MAX_SPEED_HZ
,
&
CLOCK
)
==
-
1
)
{
perror
(
"Can`t set write CLOCK"
);
return
-
1
;
}
if
(
ioctl
(
fd
,
SPI_IOC_RD_MAX_SPEED_HZ
,
&
CLOCK
)
==
-
1
)
{
perror
(
"Can`t set read CLOCK"
);
return
-
1
;
}
return
0
;
}
uint8_t
control_bits_differential
(
uint8_t
channel
)
{
return
(
channel
&
7
)
<<
4
;
}
uint8_t
control_bits
(
uint8_t
channel
)
{
return
0x8
|
control_bits_differential
(
channel
);
}
int
readadc
(
int
fd
,
uint8_t
channel
)
{
uint8_t
tx
[]
=
{
1
,
control_bits
(
channel
),
0
};
uint8_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
,
};
if
(
ioctl
(
fd
,
SPI_IOC_MESSAGE
(
1
),
&
tr
)
==
1
)
{
perror
(
"IO Error"
);
abort
();
}
return
((
rx
[
1
]
<<
8
)
&
0x300
)
|
(
rx
[
2
]
&
0xFF
);
}
char
gasPacket
[
15
]
=
{
0
,};
int
gasV
=
0
;
int
main
()
{
gasPacket
[
0
]
=
'5'
;
if
(
-
1
==
GPIOExport
(
BTNOUT
)
||
-
1
==
GPIOExport
(
BTN
))
return
2
;
if
(
-
1
==
GPIODirection
(
BTNOUT
,
OUT
)
||
-
1
==
GPIODirection
(
BTN
,
IN
))
return
(
2
);
int
fd
=
open
(
DEVICE
,
O_RDWR
);
if
(
fd
<=
0
)
{
printf
(
"Device %s not found
\n
"
,
DEVICE
);
return
-
1
;
}
if
(
prepare
(
fd
)
==
-
1
)
return
-
1
;
initUltrawave
();
while
(
1
)
{
usleep
(
1000000
);
if
(
initConnection
()
==
1
)
break
;
printf
(
"trying reconnect...
\n
"
);
}
printf
(
"socket connected
\n
"
);
int
prev
=
1
;
while
(
1
)
{
usleep
(
20000
);
clearBuffer
();
if
(
-
1
==
GPIOWrite
(
BTNOUT
,
1
))
return
(
3
);
if
(
prev
==
1
&&
GPIORead
(
BTN
)
==
0
)
{
//Btn is Pushed.
write
(
sock
,
btnEvent
,
sizeof
(
1024
));
pushBtnEvent
();
}
gasV
=
readadc
(
fd
,
0
);
if
(
gasV
>
200
)
{
printf
(
"GasOverFlow = %d
\n
"
,
gasV
);
gasPacket
[
1
]
=
'1'
;
write
(
sock
,
gasPacket
,
15
);
}
int
ret
=
recv
(
sock
,
buffer
,
sizeof
(
buffer
),
MSG_DONTWAIT
);
if
(
ret
<
0
)
{
//printf("read wait\n");
}
else
{
int
data
=
(
int
)
logicUltrawave
();
if
(
buffer
[
2
]
==
'1'
)
{
//send ultrawave
char
packet
[
15
]
=
{
0
,};
makeNumPakcet
(
4
,
data
,
packet
);
write
(
sock
,
packet
,
1024
);
//printf("ultrapacket = %s\n",packet);
}
printf
(
"buffer = %s, len = %d
\n
"
,
buffer
,
sizeof
(
buffer
));
}
prev
=
GPIORead
(
BTN
);
}
printf
(
"Client ended
\n
"
);
close
(
sock
);
if
(
-
1
==
GPIOUnexport
(
POUT
))
return
(
4
);
return
(
0
);
}
This diff is collapsed.
Click to expand it.
server.c
0 → 100644
+
474
−
0
View file @
cb521268
#include
<stdio.h>
#include
<string.h>
//strlen
#include
<stdlib.h>
#include
<errno.h>
#include
<unistd.h>
//close
#include
<arpa/inet.h>
//close
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<sys/time.h>
//FD_SET, FD_ISSET, FD_ZERO macros
#include
<sys/stat.h>
#include
<fcntl.h>
#include
<string.h>
#include
<time.h>
#define TRUE 1
#define FALSE 0
#define PORT 8888
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define POUT 17
#define PIN1 24
#define POUT1 23
#define BTNOUT 21
#define BTN 20
#define VALUE_MAX 45
#define BUFFER_MAX 45
#define DIRECTION_MAX 45
#define LED 0
static
int
PWMExport
(
int
pwmnum
)
{
char
buffer
[
BUFFER_MAX
];
int
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/pwm/pwmchip0/unexport"
,
O_WRONLY
);
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
)
{
fprintf
(
stderr
,
"Failed to open in export!
\n
"
);
return
-
1
;
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pwmnum
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
sleep
(
1
);
return
0
;
}
static
int
PWMEnable
(
int
pwmnum
)
{
static
const
char
s_unenable_str
[]
=
"0"
;
static
const
char
s_enable_str
[]
=
"1"
;
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
)
{
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
)
{
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
return
-
1
;
}
write
(
fd
,
s_enable_str
,
strlen
(
s_enable_str
));
close
(
fd
);
return
0
;
}
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
)
{
fprintf
(
stderr
,
"Faild to open in period!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
VALUE_MAX
,
"%d"
,
value
);
//fprintf(stderr, "byte: %s\n", s_values_str);
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
;
}
static
int
PWMWriteDutyCycle
(
int
pwmnum
,
int
value
)
{
char
s_values_str
[
VALUE_MAX
];
char
path
[
VALUE_MAX
];
int
fd
,
byte
;
#define VALUE_MAX 45
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm%d/duty_cycle"
,
pwmnum
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Faild to open in duty_cycle!
\n
"
);
return
-
1
;
}
byte
=
snprintf
(
s_values_str
,
VALUE_MAX
,
"%d"
,
value
);
//fprintf(stderr, "byte: %s\n", s_values_str);
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
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
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!, pin: %d\n", pin);
return
(
-
1
);
}
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
fprintf
(
stderr
,
"Failed to set direction!
\n
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
0
;
}
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
"
);
close
(
fd
);
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
);
//fprintf(stderr,path);
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
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
(
atoi
(
value_str
));
}
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
);
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
static
void
initLED
(
int
pwmnum
)
{
PWMExport
(
pwmnum
);
PWMWritePeriod
(
pwmnum
,
20000000
);
PWMWriteDutyCycle
(
pwmnum
,
0
);
PWMEnable
(
pwmnum
);
}
static
void
TurnOnLed
(
int
pwmbtnnum
,
int
bright
)
{
PWMWriteDutyCycle
(
pwmbtnnum
,
bright
);
}
int
opt
=
TRUE
;
int
master_socket
,
addrlen
,
new_socket
,
client_socket
[
30
]
,
max_clients
=
30
,
activity
,
i
,
valread
,
sd
;
int
max_sd
;
struct
sockaddr_in
address
;
char
buffer
[
1025
];
//data buffer of 1K
int
bright
;
void
setLED
()
{
if
(
sizeof
(
buffer
)
>
1
&&
buffer
[
1
]
>=
'0'
)
bright
=
(
buffer
[
1
]
-
'0'
)
*
750000
;
if
(
bright
>
1400000
)
bright
=
0
;
TurnOnLed
(
LED
,
bright
);
printf
(
"led bright: %d
\n
"
,
bright
);
}
void
broadcast
(
char
*
buffer
,
int
valread
)
{
for
(
i
=
0
;
i
<
max_clients
;
i
++
){
sd
=
client_socket
[
i
];
buffer
[
valread
]
=
'\0'
;
send
(
sd
,
buffer
,
strlen
(
buffer
)
,
0
);
}
}
void
receiveCallback
(
int
sd
,
char
*
buffer
,
int
valread
)
{
if
(
buffer
[
0
]
==
'1'
){
printf
(
"broadcast: %s
\n
"
,
buffer
);
broadcast
(
buffer
,
valread
);
setLED
();
}
if
(
buffer
[
0
]
==
'4'
){
char
disStr
[
15
]
=
{
0
,};
for
(
int
i
=
1
;
i
<
15
;
i
++
)
disStr
[
i
-
1
]
=
buffer
[
i
];
int
dis
=
atoi
(
disStr
);
printf
(
"distance: %d
\n
"
,
dis
);
if
(
dis
>
5
)
{
TurnOnLed
(
LED
,
1300000
);
broadcast
(
buffer
,
valread
);
}
else
{
TurnOnLed
(
LED
,
0
);
}
}
if
(
buffer
[
0
]
==
'5'
){
broadcast
(
buffer
,
valread
);
setLED
();
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
initLED
(
LED
);
GPIOExport
(
5
);
GPIODirection
(
5
,
OUT
);
GPIOWrite
(
5
,
1
);
//set of socket descriptors
fd_set
readfds
;
//a message
char
*
message
=
"ECHO Daemon v1.0
\r\n
"
;
//initialise all client_socket[] to 0 so not checked
for
(
i
=
0
;
i
<
max_clients
;
i
++
)
{
client_socket
[
i
]
=
0
;
}
//create a master socket
if
(
(
master_socket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
0
)
{
perror
(
"socket failed"
);
exit
(
EXIT_FAILURE
);
}
//set master socket to allow multiple connections ,
//this is just a good habit, it will work without this
if
(
setsockopt
(
master_socket
,
SOL_SOCKET
,
SO_REUSEADDR
,
(
char
*
)
&
opt
,
sizeof
(
opt
))
<
0
)
{
perror
(
"setsockopt"
);
exit
(
EXIT_FAILURE
);
}
//type of socket created
address
.
sin_family
=
AF_INET
;
address
.
sin_addr
.
s_addr
=
INADDR_ANY
;
address
.
sin_port
=
htons
(
PORT
);
//bind the socket to localhost port 8888
if
(
bind
(
master_socket
,
(
struct
sockaddr
*
)
&
address
,
sizeof
(
address
))
<
0
)
{
perror
(
"bind failed"
);
exit
(
EXIT_FAILURE
);
}
printf
(
"Listener on port %d
\n
"
,
PORT
);
//try to specify maximum of 3 pending connections for the master socket
if
(
listen
(
master_socket
,
3
)
<
0
)
{
perror
(
"listen"
);
exit
(
EXIT_FAILURE
);
}
//accept the incoming connection
addrlen
=
sizeof
(
address
);
puts
(
"Waiting for connections ..."
);
while
(
TRUE
)
{
//clear the socket set
FD_ZERO
(
&
readfds
);
//add master socket to set
FD_SET
(
master_socket
,
&
readfds
);
max_sd
=
master_socket
;
//add child sockets to set
for
(
i
=
0
;
i
<
max_clients
;
i
++
)
{
//socket descriptor
sd
=
client_socket
[
i
];
//if valid socket descriptor then add to read list
if
(
sd
>
0
)
FD_SET
(
sd
,
&
readfds
);
//highest file descriptor number, need it for the select function
if
(
sd
>
max_sd
)
max_sd
=
sd
;
}
//wait for an activity on one of the sockets , timeout is NULL ,
//so wait indefinitely
activity
=
select
(
max_sd
+
1
,
&
readfds
,
NULL
,
NULL
,
NULL
);
if
((
activity
<
0
)
&&
(
errno
!=
EINTR
))
{
printf
(
"select error"
);
}
//If something happened on the master socket ,
//then its an incoming connection
if
(
FD_ISSET
(
master_socket
,
&
readfds
))
{
if
((
new_socket
=
accept
(
master_socket
,
(
struct
sockaddr
*
)
&
address
,
(
socklen_t
*
)
&
addrlen
))
<
0
)
{
perror
(
"accept"
);
exit
(
EXIT_FAILURE
);
}
//inform user of socket number - used in send and receive commands
printf
(
"New connection , socket fd is %d , ip is : %s , port : %d
\n
"
,
new_socket
,
inet_ntoa
(
address
.
sin_addr
)
,
ntohs
(
address
.
sin_port
));
//send new connection greeting message
if
(
send
(
new_socket
,
message
,
strlen
(
message
),
0
)
!=
strlen
(
message
)
)
{
perror
(
"send"
);
}
puts
(
"Welcome message sent successfully"
);
//add new socket to array of sockets
for
(
i
=
0
;
i
<
max_clients
;
i
++
)
{
//if position is empty
if
(
client_socket
[
i
]
==
0
)
{
client_socket
[
i
]
=
new_socket
;
printf
(
"Adding to list of sockets as %d
\n
"
,
i
);
break
;
}
}
}
//else its some IO operation on some other socket
for
(
i
=
0
;
i
<
max_clients
;
i
++
)
{
sd
=
client_socket
[
i
];
if
(
FD_ISSET
(
sd
,
&
readfds
))
{
//Check if it was for closing , and also read the
//incoming message
if
((
valread
=
read
(
sd
,
buffer
,
1024
))
==
0
)
{
getpeername
(
sd
,
(
struct
sockaddr
*
)
&
address
,
\
(
socklen_t
*
)
&
addrlen
);
printf
(
"Host disconnected , ip %s , port %d
\n
"
,
inet_ntoa
(
address
.
sin_addr
)
,
ntohs
(
address
.
sin_port
));
close
(
sd
);
client_socket
[
i
]
=
0
;
}
else
{
receiveCallback
(
sd
,
buffer
,
valread
);
}
}
}
}
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