Advertisement
pleasedontcode

"SPI Initialization" rev_02

Jun 30th, 2024
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "SPI Initialization"
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-30 07:05:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* i need a function for log and notfication. i need */
  21.     /* a function wjich i call this form : log.debug(); */
  22.     /* we have some input argument , "message" as */
  23.     /* string,and select one output port serial port,sd */
  24.     /* card,web serial ,WebService . */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SPI.h>
  29. #include <SdFat.h>  //https://github.com/greiman/SdFat
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void logDebug(const char* message, int outputPort);
  35.  
  36. /***** DEFINITION OF SPI PINS *****/
  37. const uint8_t SD_SDCardModule_SPI_PIN_MOSI_D23 = 23;
  38. const uint8_t SD_SDCardModule_SPI_PIN_MISO_D19 = 19;
  39. const uint8_t SD_SDCardModule_SPI_PIN_SCLK_D18 = 18;
  40. const uint8_t SD_SDCardModule_SPI_PIN_CS_D5 = 5;
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. SdFat sd;
  44. SdFile binFile;
  45.  
  46. /****** OUTPUT PORT DEFINITIONS *****/
  47. enum OutputPort {
  48.     SERIAL_PORT,
  49.     SD_CARD,
  50.     WEB_SERIAL,
  51.     WEB_SERVICE
  52. };
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.     Serial.begin(9600);
  58.     pinMode(SD_SDCardModule_SPI_PIN_CS_D5, OUTPUT);
  59.     digitalWrite(SD_SDCardModule_SPI_PIN_CS_D5, HIGH);
  60.  
  61.     // start the SPI library:
  62.     SPI.begin(SD_SDCardModule_SPI_PIN_SCLK_D18, SD_SDCardModule_SPI_PIN_MISO_D19, SD_SDCardModule_SPI_PIN_MOSI_D23, SD_SDCardModule_SPI_PIN_CS_D5);
  63.  
  64.     // Initialize the SD card
  65.     if (!sd.begin(SD_SDCardModule_SPI_PIN_CS_D5, SD_SCK_MHZ(50))) {
  66.         sd.initErrorPrint(&Serial);
  67.         while (true) {}
  68.     }
  69. }
  70.  
  71. void loop(void)
  72. {
  73.     // put your main code here, to run repeatedly:
  74.     // Example: Print a message to the serial monitor
  75.     logDebug("SD card initialized successfully!", SERIAL_PORT);
  76.     delay(1000); // Wait for 1 second
  77. }
  78.  
  79. /****** LOGGING FUNCTION *****/
  80. void logDebug(const char* message, int outputPort) {
  81.     switch (outputPort) {
  82.         case SERIAL_PORT:
  83.             Serial.println(message);
  84.             break;
  85.         case SD_CARD:
  86.             if (binFile.open("log.txt", O_RDWR | O_CREAT | O_AT_END)) {
  87.                 binFile.print(message);  // Use print instead of println
  88.                 binFile.print("\n");     // Add newline character manually
  89.                 binFile.close();
  90.             } else {
  91.                 Serial.println("Failed to open log file on SD card.");
  92.             }
  93.             break;
  94.         case WEB_SERIAL:
  95.             // Implement web serial logging
  96.             break;
  97.         case WEB_SERVICE:
  98.             // Implement web service logging
  99.             break;
  100.         default:
  101.             Serial.println("Invalid output port.");
  102.             break;
  103.     }
  104. }
  105.  
  106. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement