Пример #1
0
void deleteFile(GL_HBUTTON objectPtr, GL_HEVENT e)
{
    UINT8 id;

    if (objectPtr == &fileBtn_0) {
        id = 0;
    } else if (objectPtr == &fileBtn_1) {
        id = 1;
    } else if (objectPtr == &fileBtn_2) {
        id = 2;
    } else if (objectPtr == &fileBtn_3) {
        id = 3;
    } else if (objectPtr == &fileBtn_4) {
        id = 4;
    } else {
        return;
    }
    // delete file linked with button
    if (buttonFileName[id][0] != '\0') {
        FAT_FileOpen(buttonFileName[id], DELETE);
        FAT_FileClose();
        DELETE_BEEP();
    }
    loadFiles();
    redraw();
}
Пример #2
0
void DC16_RX_Mode (void)
{
  unsigned int i, j, k;
  volatile UINT16 u16Error;
  UINT16 u16CRC;
  UINT32 fileSize, progressCount, ledcnt;

  RTCSC_RTCPS = 0xA; // re-enable RTC (set prescaler = /4, interrupt every 4ms)
  IR_TX_Off;
      
  // enable IR receive mode
  // prepare TPM2 to be used for IR TPM2MOD = IR_RC_PERIOD; TPM2C1 as falling edge
  ir_init(IR_RC_PERIOD);    
 
  (void)memset(gau8Minicom, 0x00, MINICOM_BUFFER_SIZE); // clear buffer

  // wait to start receiving data
  // and when we do, get filename (should be in 8.3 format + NULL)
  k = 0; 
  do
  {
    while (!ir_rx_byte) // wait to start receiving IR data...
    {
      if (SD_PRESENT == HIGH) // if no SD card, exit mode
      {
        delay_ms(500);
        return;
      }
        
      if (!SW_MODE)
      {
        delay_ms(50); // poor man's debounce
          
        while (!SW_MODE)
        {
          IR_RX_Off; // disable RX when we're done
          led_state = ALL_OFF;
          state_change_flag = 1; // go to the TX state, since we haven't started receiving yet
       }
        return; // get outta here!
      }
    }

    // if we get here, that means we've received a valid byte
    ir_rx_byte = FALSE;

    gau8Minicom[k] = ir_command;
    ++k;
  } while ((ir_command != '\0') && (k < 13));
  gau8Minicom[k-1] = '\0'; // add a null in case we've received incorrect filename data

  if (gau8Minicom[0] == '\0') // if no filename is received, abort transfer
  {
      if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("Filename invalid.\n\r");
      DC16_Error(errRX, 0);
      return;     
  }
  
  // ensure the filename is uppercase before working with it
  // gotta love FAT16!
  for (j = 0; gau8Minicom[j] != '\0'; ++j)
    gau8Minicom[j] = (UINT8)toupper(gau8Minicom[j]);
     
  // get file size (4 bytes long)
  fileSize = 0;
  for (i = 0; i < 4; ++i)
  {
    timeout = 0;
    while (!ir_rx_byte) // wait to start receiving IR data...
    {
      if (timeout == 1250) // if we haven't received a byte for ~5 seconds, abort
      {             
        DC16_Error(errRX, 0);
        return;                          
      }
        
      if (!SW_MODE)
      {
        delay_ms(50); // poor man's debounce
          
        while (!SW_MODE)
        {
          IR_RX_Off; // disable RX when we're done
          led_state = ALL_OFF;
          state = TX; // go to SLEEP mode to avoid sending data as soon as we cancel a receive
          state_change_flag = 1;
        }
        return; // get outta here!
      }
    }

    // if we get here, that means we've received a valid byte
    ir_rx_byte = FALSE;

    fileSize <<= 8;
    fileSize |= ir_command; 
  }
  
  if (fileSize == 0xFFFFFFFF) // we get this if one badge receives another badge's power on IR test string (the Sony TV power off code), so just ignore it
  {
    delay_ms(500);
    return;
  }
  //else if ((fileSize > FAT16_MAX_FILE_SIZE) || (fileSize == 0)) // if the received file size is bigger than the possible FAT16 limit, we obviously have a corrupted transfer
  else if ((fileSize > 131072) || (fileSize == 0)) // limit filesize to 128KB to reduce transfer errors and prevent people from standing in front of each other for hours!
  {
     // so abort...
     if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("Filesize error!\n\r");
     DC16_Error(errRX, 0);
     return;        
  }
  
  // if the filename and file size appear to be valid, we can now proceed...
  if (SD_PRESENT != HIGH)
  {
    u16Error=SD_Init(); // SD card init
    if (u16Error == OK) 
    {
      FAT_Read_Master_Block(); // FAT driver init
    }
    else 
    {
      DC16_Error(errSD, u16Error);
      return;
    }
    
    // see if a file already exists with the name we've just received
    u16Error=FAT_FileOpen(gau8Minicom, READ); 
    if (u16Error == FILE_FOUND) 
    { 
      for (j = '0'; j <= '9'; ++j)
      {
        // replace the last character in the filename with a numeral
        // this will let us receive 10 more files with the same name (0-9) 
        gau8Minicom[k-2] = (UINT8)j; // k is the index of the filename string         
        u16Error=FAT_FileOpen(gau8Minicom, READ); 
    
        if (u16Error != FILE_FOUND) break; // once we've found a filename that doesn't exist, we can move on...
      }
    }
          
    if (WP_ENABLED == HIGH)
    {
      if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("SD card write protect enabled.\n\r");
      DC16_Error(errRX, 0);
      return;    
    }
    else
    {
      // create a new file on the SD card
      u16Error=FAT_FileOpen(gau8Minicom, CREATE); 
      if (u16Error != FILE_CREATE_OK) 
      { 
        DC16_Error(errFAT, u16Error);
        return;
      }
    }
  }
  else
  {
    if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("No SD card inserted.\n\r");
    state = TX; // go to SLEEP mode to avoid receiving unsynchronized data
    state_change_flag = 1;
    return;
  }
  
  if (usb_enabled_flag && USB_DETECT)
  { 
    Terminal_Send_String("File name: ");
    Terminal_Send_String(gau8Minicom);
    Terminal_Send_String("\n\r");
     
    itoa(fileSize, gau8Minicom, 12);      
    Terminal_Send_String("File size: ");
    Terminal_Send_String(gau8Minicom);
    Terminal_Send_String(" bytes\n\r");
    Terminal_Send_String("Starting IR file receive.\n\r");
  }
  
  led_state = IGNORE; // set this so the timer interrupt routine doesn't mess with our LED output
  do_cool_LED_stuff(ALL_OFF); // we're going to use LEDs as a transfer progress indicator, so we want complete control

  progressCount = fileSize >> 3;
  ledcnt = 0;
  gu8Led = 0x00;
  
  if (fileSize >= MINICOM_BUFFER_SIZE)
  {
    do
    {
      // assuming each block sent is MINICOM_BUFFER_SIZE (512 bytes), except for the last one which might be shorter
      for (i = 0; i < MINICOM_BUFFER_SIZE; ++i) 
      {  
        timeout = 0;  
        while (!ir_rx_byte) // wait to start receiving IR data...
        {
          if (timeout == 1250) // if we haven't received a byte for ~5 seconds, abort
          {
            FAT_FileClose();
            DC16_Error(errRX, 0);
            return;                          
          }
          
          if (!SW_MODE)
          {
            delay_ms(50); // poor man's debounce
              
            while (!SW_MODE)
            {
              IR_RX_Off; // disable RX when we're done
              led_state = ALL_OFF;
              state = TX; // go to SLEEP mode to avoid sending data as soon as we cancel a receive
              state_change_flag = 1;
            }
            
            FAT_FileClose(); // properly close the file if we're aborting the transfer
            return; // get outta here!
          }
        }

        // if we get here, that means we've received a valid byte
        ir_rx_byte = FALSE; 
        gau8Minicom[i] = ir_command;
        fileSize--;
        ledcnt++;
        if (ledcnt == progressCount) // decrement LEDs as transfer progresses
        {
          ledcnt = 0;
          gu8Led <<= 1;
          gu8Led |= 1;
          do_cool_LED_stuff(BYTE);
        }    
      }
      
      u16CRC = DC16_RX_CRC();
      if (u16CRC == 0) return;
      
      // calculate CRC16 checksum on this block 
      j = crc16_ccitt(gau8Minicom, MINICOM_BUFFER_SIZE);
      
      if (usb_enabled_flag && USB_DETECT)
      {
        Terminal_Send_String("\n\rCRC16 = 0x");
        Terminal_Send_String(num2asc((j >> 8) & 0xFF));
        Terminal_Send_String(num2asc(j & 0xFF));
      }    
       
      // if it doesn't match what we've just received, abort the transfer
      if (u16CRC != j)
      {
        FAT_FileClose();
        DC16_Error(errRX, 0);
        return;      
      }
      else // if it matches, write the data to the card
      {
        FAT_FileWrite(gau8Minicom,MINICOM_BUFFER_SIZE); 
      }
    } while (fileSize >= MINICOM_BUFFER_SIZE);
  }