Ejemplo n.º 1
0
void setup(void) {
  Serial.begin(9600);
  pinMode(JOYSTICK_BUTTON, INPUT);
  pinMode(RATING_LED_0, OUTPUT);
  pinMode(RATING_LED_1, OUTPUT);
  pinMode(RATING_LED_2, OUTPUT);
  pinMode(RATING_LED_3, OUTPUT);
  pinMode(RATING_LED_4, OUTPUT);

  digitalWrite(JOYSTICK_BUTTON, HIGH);

  tft.initR(INITR_REDTAB);

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    return;
  }
  Serial.println("OK!");

  drawMap();

  // test out reading blocks from the SD card
  if (!card.init(SPI_HALF_SPEED, SD_CS)) {
      Serial.println("Raw SD Initialization has failed");
      while (1) {};  // Just wait, stuff exploded.
  }

  verticalMidpoint = getVertical();
}
Ejemplo n.º 2
0
void setup() {
  Serial.begin(BPS_115200);
  PgmPrintln("Type any character to start");
  while (!Serial.available());
  
  // initialize the SD card at SPI_FULL_SPEED for best performance.
  // try SPI_HALF_SPEED if bus errors occur.
  if (!card.init(SPI_FULL_SPEED)) error("card.init failed");
  
  // initialize a FAT volume
  if (!volume.init(&card)) error("volume.init failed");

  // open the root directory
  if (!root.openRoot(&volume)) error("openRoot failed");
  
  // write files to root if FAT32
  if (volume.fatType() == 32) {
    PgmPrintln("Writing files to root");
    dirAllocTest(root);
  }
  
  // create sub1 and write files
  SdFile sub1;
  if (!sub1.makeDir(&root, "SUB1")) error("makdeDir SUB1 failed");
  PgmPrintln("Writing files to SUB1");
  dirAllocTest(sub1);

  // create sub2 and write files
  SdFile sub2;
  if (!sub2.makeDir(&sub1, "SUB2")) error("makeDir SUB2 failed");
  PgmPrintln("Writing files to SUB2"); 
  dirAllocTest(sub2);
  
  PgmPrintln("Done");
}
Ejemplo n.º 3
0
int initSDCard(void){
    
  if (!card.init(SPI_HALF_SPEED, SS_CS_Pin))  //Set SCK rate to F_CPU/4 (mode 1)
  {
  	Serial.println("Error Init SD-Card");
  	return SDCARD_ERROR;
  }
  else
  {	 
  	// initialize a FAT volume
  	if (!volume.init(&card))
  	{
  	  	Serial.println("Error Init Volume in SD-Card");
  	  	return SDCARD_ERROR;
	}  	  	
  	else
  	{ 
  		// Open volume
  		if (!root.openRoot(&volume))
  		{
  			Serial.println("Error Open Volume in SD-Card");
  			return SDCARD_ERROR;
		}  	  	
  		else
  		{   
  			return SUCCESS;    			
        } 
    }
  }            			
}    			
Ejemplo n.º 4
0
Archivo: sd.cpp Proyecto: f32c/arduino
// reading sd card type is slow
void sd_read(char *a)
{
  char *cardtype;
  if (card.init(SPI_HALF_SPEED, 2))
  {
    switch (card.type())
    {
    case SD_CARD_TYPE_SD1:
      cardtype = "1 ";
      break;
    case SD_CARD_TYPE_SD2:
      cardtype = "2 ";
      break;
    case SD_CARD_TYPE_SDHC:
      cardtype = "HC";
      break;
    default:
      cardtype = "? ";
    }
    sprintf(a, "SD: %s OK", cardtype);
  }
  else
  {
    sprintf(a, "SD: FAIL ");
  }
}
Ejemplo n.º 5
0
void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    tft.initR(INITR_REDTAB);
    randomSeed(analogRead(4));
    joystickXCentre = analogRead(JOYSTICK_MOVE_X_PIN) - 512;
    joystickYCentre = analogRead(JOYSTICK_MOVE_Y_PIN) - 512;
    pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
    pinMode(COUNTDOWN_START_RED_1, OUTPUT);
    pinMode(COUNTDOWN_START_RED_2, OUTPUT);
    pinMode(COUNTDOWN_MID_RED_1, OUTPUT);
    pinMode(COUNTDOWN_MID_RED_2, OUTPUT);
    pinMode(COUNTDOWN_GREEN, OUTPUT);
    tft.setRotation(1); //because our screen is nonstandard rotation
    player1.score = 0;
    player2.score = 0;
    if (!SD.begin(SD_CS)) {
        Serial.println("SD Init Failed");
        return;
    }
    if (!card.init(SPI_HALF_SPEED, SD_CS)) {
        Serial.println("Raw SD Init failed");
        while (1);
    }
    tft.fillScreen(ST7735_BLACK);
}
Ejemplo n.º 6
0
void setup(void) {
  Serial.begin(BPS_115200);
  Serial.println();
  
#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
  
  // initialize a FAT volume
  if (!volume.init(&card)) error("volume.init failed");
  
  // open root directory
  if (!root.openRoot(&volume)) error("openRoot failed");
  
  // create a new file
  char name[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    name[6] = i/10 + '0';
    name[7] = i%10 + '0';
    if (file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)) break;
  }
  if (!file.isOpen()) error ("file.create");
  Serial.print("Logging to: ");
  Serial.println(name);

  // write header
  file.writeError = 0;
  file.print("millis");
