Skip to content
Snippets Groups Projects
Commit 5e64af02 authored by Xoaquin Castrelo's avatar Xoaquin Castrelo
Browse files

Added UART command for displaying text.

parent eb1c6306
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,9 @@
struct command command_buf[NUM_COMMANDS];
uint16_t comm_start = 0, comm_end = 0;
char comm_str[33];
uint8_t comm_str_ind = 0;
/* Add y to x, wrapping around limit */
static inline void add_wrap(uint16_t *x, uint16_t y, uint16_t limit)
{
......@@ -17,7 +20,7 @@ static inline void add_wrap(uint16_t *x, uint16_t y, uint16_t limit)
}
enum state {
STATE_COMM, STATE_COMM_SPACE, STATE_ARG
STATE_COMM, STATE_COMM_SPACE, STATE_ARG, STATE_STRING
};
/* Return 1 if new command ready */
......@@ -36,7 +39,15 @@ uint8_t uart_add_ch(char c)
break;
case STATE_COMM_SPACE:
if (c == ' ') {
if (curr->comm_ch == 't')
{
comm_str_ind = 0;
state = STATE_STRING;
}
else
{
state = STATE_ARG;
}
} else {
state = STATE_COMM;
add_wrap(&comm_end, 1, NUM_COMMANDS);
......@@ -58,6 +69,18 @@ uint8_t uart_add_ch(char c)
if (argbuf_ind < 19) argbuf[argbuf_ind++] = c;
}
break;
case STATE_STRING:
if (comm_str_ind == sizeof(comm_str) - 1 || c == '\n')
{
comm_str[comm_str_ind] = '\0';
state = STATE_COMM;
add_wrap(&comm_end, 1, NUM_COMMANDS);
}
else
{
comm_str[comm_str_ind++] = c;
}
break;
}
return state == STATE_COMM;
}
......
......@@ -14,6 +14,8 @@ extern "C" {
int16_t arg[NUM_ARGS];
};
extern char comm_str[33];
uint8_t uart_add_ch(char c);
struct command *get_command(void);
......
#include "servos.hpp"
#include "command.h"
#include "time.hpp"
#include "display.hpp"
#include <avr/interrupt.h>
#include <string.h>
enum State
{
......@@ -17,13 +19,15 @@ static uint32_t velocitiesEndMillis[NUM_SERVOS];
static int16_t startAngles[NUM_SERVOS];
static uint32_t startMillis[NUM_SERVOS];
static char displayString[33];
void parseCommand(command *comm)
{
uint32_t currentMillis = getCurrentMillis();
switch (comm->comm_ch)
{
case 's':
case 's': // set
if (0 <= comm->arg[0] - 1 && comm->arg[0] - 1 < NUM_SERVOS)
{
setServoAngle(comm->arg[0] - 1, comm->arg[1] * 60);
......@@ -31,7 +35,7 @@ void parseCommand(command *comm)
servoVelocities[comm->arg[0] - 1] = 0;
}
break;
case 'v':
case 'v': // velocity
if (0 <= comm->arg[0] - 1 && comm->arg[0] - 1 < NUM_SERVOS)
{
servoVelocities[comm->arg[0] - 1] = comm->arg[1];
......@@ -40,10 +44,23 @@ void parseCommand(command *comm)
startMillis[comm->arg[0] - 1] = currentMillis;
}
break;
case 'd':
case 'd': // delay
delayEnd = getCurrentMillis() + comm->arg[0];
state = STATE_DELAY;
break;
case 't': // text
clearDisplay();
setDisplayCursor(0, 0);
uint8_t i = 0;
while (char c = displayString[i++])
{
writeDisplay(c);
if (i == 16)
setDisplayCursor(0, 1);
}
break;
}
}
......@@ -58,6 +75,7 @@ void updateState()
cli();
command *tmp = get_command();
command comm = *tmp;
memcpy(displayString, comm_str, sizeof(displayString));
sei();
if (tmp != nullptr)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment