Pages: [1]   Go Down
Author Topic: uint64_t in Serial.print  (Read 3998 times)
0 Members and 1 Guest are viewing this topic.
Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   
    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);
« Last Edit: November 25, 2012, 01:36:16 pm by Steve Spence - KK4HFJ » Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


add this to print.cpp and the signatures to print.h - be aware long long is slow slow smiley-wink
Code:
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));
}
Logged

Rob Tillaart

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

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
  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) {
  
}

* Print.cpp (6.46 KB - downloaded 33 times.)
* Print.h (2.71 KB - downloaded 29 times.)
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

and the signatures to print.h

Did you do that?
Logged

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I attached print.h

Does it look correct?
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll will try the code myself, be back asap
[update]
unexpected conflicts - pity ...
« Last Edit: November 25, 2012, 01:09:15 pm by robtillaart » Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

a workaround: print it in 2 steps

Code:
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(){}
Logged

Rob Tillaart

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

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ok. I get

start...
12345678912345678
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Rob Tillaart

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

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure how to merge that into the sd cardinfo sketch .....
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
  // Serial.print("Volume size (bytes): ");
  // Serial.println(volumesize);
becomes

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

Rob Tillaart

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

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

* SD_CardInfo.ino (3.69 KB - downloaded 14 times.)
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this what you expected? if not what did you expect?
Logged

Rob Tillaart

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

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 239
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I was expecting somewhere around 16,000,000,000 bytes .....
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can you post your complete code again?
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: