Skip to content
Snippets Groups Projects
Commit 7fdfdfdd authored by burneykb's avatar burneykb
Browse files

CLI responds correctly to user input

parent 19f848b4
No related branches found
No related tags found
No related merge requests found
......@@ -34,5 +34,5 @@
# vrpn/build files
bt_poc/src/vrpn/build*
src/vrpn/build*
src/vrpn/pc_linux64/*
No preview for this file type
......@@ -34,6 +34,7 @@ void printVrpnData(struct ucart_vrpn_TrackerData *);
int connectToZybo();
void *handleQuadResponse();
void *handleCliInput();
int atomic_check(int*, pthread_mutex_t*);
static void cb(struct ucart_vrpn_TrackerData *);
......@@ -80,14 +81,12 @@ void *handleQuadResponse() {
unsigned char buffer[255];
while(keepRunning) {
printf("in quad response thread\n"); fflush(0);
// Clear the buffer and read the message from the socket (from the server)
memset(buffer, 0, 255);
// If there was an error reading from the socket, throw an error
if(read(zyboSocket, buffer, 255) <= 0) {
fprintf(stderr, "CLI QUAD: ERROR reading from quad.\n");
fflush(stderr);
continue;
}
......@@ -98,7 +97,7 @@ void *handleQuadResponse() {
//parse_packet(buffer, &respBuf, &metadata);
printf("CLI QUAD: %s\n", buffer);
fprintf(stderr, "CLI QUAD: %s\n", buffer);
}
pthread_exit(NULL);
......@@ -107,7 +106,6 @@ void *handleQuadResponse() {
void *handleCliInput() {
while(keepRunning)
{
printf("in cli input thread\n"); fflush(0);
char userCommand[CMD_MAX_LENGTH] = {};
fprintf(stderr, "$microcart> ");
......@@ -123,6 +121,9 @@ void *handleCliInput() {
continue;
}
if((userCommand[strlen(userCommand) - 1] == '\n') || (userCommand[strlen(userCommand) - 1] == '\r'))
userCommand[strlen(userCommand) - 1] = '\0';
pthread_mutex_lock(&cliInputMutex);
newCliInput = 1;
memcpy(commandBuf, &userCommand, CMD_MAX_LENGTH);
......@@ -148,11 +149,11 @@ int main(int argc, char **argv)
signal(SIGINT, killHandler);
// if ((zyboSocket = connectToZybo()) < 0)
// {
// perror("Error connecting to Zybo...");
// exit(1);
// }
if ((zyboSocket = connectToZybo()) < 0)
{
perror("Error connecting to Zybo...");
exit(1);
}
// create vrpnTracker instance
// tracker = ucart_vrpn_tracker_createInstance(TRACKER_IP);
......@@ -181,12 +182,21 @@ int main(int argc, char **argv)
// this function will be called whenever tracker receives data
// ucart_vrpn_tracker_addCallback(tracker, cb);
int updatePrompt = 0;
while(1)
{
char tmpRespBuf[1024];
char tmpCommandBuf[CMD_MAX_LENGTH];
if(updatePrompt)
{
fprintf(stderr, "$microcart> ");
updatePrompt = !updatePrompt;
}
//check for user input via cli
if(newCliInput)
if(atomic_check(&newCliInput, &cliInputMutex))
{
pthread_mutex_lock(&cliInputMutex);
newCliInput = 0;
......@@ -194,21 +204,24 @@ int main(int argc, char **argv)
pthread_mutex_unlock(&cliInputMutex);
// I can use printf becuase the command was gathered using fgets.
printf("\nINPUT FOUND via CLI: '%s'\n", tmpCommandBuf); fflush(0);
fprintf(stderr, "\rINPUT FOUND via CLI: '%s'\n", tmpCommandBuf);
updatePrompt = 1;
}
//check for update/response from quad
if(newQuadResponse)
if(atomic_check(&newQuadResponse, &quadResponseMutex))
{
pthread_mutex_lock(&quadResponseMutex);
newQuadResponse = 0;
memcpy(tmpRespBuf, respBuf, 1024);
pthread_mutex_unlock(&quadResponseMutex);
printf("\nINPUT FOUND via QUAD: '"); fflush(0);
fprintf(stderr, "\rINPUT FOUND via QUAD: '");
fwrite(tmpRespBuf, 1024, 1, stdout);
printf("'\n"); fflush(0);
fprintf(stderr, "'\n");
updatePrompt = 1;
}
usleep(10000);
}
......@@ -351,3 +364,10 @@ int connectToZybo() {
return sock;
}
}
int atomic_check(int* atomicFlag, pthread_mutex_t* mutex) {
pthread_mutex_lock(mutex);
int result = *atomicFlag;
pthread_mutex_unlock(mutex);
return result;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment