<commit_before>/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
  AvrdudeUploader - uploader implementation using avrdude
  Part of the Arduino project - http://www.arduino.cc/

  Copyright (c) 2004-05
  <NAME>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  
  $Id$
*/

package processing.app.debug;

import processing.app.Base;
import processing.app.Preferences;
import processing.app.Serial;
import processing.app.SerialException;
import static processing.app.I18n._;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import gnu.io.*;


public class AvrdudeUploader extends Uploader  {
  public AvrdudeUploader() {
  }

  public boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer)
  throws RunnerException, SerialException {
    this.verbose = verbose;
    Map<String, String> boardPreferences = Base.getBoardPreferences();

    // if no protocol is specified for this board, assume it lacks a 
    // bootloader and upload using the selected programmer.
    if (usingProgrammer || boardPreferences.get("upload.protocol") == null) {
      String programmer = Preferences.get("programmer");
      Target target = Base.getTarget();

      if (programmer.indexOf(":") != -1) {
        target = Base.targetsTable.get(programmer.substring(0, programmer.indexOf(":")));
        programmer = programmer.substring(programmer.indexOf(":") + 1);
      }
      
      Collection params = getProgrammerCommands(target, programmer);
      params.add("-Uflash:w:" + buildPath + File.separator + className + ".hex:i");
      return avrdude(params);
    }

    return uploadViaBootloader(buildPath, className);  
  }
  
  private boolean uploadViaBootloader(String buildPath, String className)
  		throws RunnerException, SerialException {
    Map<String, String> boardPreferences = Base.getBoardPreferences();
    List commandDownloader = new ArrayList();
    String protocol = boardPreferences.get("upload.protocol");
    
    // avrdude wants "stk500v1" to distinguish it from stk500v2
    if (protocol.equals("stk500"))
      protocol = "stk500v1";      
    
    String uploadPort = Preferences.get("serial.port");

    // need to do a little dance for Leonardo and derivatives:
    // open then close the port at the magic baudrate (usually 1200 bps) first
    // to signal to the sketch that it should reset into bootloader. after doing
    // this wait a moment for the bootloader to enumerate. On Windows, also must
    // deal with the fact that the COM port number changes from bootloader to
    // sketch.
    if (boardPreferences.get("bootloader.path").equals("caterina_LUFA")) {
      String caterinaUploadPort = null;
      try {
        // Toggle 1200 bps on selected serial port to force board reset.
        List<String> before = Serial.list();
        if (before.contains(uploadPort)) {
          if (verbose)
            System.out
                .println(_("Forcing reset using 1200bps open/close on port ")
                    + uploadPort);
          Serial.touchPort(uploadPort, 1200);
        }

        // Wait for a port to appear on the list
        int elapsed = 0;
        while (elapsed < 20000) {
          List<String> now = Serial.list();
          List<String> diff = new ArrayList<String>(now);
          diff.removeAll(before);
          System.out.print("{");
          for (String p : before)
            System.out.print(p+",");
          System.out.print("} / {");
          for (String p : now)
            System.out.print(p+",");
          System.out.print("} => {");
          for (String p : diff)
            System.out.print(p+",");
          System.out.println("}");
          if (diff.size() > 0) {
            caterinaUploadPort = diff.get(0);
            System.out.println("found leo: " + caterinaUploadPort);
            break;
          }
          
          // Keep track of port that disappears
          before = now;
          Thread.sleep(500);
          elapsed += 500;
          
          // If after 4 seconds the selected port is active use that port
          if (elapsed == 4000) {
            System.out.println("using selected port: " + uploadPort);
            if (now.contains(uploadPort)) {
              caterinaUploadPort = uploadPort;
              break;
            }
          }
        }
        
        if (caterinaUploadPort == null)
          // Something happened while detecting port
          return false;
        
        uploadPort = caterinaUploadPort;
      } catch (SerialException ex) {
        return false;
      } catch (InterruptedException ex) {
        return false;
      }
    }<commit_msg>Added error messages for Leonardo upload<commit_after>/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
  AvrdudeUploader - uploader implementation using avrdude
  Part of the Arduino project - http://www.arduino.cc/

  Copyright (c) 2004-05
  <NAME>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  
  $Id$
*/

package processing.app.debug;

import processing.app.Base;
import processing.app.Preferences;
import processing.app.Serial;
import processing.app.SerialException;
import static processing.app.I18n._;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import gnu.io.*;


public class AvrdudeUploader extends Uploader  {
  public AvrdudeUploader() {
  }

  public boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer)
  throws RunnerException, SerialException {
    this.verbose = verbose;
    Map<String, String> boardPreferences = Base.getBoardPreferences();

    // if no protocol is specified for this board, assume it lacks a 
    // bootloader and upload using the selected programmer.
    if (usingProgrammer || boardPreferences.get("upload.protocol") == null) {
      String programmer = Preferences.get("programmer");
      Target target = Base.getTarget();

      if (programmer.indexOf(":") != -1) {
        target = Base.targetsTable.get(programmer.substring(0, programmer.indexOf(":")));
        programmer = programmer.substring(programmer.indexOf(":") + 1);
      }
      
      Collection params = getProgrammerCommands(target, programmer);
      params.add("-Uflash:w:" + buildPath + File.separator + className + ".hex:i");
      return avrdude(params);
    }

    return uploadViaBootloader(buildPath, className);  
  }
  
  private boolean uploadViaBootloader(String buildPath, String className)
  		throws RunnerException, SerialException {
    Map<String, String> boardPreferences = Base.getBoardPreferences();
    List commandDownloader = new ArrayList();
    String protocol = boardPreferences.get("upload.protocol");
    
    // avrdude wants "stk500v1" to distinguish it from stk500v2
    if (protocol.equals("stk500"))
      protocol = "stk500v1";      
    
    String uploadPort = Preferences.get("serial.port");

    // need to do a little dance for Leonardo and derivatives:
    // open then close the port at the magic baudrate (usually 1200 bps) first
    // to signal to the sketch that it should reset into bootloader. after doing
    // this wait a moment for the bootloader to enumerate. On Windows, also must
    // deal with the fact that the COM port number changes from bootloader to
    // sketch.
    if (boardPreferences.get("bootloader.path").equals("caterina_LUFA")) {
      String caterinaUploadPort = null;
      try {
        // Toggle 1200 bps on selected serial port to force board reset.
        List<String> before = Serial.list();
        if (before.contains(uploadPort)) {
          if (verbose)
            System.out
                .println(_("Forcing reset using 1200bps open/close on port ")
                    + uploadPort);
          Serial.touchPort(uploadPort, 1200);
        }

        // Wait for a port to appear on the list
        int elapsed = 0;
        while (elapsed < 20000) {
          List<String> now = Serial.list();
          List<String> diff = new ArrayList<String>(now);
          diff.removeAll(before);
          System.out.print("{");
          for (String p : before)
            System.out.print(p+",");
          System.out.print("} / {");
          for (String p : now)
            System.out.print(p+",");
          System.out.print("} => {");
          for (String p : diff)
            System.out.print(p+",");
          System.out.println("}");
          if (diff.size() > 0) {
            caterinaUploadPort = diff.get(0);
            System.out.println("found leo: " + caterinaUploadPort);
            break;
          }
          
          // Keep track of port that disappears
          before = now;
          Thread.sleep(500);
          elapsed += 500;
          
          // If after 5 seconds the selected port is active use that port
          if (elapsed == 5000 && now.contains(uploadPort)) {
            System.out.println("using selected port: " + uploadPort);
            caterinaUploadPort = uploadPort;
            break;
          }
        }
        
        if (caterinaUploadPort == null)
          // Something happened while detecting port
          throw new RunnerException(
              _("Couldn’t find the selected board. Try pressing the reset button after initiating the upload."));
        
        uploadPort = caterinaUploadPort;
      } catch (SerialException e) {
        throw new RunnerException(e.getMessage());
      } catch (InterruptedException e) {
        throw new RunnerException(e.getMessage());
      }
    }
{'fraction_non_alphanumeric': 0.09922466309765553, 'fraction_numerical': 0.014214509876315303, 'mean_word_length': 2.505338078291815, 'pattern_counts': {'":': 6, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2488171', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2860, 'n_words': 973}