#if ECHO_TO_SERIAL 
  Serial.print("millis");
#endif //ECHO_TO_SERIAL

#if SENSOR_COUNT > 6
#error SENSOR_COUNT too large
#endif //SENSOR_COUNT

  for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
    file.print(",sens");file.print(i, DEC);    
#if ECHO_TO_SERIAL
    Serial.print(",sens");Serial.print(i, DEC);
#endif //ECHO_TO_SERIAL
  }
  file.println();  
#if ECHO_TO_SERIAL
  Serial.println();
#endif  //ECHO_TO_SERIAL

  if (file.writeError || !file.sync()) {
    error("write header failed");
  }
}
Ejemplo n.º 7
0
int App::run()
{
    if (!card.init(SPI_HALF_SPEED, 4))
    {
        Serial.puts("initialization failed. Things to check:\r\n");
        Serial.puts("* is a card is inserted?\r\n");
        Serial.puts("* Is your wiring correct?\r\n");
        Serial.puts("* did you change the chipSelect pin to match your shield or module?\r\n");
        return 0;
    }
    else
    {
        Serial.puts("Wiring is correct and a card is present.\r\n");
    }

    Serial.puts("\r\nCard type: ");
    switch(card.type())
    {
    case SD_CARD_TYPE_SD1:
        Serial.puts("SD1\r\n");
        break;
    case SD_CARD_TYPE_SD2:
        Serial.puts("SD2\r\n");
        break;
    case SD_CARD_TYPE_SDHC:
        Serial.puts("SDHC\r\n");
        break;
    default:
        Serial.puts("Unknown\r\n");
    }

    if (!volume.init(card))
    {
        Serial.puts("Could not find FAT16/FAT32 partition.\r\n");
        Serial.puts("Make sure you've formatted the card\r\n");
        return 0;
    }

    uint32_t volumesize;
    Serial.printf("\r\nVolume type is FAT%u\r\n", volume.fatType());
    volumesize = volume.blocksPerCluster();
    volumesize *= volume.clusterCount();
    volumesize *= 512;
    Serial.printf("Volume size (bytes): %u\r\n", volumesize);
    volumesize /= 1024;
    Serial.printf("Volume size (Kbytes): %u\r\n");
    volumesize /= 1024;
    Serial.printf("Volume size (Mbytes): %u\r\n");
    Serial.puts("\r\nFiles found on the card (name, date and size in bytes): \r\n");
    root.openRoot(volume);
    root.ls(LS_R | LS_DATE | LS_SIZE, 0, Serial);
    while (true) { }
    return 0;
}
Ejemplo n.º 8
0
void setup(void) {
    Serial.begin(9600);

    // If your TFT's plastic wrap has a Red Tab, use the following:
    tft.initR(INITR_REDTAB);   // initialize a ST7735R chip, red tab
    // If your TFT's plastic wrap has a Green Tab, use the following:
    //tft.initR(INITR_GREENTAB); // initialize a ST7735R chip, green tab

    // how much memory have we got left at this point?
    Serial.print("Avail mem (bytes):");
    Serial.println(AVAIL_MEM);

    Serial.print("Initializing SD card...");
    if (!SD.begin(SD_CS)) {
      Serial.println("failed!");
      return;
    }
    Serial.println("OK!");

    // clear to yellow
    tft.fillScreen(tft.Color565(0xff, 0xff, 0x00));

    lcd_image_draw(&map_image, &tft, 0, 0, 0, 0, 128, 128);

    // how much memory have we got left at this point?
    Serial.print("Avail mem (bytes):");
    Serial.println(AVAIL_MEM);

    // test out reading blocks from the SD card
    if (!card.init(SPI_HALF_SPEED, SD_CS)) {
        Serial.println("Raw SD Initialization has failed");
        while (1) {};  // Just wait, stuff exploded.
        }

    // how much memory have we got left at this point?
    Serial.print("Avail mem (bytes):");
    Serial.println(AVAIL_MEM);
 
    uint32_t block_num = 4000000;
    uint32_t start = millis();
    for (int l=0; l<135; l++) {
        card.readBlock( block_num, (uint8_t *) block_buf);
        // Serial.println(block_buf[1].name);
        }
    uint32_t stop = millis();
    //Serial.println(stop - start);
    
    //dump_block((uint8_t *) block_buf, BLOCK_LEN);
    // Serial.println(block_buf[1].name);

    for(int i = 0; i < 1066; i++) {
      printRest(i);
    }
}
Ejemplo n.º 9
0
bool FileSystem::initSDCard() {
    boolean status;

    if (root.isOpen())
        root.close();      // allows repeated calls

    // First, detect the card
    status = card.init(pinCS); // Audio shield has SD card SD on pin 10
    if (status) {
        Serial.println("SD card is connected :-)");
    } else {
        Serial.println("SD card is not connected or unusable :-(");
        sdCardInitialized = false;
        return sdCardInitialized;
    }

    // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    if (!volume.init(card)) {
        Serial.println(
                "Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
        sdCardInitialized = false;
        return sdCardInitialized;
    }

    root.openRoot(volume);

    // print the type and size of the first FAT-type volume
    Serial.print("\nVolume type is FAT");
    Serial.println(volume.fatType(), DEC);
    Serial.println();

    float size = volume.blocksPerCluster() * volume.clusterCount();
    size = size * (512.0 / 1e6); // convert blocks to millions of bytes
    Serial.print("File system space is ");
    Serial.print(size);
    Serial.println(" Mbytes.");

    status = SD.begin(pinCS); // Audio shield has SD card CS on pin 10
    if (status) {
        Serial.println("SD library is able to access the filesystem");
    } else {
        Serial.println("SD library can not access the filesystem!");
        Serial.println(
                "Please report this problem, with the make & model of your SD card.");
        Serial.println(
                "  http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports");
    }
    sdCardInitialized = true;
    return sdCardInitialized;
}
Ejemplo n.º 10
0
void setup() {
  //Serial.begin(9600);

  PgmPrint("Free RAM: ");
  //Serial.println(FreeRam());  

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  pinMode(10, OUTPUT);                       // set the SS pin as an output (necessary!)
  digitalWrite(10, HIGH);                    // but turn off the W5100 chip!

  if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");

  // initialize a FAT volume
  if (!volume.init(&card)) error("vol.init failed!");

  PgmPrint("Volume is FAT");
  //Serial.println(volume.fatType(),DEC);
  //Serial.println();

  if (!root.openRoot(&volume)) error("openRoot failed");

  // list file in root with date and size
  PgmPrintln("Files found in root:");
  root.ls(LS_DATE | LS_SIZE);
  //Serial.println();

  // Recursive list of all directories
  PgmPrintln("Files found in all dirs:");
  root.ls(LS_R);

  //Serial.println();
  PgmPrintln("Done");

  // Debugging complete, we start the server!
  Ethernet.begin(mac, ip);
  server.begin();
  
  // Start up the temperature library
  sensorsa.begin();
  sensorsb.begin();
  sensorsc.begin();
  sensorsd.begin();
  
  setTime(0); // start the clock
  time33mins = time60mins = now();

}
Ejemplo n.º 11
0
void setup(void) {
  Serial.begin(BPS_115200);
  Serial.println();
  PgmPrintln("Type any character to start");
  while (!Serial.available());
  
  //PgmPrint("FreeRam: ");
  //Serial.println(FreeRam());
  
  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!card.init(SPI_HALF_SPEED)) error("card.init failed");

  // initialize a FAT volume
  if (!volume.init(&card)) error("volume.init failed");

  // open the root directory
  if (!root.openRoot(&volume)) error("openRoot failed");
  
  strcpy_P(buf, PSTR("APPEND.TXT"));
  // open for read
  if (!from.open(&root, buf, O_READ)) {
    PgmPrint("Can't open "); 
    Serial.println(buf);
    PgmPrintln("Run the append example to create the file.");
    error("from.open failed");
  }
  strcpy_P(buf, PSTR("ACOPY.TXT"));
  // create if needed, truncate to zero length, open for write
  if (!copy.open(&root, buf, O_CREAT | O_TRUNC | O_WRITE)) {
    error("copy.open failed");
  }
  // count for printing periods
  uint16_t p = 0;
  int16_t n;  
  while ((n = from.read(buf, sizeof(buf))) > 0) {
    if (copy.write(buf, n) != n) error("write failed");
    // print progress periods
    if (!(p++ % 25)) Serial.print('.');
    if (!(p % 500)) Serial.println();
  }
  Serial.println();
  if (n != 0) error ("read");
  // force write of directory entry and last data
  if (!copy.close()) error("copy.close failed");
  PgmPrintln("Copy done.");
}
Ejemplo n.º 12
0
/**
* initialize
* Initialize Writter and SD Card behavior
* @return {void}
*/
bool SDWriter::initialize()
{
	Sd2Card card;
	card.init(SPI_HALF_SPEED, pin);

	SdVolume volume;
  	volume.init(card);
  
  	volumesize = volume.blocksPerCluster() * volume.clusterCount() * 512;

	if (!SD.begin(pin))
		state = false;
	else
		state = true;

	return state;
}
Ejemplo n.º 13
0
// ----------------------------------------------------------------------------
//	Scan the SD card and open the volume
//	Set reg[STATUS] to FDC_ST_NOTREADY if no card present
// ----------------------------------------------------------------------------
void scanSD()
{
	if (!card.init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN))
	{
		Serial.print("Init failed, error:");
		Serial.println(card.errorCode());
		mb8877.reg[STATUS] = FDC_ST_NOTREADY;
		return;
	}

