NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » String manipulation

October 12, 2011
by claesz
claesz's Avatar

This is really embarrassing, but C is not my strong side and I just got my NK the other day and though I'd test it out by making a scrolling text through string manipulation. I know there are many examples of LCD scrollers on this forum that are all much better and more efficient, and this was just meant as a bit of practice for me. The thing works (though I must be overwriting some null str termination somewhere as it from time to time it will mess itself up).

Now, my though was take a text string. Start by adding screen width number of spaces + text string. Only spaces will show. Next step add width-1 number of spaces + text. One letter will show, etc, etc. When number of spaces = 0, just write text+1, text+2 etc. If strlen less than screen width add spaces to fill up.

The principle works of course, all though it is a silly way of making a scroller. However, and finally I get to my question. The line //printline[21] = 'x'; has been commented out. In my mind that should actually add an x just off the screen every time (or next line if you don't cut off the string), since the print function will print from start of array until end. Here I must have misunderstood something in how C works, because in reality it will add an "x" as the first letter of the text-to-show string (the first 20 chrs are spaces) and move it along the whole screen as it scrolls. Originally I wanted to add a "0" to avoid writing a string so long that it continued on to the next line. For testing purposes I have put X there now. What am I misunderstanding?

I know this is not a NK question and I should have found a C forum to post this question in, but you guys seem friendly so I though I'd give it a shot.

/ initialload.c
// for NerdKits with ATmega168
// mrobbins@mit.edu

#define F_CPU 14745600

#include <stdio.h>
#include <string.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

// PIN DEFINITIONS:
//
// PC2 -- RED LCD
// PC3 -- GREEN LCD
// PC4 -- Switch to start stop scrolling

int main() {
    // LED as output
    lcd_init();
    FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
    lcd_home();

    int maxlen = 20;
    int waittime = 75;
    int startpos = maxlen;
    int i; // a counter
    int appendMe;
    char text[256] = "This is just a scrolling text.  Seems to work somehow."; //56+20
    char outstr[256];
    char printline[21];

    lcd_clear_and_home();

    while(1) {
        i = 0;
        strcpy(outstr, "\0");
        strcpy(printline, "\0");
        while(i <= startpos) {
            strncat( outstr, " ", i); // adds spaces in front
            i++;
        }
        if (startpos<0) {
            strncat(outstr, text+(0-startpos), maxlen); // removes passed chrs from text. 0-x to make pos
            outstr[maxlen+1] = '\0'; // nullzero. If maxlen less than outstr, strncpy will not nullterminate
        } else {
            strncat(outstr, text, maxlen); // adds text to preceeding spaces
            outstr[maxlen+1] = '\0'; // nullzero. If maxlen less than outstr, strncpy will not nullterminate                            
        }
        strncpy(printline, outstr, maxlen);  // puts everything into a nice output array with a max 20 len 
        //printline[21] = 'x'; // nullzero. If maxlen less than outstr, strncpy will not nullterminate
        if (strlen(printline) < 1) {        
            startpos = maxlen; // all text used, restart
                lcd_line_two();
                fprintf_P(&lcd_stream, PSTR("Restart"));
        }
        if (strlen(printline) < (maxlen)) {  // if there is still space on the line after printing the remaining text
            appendMe = maxlen - (strlen(printline));
            strncat(printline, "                                         ", appendMe); // basically adds enough space to cover the EOL      
        }

        fputs(printline, &lcd_stream); // prints string             
        delay_ms(waittime);
        startpos--;
        lcd_home();
    }

    return 0;
}
October 12, 2011
by claesz
claesz's Avatar

Sorry. I obviously posted this in the wrong forum. I can't move it so I will post again in the right "C programming" forum. My apologies.

Post a Reply

Please log in to post a reply.

Did you know that spinning a permanent magnet motor makes a voltage? Learn more...