formatting.ino 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Print a table with various formatting options
  3. * Format dates
  4. */
  5. #include <SPI.h>
  6. #include "SdFat.h"
  7. #include "sdios.h"
  8. // create Serial stream
  9. ArduinoOutStream cout(Serial);
  10. //------------------------------------------------------------------------------
  11. // print a table to demonstrate format manipulators
  12. void example(void) {
  13. const int max = 10;
  14. const int width = 4;
  15. for (int row = 1; row <= max; row++) {
  16. for (int col = 1; col <= max; col++) {
  17. cout << setw(width) << row * col << (col == max ? '\n' : ' ');
  18. }
  19. }
  20. cout << endl;
  21. }
  22. //------------------------------------------------------------------------------
  23. // print a date as mm/dd/yyyy with zero fill in mm and dd
  24. // shows how to set and restore the fill character
  25. void showDate(int m, int d, int y) {
  26. // convert two digit year
  27. if (y < 100) {
  28. y += 2000;
  29. }
  30. // set new fill to '0' save old fill character
  31. char old = cout.fill('0');
  32. // print date
  33. cout << setw(2) << m << '/' << setw(2) << d << '/' << y << endl;
  34. // restore old fill character
  35. cout.fill(old);
  36. }
  37. //------------------------------------------------------------------------------
  38. void setup(void) {
  39. Serial.begin(9600);
  40. // Wait for USB Serial
  41. while (!Serial) {
  42. yield();
  43. }
  44. delay(2000);
  45. cout << endl << "default formatting" << endl;
  46. example();
  47. cout << showpos << "showpos" << endl;
  48. example();
  49. cout << hex << left << showbase << "hex left showbase" << endl;
  50. example();
  51. cout << internal << setfill('0') << uppercase;
  52. cout << "uppercase hex internal showbase fill('0')" <<endl;
  53. example();
  54. // restore default format flags and fill character
  55. cout.flags(ios::dec | ios::right | ios::skipws);
  56. cout.fill(' ');
  57. cout << "showDate example" <<endl;
  58. showDate(7, 4, 11);
  59. showDate(12, 25, 11);
  60. }
  61. void loop(void) {}