#ifdef SD_DEBUG
	Serial.print("\nCard type: ");
	switch(card.type()) {
		case SD_CARD_TYPE_SD1: Serial.println("SD1"); break;
		case SD_CARD_TYPE_SD2: Serial.println("SD2"); break;
		case SD_CARD_TYPE_SDHC: Serial.println("SDHC"); break;
		default: Serial.println("Unknown");
	}
#endif

	if (!volume.init(card)) {
#ifdef SD_DEBUG
		Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
#endif
		mb8877.reg[STATUS] = FDC_ST_NOTREADY;
		return;
	}

#ifdef SD_DEBUG
	// ----- Print the type and size of the first FAT-type volume
	Serial.print("\nVolume type is FAT");
	Serial.println(volume.fatType(), DEC);
	Serial.println();

	long volumesize;
	volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
	volumesize *= volume.clusterCount();       // we'll have a lot of clusters
	volumesize *= 512;                            // SD card blocks are always 512 bytes
	Serial.print("Volume size (bytes): ");
	Serial.println(volumesize);
#endif
	root.openRoot(volume);
	mb8877.reg[STATUS]=0x00;
}
Ejemplo n.º 14
0
void setup() {
  Serial.begin(BPS_115200);
  PgmPrintln("Type any character to start");
  while (!Serial.available());
  
  // initialize the SD card at SPI_FULL_SPEED for best performance.
  // try SPI_HALF_SPEED if bus errors occur.
  if (!card.init(SPI_FULL_SPEED)) error("card.init failed");
  
  // initialize a FAT volume
  if (!volume.init(&card)) error("volume.init failed");

  // open the root directory
  if (!root.openRoot(&volume)) error("openRoot failed");
  
  // delete files in root if FAT32
  if (volume.fatType() == 32) {
    PgmPrintln("Remove files in root");
    deleteFiles(root);
  }
  
  // open SUB1 and delete files
  SdFile sub1;
  if (!sub1.open(&root, "SUB1", O_READ)) error("open SUB1 failed");
  PgmPrintln("Remove files in SUB1");
  deleteFiles(sub1);

  // open SUB2 and delete files
  SdFile sub2;
  if (!sub2.open(&sub1, "SUB2", O_READ)) error("open SUB2 failed");
  PgmPrintln("Remove files in SUB2");
  deleteFiles(sub2);

  // remove SUB2
  if (!sub2.rmDir()) error("sub2.rmDir failed");
  PgmPrintln("SUB2 removed");
  
  // remove SUB1
  if (!sub1.rmDir()) error("sub1.rmDir failed");
  PgmPrintln("SUB1 removed");

  PgmPrintln("Done");
}
Ejemplo n.º 15
0
void setup(void) {
    Serial.begin(9600);
    Serial.println();
    Serial.println("type any character to start");
    while (!Serial.available());
    Serial.println();

    // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
    // breadboards.  use SPI_FULL_SPEED for better performance.
    if (!card.init(SPI_HALF_SPEED)) error("card.init failed");

    // initialize a FAT volume
    if (!volume.init(&card)) error("volume.init failed");

    // open the root directory
    if (!root.openRoot(&volume)) error("openRoot failed");

    // open a file
    if (file.open(&root, "PRINT00.TXT", O_READ)) {
        Serial.println("Opened PRINT00.TXT");
    }
    else if (file.open(&root, "WRITE00.TXT", O_READ)) {
        Serial.println("Opened WRITE00.TXT");
    }
    else {
        error("file.open failed");
    }
    Serial.println();

    // copy file to serial port
    int16_t n;
    uint8_t buf[7];// nothing special about 7, just a lucky number.
    while ((n = file.read(buf, sizeof(buf))) > 0) {
        for (uint8_t i = 0; i < n; i++) Serial.print(buf[i]);
    }
    /* easier way
    int16_t c;
    while ((c = file.read()) > 0) Serial.print((char)c);
    */
    Serial.println("\nDone");
}
Ejemplo n.º 16
0
void setup() {
  Serial.begin(115200);
  while (!Serial.available()) SPARK_WLAN_Loop();
  while (Serial.available()) Serial.read();

  // initialize the SD card at SPI_FULL_SPEED for best performance.
  // try SPI_HALF_SPEED if bus errors occur.
  // Initialize HARDWARE SPI with user defined chipSelect
  if (!card.init(SPI_FULL_SPEED, chipSelect)) error("card.init failed");
  
  // Initialize SOFTWARE SPI
  //if (!card.init(mosiPin, misoPin, clockPin, chipSelect)) error("card.init failed");

  // initialize a FAT volume
  if (!volume.init(&card)) error("volume.init failed!");

  Serial.print("Type is FAT");
  Serial.println(volume.fatType(), DEC);

  if (!root.openRoot(&volume)) error("openRoot failed");
}
Ejemplo n.º 17
0
void setup() {


  Serial.begin(9600);

  pinMode(greenLEDandBEEP, OUTPUT);
  pinMode(redLEDpin, OUTPUT);

  PgmPrint("Free RAM: ");
  Serial.println(FreeRam());  

  pinMode(10, OUTPUT);              
  digitalWrite(10, HIGH);              

  if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");

  if (!volume.init(&card)) error("vol.init failed!");

  PgmPrint("Volume is FAT");
  Serial.println(volume.fatType(),DEC);
  Serial.println();

  if (!root.openRoot(&volume)) error("openRoot failed");

  PgmPrintln("Files found in root:");
  root.ls(LS_DATE | LS_SIZE);
  Serial.println();

  PgmPrintln("Files found in all dirs:");
  root.ls(LS_R);

  Serial.println();
  PgmPrintln("Done");

  Ethernet.begin(mac, ip);
  server.begin();
}
Ejemplo n.º 18
0
void setupLog() {
    if (!card.init(SPI_HALF_SPEED, SD_CS)) {
        Serial.println(F("No SD card inserted, disabling data logger."));
        disableLogging = true;
    }

    if (!disableLogging && !volume.init(card)) {
        Serial.println(F("Unable to initialize SD volume"));
        disableLogging = true;
    }

    if (!disableLogging && !root.openRoot(volume)) {
        Serial.println(F("Unable to open volume root"));
        disableLogging = true;
    }

#ifdef DEBUG
    printCardInfo();
#endif

    if (!disableLogging) {
        openLog();
    }
}
Ejemplo n.º 19
0
void setup()
{
  Serial.begin(115200);
  while (!Serial.available());

  Serial.print("\nInitializing SD card...");
  
  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  // Initialize HARDWARE SPI with user defined chipSelect
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {

  // Initialize SOFTWARE SPI (uncomment and comment out above line to use)
//  if (!card.init(mosiPin, misoPin, clockPin, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present."); 
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                         // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}
Ejemplo n.º 20
0
void setup() 
{
  Serial.begin(BPS_115200);
  Serial.println("> type any char to start");
  while (!Serial.available());
  Serial.println();

  if (!card.init(SPI_HALF_SPEED)) 
    Serial.println("> card.init failed");
  else
    Serial.println("> card.init passed");
//  spi.end();
  
  SD_SPI.begin(SPI_HIGH_CLOCK, MSBFIRST, 0);
  
  delay(100);
  
  // initialize a FAT volume
  if (!volume.init(&card,1)) 
    Serial.println("> volume.init failed");
  else
    Serial.println("> volume.init passed");
    
  // open the root directory
  if (!root.openRoot(&volume)) 
    Serial.println("> openRoot failed");
  else
    Serial.println("> openRoot passed");

  // open a file
  if (file.open(&root, "Read.txt", O_READ)) 
  {
    Serial.println("> Opened Read.txt");
    Serial.println("> Reading ...");
    Serial.println();
    for(int i=0; i<15; i++)
      Serial.print((char)file.read());
    Serial.println("");
  }
  else
  {
    Serial.println("file.open failed");
  }
  Serial.println();  
  Serial.println("> Reading remained data...");
  Serial.println();  
  int16_t n;
  uint8_t buf[7];// nothing special about 7, just a lucky number.
  while ((n = file.read(buf, sizeof(buf))) > 0) 
  {
    for (uint8_t i = 0; i < n; i++) 
      Serial.print((char)buf[i]);
  }
  /* easier way
  int16_t c;
  while ((c = file.read()) > 0) Serial.print((char)c);
  */
  file.close();
  Serial.println("\n> Done");
  SD_SPI.end();
  
}
Ejemplo n.º 21
0
void setup()
{
  Serial.begin(BPS_115200);
  PgmPrintln("Type any character to start");
  while (!Serial.available());

  Serial.print("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  //pinMode(10, OUTPUT);     // change this to 53 on a mega


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present."); 
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

  
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}
Ejemplo n.º 22
0
int main() {
  // SETUP
  init();
  Serial.begin(9600);
  tft.initR(INITR_BLACKTAB);  // initialize screen
  
  // Setting the joystick button and LEDs
  pinMode(JOYSTICK_BUTTON, INPUT);
  digitalWrite(JOYSTICK_BUTTON, HIGH);
  
  // Initialize the SD card
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    while(true) {} // something is wrong
  } 
  else {Serial.println("OK!");}
  
  // More initialization
  Serial.print("Initializing Raw SD card...");
  if (!card.init(SPI_HALF_SPEED, SD_CS)) {
    Serial.println("failed!");
    while(true) {} // something is wrong
  } 
  else {Serial.println("OK!");}
  
  // Create states for different modes
  // C1 for Mode 1 - MENU screen
  // C2 for Mode 2 - Snake Game
  // C3 for Mode 3 - GAME OVER screen
  // C4 for Mode 4 - Choose level
  // C5 for Mode 5 - PAUSE screen
  typedef enum {C1 = 1, C2, C3, C4, C5, ERR} State;
  State state = C1;
  int select, snakelength;
  
  while (state!=ERR) {
    if  (state == C1) {
      /// ====== MODE 1 - MENU ====== ///
      Serial.println("Currently in Mode 1");
      snakelength = 1;
      init_vert = analogRead(JOYSTICK_VERT); 
      init_horiz = analogRead(JOYSTICK_HORIZ);
      
      // preparations for the game - to not overlap with the pause menu
      q = q_create(720);
      i = 64; // x component
      j = 80; // y component
      q_add(q,i,j); // load into the queue
      random_x = food_x(); // load x coordinate of food piece
      random_y = food_y(); // load y coordinate of food piece
      pausedirection = 0; // set paused direction to 0
      // reset grid to 0
      for (int a = 0; a < 24; a++) {
        for (int b = 0; b < 30; b++) {
        grid[a][b] = 0;
        }
      }
      
      // display main menu
      snake();
      tft.setTextSize(2);
      
      while(true) {
        // alternate highlighting of START
        unsigned long time = millis()%1000;
        int a = time%1000;
        if ((a<17)) {
        tft.setCursor(34, 83);
        tft.fillRoundRect(30,80,65,20,5,WHITE);
        tft.setTextColor(RED);
        tft.print("START");
        }
        else if ((a>500) && (a<520)) {
        tft.setCursor(34, 83);
        tft.fillRoundRect(30,80,65,20,5,RED);
        tft.setTextColor(WHITE);
        tft.print("START");
        }
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          break;
        }
      }
      state = C4; 
    }
    
    else if (state == C2) {
      /// ====== MODE 2 - SNAKE GAME ====== ///
      Serial.println("Currently in Mode 2");
      delay(50);
      soundsetup(); //setting up sound pin
      // print the background
      tft.fillScreen(DARKRED);
      tft.fillRect(4,5,120,150,DARKGRN);
      
      // print the snake
      int x,y;
      x = q_frontx(q);
      y = q_fronty(q);
      tft.fillRect(x,y,5,5, WHITE);
      
      //Bringing the food in, outside while loop first.
      tft.fillRect(random_x, random_y, 5, 5, YELLOW);
      
      // do auto calibration
      int px, py;
      int lastmove;
      
      // read beginning direction chosen by user
      if (pausedirection == 0) {
        direction = read_direction();
      }
      else {
        direction = pausedirection;
      }
      lastmove = direction;
      
      while (true) {
        
        // to direct movement 
        // (without going in reverse direction of previous movement)
        
        // up
        if (direction == 1) {
          if (lastmove == 2) {
            direction = 2;
            j = j-5;
          }
          else {
            j = j+5;
        }
        q_add(q,i,j);
        }
        // down
        else if (direction == 2) {
          if (lastmove == 1) {
            direction = 1;
            j = j+5;
          }
          else {
            j = j-5;
          }
        q_add(q,i,j);
        }
        // right
        else if (direction == 3) {
          if (lastmove == 4) {
            direction = 4;
            i = i-5;
          }
          else {
            i = i+5;
          }
        q_add(q,i,j);
        }
        // left
        else if (direction == 4) {
          if (lastmove == 3) {
            direction = 3;
            i = i+5;
          }
          else {
            i = i-5;
          }
        q_add(q,i,j);
        }
        
        // if the direction is changed, store the new direction & last move
        int new_direc = read_direction();
        if ((new_direc != direction) && (new_direc != 0)) {
          lastmove = direction;
          direction = new_direc;
        }
        
        // if the snake hits a piece of food, the food vanishes and gets replaced 
        if ((i == random_x) && (j == random_y)) {
          // snake grows by 4 squares, except for the first time
          // this allows for it to end up as a max of 720 in the queue
          if (snakelength == 1) {
            q_add(q,i,j);
            q_add(q,i,j);
            q_add(q,i,j);
            snakelength += 3;
          }
          else {
            q_add(q,i,j);
            q_add(q,i,j);
            q_add(q,i,j);
            q_add(q,i,j);
            snakelength += 4;
          }
      if (snakelength < 720) {
        random_x = food_x();
        random_y = food_y();
      
        // if the snake is already there, find a new spot for the food
        while (grid[random_x/5][random_y/5-1] == 1) {
          random_x = food_x();
          random_y = food_y();
        }
        // print the new food
        tft.fillRect(random_x, random_y, 5, 5, YELLOW);
          }
        }
        
        // if the snake runs over itself
        if ((snakelength > 1) && (grid[i/5][j/5-1] == 1)) {
          delay(450); // pause when snake runs into itself
          int m = 0;
          soundLoop();
          while(m < 6000) {
            int rand_x = dissolve_x();
            int rand_y = dissolve_y();
            tft.fillRect(rand_x, rand_y, 5, 5, BLACK);
            m++;
          }
          state = C3;
          break;
        }
        
        px = q_frontx(q);
        py = q_fronty(q);
        // reprint the snake if there is movement
        if ((i != px) || (j != py)) {
          tft.fillRect(i,j,5,5, WHITE);
          grid[i/5][j/5-1] = 1;          // snake body is in grid
          tft.fillRect(px,py,5,5,DARKGRN);
          grid[px/5][py/5-1] = 0;        // snake body is no longer in grid
          q_remove(q);                   // take away from the queue
          delay(speed);                  // controls the speed of the snake
        }
       
        // if any of the borders are hit
        if ((i < 4)||(j < 5)||(i > 119)||(j > 150)) {
          delay(450); // pause when border is hit
          // dissolve the screen
          int m = 0;
          soundLoop();
          while(m < 6000) {
            int rand_x = dissolve_x();
            int rand_y = dissolve_y();
            tft.fillRect(rand_x, rand_y, 5, 5, BLACK);
            m++;
          }
          //~ delay(250);
          state = C3; 
          break;
        }
        
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          state = C5;
          break;
        }
      }
    }
    
    else if (state == C3) {
      /// ====== MODE 3 - GAME OVER ====== ///
      Serial.println("Currently in Mode 3");
      q_destroy(q); // clear the queue
      tft.fillScreen(BLACK);
      tft.fillRoundRect(5,20,118,25,5,RED);
      tft.setCursor(10, 25); 
      tft.setTextColor(BLACK);
      tft.setTextSize(2);
      tft.setTextWrap(true);
      tft.print("GAME OVER");
      tft.print("\n"); 
      
      tft.setCursor(10, 55);
      tft.setTextColor(RED);
      tft.setTextSize(1.5);
      if (snakelength >= 720) {
        snakelength = 720;
        tft.print("YOU WON! CONGRATZ");
      }
      else {
        tft.print("      Oh no!         You hit something!");
      }
      
      tft.setCursor(10, 80);
      tft.setTextColor(WHITE);
      tft.setTextSize(1);
      tft.print("Length of Snake:");
      tft.print(snakelength);
      tft.setCursor(10, 100);
      tft.print("Press the joystick   to return to main    menu");
      
      // Read the Joystick - HIGH if not pressed, LOW otherwise
      while (true) {
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          break;
        }
      }
      state = C1;
    }
    
    else if (state == C4) {
      /// ====== MODE 4 - CHOOSE LEVEL ====== ///
      Serial.println("Currently in Mode 4");
      // printing
      // snake display
      snake();
      // difficulty levels
      tft.setTextSize(2);  
      tft.setTextColor(WHITE);
      easy(RED);
      tft.setTextColor(RED);
      medium(WHITE);
      hard(WHITE);
      
      int selection = 1;
      int oldselection;
      while(true) {
        // read direction from the user for updating selection
        oldselection = selection;
        vertical = analogRead(JOYSTICK_VERT);      // will be 0-1023
        delay(100);
        
        // scroll down
        if (vertical > init_vert + 200) {
        selection++;
          if (selection > 3) {
            selection = 0;
          }
        } 
        // scroll up
        else if (vertical < init_vert - 200) {
          selection--;
          if (selection < 0) {
            selection = 3;
          }
        }
        
        if (selection != oldselection) {
          update(selection);
        }
        
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          Serial.print("made selection: ");
          Serial.println(selection);
          if (selection == 1) {speed = 225;}
          else if (selection == 2) {speed = 150;}
          else if (selection == 3) {speed = 75;}
          break;
        }
      }
      state = C2;
    }
    
    else if (state == C5) {
      /// ====== MODE 5 - PAUSE MENU ====== ///
      Serial.println("Currently in Mode 5");
      pausedirection = direction;
      
      // printing snake and pause
      snake();
      tft.setTextSize(2);
      tft.setCursor(34, 73); 
      tft.fillRoundRect(30,70,65,20,5,WHITE);
      tft.setTextColor(RED);
      tft.print("Pause");
      
      while(true) {
        // Read the Joystick - HIGH if not pressed, LOW otherwise
        select = digitalRead(JOYSTICK_BUTTON);     
        if (select == LOW) {
          break;
        }
      }
      // reset grid to 0
      for (int a = 0; a < 24; a++) {
        for (int b = 0; b < 30; b++) {
        grid[a][b] = 0;
        }
      }
      state = C2; 
    }
    //if not any of this:
    else { 
      Serial.println("There has been an error");
      state = ERR; 
    }
  }
    
  Serial.end();
  return 0;
}
Ejemplo n.º 23
0
void setup(void) {
	Serial.begin(9600);
  
	// set buttonPin to INPUT and 
	// turn on internal pull up resistor 
	pinMode(BUTTJOY, INPUT);
	digitalWrite(BUTTJOY, HIGH);
  
	// establish interrupts on joystick button falling (when the button is initially pushed)
	// NOTE: BUTTJOY has been set up with a debounced circut
	attachInterrupt(BUTTJOY, buttonPress, FALLING);
  
	// Define the cursor radius to be 2 pixles
	cursor.r = CURSOR_RADIUS;
  
	// Read initial joystick position
	iniJoy.x = analogRead(HORZJOY);
	iniJoy.y = analogRead(VERTJOY);
  
	// If your TFT's plastic wrap has a Red Tab, use the following:
	tft.initR(INITR_REDTAB);   // initialize a ST7735R chip, red tab
	// If your TFT's plastic wrap has a Green Tab, use the following:
	//tft.initR(INITR_GREENTAB); // initialize a ST7735R chip, green tab

	#if DEBUG
	Serial.print("Initializing SD card...");
	#endif
	if (!SD.begin(SD_CS))
	{
		Serial.println("failed!");
		return;
	}
	#if DEBUG
	Serial.println("OK!");
	#endif
  
	// test out reading blocks from the SD card
	if (!card.init(SPI_HALF_SPEED, SD_CS)) {
		#if DEBUG
		Serial.println("Raw SD Initialization has failed");
		#endif
		while (1) {};  // Just wait, stuff exploded.
	}
	
	// Centers map on pre-selected coord
	m_map.x = m_map.x - tft.width()/2;
	m_map.y = m_map.y - tft.height()/2;
	
	// Places cursor in the center of the screen
	cursor.position.x = (int) tft.width()/2.;
	cursor.position.y = (int) tft.height()/2.;

	// clear to blue
	tft.fillScreen(tft.Color565(137, 207, 240));
	
	// Added for debug purposes
	#if DEBUG
	Serial.print("TFT Height: ");
	Serial.print( tft.height() );
	Serial.print(", TFT Width: ");
	Serial.println( tft.width() );
	#endif

	// Draw initial screen
	lcd_image_draw(&map_image, &tft, &m_map, &c_zero,  tft.width(), tft.height());
	// Draw Cursor on map
	drawCursor(&tft, &cursor);
}
Ejemplo n.º 24
0
void setup() {
  Serial.begin(BPS_115200);
  PgmPrintln("Type any character to start");
  while (!Serial.available());
  
 // PgmPrint("Free RAM: ");
 /// Serial.println(FreeRam());  
 
  // initialize the SD card at SPI_FULL_SPEED for best performance.
  // try SPI_HALF_SPEED if bus errors occur.
  if (!card.init(SPI_FULL_SPEED)) error("card.init failed");
  
  // initialize a FAT volume
  if (!volume.init(&card)) error("volume.init failed!");

  PgmPrint("Type is FAT");
  Serial.println(volume.fatType(), DEC);
  
  if (!root.openRoot(&volume)) error("openRoot failed");
  
  // open or create file - truncate existing file.
  if (!file.open(&root, "BENCH.DAT", O_CREAT | O_TRUNC | O_RDWR)) {
    error("open failed");
  }
  
  // fill buf with known data
  for (uint16_t i = 0; i < (BUF_SIZE-2); i++) {
    buf[i] = 'A' + (i % 26);
  }
  buf[BUF_SIZE-2] = '\r';
  buf[BUF_SIZE-1] = '\n';
  
  PgmPrint("File size ");
  Serial.print(FILE_SIZE_MB);
  PgmPrintln(" MB");
  PgmPrintln("Starting write test.  Please wait up to a minute");
  
  // do write test
  uint32_t n = FILE_SIZE/sizeof(buf);
  uint32_t t = millis();
  for (uint32_t i = 0; i < n; i++) {
    if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
      error("write failed");
    }
  }
  t = millis() - t;
  file.sync();
  double r = (double)file.fileSize()/t;
  PgmPrint("Write ");
  Serial.print(r);
  PgmPrintln(" KB/sec");
  Serial.println();
  PgmPrintln("Starting read test.  Please wait up to a minute");
  
  // do read test
  file.rewind();
  t = millis();
  for (uint32_t i = 0; i < n; i++) {
    if (file.read(buf, sizeof(buf)) != sizeof(buf)) {
      error("read failed");
    }
  }
  t = millis() - t;
  r = (double)file.fileSize()/t;
  PgmPrint("Read ");
  Serial.print(r);
  PgmPrintln(" KB/sec");
  PgmPrintln("Done");
}
Ejemplo n.º 25
0
void setup() {

    int logNo;
	char configLineBuffer[LINE_BUFFER_MAX];
    spi.begin(SPI_281_250KHZ, MSBFIRST, 0);
	
	pinMode(GRN_LED,OUTPUT);
	pinMode(ORN_LED,OUTPUT);
	pinMode(RED_LED,OUTPUT);
	digitalWrite(GRN_LED,HIGH);
	digitalWrite(ORN_LED,LOW);
	digitalWrite(RED_LED,LOW);

    iwdg_init(IWDG_PRE_256, WATCHDOG_TIMEOUT);
    Watchdog_Reset();

	
    if (!card.init(&spi)) { 
    //if (!card.init()) { 
        console.printf("FTL: card.init failed");
    }
    delay(100);
    
    // initialize a FAT volume
    if (!volume.init(&card)) {
        console.printf("FTL: volume.init failed");
    }
    
    // open the root directory
    if (!root.openRoot(&volume)) 
        ;//SerialUSB.println("FTL: openRoot failed");
    
    for (logNo=0; (!logOpened) && logNo<512; logNo++) {
        Watchdog_Reset();
       //int snprintf(char *str, size_t size, const char *format, ...);
        snprintf(logFileName,15,"LOG%03d.TXT",logNo);
        if (file.open(&root, logFileName, O_READ)) {
            //SerialUSB.print("DBG: Exists  :"); SerialUSB.println(logFileName);
            file.close();
        } else if (file.open(&root, logFileName, O_CREAT|O_READ|O_WRITE)) {
            //SerialUSB.print("DBG: New File:"); SerialUSB.println(logFileName); 
            logOpened=true;
            file.sync();
            file.close();
            file.open(&root,logFileName,O_WRITE|O_READ);
			while (file.read(configLineBuffer,LINE_BUFFER_MAX)) {
				
			}
            file.sync();
        }    
    }
    //if (!logOpened) SerialUSB.println("FTL: openRoot failed");

	digitalWrite(GRN_LED,LOW);
	digitalWrite(RED_LED,HIGH);
    readSettings();
	console.printf("LSV:" BOM_VERSION "\r\n");
    console.printf("NST: %s\r\n",networkStatus()?"CONNECTED":"NOT CONNECTED");
	digitalWrite(ORN_LED,HIGH);
	digitalWrite(RED_LED,networkStatus()?HIGH:LOW);
	
}