/********* 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: Scanner
	- Source Code compiled for: Arduino Pro Mini 3.3V
	- Source Code created on: 2023-11-07 14:26:05
	- Source Code generated by: AlexWind

********* Pleasedontcode.com **********/
/****** DEFINITION OF LIBRARIES *****/
#include <Arduino.h>
#include <SPI.h>
#include <SdFat.h>
#include <EasyButton.h>

/****** SYSTEM REQUIREMENT 1 *****/
/* each time the button is pressed, read one bit from */
/* sd card and change the state of led depending from */
/* bit. */

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t pulsante_PushButton_PIN_D3 = 3;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t led_LED_PIN_D2 = 2;

/***** DEFINITION OF SPI PINS *****/
const uint8_t memoria_SDCardModule_SPI_PIN_MOSI_D11 = 11;
const uint8_t memoria_SDCardModule_SPI_PIN_MISO_D12 = 12;
const uint8_t memoria_SDCardModule_SPI_PIN_SCLK_D13 = 13;
const uint8_t memoria_SDCardModule_SPI_PIN_CS_D10 = 10;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
SdFat SD;
EasyButton button(pulsante_PushButton_PIN_D3);

void onPressed()
{
  // Open the file on the SD card
  SdFile file;
  if (!file.open("data.txt", O_READ))
  {
    // Error handling code
    return;
  }

  // Read one byte from the file
  uint8_t byteValue;
  if (file.read(&byteValue, sizeof(byteValue)) != sizeof(byteValue))
  {
    // Error handling code
    file.close();
    return;
  }

  // Close the file
  file.close();

  // Extract the bit value from the byte
  bool bitValue = (byteValue & (1 << 0)) != 0;

  // Change the state of the LED depending on the bit value
  digitalWrite(led_LED_PIN_D2, bitValue);
}

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(pulsante_PushButton_PIN_D3, INPUT_PULLUP);

  pinMode(led_LED_PIN_D2, OUTPUT);

  pinMode(memoria_SDCardModule_SPI_PIN_CS_D10, OUTPUT);
  // start the SPI library:
  SPI.begin();

  // Initialize SD card
  if (!SD.begin(memoria_SDCardModule_SPI_PIN_CS_D10, SPI_FULL_SPEED))
  {
    // SD card initialization failed
    while (1)
    {
      // Error handling code
    }
  }

  button.onPressed(onPressed);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  button.read();
}
