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

LineInput (read X characters) routine.

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

Joined: 17 Jun 2019
Posts: 537
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:

char buffer[80];
int bytesRead;

bytesRead = LineInput (buffer, 80);

Simple Line Input routine.
By Allen C. Huffman (

#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;
    //if (Serial.available()>0)
      ch = getch();
      case CR:
        printf ("\r\n");
        buffer[len] = '\0';
        done = true;

      case CAN:
        while (len > 0)
            putc (BS);
            putc (' ');
            putc (BS);
        //len = 0;

      case BS:
        if (len>0)
            putc (BS);
          //Serial.print(F(" "));
            putc (' ');
            putc (BS);

        // 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);
            putc (ch);
            buffer[len] = toupper(ch);
          //Serial.write(BEL); // Overflow. Ring 'dat bell.
          putc (BEL);
      } // 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)
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer. ?
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