CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

LineInput (read X characters) routine.

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 413
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

LineInput (read X characters) routine.
PostPosted: Thu Dec 19, 2019 12:08 pm     Reply with quote

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 (alsplace@pobox.com)
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) www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC programmer.
www.whywouldyouwanttodothat.com ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group