Coding Explanation

Can someone explain the coding for the bargraph for me. This code snip comes from the line follow demo code.

// Data for generating the characters used in load_custom_characters
// and display_readings.  By reading levels[] starting at various
// offsets, we can generate all of the 7 extra characters needed for a
// bargraph.
const char levels[] PROGMEM = {
	0b00000,
	0b00000,
	0b00000,
	0b00000,
	0b00000,
	0b00000,
	0b00000,
	0b11111,
	0b11111,
	0b11111,
	0b11111,
	0b11111,
	0b11111,
	0b11111
};

// This character is a back arrow.
const prog_char back_arrow[] PROGMEM = {
	0b00000,
	0b00010,
	0b00001,
	0b00101,
	0b01001,
	0b11110,
	0b01000,
	0b00100,
};

//-------------------------------------------------------------------------------
// This function loads custom characters into the LCD.  Up to 8
// characters can be loaded; we use them for 6 levels of a bar graph
// plus a back arrow and a musical note character.
//-------------------------------------------------------------------------------
void load_custom_characters()
{
	lcd_load_custom_character(levels+0,0); // no offset, e.g. one bar
	lcd_load_custom_character(levels+1,1); // two bars
	lcd_load_custom_character(levels+2,2); // etc...
	lcd_load_custom_character(levels+4,3); // skip level 3
	lcd_load_custom_character(levels+5,4);
	lcd_load_custom_character(levels+6,5);
	lcd_load_custom_character(back_arrow,6);
	lcd_load_custom_character(note,7);
	clear(); // the LCD must be cleared for the characters to take effect
}

The other question is: how do I need to rewrite the function load_custom_characters() if I don’t load the variable levels[] into the PROGMEM?

Looking forward to your advise. Thanks!

Hello, iRobot.

Do you have specific questions about the code beyond what is written in the comments?

If you do not want to put your characters in PROGMEM, you will have to rewrite load_custom_character. But that is probably a bad idea - why do you want to do that?

-Paul

I would like to understand how the bars are actually build up. The variable “Levels” contains 7 hex 00 and 7 hex FF. Not sure how that builds the different bar graph levels.

I wanted to learn to re-write load_custom_character for non-PROGMEM storage to better understand the 2 different approaches and their impact on the coding.

Thanks for your interest in helping me.
Volker

Hello,

As it says in the comments, the levels[] variable is read with various offsets. For example, with an offset of zero, the load_custom_character function will read bytes 0-7 from levels[], which produces a character with one bar:

   0b00000,
   0b00000,
   0b00000,
   0b00000,
   0b00000,
   0b00000,
   0b00000,
   0b11111,

With an offset of 1, you lose the first line and get two bars:

   0b00000,
   0b00000,
   0b00000,
   0b00000,
   0b00000,
   0b00000,
   0b11111,
   0b11111,

And so on.

There is not much reason to load the array from RAM instead of flash, since it has to be stored in flash initially anyway, and that approach will just cause you to use up more RAM.

-Paul