Printing fonts on HT1632 LED matrix (bitwise math and PROGMEM help!)

I'm trying to implement some simple font printing on my HT1632 matrix, but I'm currently stuck at a very basic step of the implementation. I'm using stealing the fonts from the arduino-tvout library. This is how a font definition looks like:

PROGMEM const unsigned char font4x6[] = {
4,6,32,
//space
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
//!
0b01000000,
0b01000000,
0b01000000,
0b00000000,
0b01000000,
0b00000000,
[...]

My problem now is, how can I extract the bit information for my panel. I'm using two functions to set a LED on or off, how can I get a specific bit from the byte?

You want to read one bit from a byte value? How about using bitRead()?

Or use the more obtuse, but somehow more satisfying, bitwise operators to mask the bits you don't want, leaving only the one(s) that you do want. The bitRead() function is easier to understand but make sure that you read the description in the reference to understand exactly which bit you are reading. ie bit 0 is not the leftmost bit.

PeterH:
You want to read one bit from a byte value? How about using bitRead()?

I thought of that but I think it's better with bitwise math. The problem is, I can't think of a way to achieve that

UKHeliBob:
Or use the more obtuse, but somehow more satisfying, bitwise operators to mask the bits you don't want, leaving only the one(s) that you do want. The bitRead() function is easier to understand but make sure that you read the description in the reference to understand exactly which bit you are reading. ie bit 0 is not the leftmost bit.

That's exactly what I'm looking for, the problem is I can't find anything related on the internet. What bitwise operation should I look for, for example?

Abfahrt:
I thought of that but I think it's better with bitwise math. The problem is, I can't think of a way to achieve that

I'm not sure in which way you think it's "better". The Arduino functions save you from having to understand how to use the bitwise operators ('&', '|', '~') yourself. If you want to use the bitwise operators yourself then you will need to work out how to use them. It isn't hard, and this is a standard part of the C++ language which is well documented. But if you don't know how to use the bitwise operators and don't want to learn how, then use the helper functions that Arduino provides. Since you're only accessing single bits, the helper functions do EXACTLY the same thing as the bitwise operators would, and are slightly simpler to use.

Start with the Arduino reference installed with the IDE. Do a find on the main page and look for bitwise

You can't find anything about bitwise operators on the Internet ?
How about LMGTFY - Let Me Google That For You ?

Thanks for all the replies, my problem was easier than I thought when I was creating the post (I blame it on sleep) ]:smiley:

I'm stuck on another problem now. I also had this problem in the past and I never solved it. My array is stored in PROGMEM, so I need to use pgm_read_byte to get the data. All I get is garbare, not what I expect and get when not using PROGMEM and pgm_read_byte (aka a simple array).

This is the routine I wrote (I don't use the panel yet). The array definition is at the top of the file.

void print_char(char c, byte height, byte width) {
  c = c - 32;
  c = c * 6;

  for (int j = 0; j < height; j++) {
    for (int i = 7; i >= 0 ; i--) {
      if ((pgm_read_byte(&font4x6[c + j + 3]) & (1 << i)) != 0) {
        Serial.print("# ");
      }
      else {
        Serial.print("@ ");
      }
    }
    Serial.println("");
  }
}

Thanks for your answers :slight_smile:

This is the routine I wrote (I don't use the panel yet). The array definition is at the top of the file.

Well, that's just ducky. You don't suppose that we need to see it, to confirm that the code is accessing the data correctly?

All I get is garbare, not what I expect and get when not using PROGMEM and pgm_read_byte (aka a simple array).

We can't confirm that you are reading the data correctly. In fact this statement implies that you are not. Without seeing the declaration, all we can do is wish you luck.

The array declaration is at the first post, if that's what you mean!

EDIT: Oops, I meant at the declaration is at the top of the thread, not the file.

If I was doing the debugging, I'd want to know that I could read the first three elements correctly.

Then, I'd want to know what c was on input to the function.

Then, I'd want to know what index I was computing for the array.

Then, I'd want to know what value I actually read from the array.

Only then would I do the bit shifting and anding.

Everything you mentioned works correctly when I'm not using the PROGMEM directive and the pgm_read_byte function (just a plain array).

In other words, everything is fine (work as expected) until I move to the PROGMEM.

EDIT: I think I understand what's going on. I'm using pgm_read_byte(font4x6[x]) which is wrong. Changing to pgm_read_byte(font4x6 + x) works. Ahh.