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.

Microcontroller Programming » LED array do_scrolling_display() with more comments

December 05, 2009
by Haprog
Haprog's Avatar

I added more comments to the do_scrolling_display() function and thought I'd share them. I hope this helps someone understand the function more easily.

// MCU listens for characters from the PC (or other device).
// the PC starts the communication by sending one character
// and then waits for MCU to send 'n' as a signal when it's ready to
// accept another character
void do_scrolling_display() {
  //initialize la_data array with 0s
  ledarray_blank();
  int8_t offset = 0, next_offset = 0;
  //tells if there's a scrolling message in progress
  uint8_t is_started = 0;
  char x=' ';

  while(1) {
    if(is_started) {
      delay_ms(125);
      // shift current frame one pixel left.
      // the purpose of this is to move all "old" characters
      // that are already fully on the display.
      ledarray_left_shift();

      if(next_offset > 0) {
        // shift offsets one pixel left.
        offset -= 1;
        next_offset -= 1;
      } else {
        // if next_offset <= 0 then the current scroll is over
        is_started = 0;
      }
      // display current character (not the first time)
      font_display(x, offset); 
    } else {
      // if no scrolling in progress:
      // set current offset to the right edge of the array
      offset = COLS-1;
    }
    // if we can now accept a new character, tell the computer
    if(next_offset == COLS)
      uart_write('n');

    // "while there is a new character to be read"
    // this assumes that the PC doesn't try to send characters
    // before we have requested for another char with signal 'n' (excluding the first character)
    while(uart_char_is_waiting()) {
      if(is_started) {
        // use next offset (when scrolling is in progress, so we know next_offset is defined
        //                  and this isn't the first character of the message)
        offset = next_offset;
      }

      x = uart_read(); 
      if(x=='a') {
        ledarray_blank();
        return;
      }
      // display current character (for the first time)
      font_display(x, offset);
      // set next offset to be after current character
      next_offset = offset + font_width(x)+1;
      is_started = 1;  
      // if we can now accept a new character, tell the computer.
      // (this happens when the current character is already fully shown on the display
      //  and a new character is potentially going to be scrolled in next.)
      if(next_offset <= COLS)
        uart_write('n');
    }
  }
}
December 05, 2009
by Rick_S
Rick_S's Avatar

That's a good addition. I wish I'd had that when I 1st started trying to interpret the array code.

December 05, 2009
by Haprog
Haprog's Avatar

I had some trouble interpreting that particular function for the first time myself. For a while I somehow didn't see the first else statement and couldn't understand how the function worked when offset seemed to start at 0 and not be set to the right side at any point. :P

December 07, 2009
by Rick_S
Rick_S's Avatar

Hey Haprog, Just saw your photos of your array build on your website from the Newsletter. Did you post a link to that before, If so, I'm sorry I missed it. Those are some excellent photo's. I love the idea of the sheet cork board. I imagine that would make wiring pretty easy. I really like how you ran both the row and column wires on the back side. It looks great. Now you just need to make a pretty wood box to put it in :) :D

Rick

Post a Reply

Please log in to post a reply.

Did you know that you can generate hundreds of volts AC from your microcontroller with a little bit of circuitry? Learn more...