1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-20 14:54:31 +01:00

Merge remote-tracking branch 'origin/master' into merge-1.0.1

This commit is contained in:
Cristian Maglie 2012-10-18 20:31:51 +02:00
commit 05a2d77f15
25 changed files with 226 additions and 145 deletions

View File

@ -2596,24 +2596,43 @@ public class Base {
}
}
public void handleAddLibrary(Editor editor) {
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
fileChooser.setDialogTitle(_("Select a zip file or a folder containing the library you'd like to add"));
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
public void handleAddZipLibrary(Editor editor) {
String prompt = _("Select a zip file containing the library you'd like to add");
FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD);
fd.setDirectory(System.getProperty("user.home"));
fd.setVisible(true);
Dimension preferredSize = fileChooser.getPreferredSize();
fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
int returnVal = fileChooser.showOpenDialog(editor);
String directory = fd.getDirectory();
String filename = fd.getFile();
if (filename == null) return;
File sourceFile = new File(directory, filename);
try {
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
zipDeflater.deflate();
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
} catch (IOException e) {
editor.statusError(e);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
File sourceFile = fileChooser.getSelectedFile();
if (sourceFile.isDirectory()) {
File destinationFolder = new File(getSketchbookLibrariesFolder(), sourceFile.getName());
if (!destinationFolder.mkdir()) {
editor.statusError("Can't create folder: " + sourceFile.getName() + " into libraries folder");
return;
}
try {
FileUtils.copy(sourceFile, destinationFolder);
} catch (IOException e) {
editor.statusError(e);
return;
}
} else {
try {
ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder());
zipDeflater.deflate();
} catch (IOException e) {
editor.statusError(e);
return;
}
}
editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
}
}

View File

@ -634,10 +634,10 @@ public class Editor extends JFrame implements RunnerListener {
}
sketchMenu.add(importMenu);
item = new JMenuItem(_("Add Library from ZIP"));
item = new JMenuItem(_("Add Library..."));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
base.handleAddZipLibrary(Editor.this);
base.handleAddLibrary(Editor.this);
base.onBoardOrPortChange();
base.rebuildImportMenu(Editor.importMenu);
}
@ -693,7 +693,7 @@ public class Editor extends JFrame implements RunnerListener {
if (boardsMenu == null) {
boardsMenu = new JMenu(_("Board"));
cpuTypeMenu = new JMenu(_("CPUType"));
cpuTypeMenu = new JMenu(_("Processor"));
base.rebuildBoardsMenu(boardsMenu, cpuTypeMenu);
//Debug: rebuild imports
importMenu.removeAll();

View File

@ -1,13 +1,14 @@
package processing.app.helpers;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileUtils {
/**
* Checks, whether the child directory is a subdirectory of the base
* directory.
* Checks, whether the child directory is a subdirectory of the base directory.
*
* @param base
* the base directory.
@ -32,4 +33,49 @@ public class FileUtils {
}
return false;
}
public static void copy(File sourceFolder, File destFolder) throws IOException {
for (File file : sourceFolder.listFiles()) {
File destFile = new File(destFolder, file.getName());
if (file.isDirectory()) {
if (!destFile.mkdir()) {
throw new IOException("Unable to create folder: " + destFile);
}
copy(file, destFile);
} else {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(destFile);
byte[] buf = new byte[4096];
int readBytes = -1;
while ((readBytes = fis.read(buf, 0, buf.length)) != -1) {
fos.write(buf, 0, readBytes);
}
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
}
}
public static void recursiveDelete(File file) {
if (file.isDirectory()) {
for (File current : file.listFiles()) {
if (current.isDirectory()) {
recursiveDelete(current);
} else {
current.delete();
}
}
}
file.delete();
}
}

View File

@ -10,30 +10,36 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import processing.app.helpers.FileUtils;
public class ZipDeflater {
private final ZipFile zipFile;
private final File destFolder;
private final Random random;
private final File file;
public ZipDeflater(File file, File destFolder) throws ZipException, IOException {
this.file = file;
this.destFolder = destFolder;
this.zipFile = new ZipFile(file);
this.random = new Random();
}
public void deflate() throws IOException {
String folderName = tempFolderNameFromZip();
String tmpFolderName = folderNameFromZip() + random.nextInt(1000000);
File folder = new File(destFolder, folderName);
File tmpFolder = new File(destFolder, tmpFolderName);
if (!folder.mkdir()) {
throw new IOException("Unable to create folder " + folderName);
if (!tmpFolder.mkdir()) {
throw new IOException("Unable to create folder " + tmpFolderName);
}
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
ensureFoldersOfEntryExist(folder, entry);
File entryFile = new File(folder, entry.getName());
ensureFoldersOfEntryExist(tmpFolder, entry);
File entryFile = new File(tmpFolder, entry.getName());
if (entry.isDirectory()) {
entryFile.mkdir();
} else {
@ -58,8 +64,20 @@ public class ZipDeflater {
}
}
// Test.zip may or may not contain Test folder. We use zip name to create libraries folder. Therefore, a contained Test folder is useless and must be removed
ensureOneLevelFolder(folder);
deleteUndesiredFoldersAndFiles(tmpFolder);
// Test.zip may or may not contain Test folder. If it does, we keep it. If not, we use zip name.
ensureOneLevelFolder(tmpFolder);
}
private void deleteUndesiredFoldersAndFiles(File folder) {
for (File file : folder.listFiles()) {
if (file.isDirectory() && "__MACOSX".equals(file.getName())) {
FileUtils.recursiveDelete(file);
} else if (file.getName().startsWith(".")) {
FileUtils.recursiveDelete(file);
}
}
}
private void ensureFoldersOfEntryExist(File folder, ZipEntry entry) {
@ -73,25 +91,22 @@ public class ZipDeflater {
private void ensureOneLevelFolder(File folder) {
File[] files = folder.listFiles();
if (files.length == 1 && files[0].isDirectory()) {
File tempFile = new File(files[0].getPath() + new Random().nextInt(1000));
files[0].renameTo(tempFile);
for (File file : tempFile.listFiles()) {
file.renameTo(new File(folder, file.getName()));
}
tempFile.delete();
if (files.length != 1) {
folder.renameTo(new File(folder.getParentFile(), folderNameFromZip()));
return;
}
files[0].renameTo(new File(folder.getParentFile(), files[0].getName()));
FileUtils.recursiveDelete(folder);
}
private String tempFolderNameFromZip() {
String folderName = zipFile.getName();
if (folderName.lastIndexOf(".") != -1) {
folderName = folderName.substring(0, folderName.lastIndexOf("."));
private String folderNameFromZip() {
String filename = file.getName();
if (filename.lastIndexOf(".") != -1) {
filename = filename.substring(0, filename.lastIndexOf("."));
}
if (folderName.lastIndexOf(File.separator) != -1) {
folderName = folderName.substring(folderName.lastIndexOf(File.separator) + 1);
}
return folderName;
return filename;
}
}

BIN
app/test/Keypad_mac.zip Normal file

Binary file not shown.

Binary file not shown.

View File

@ -10,7 +10,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import processing.app.tools.ZipDeflater;
import processing.app.helpers.FileUtils;
public class ZipDeflaterTest {
@ -62,20 +62,50 @@ public class ZipDeflaterTest {
assertEquals("readme.txt", files[4]);
}
@After
public void deleteTempFolder() {
recursiveDelete(destFolder);
@Test
public void shouldDeflateMacZip() throws Exception {
File file = new File(ZipDeflater.class.getResource("/Keypad_mac.zip").getFile());
new ZipDeflater(file, destFolder).deflate();
String[] files = destFolder.list();
assertEquals(1, files.length);
assertEquals("Keypad", files[0]);
file = destFolder.listFiles()[0];
files = file.list();
assertEquals(4, files.length);
Arrays.sort(files);
assertEquals("Keypad.cpp", files[0]);
assertEquals("Keypad.h", files[1]);
assertEquals("examples", files[2]);
assertEquals("keywords.txt", files[3]);
files = new File(file, "examples").list();
assertEquals(4, files.length);
Arrays.sort(files);
assertEquals("CustomKeypad", files[0]);
assertEquals("DynamicKeypad", files[1]);
assertEquals("EventKeypad", files[2]);
assertEquals("HelloKeypad", files[3]);
}
private void recursiveDelete(File folder) {
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
recursiveDelete(file);
} else {
file.delete();
}
}
folder.delete();
@Test
public void shouldDeleteHiddenFiles() throws Exception {
File file = new File(ZipDeflater.class.getResource("/Keypad_with_hidden_files.zip").getFile());
new ZipDeflater(file, destFolder).deflate();
String[] files = destFolder.list();
assertEquals(1, files.length);
assertEquals("Keypad_with_hidden_files", files[0]);
file = destFolder.listFiles()[0];
files = file.list();
assertEquals(4, files.length);
}
@After
public void deleteTempFolder() {
FileUtils.recursiveDelete(destFolder);
}
}

View File

@ -11,6 +11,7 @@
This example code is in the public domain.
*/
String stringOne, stringTwo;
void setup() {

View File

@ -11,6 +11,7 @@
This example code is in the public domain.
*/
String txtMsg = ""; // a string for incoming text
int lastStringLength = txtMsg.length(); // previous length of the String

View File

@ -49,9 +49,11 @@ EthernetClient client;
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
char server[] = "api.cosm.com"; // name address for cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10L*1000L; // delay between updates to cosm.com
// the "L" is needed to use long type numbers
void setup() {
// start serial port:

View File

@ -51,10 +51,10 @@ EthernetClient client;
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
char server[] = "api.cosm.com"; // name address for Cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10L*1000L; // delay between updates to Cosm.com
// the "L" is needed to use long type numbers
void setup() {
// start serial port:
Serial.begin(9600);

View File

@ -39,9 +39,10 @@ EthernetClient client;
char server[] = "www.arduino.cc";
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60*1000; // delay between updates, in milliseconds
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60L*1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
void setup() {
// start serial port:

View File

@ -1,5 +1,5 @@
arduino_due_x_dbg.name=Arduino Due Dev. Ed. (Programming port)
arduino_due_x_dbg.name=Arduino Due (Programming Port)
arduino_due_x_dbg.upload.tool=bossac
arduino_due_x_dbg.upload.protocol=sam-ba
arduino_due_x_dbg.upload.maximum_size=524288
@ -16,7 +16,7 @@ arduino_due_x_dbg.build.variant_system_lib=libsam_sam3x8e_gcc_rel.a
arduino_due_x_dbg.build.vid=0x2341
arduino_due_x_dbg.build.pid=0x003e
arduino_due_x.name=Arduino Due Dev. Ed. (Native port)
arduino_due_x.name=Arduino Due (Native USB Port)
arduino_due_x.upload.tool=bossac
arduino_due_x.upload.protocol=sam-ba
arduino_due_x.upload.maximum_size=524288

View File

@ -46,17 +46,18 @@ EthernetClient client;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
// IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
char server[] = "api.cosm.com"; // name address for cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10L*1000L; // delay between updates to cosm.com
// the "L" is needed to use long type numbers
void setup() {
// start serial port:
Serial.begin(9600);
// start the Ethernet connection:
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// DHCP failed, so use a fixed IP address:

View File

@ -36,7 +36,7 @@
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
@ -48,12 +48,13 @@ EthernetClient client;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
// IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
char server[] = "api.cosm.com"; // name address for Cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10L*1000L; // delay between updates to Cosm.com
// the "L" is needed to use long type numbers
void setup() {
// start serial port:

View File

@ -28,7 +28,7 @@ byte mac[] = {
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial) {

View File

@ -1,5 +1,5 @@
/*
DHCP Chat Server
DHCP Chat Server
A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type.
@ -26,9 +26,9 @@
// gateway and subnet are optional:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress ip(192,168,1,177);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,0,0);
// telnet defaults to port 23
EthernetServer server(23);

View File

@ -19,7 +19,7 @@
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "www.google.com";
// Initialize the Ethernet client library
@ -28,7 +28,7 @@ char serverName[] = "www.google.com";
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
@ -45,8 +45,7 @@ void setup() {
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
// if you get a connection, report back via serial:
if (client.connect(serverName, 80)) {
Serial.println("connected");
// Make a HTTP request:
@ -54,7 +53,7 @@ void setup() {
client.println();
}
else {
// kf you didn't get a connection to the server:
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}

View File

@ -39,7 +39,7 @@ EthernetClient client;
void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only

View File

@ -1,5 +1,6 @@
/*
UDPSendReceive.pde:
UDPSendReceive
This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender
@ -10,24 +11,25 @@
by Michael Margolis
This code is in the public domain.
*/
#include <SPI.h> // needed for Arduino versions later than 0018
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
IPAddress ip(192, 168,1,177);
unsigned int localPort = 8888; // local port to listen on
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP

View File

@ -1,6 +1,5 @@
/*
Udp NTP Client
Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
@ -38,7 +37,7 @@ EthernetUDP Udp;
void setup()
{
// Open serial communications and wait for port to open:
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
@ -59,7 +58,7 @@ void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
// wait to see if a reply is available
delay(1000);
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it

View File

@ -52,7 +52,7 @@ void setup() {
client.println();
}
else {
// kf you didn't get a connection to the server:
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}

View File

@ -39,9 +39,10 @@ EthernetClient client;
char server[] = "www.arduino.cc";
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60*1000; // delay between updates, in milliseconds
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60L*1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
void setup() {
// start serial port:

View File

@ -22,7 +22,7 @@
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 177);
IPAddress ip(192,168,1,177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
@ -30,7 +30,7 @@ IPAddress ip(192,168,1, 177);
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
@ -67,7 +67,7 @@ void loop() {
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {

View File

@ -1,37 +0,0 @@
#include <SPI.h>
// Flash memory is connected on SPI pin SS3
#define FLASH PIN_SPI_SS3
void setup() {
Serial.begin(9600);
// Start SPI with FLASH device
SPI.begin(FLASH);
// Half clock speed: we are too fast with 1
SPI.setClockDivider(FLASH, 2);
}
void loop() {
// Send "identify" command (9f) and receive response
// on the same SPI transaction. Parameter SPI_CONTINUE
// keeps the SS pin active.
Serial.println("Sending 'Identify' cmd to flash => 9F");
SPI.transfer(FLASH, 0x9f, SPI_CONTINUE);
char a1 = SPI.transfer(FLASH, 0x00, SPI_CONTINUE);
char a2 = SPI.transfer(FLASH, 0x00, SPI_CONTINUE);
char a3 = SPI.transfer(FLASH, 0x00, SPI_CONTINUE);
char a4 = SPI.transfer(FLASH, 0x00, SPI_CONTINUE);
char a5 = SPI.transfer(FLASH, 0x00);
// Print response over serial port
Serial.print("Received signature: ");
Serial.print(a1, HEX);
Serial.print(a2, HEX);
Serial.print(a3, HEX);
Serial.print(a4, HEX);
Serial.println(a5, HEX);
delay(1000);
}