Jump to content
43oh

Interested in opinions.


Recommended Posts

So I was writing some input code for serial UART a few days ago, and when I picked back up on it yesterday. I just started banging away, and wound up with the code listing below just before I went to bed. After looking through it today, I started to think about how much of this code is really necessary for most of the projects I will be working on. Then I started wondering How this could be done better. I have my own ideas, but was curious as to how others would implement a similar feature.

 

All comments welcome.

 

#include <msp430.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "input.h"

#define BUFFER_LENGTH 9

struct 
{
	unsigned int selected:1;
	unsigned int complete:1;
	unsigned int confirmed:1;
	unsigned int value;
} selection;

void get_selection(void);
void print_selection(void);
void test_selection(void);
void confirm_selection(void);
unsigned char getc(void);
void printf(char *, ...); 

int get_input(void)
{
	selection.selected = 0;
	selection.complete = 0;
	selection.confirmed = 0;
	selection.value = 0;

	get_selection();
	return selection.value;
}

void get_selection(void)
{
	while(!selection.confirmed)
	{
		print_selection();

		while(!selection.selected)
		{
			test_selection();
		}
		confirm_selection();
	}
}
/**
 * [test_selection description]
 *
 * Gets imput from user, and tests if input value is a valid selection.
 */
void test_selection(void)
{
	char buffer[BUFFER_LENGTH];
	char* buffer_ptr;
	unsigned char input;

	memset(buffer, '\0', BUFFER_LENGTH);
	buffer_ptr = &buffer[0];
	// Loop until buffer is NULL terminated.
	while (buffer_ptr)
	{
		// Test buffer length.
		if (strlen(buffer) < (BUFFER_LENGTH -1)) 
		{
			// Get input from user.
			input = getc();
			// Test for return keypress.	
			if (input == '\r')	
			{
				// Terminate buffer with NULL ( exits loop )
				buffer_ptr = '\0';
			}// Test for numeric or alpha numeric input.
			else if (isdigit(input) || isalpha(input))
			{
				// Place input into buffer.
				*buffer_ptr = input;
				// Echo value back to terminal so we can see what key we pressed.
				printf("%s", buffer_ptr);
				// Increment buffer pointer to next position.
				++buffer_ptr;
			}
		}
		else
		{
			// Buffer size limit reached, NULL terminate buffer (exits loop ).
			buffer_ptr = '\0';
			printf("\r\n");
		}
	}
	// Values from input valid. Move on to confirming input.
	if(strcmp(buffer, "1") == 0){selection.selected = 1;}
	// Invalid selection. Let user know then re-enter input loop.
	else{printf("Invalid selection: %s\r\n", buffer);}
}

void print_selection(void)
{
	printf("make a selection.\r\n");
	// TODO: Add options list.
}

void confirm_selection(void)
{
	printf("Press Y to continue, or N to cancel.\r\n");
	char input = getc();
	if (isalpha(input))
	{
		if (input == 'Y' || input == 'y')
		{
			selection.confirmed = 1;
		}
		else if (input == 'N' || input == 'n')
		{
			selection.selected = 0;
		}
	}
}

unsigned char getc(void)
{
    while (!(IFG2 & UCA0RXIFG));             
	return UCA0RXBUF;
}

 

wow those comments are annoying, lets see if i can fix that. Semi fixed, but no idea where that crazy indent is coming from.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...