CCCPaste

"SPI Initialization" rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "SPI Initialization"
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-06-30 07:05:22

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* i need a function for log and notfication. i need */
	/* a function wjich i call this form : log.debug(); */
	/* we have some input argument , "message" as */
	/* string,and select one output port serial port,sd */
	/* card,web serial ,WebService . */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include   //https://github.com/greiman/SdFat

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void logDebug(const char* message, int outputPort);

/***** DEFINITION OF SPI PINS *****/
const uint8_t SD_SDCardModule_SPI_PIN_MOSI_D23 = 23;
const uint8_t SD_SDCardModule_SPI_PIN_MISO_D19 = 19;
const uint8_t SD_SDCardModule_SPI_PIN_SCLK_D18 = 18;
const uint8_t SD_SDCardModule_SPI_PIN_CS_D5 = 5;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
SdFat sd;
SdFile binFile;

/****** OUTPUT PORT DEFINITIONS *****/
enum OutputPort {
    SERIAL_PORT,
    SD_CARD,
    WEB_SERIAL,
    WEB_SERVICE
};

void setup(void)
{
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(SD_SDCardModule_SPI_PIN_CS_D5, OUTPUT);
    digitalWrite(SD_SDCardModule_SPI_PIN_CS_D5, HIGH);

    // start the SPI library:
    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);

    // Initialize the SD card
    if (!sd.begin(SD_SDCardModule_SPI_PIN_CS_D5, SD_SCK_MHZ(50))) {
        sd.initErrorPrint(&Serial);
        while (true) {}
    }
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    // Example: Print a message to the serial monitor
    logDebug("SD card initialized successfully!", SERIAL_PORT);
    delay(1000); // Wait for 1 second
}

/****** LOGGING FUNCTION *****/
void logDebug(const char* message, int outputPort) {
    switch (outputPort) {
        case SERIAL_PORT:
            Serial.println(message);
            break;
        case SD_CARD:
            if (binFile.open("log.txt", O_RDWR | O_CREAT | O_AT_END)) {
                binFile.print(message);  // Use print instead of println
                binFile.print("\n");     // Add newline character manually
                binFile.close();
            } else {
                Serial.println("Failed to open log file on SD card.");
            }
            break;
        case WEB_SERIAL:
            // Implement web serial logging
            break;
        case WEB_SERVICE:
            // Implement web service logging
            break;
        default:
            Serial.println("Invalid output port.");
            break;
    }
}

/* END CODE */