Go Down

Topic: uint64_t in Serial.print (Read 5020 times) previous topic - next topic

Steve Spence - KK4HFJ

Nov 25, 2012, 01:04 pm Last Edit: Nov 25, 2012, 07:36 pm by Steve Spence - KK4HFJ Reason: 1
I'm working with the Card info sketch in the sd examples, and my 16gb sd card reports as 3gb. It's my understanding that this is a result of Serial.print only understanding uint32_t. I've tried editing print.h to understand long long int, but it's not taking.

What am I not understanding?

Code: [Select]
   
   size_t print(const __FlashStringHelper *);
   size_t print(const String &);
   size_t print(const char[]);
   size_t print(char);
   size_t print(unsigned char, int = DEC);
   size_t print(int, int = DEC);
   size_t print(unsigned int, int = DEC);
   size_t print(long, int = DEC);
//size_t print(long long, int = DEC);
   size_t print(unsigned long, int = DEC);
//size_t print(unsigned long long, int = DEC);
   size_t print(double, int = 2);
   size_t print(const Printable&);

   size_t println(const __FlashStringHelper *);
   size_t println(const String &s);
   size_t println(const char[]);
   size_t println(char);
   size_t println(unsigned char, int = DEC);
   size_t println(int, int = DEC);
   size_t println(unsigned int, int = DEC);
   size_t println(long, int = DEC);
//size_t println(long long, int = DEC);
   size_t println(unsigned long, int = DEC);
//size_t println(unsigned long long, int = DEC);
   size_t println(double, int = 2);
   size_t println(const Printable&);
   size_t println(void);
Steve Spence - ET/AT/R&D KK4HFJ

robtillaart


add this to print.cpp and the signatures to print.h - be aware long long is slow slow ;)
Code: [Select]

void Print::println(long long n, int base)
{
  print(n, base);
  println();
}


void Print::print(long long n, int base)
{
  if (n < 0) write('-');
  if (base == 0) write(n);
  else printLLNumber(n, base);
}


void Print::println(unsigned long long n, int base)
{
  print(n, base);
  println();
}


void Print::print(unsigned long long n, int base)
{
  if (base == 0) write(n);
  else printLLNumber(n, base);
}



void Print::printLLNumber(unsigned long long n, uint8_t base)
{
  unsigned char buf[16 * sizeof(long)]; // Assumes 8-bit chars.
  unsigned long long i = 0;

  if (n == 0) {
    print('0');
    return;
  }

  while (n > 0) {
    buf[i++] = n % base;
    n /= base;
  }

  for (; i > 0; i--)
    print((char) (buf[i - 1] < 10 ?
      '0' + buf[i - 1] :
      'A' + buf[i - 1] - 10));
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Steve Spence - KK4HFJ

I get

C:\Users\Steve\Programs\arduino-1.0.2\hardware\arduino\cores\arduino\nPrint.cpp:230: error: prototype for 'size_t Print::printNumber(long long unsigned int, uint8_t)' does not match any in class 'Print'
C:\Users\Steve\Programs\arduino-1.0.2\hardware\arduino\cores\arduino\/Print.h:38: error: candidate is: size_t Print::printNumber(long unsigned int, uint8_t)

When I run

Code: [Select]

/*
 SD card test
 
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.

The circuit:
 * SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples


created  28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
*/
// include the SD library:
#include <SD.h>


// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;    

void setup()
{
// Open serial communications and wait for port to open:
 Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }


 Serial.print("\nInitializing SD card...");
 // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
 // Note that even if it's not used as the CS pin, the hardware SS pin
 // (10 on most Arduino boards, 53 on the Mega) must be left as an output
 // or the SD library functions will not work.
 pinMode(10, OUTPUT);     // change this to 53 on a mega


 // we'll use the initialization code from the utility libraries
 // since we're just testing if the card is working!
 if (!card.init(SPI_HALF_SPEED, chipSelect)) {
   Serial.println("initialization failed. Things to check:");
   Serial.println("* is a card is inserted?");
   Serial.println("* Is your wiring correct?");
   Serial.println("* did you change the chipSelect pin to match your shield or module?");
   return;
 } else {
  Serial.println("Wiring is correct and a card is present.");
 }

 // print the type of card
 Serial.print("\nCard type: ");
 switch(card.type()) {
   case SD_CARD_TYPE_SD1:
     Serial.println("SD1");
     break;
   case SD_CARD_TYPE_SD2:
     Serial.println("SD2");
     break;
   case SD_CARD_TYPE_SDHC:
     Serial.println("SDHC");
     break;
   default:
     Serial.println("Unknown");
 }

 // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
 if (!volume.init(card)) {
   Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
   return;
 }


 // print the type and size of the first FAT-type volume
 uint64_t volumesize;
 Serial.print("\nVolume type is FAT");
 Serial.println(volume.fatType(), DEC);
 Serial.println();
 
 volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
 volumesize *= volume.clusterCount();       // we'll have a lot of clusters
 // volumesize *= 512;                            // SD card blocks are always 512 bytes
 // Serial.print("Volume size (bytes): ");
 // Serial.println(volumesize);
 Serial.print("Volume size (Kbytes): ");
 // volumesize /= 1024;
 volumesize /= 2;
 Serial.println(volumesize);
 Serial.print("Volume size (Mbytes): ");
 volumesize /= 1024;
 Serial.println(volumesize);

 
 Serial.println("\nFiles found on the card (name, date and size in bytes): ");
 root.openRoot(volume);
 
 // list all files in the card with date and size
 root.ls(LS_R | LS_DATE | LS_SIZE);
}


void loop(void) {
 
}

Steve Spence - ET/AT/R&D KK4HFJ

WizenedEE


Steve Spence - KK4HFJ

I attached print.h

Does it look correct?
Steve Spence - ET/AT/R&D KK4HFJ

robtillaart

#5
Nov 25, 2012, 06:19 pm Last Edit: Nov 25, 2012, 07:09 pm by robtillaart Reason: 1
I'll will try the code myself, be back asap
[update]
unexpected conflicts - pity ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

a workaround: print it in 2 steps

Code: [Select]
void setup()
{
  Serial.begin(9600);
  Serial.println("start...");
 
  uint64_t ll = 123456789012345678ULL;
  uint64_t xx = ll/1000000000ULL;

  if (xx >0) Serial.print((long)xx);
  Serial.print((long)(ll-xx*1000000000));
}

void loop(){}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Steve Spence - KK4HFJ

ok. I get

start...
12345678912345678
Steve Spence - ET/AT/R&D KK4HFJ

robtillaart

does this 'trick' solve your problem enough?

An "integrated solution" is more difficult than I expected.
I also had a quick look at - http://www.nongnu.org/avr-libc/user-manual/modules.html - but found no solutions there either.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Steve Spence - KK4HFJ

Not sure how to merge that into the sd cardinfo sketch .....
Steve Spence - ET/AT/R&D KK4HFJ

robtillaart


Code: [Select]
  // Serial.print("Volume size (bytes): ");
  // Serial.println(volumesize);

becomes

Code: [Select]

  Serial.print("Volume size (bytes): ");
  uint64_t xx = volumesize/1000000000ULL;

  if (xx >0) Serial.print((long)xx);
  Serial.print((long)(volumesize - xx*1000000000));


give it a try
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Steve Spence - KK4HFJ

I get:

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size (bytes): 31099904
Files found on the card (name, date and size in bytes):
DCIM/         2011-06-20 14:58:52
 100_PANA/     2011-06-20 19:07:48
   P1000812.JPG  2011-09-08 09:19:54 4158647
   P1000781.JPG  2011-08-20 21:02:02 4309863
   P1000813.JPG  2011-09-08 09:20:00 4258311
Steve Spence - ET/AT/R&D KK4HFJ

robtillaart

Is this what you expected? if not what did you expect?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Steve Spence - KK4HFJ

I was expecting somewhere around 16,000,000,000 bytes .....
Steve Spence - ET/AT/R&D KK4HFJ

robtillaart

can you post your complete code again?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up