Search code examples
arduinoarduino-c++

How to print a number on with HT1632 only accepting text


I just bought a 8x32 lattice board (led matrix) and I control it with Arduino. The problem is that I can only use text with the library I got on github. But not numbers, how can I do it?

I'm going to put the code below, the code of the scrolling text and the part of the code in the library that specifies the function used to set the text.

The arduino code that program the scrolling text is here:

#include <HT1632.h>
#include <font_5x4.h>
#include <images.h>

int i = 0;
int wd;
char disp[] = "Hello, how are you?";
int x = 10;

void setup() {
  HT1632.begin(A5, A4, A3);

  wd = HT1632.getTextWidth(disp, FONT_5X4_END, FONT_5X4_HEIGHT);
}
void loop() {
  HT1632.renderTarget(1);
  HT1632.clear();

  HT1632.drawText(disp, OUT_SIZE - i, 2, FONT_5X4, FONT_5X4_END,
                  FONT_5X4_HEIGHT);
  HT1632.render();

  i = (i + 1) % (wd + OUT_SIZE);

  delay(100);
}

The library code that specifies the printing of the text is this:

void HT1632Class::drawText(const char text[], int x, int y, const byte font[],
                           int font_end[], uint8_t font_height,
                           uint8_t gutter_space) {
  int curr_x = x;
  char i = 0;
  char currchar;

  // Check if string is within y-bounds
  if (y + font_height < 0 || y >= COM_SIZE)
    return;

  while (true) {
    if (text[i] == '\0')
      return;

    currchar = text[i] - 32;
    if (currchar >= 65 &&
        currchar <=
            90) // If character is lower-case, automatically make it upper-case
      currchar -= 32; // Make this character uppercase.

    if (currchar < 0 || currchar >= 64) { // If out of bounds, skip
      ++i;
      continue; // Skip this character.
    }

    // Check to see if character is not too far right.
    if (curr_x >= OUT_SIZE)
      break; // Stop rendering - all other characters are no longer within the
             // screen

    // Check to see if character is not too far left.
    int chr_width = getCharWidth(font_end, font_height, currchar);
    if (curr_x + chr_width + gutter_space >= 0) {
      drawImage(font, chr_width, font_height, curr_x, y,
                getCharOffset(font_end, currchar));

      // Draw the gutter space
      for (char j = 0; j < gutter_space; ++j)
        drawImage(font, 1, font_height, curr_x + chr_width + j, y, 0);
    }

    curr_x += chr_width + gutter_space;
    ++i;
  }
}

Solution

  • You need to look at snprintf. This allows you to format a string of characters just like printf. It allows you to convert something like a int into a part of a string.

    an example:

    int hour = 10;
    int minutes = 50;
    char buffer[60];
    
    int status = snprintf(buffer, 60, "the current time is: %i:%i\n", hour, minutes);
    

    buffer now contains:"the current time is: 10:50" (and several empty characters past the \0).