You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8KB

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