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
6aa159ba
Commit
6aa159ba
authored
3 years ago
by
jp7g21
Browse files
Options
Downloads
Patches
Plain Diff
Command reading code
parent
3a87c5ef
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
emb/command.c
+74
-0
74 additions, 0 deletions
emb/command.c
emb/command.h
+14
-0
14 additions, 0 deletions
emb/command.h
with
88 additions
and
0 deletions
emb/command.c
0 → 100644
+
74
−
0
View file @
6aa159ba
#include
<stdio.h>
#include
<stdlib.h>
#include
"command.h"
#define NUM_COMMANDS 20
/* Cyclic buffer; when comm_start == comm_end, length = 0 */
struct
command
command_buf
[
NUM_COMMANDS
];
uint16_t
comm_start
=
0
,
comm_end
=
0
;
/* Add y to x, wrapping around limit */
static
inline
void
add_wrap
(
uint16_t
*
x
,
uint16_t
y
,
uint16_t
limit
)
{
*
x
+=
y
;
if
(
*
x
>=
limit
)
*
x
%=
limit
;
while
(
*
x
<
0
)
*
x
+=
limit
;
}
enum
state
{
STATE_COMM
,
STATE_COMM_SPACE
,
STATE_ARG
};
/* Return 1 if new command ready */
uint8_t
uart_add_ch
(
char
c
)
{
static
enum
state
state
=
STATE_COMM
;
static
char
argbuf
[
20
];
/* Buffer for storing argument */
static
int
argbuf_ind
=
0
;
/* Index into argbuf of next character */
static
int
argnum
=
0
;
struct
command
*
curr
=
&
command_buf
[
comm_end
];
switch
(
state
)
{
case
STATE_COMM
:
curr
->
comm_ch
=
c
;
state
=
STATE_COMM_SPACE
;
break
;
case
STATE_COMM_SPACE
:
if
(
c
==
' '
)
{
state
=
STATE_ARG
;
}
else
{
state
=
STATE_COMM
;
add_wrap
(
&
comm_end
,
1
,
NUM_COMMANDS
);
}
break
;
case
STATE_ARG
:
if
(
c
==
' '
||
c
==
'\n'
)
{
argbuf
[
argbuf_ind
]
=
'\0'
;
/* Terminate string */
curr
->
arg
[
argnum
]
=
atoi
(
argbuf
);
argbuf_ind
=
0
;
if
(
c
==
' '
)
{
/* More arguments follow */
argnum
++
;
}
else
{
/* End of line */
argnum
=
0
;
state
=
STATE_COMM
;
add_wrap
(
&
comm_end
,
1
,
NUM_COMMANDS
);
}
}
else
{
if
(
argbuf_ind
<
19
)
argbuf
[
argbuf_ind
++
]
=
c
;
}
break
;
}
return
state
==
STATE_COMM
;
}
struct
command
*
get_command
(
void
)
{
if
(
comm_start
!=
comm_end
)
{
struct
command
*
ret
=
&
command_buf
[
comm_start
];
add_wrap
(
&
comm_start
,
1
,
NUM_COMMANDS
);
return
ret
;
}
else
{
return
NULL
;
}
}
This diff is collapsed.
Click to expand it.
emb/command.h
0 → 100644
+
14
−
0
View file @
6aa159ba
#ifndef _COMMAND_H
#define _COMMAND_H
#include
<stdint.h>
struct
command
{
char
comm_ch
;
/* Character of command (ie 's', 'v', 'd') */
int16_t
arg
[
2
];
};
uint8_t
uart_add_ch
(
char
c
);
struct
command
*
get_command
(
void
);
#endif
/* _COMMAND_H */
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