RA8875 print() method question

econjack

Well-known member
My colleague and I are working on a Software Define Transceiver using the Teensy 4.1. He wanted to print out a value, val[], which is an unsigned long. This code fragment illustrates what I saw:

Code:
#include <SPI.h>
#include <RA8875.h>                              // https://github.com/mjs513/RA8875/tree/RA8875_t4

RA8875 tft = RA8875(10, 9);

void setup() {
  unsigned long val[] = {50, 500};

  tft.begin(RA8875_800x480, 8, 20000000UL, 4000000UL);              // parameter list from library code

  tft.setCursor(100, 100);
  tft.print(val[0], 0);
}

void loop() {
}

The answer is displayed as 2. If I remove the second parameter in the print() method, it displays correctly. I'm curious why the value 2 is printed above.
 
Here is an extract from the supplied source
Code:
	// TODO: make these checks as inline, since base is
	// almost always a constant.  base = 0 (BYTE) should
	// inline as a call directly to write()
	if (base == 0) {
		return write((uint8_t)n);
(uint8_t)50 = '2'
 
That doesn't seem to be designed to work like the Arduino print 2nd param modification.

But working as designed
 
Not sure what you are expecting?

The 2nd parameter for integer types, is the base to use to print, where we have defined:
Code:
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2

// BYTE was defined in very old versions of Arduino
// maybe this now causes more trouble than it's worth?
//#ifndef BYTE
//#define BYTE 0
//#endif

So it looks like 0 was to just output one byte, and as mentioned 50(0x32) is '2' in the ASCII table.

EDIT: Looks like Arduino current AVR code has:
Code:
 // prevent crash if called with base == 1
  if (base < 2) base = 10;

Now if: val was a floating point type number:
size_t print(double n, int digits = 2) { return printFloat(n, digits); }

Then the 2nd parameter would be the number of decimal points which defaults to 2.
 
Back
Top