Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
duck
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jp7g21
duck
Commits
7a43fb3b
Commit
7a43fb3b
authored
3 years ago
by
jp7g21
Browse files
Options
Downloads
Patches
Plain Diff
Added debug mode to libduck
parent
a8feb213
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
libduck/libduck.c
+40
-15
40 additions, 15 deletions
libduck/libduck.c
libduck/libduck.h
+2
-0
2 additions, 0 deletions
libduck/libduck.h
libduck/libduck_debug.c
+0
-72
0 additions, 72 deletions
libduck/libduck_debug.c
with
42 additions
and
87 deletions
libduck/libduck.c
+
40
−
15
View file @
7a43fb3b
...
@@ -13,25 +13,35 @@
...
@@ -13,25 +13,35 @@
static
int
duckfd
=
0
;
static
int
duckfd
=
0
;
int
duck_debug_mode
=
0
;
void
open_duck
(
const
char
*
fname
)
void
open_duck
(
const
char
*
fname
)
{
{
duckfd
=
open
(
fname
,
O_NOCTTY
|
O_RDWR
);
if
(
duck_debug_mode
)
{
printf
(
"Opening duck
\n
"
);
if
(
duckfd
==
-
1
)
{
}
else
{
err
(
EXIT_FAILURE
,
"Failed to open '%s'"
,
fname
);
duckfd
=
open
(
fname
,
O_NOCTTY
|
O_RDWR
);
if
(
duckfd
==
-
1
)
{
err
(
EXIT_FAILURE
,
"Failed to open '%s'"
,
fname
);
}
}
}
}
}
void
configure_duck
(
void
)
void
configure_duck
(
void
)
{
{
struct
termios
tio
;
if
(
duck_debug_mode
)
{
if
(
tcgetattr
(
duckfd
,
&
tio
))
{
printf
(
"Configuring duck
\n
"
);
err
(
EXIT_FAILURE
,
"tcgetattr failed"
);
}
else
{
}
struct
termios
tio
;
cfsetospeed
(
&
tio
,
DUCK_BAUD
);
if
(
tcgetattr
(
duckfd
,
&
tio
))
{
err
(
EXIT_FAILURE
,
"tcgetattr failed"
);
if
(
tcsetattr
(
duckfd
,
TCSANOW
,
&
tio
))
{
}
err
(
EXIT_FAILURE
,
"tcsetattr failed"
);
cfsetospeed
(
&
tio
,
DUCK_BAUD
);
if
(
tcsetattr
(
duckfd
,
TCSANOW
,
&
tio
))
{
err
(
EXIT_FAILURE
,
"tcsetattr failed"
);
}
}
}
}
}
...
@@ -39,7 +49,11 @@ static int duck_printf(const char *fmt, ...)
...
@@ -39,7 +49,11 @@ static int duck_printf(const char *fmt, ...)
{
{
va_list
ap
;
va_list
ap
;
va_start
(
ap
,
fmt
);
va_start
(
ap
,
fmt
);
return
vdprintf
(
duckfd
,
fmt
,
ap
)
<
0
;
if
(
duck_debug_mode
)
{
return
vprintf
(
fmt
,
ap
);
}
else
{
return
vdprintf
(
duckfd
,
fmt
,
ap
)
<
0
;
}
}
}
static
int
validate_motor
(
int
motor
)
static
int
validate_motor
(
int
motor
)
...
@@ -47,10 +61,17 @@ static int validate_motor(int motor)
...
@@ -47,10 +61,17 @@ static int validate_motor(int motor)
return
motor
>=
1
&&
motor
<=
3
;
return
motor
>=
1
&&
motor
<=
3
;
}
}
static
int
wrap_angle
(
int
angle
)
{
while
(
angle
>
180
)
angle
-=
360
;
while
(
angle
<
-
180
)
angle
+=
360
;
return
angle
;
}
int
duck_set_position
(
int
motor
,
int
angle
)
int
duck_set_position
(
int
motor
,
int
angle
)
{
{
if
(
!
validate_motor
(
motor
))
return
-
1
;
if
(
!
validate_motor
(
motor
))
return
-
1
;
if
(
abs
(
angle
)
>
180
)
return
-
1
;
angle
=
wrap_angle
(
angle
)
;
return
duck_printf
(
"s %d %d
\n
"
,
motor
,
angle
);
return
duck_printf
(
"s %d %d
\n
"
,
motor
,
angle
);
}
}
...
@@ -68,5 +89,9 @@ int duck_set_velocity(int motor, int deg_per_sec)
...
@@ -68,5 +89,9 @@ int duck_set_velocity(int motor, int deg_per_sec)
void
close_duck
(
void
)
void
close_duck
(
void
)
{
{
close
(
duckfd
);
if
(
duck_debug_mode
)
{
printf
(
"Closing duck
\n
"
);
}
else
{
close
(
duckfd
);
}
}
}
This diff is collapsed.
Click to expand it.
libduck/libduck.h
+
2
−
0
View file @
7a43fb3b
...
@@ -9,6 +9,8 @@
...
@@ -9,6 +9,8 @@
# define DEFAULT_DUCK_FNAME "/dev/ttyUSB0"
# define DEFAULT_DUCK_FNAME "/dev/ttyUSB0"
#endif
/* DEFAULT_DUCK_FNAME */
#endif
/* DEFAULT_DUCK_FNAME */
extern
int
duck_debug_mode
;
void
open_duck
(
const
char
*
fname
);
void
open_duck
(
const
char
*
fname
);
void
configure_duck
(
void
);
void
configure_duck
(
void
);
int
duck_set_position
(
int
motor
,
int
angle
);
int
duck_set_position
(
int
motor
,
int
angle
);
...
...
This diff is collapsed.
Click to expand it.
libduck/libduck_debug.c
deleted
100644 → 0
+
0
−
72
View file @
a8feb213
#include
<stdio.h>
#include
<fcntl.h>
#include
<unistd.h>
#include
<err.h>
#include
<stdlib.h>
#include
<termios.h>
#include
<stdarg.h>
#include
"libduck.h"
#ifndef DUCK_BAUD
# define DUCK_BAUD B115200
#endif
/* DUCK_BAUD */
static
int
duckfd
=
0
;
void
open_duck
(
const
char
*
fname
)
{
duckfd
=
open
(
fname
,
O_NOCTTY
|
O_RDWR
);
if
(
duckfd
==
-
1
)
{
err
(
EXIT_FAILURE
,
"Failed to open '%s'"
,
fname
);
}
}
void
configure_duck
(
void
)
{
struct
termios
tio
;
if
(
tcgetattr
(
duckfd
,
&
tio
))
{
err
(
EXIT_FAILURE
,
"tcgetattr failed"
);
}
cfsetospeed
(
&
tio
,
DUCK_BAUD
);
if
(
tcsetattr
(
duckfd
,
TCSANOW
,
&
tio
))
{
err
(
EXIT_FAILURE
,
"tcsetattr failed"
);
}
}
static
int
duck_printf
(
const
char
*
fmt
,
...)
{
va_list
ap
;
va_start
(
ap
,
fmt
);
return
vprintf
(
fmt
,
ap
)
<
0
;
}
static
int
validate_motor
(
int
motor
)
{
return
motor
>=
1
&&
motor
<=
3
;
}
int
duck_set_position
(
int
motor
,
int
angle
)
{
if
(
!
validate_motor
(
motor
))
return
-
1
;
if
(
abs
(
angle
)
>
180
)
return
-
1
;
return
duck_printf
(
"s %d %d
\n
"
,
motor
,
angle
);
}
int
duck_delay
(
int
ms
)
{
return
duck_printf
(
"d %d
\n
"
,
ms
);
}
int
duck_set_velocity
(
int
motor
,
int
deg_per_sec
)
{
if
(
!
validate_motor
(
motor
))
return
-
1
;
if
(
abs
(
deg_per_sec
)
>
1000
)
return
-
1
;
return
duck_printf
(
"v %d %d
\n
"
,
motor
,
deg_per_sec
);
}
void
close_duck
(
void
)
{
close
(
duckfd
);
}
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