Commit 5e64af02 authored by Xoaquin Castrelo's avatar Xoaquin Castrelo
Browse files

Added UART command for displaying text.

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