Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AADLib
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
TASTE
AADLib
Commits
e3d6b526
Commit
e3d6b526
authored
Jun 08, 2019
by
yoogx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
* Simplify code
parent
21dae839
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
64 deletions
+12
-64
examples/minepump_ba/simu.c
examples/minepump_ba/simu.c
+12
-64
No files found.
examples/minepump_ba/simu.c
View file @
e3d6b526
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#ifdef __SIMU_SOCKET__
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
/* __SIMU_SOCKET__ */
#include "simu.h"
#ifdef __SIMU_SOCKET__
FILE
*
sockin
;
#endif
/* __SIMU_SOCKET__ */
FILE
*
sockout
;
pthread_mutex_t
mutex_simu_minepump
;
int
CmdPump_Value
=
0
;
/* c = 0, pump is off, c = 1, pump is on */
int
WaterLevel_Value
=
50
;
/*****************************************************************************/
void
InitSimu
(
void
)
{
if
(
sockout
!=
NULL
)
return
;
#ifdef __SIMU_SOCKET__
int
sock
;
struct
sockaddr_in
address
;
if
((
sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
))
<
0
)
perror
(
"Can't open socket"
);
address
.
sin_family
=
AF_INET
;
/* Internet address family */
address
.
sin_addr
.
s_addr
=
inet_addr
(
"127.0.0.1"
);
/* Server IP address */
address
.
sin_port
=
htons
(
4242
);
/* Server port */
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
address
,
sizeof
(
address
))
<
0
)
perror
(
"Je ne peux me connecter au simulateur. Assurez-vous que minepump.tcl s'execute"
);
sockin
=
fdopen
(
sock
,
"r"
);
sockout
=
fdopen
(
sock
,
"w"
);
static
bool
init_done
=
false
;
#else
sockout
=
stdout
;
#endif
/* __SIMU_SOCKET__ */
pthread_mutex_init
(
&
mutex_simu_minepump
,
0
);
if
(
!
init_done
)
{
init_done
=
true
;
pthread_mutex_init
(
&
mutex_simu_minepump
,
0
);
}
}
/*****************************************************************************/
...
...
@@ -55,14 +26,8 @@ readhls (int *hls) {
int
b
;
pthread_mutex_lock
(
&
mutex_simu_minepump
);
#ifdef __SIMU_SOCKET__
fprintf
(
sockout
,
"HLS
\n
"
);
fflush
(
sockout
);
b
=
fgetc
(
sockin
);
#else
WaterLevel_Value
=
WaterLevel_Value
-
CmdPump_Value
*
4
+
2
;
b
=
(
WaterLevel_Value
>
100
)
?
1
:
0
;
#endif
/* __SIMU_SOCKET__ */
pthread_mutex_unlock
(
&
mutex_simu_minepump
);
*
hls
=
b
;
...
...
@@ -73,16 +38,9 @@ readlls(int *lls) {
InitSimu
();
int
b
;
pthread_mutex_lock
(
&
mutex_simu_minepump
);
#ifdef __SIMU_SOCKET__
fprintf
(
sockout
,
"LLS
\n
"
);
fflush
(
sockout
);
b
=
fgetc
(
sockin
);
#else
pthread_mutex_lock
(
&
mutex_simu_minepump
);
b
=
(
WaterLevel_Value
>
80
)
?
1
:
0
;
#endif
/* __SIMU_SOCKET__ */
pthread_mutex_unlock
(
&
mutex_simu_minepump
);
*
lls
=
b
;
}
...
...
@@ -92,25 +50,18 @@ readms(int *ms) {
InitSimu
();
static
int
b
=
50
;
static
int
b_increment
=
2
;
pthread_mutex_lock
(
&
mutex_simu_minepump
);
#ifdef __SIMU_SOCKET__
fprintf
(
sockout
,
"MS
\n
"
);
fflush
(
sockout
);
b
=
fgetc
(
sockin
);
#else
/* If there is no GUI, we simply emulate the methane sensor. The methane
gets up and down */
static
int
b_increment
=
2
;
b
+=
b_increment
;
if
(
b
==
100
||
b
==
40
)
b_increment
=
-
b_increment
;
#endif
/* __SIMU_SOCKET__ */
pthread_mutex_unlock
(
&
mutex_simu_minepump
);
*
ms
=
b
;
}
...
...
@@ -121,11 +72,8 @@ void cmdpump(int cmd) {
pthread_mutex_lock
(
&
mutex_simu_minepump
);
CmdPump_Value
=
cmd
?
1
:
0
;
#ifndef __SIMU_SOCKET__
fprintf
(
sockout
,
"WL = %3d
\t
"
,
WaterLevel_Value
);
#endif
/* __SIMU_SOCKET__ */
fprintf
(
sockout
,
"Pump %d
\n
"
,
CmdPump_Value
);
fflush
(
sockout
);
printf
(
"Pump %d
\n
"
,
CmdPump_Value
);
fflush
(
stdout
);
pthread_mutex_unlock
(
&
mutex_simu_minepump
);
}
...
...
@@ -136,7 +84,7 @@ void cmdalarm(int cmd) {
int
c
=
cmd
?
1
:
0
;
/* c = 0, alarm is off, c = 1, alarm is on */
pthread_mutex_lock
(
&
mutex_simu_minepump
);
fprintf
(
sockout
,
"Alarm %d
\t
"
,
c
);
fflush
(
s
ock
out
);
printf
(
"Alarm %d
\t
"
,
c
);
fflush
(
s
td
out
);
pthread_mutex_unlock
(
&
mutex_simu_minepump
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment