Pages: [1]   Go Down
Author Topic: Printing fonts on HT1632 LED matrix (bitwise math and PROGMEM help!)  (Read 631 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 2
Posts: 85
(╯°□°)╯︵ ┻━┻
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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?
« Last Edit: February 12, 2013, 07:08:23 am by Abfahrt » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 2
Posts: 85
(╯°□°)╯︵ ┻━┻
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 http://bit.ly/XraSG4 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 2
Posts: 85
(╯°□°)╯︵ ┻━┻
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.
Code:
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 smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49027
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Quote
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.
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 85
(╯°□°)╯︵ ┻━┻
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 12, 2013, 07:41:31 am by Abfahrt » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49027
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 85
(╯°□°)╯︵ ┻━┻
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
pgm_read_byte(font4x6[x])
which is wrong. Changing to
Code:
pgm_read_byte(font4x6 + x)
works. Ahh.
« Last Edit: February 12, 2013, 08:23:57 am by Abfahrt » Logged

Pages: [1]   Go Up
Jump to: