|
| 1 | +package com.github.sandeepmistry.arduino.nRF5; |
| 2 | + |
| 3 | +import cc.arduino.packages.Uploader; |
| 4 | +import cc.arduino.packages.uploaders.SerialUploader; |
| 5 | + |
| 6 | +import java.awt.Dimension; |
| 7 | + |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.io.File; |
| 12 | + |
| 13 | +import java.lang.Runnable; |
| 14 | +import java.lang.Thread; |
| 15 | + |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.nio.file.Paths; |
| 19 | + |
| 20 | +import java.net.URL; |
| 21 | + |
| 22 | +import javax.swing.JOptionPane; |
| 23 | +import javax.swing.JScrollPane; |
| 24 | +import javax.swing.JTextArea; |
| 25 | + |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.zip.ZipInputStream; |
| 29 | +import java.util.zip.ZipEntry; |
| 30 | + |
| 31 | +import processing.app.Editor; |
| 32 | +import processing.app.PreferencesData; |
| 33 | +import processing.app.tools.Tool; |
| 34 | + |
| 35 | +public class nRF5FlashSoftDevice implements Tool { |
| 36 | + private Map<String, String> urls = new HashMap<String, String>(); |
| 37 | + private Editor editor; |
| 38 | + |
| 39 | + public void init(Editor editor) { |
| 40 | + urls.put("s110", "http://www.nordicsemi.com/eng/content/download/80234/1351257/file/s110_nrf51_8.0.0.zip"); |
| 41 | + urls.put("s130", "http://www.nordicsemi.com/eng/content/download/95150/1606929/file/s130_nrf51_2.0.0.zip"); |
| 42 | + urls.put("s132", "http://www.nordicsemi.com/eng/content/download/95151/1606944/file/s132_nrf52_2.0.0.zip"); |
| 43 | + |
| 44 | + this.editor = editor; |
| 45 | + } |
| 46 | + |
| 47 | + public String getMenuTitle() { |
| 48 | + return "nRF5 Flash SoftDevice"; |
| 49 | + } |
| 50 | + |
| 51 | + public void run() { |
| 52 | + if (!PreferencesData.get("target_platform").equals("nRF5")) { |
| 53 | + editor.statusError(getMenuTitle() + " is only supported on 'Nordic Semiconductor nRF5 Boards' boards!"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + String softDevice = PreferencesData.get("custom_softdevice"); |
| 58 | + if (softDevice == null || softDevice.endsWith("none")) { |
| 59 | + editor.statusError("No SoftDevice selected!"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + String programmer = PreferencesData.get("programmer"); |
| 64 | + if (programmer == null || !programmer.startsWith("sandeepmistry:")) { |
| 65 | + editor.statusError("Unsupported programmer!"); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + softDevice = softDevice.substring(softDevice.lastIndexOf("_") + 1); |
| 70 | + String url = urls.get(softDevice); |
| 71 | + |
| 72 | + if (url == null) { |
| 73 | + editor.statusError("Unsupported SoftDevice!"); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + Path softdevicePath = Paths.get(PreferencesData.get("runtime.platform.path"), "cores", "nRF5", "SDK", "components", "softdevice", softDevice, "hex"); |
| 78 | + |
| 79 | + Runnable runnable = () -> { |
| 80 | + try { |
| 81 | + if (Files.list(softdevicePath).count() < 3) { |
| 82 | + System.out.println("Downloading '" +url + "' ..."); |
| 83 | + |
| 84 | + InputStream is = new URL(url).openStream(); |
| 85 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 86 | + |
| 87 | + byte[] chunk = new byte[32 * 1024]; |
| 88 | + int bytesRead; |
| 89 | + |
| 90 | + while ((bytesRead = is.read(chunk)) > 0) { |
| 91 | + System.out.print("."); |
| 92 | + outputStream.write(chunk, 0, bytesRead); |
| 93 | + } |
| 94 | + |
| 95 | + System.out.println("done"); |
| 96 | + |
| 97 | + ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(outputStream.toByteArray())); |
| 98 | + ZipEntry entry; |
| 99 | + |
| 100 | + String licenseAgreementFilename = ""; |
| 101 | + StringBuilder licenseAgreementBuilder = new StringBuilder(); |
| 102 | + |
| 103 | + String softdeviceFilename = ""; |
| 104 | + StringBuilder softdeviceBuilder = new StringBuilder(); |
| 105 | + |
| 106 | + while ((entry = zis.getNextEntry()) != null) { |
| 107 | + if (entry.getName().endsWith("agreement.txt")) { |
| 108 | + licenseAgreementFilename = entry.getName(); |
| 109 | + |
| 110 | + for (int i = 0; i < entry.getSize(); i++) { |
| 111 | + licenseAgreementBuilder.append((char)zis.read()); |
| 112 | + } |
| 113 | + } else if (entry.getName().endsWith("softdevice.hex")) { |
| 114 | + softdeviceFilename = entry.getName(); |
| 115 | + |
| 116 | + for (int i = 0; i < entry.getSize(); i++) { |
| 117 | + softdeviceBuilder.append((char)zis.read()); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + JTextArea textArea = new JTextArea(licenseAgreementBuilder.toString()); |
| 123 | + textArea.setColumns(80); |
| 124 | + textArea.setRows(50); |
| 125 | + textArea.setLineWrap(true); |
| 126 | + textArea.setWrapStyleWord(true); |
| 127 | + textArea.setSize(textArea.getPreferredSize().width, 1); |
| 128 | + |
| 129 | + JScrollPane scrollPane = new JScrollPane(textArea); |
| 130 | + scrollPane.setPreferredSize( new Dimension(textArea.getPreferredSize().width, 500 ) ); |
| 131 | + |
| 132 | + int result = JOptionPane.showOptionDialog(null, scrollPane, "NORDIC SEMICONDUCTOR ASA SOFTDEVICE LICENSE AGREEMENT", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{"Accept", "Decline"}, "Decline"); |
| 133 | + |
| 134 | + if (result != 0) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + Files.write(Paths.get(softdevicePath.toString(), licenseAgreementFilename ), String.valueOf(licenseAgreementBuilder).getBytes()); |
| 139 | + Files.write(Paths.get(softdevicePath.toString(), softdeviceFilename), String.valueOf(softdeviceBuilder).getBytes()); |
| 140 | + } |
| 141 | + |
| 142 | + Uploader uploader = new SerialUploader(); |
| 143 | + if (uploader.burnBootloader()) { |
| 144 | + editor.statusNotice("Done flashing SoftDevice."); |
| 145 | + } else { |
| 146 | + editor.statusError("Error while flashing SoftDevice."); |
| 147 | + } |
| 148 | + } catch (Exception e) { |
| 149 | + editor.statusError("Error while flashing SoftDevice."); |
| 150 | + System.err.println(e); |
| 151 | + return; |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + Thread thread = new Thread(runnable); |
| 156 | + thread.start(); |
| 157 | + } |
| 158 | +} |
0 commit comments