allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
LineInput (read X characters) routine. |
Posted: Thu Dec 19, 2019 12:08 pm |
|
|
For some testing, I ported an old LineInput (modeled after BASIC) I wrote for Arduino to the CCS code. It supports backspace (^h) and CANcel (^x, erase entire line). If you get to the end of the line, it echos the bell character (for terminals that support that).
I originally wrote this back in the late 80's or early 90's for an OS-9 (Unix-like OS) BBS. I dig it out every few years for some other project.
Usage is like this:
Code: | char buffer[80];
int bytesRead;
bytesRead = LineInput (buffer, 80);
|
Code: | /*---------------------------------------------------------------------------*/
/*
Simple Line Input routine.
By Allen C. Huffman ([email protected])
www.subethasoftware.com
*/
/*---------------------------------------------------------------------------*/
#define CR 13
#define BEL 7
#define BS 8
#define CAN 24
byte lineInput(char *buffer, size_t bufsize)
{
char ch;
byte len = 0;
boolean done;
// TODO: spinning cursor!
done = false;
while(!done)
{
//ledBlink();
//if (Serial.available()>0)
{
ch = getch();
switch(ch)
{
case CR:
//Serial.println();
printf ("\r\n");
buffer[len] = '\0';
done = true;
break;
case CAN:
//Serial.println(F("[CAN]"));
while (len > 0)
{
putc (BS);
putc (' ');
putc (BS);
len--;
}
//len = 0;
break;
case BS:
if (len>0)
{
//Serial.write(BS);
putc (BS);
//Serial.print(F(" "));
putc (' ');
//Serial.write(BS);
putc (BS);
len--;
}
break;
default:
// If there is room, store any printable characters in the line.
if (len<bufsize)
{
if ((ch>31) && (ch<127)) // isprint(ch) does not work.
{
// UPPERCASE it...
ch = toupper(ch);
//Serial.write(ch);
putc (ch);
buffer[len] = toupper(ch);
len++;
}
}
else
{
//Serial.write(BEL); // Overflow. Ring 'dat bell.
putc (BEL);
}
break;
} // end of switch(ch)
} // end of if (Serial.available()>0)
} // end of while(!done)
return len;
} // end of lineInput() |
_________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|