Monday, February 19, 2018

How to Compile and Upload Arduino Sketches with Notepad++ -- Simple, Convenient, Accessible


Introduction
Blind people living in the real world are constantly working out hacks and alternatives to “normal” work flows that are inaccessible in whole or in part. Arduino software development is no exception, and many of the posts in this blog have been devoted to working around the sketchy (sic) accessibility of the Arduino IDE itself. For example, in an earlier post I describe (perhaps in more detail than strictly necessary) how to set up and configure the Arduino IDE from the Windows command line. In another post, Chancey Fleet provided excellent step-by-step instructions for installing and using Visual Micro to compile and upload Arduino sketches directly from Visual Studio. The current post offers the most convenient Arduino development tool chain I have yet found –editing, compiling, and uploading sketches directly from Notepad++.

Notepad++ (hereafter Npp) is my text editor of choice for almost any simple editing work including coding Arduino. It is used by many developers who don’t want the overhead of a giant IDE just to edit a bit of code. It’s free, open source, simple to install, and largely accessible with whatever screen reader you happen to prefer. It has features such as code completion, line numbers, support for different character encodings, and many others, yet it remains extremely compact and easy to use. Perhaps my favorite feature is that it doesn’t care what kind of line endings you use, CR/LF or just LF – it just does the right thing. This is really nice when using cross-platform source files such as Arduino sketches. If you don’t know what I’m talking about, just take my word for it: Notepad++ is extremely convenient and easy to use as a code editor.

Compiling and uploading Arduino sketches from the command line is great for those of us who are really old school and feel at home typing incredibly long text-based commands. For others though, those long commands are just opportunities for typos and inexplicable DOS error messages. Similarly, for hardcore hackers who have millions of lines of code under their belts, Visual Studio might be the best Arduino development environment you could want. Speaking for myself, however, Visual Studio makes me feel like the Sorcerer’s Apprentice with too many options, settings, and terms that I don’t quite understand, and an incredibly complex interface as reins to ultimate unwanted and undeserved power. In fact, I recently hit an accidental keystroke in VS and the entire thing stopped working. Of course, if I knew what I was doing in there then I’d be able to fix it, but I don’t have that kind of time and I don’t want that kind of power. I just want to write a little Arduino code!

In contrast, Npp is just my speed. It lets me do what I need to do without giving me a dangerous amount of rope. At the same time it has hidden depths which can be quite handy. You could say that I am definitely down with Npp…

But Npp is just an editor. It is not a compiler or an integrated development environment, so it can’t compile or upload your Arduino sketches. On the other hand, it can be configured to interact with other programs on your computer (such as the Arduino IDE) which can. This article explains how to set things up to let you edit an Arduino sketch with Notepad++, and then compile and upload that sketch with a single command without ever leaving Npp. The resulting Arduino compiler output and error messages are even displayed in a Notepad++ window for easy review.

The following sections offer two scripts that can be executed from inside Notepad++. The first allows you to set the Arduino COM port – convenient since this may change frequently with different boards and USB ports. The other compiles and uploads the Arduino sketch that is currently open in Notepad++.

Setting It Up

Step 2: Download the latest version of Notepad++ (Npp) and install it. This process is easy and you should have no accessibility-related problems. Take note of the directory where you are installing it because you’ll need to find it later to install the Npp plugin.

Step 3: Download NppExec (a plugin for NPP) and install it. The installation process consists of unzipping the NppExec archive and copying the appropriate DLL into the plugins directory of the Npp installation directory. You are probably using the Unicode version of Npp, so copy the NppExec.dll file from the DLL_Unicode folder of the archive to the plugins directory of Npp and restart Npp to complete the installation. For example, my Npp plugins directory is "C:\Program Files (x86)\Notepad++\plugins"

Step 4: Set up the following Arduino-related NppExec scripts in Npp. You can either copy and paste them from below or download the NppExec scripts in this convenient archive.

The easiest way to install these scripts is to copy and paste them individually into the Execute dialog in NPP. This dialog can be opened with a keyboard shortcut (F6 inside Npp), or through Npp’s menus (Plugins > NppExec > Execute…).

The Execute dialog has a multi-line edit field for pasting or typing the script itself, a Save button for naming and saving scripts, a combo box for recalling previously saved scripts, and an OK button to execute the selected script. All the controls in this dialog are accessible with NVDA or JAWS.

The Arduino COM Port Script

This script makes it easy to quickly update the Arduino IDE’s COM port. This is the port used to upload the sketch to the board. When you invoke this script from inside Npp, a dialog pops up asking for the Arduino’s COM port. Appropriate responses are like the parameters you would use on the command line when setting the com port (e.g., com3, com4, etc.).

To set up the port selection script, open the Execute dialog by pressing F6 or by selecting Execute from the NppExec sub-menu of the Plugins menu. Paste the following script into the edit field and press the Save button. In the Save dialog, type something like “ Arduino Port,” and press return. The port selection script will then be saved for future use using that name.

Before saving, be sure to edit this script to reflect the actual location of your Arduino installation. I have mine in the root directory for convenience – “c:\arduino”.


//This NppExec script sets the com port of your Arduino IDE
//By Josh Miele -- June 27, 2016
//The Blind Arduino Project
//http://blarbl.blogspot.com
set arduino_path = c:\arduino\arduino_debug //the path to your Arduino IDE.
//put up a dialog box requesting com port name
inputbox "Please enter the COM port of your Arduino (e.g., com5):" //gets com port value
//use result to set port value doesn't verify
cmd /c "$(arduino_path)" --port $(input)
npp_console 0 //hide the console

Note: This script does no checking to make sure that you have entered a valid COM port or string. If you make an error typing or enter an invalid com port, you will get no error message or other feedback to indicate your mistake. Take care!

The Compile and Upload Script

This script saves the currently open Arduino sketch, then calls the Arduino IDE with the command line parameters to compile and upload it. It then passes any output, including errors to a new Npp file so that you can easily see what happened.

To set up the compile/upload script, open the Execute dialog by pressing F6 or by selecting Execute from the NppExec sub-menu of the Plugins menu. Paste the following script into the edit field and press the Save button. In the Save dialog, type something like “ Arduino Upload,” and press return. The compile/upload script will then be saved for future use using that name.

Before saving, be sure to edit this script to reflect the actual location of your Arduino installation. I have mine in the root directory for convenience – “c:\arduino”. Also make sure that the Arduino_output.txt file is being created in a directory where you have write privileges and where it won’t cause any problems. I put mine in “c:\temp”. The first time the script runs it will ask for permission to create the output file. After that it will quietly overwrite the file with each new execution of the script.

//This NppExec script compiles and uploads the current Arduino sketch in Npp
//By Josh Miele -- June 27, 2016
//The Blind Arduino Project
//http://blarbl.blogspot.com
//Set where the output goes. Make sure you have write privileges
set arduino_output = "c:\temp\arduino messages.txt" 
//Set location of Arduino executable. 
set arduino_path = c:\arduino\arduino_debug 
npp_save //save current file before uploading
//Compile and upload sketch in current Npp window and send stdOut and stdErr to arduino_output
cmd /c "$(arduino_path)" --upload $(full_current_path)>$(arduino_output) 2>&1
npp_console 0 //hide the console
//show the results of the compile/upload in Npp window
npp_open $(arduino_output)  

Note: This script does not account for the fact that the Arduino IDE will move your sketch if it is not already inside a folder of the same name as the stem of the script. For example, if you are editing a file called MySketch.ino located in c:\temp, executing the above NppExec compile/upload script will compile and upload the sketch, but the Arduino Ide will create a directory called c:\temp\MySketch and move the file MySketch.ino inside it. Notepad++will be left with an open file that has been moved out from under it. This has the potential to lead to unexpected errors and problems, so I recommend making sure your sketches are contained within folders of the appropriate name as modeled in the Arduino examples directory.

Using the Scripts

To invoke these scripts from inside Npp, press F6 to bring up the Execute dialog, then use your arrow keys to select the desired script from the combo box and press return. Pressing ctrl-F6 will execute the last script without invoking the execute dialog.

Note: There is a way to map individual NppExec scripts to their own keyboard shortcuts using the Shortcut Mapper under the Settings menu. Unfortunately, this dialog is not accessible using NVDA or JAWS. I believe there is a way to modify the shortcut mappings by editing one of Npp’s XML files, but I don’t yet know how to do this. In the interest of getting this info into your hands, I decided to post this anyway and update the post when I find an accessible way to map the shortcuts. If you have info about how to do this, please leave it in the comments.

References and Disclaimers
I’m no expert in any of the several areas necessary for creating elegant and effective NppExec scripts. I encourage feedback in the comments and invite suggestions for improvement and additional helpful scripts.

In developing these scripts I found the following resources extremely informative:







Configuring The Arduino IDE with the Java Access Bridge



Elsewhere in this blog I have discussed the relative inaccessibility of the Arduino IDE -- the free integrated development environment downloadable from arduino.org. Various rumors and suggestions have reached me about how it can be configured to work with the Java Access Bridge, but despite hours of effort I have never been able to do it myself.

Now, Ken Perry, blind maker, frequent advisor to the Blind Arduino Blog, and software engineer at the American Printing House for the Blind, has finally documented how to make the Arduino IDE work with a Windows screen reader. It is one of the early posts in what is bound to be an awesome blog on blind electronics.

It's great that Ken has finally told us how to get the Arduino IDE to work with the JAB, but I will probably continue to write code and upload from Notepad++ rather than use JAB with the IDE. This is mostly because I have concerns about the security of the Java Access Bridge, but I also feel like I have spent enough hours of my life fruitlessly wrestling with the JAB and don't feel like doing it any more. Maybe I'll try it some day when I'm feeling energized and long on patience.

For many people working in larger institutions such as schools or libraries, the JAB approach will also not be convenient because it requires extensive modification of environment variables and system-level files. Many system administrators will not like the configuration changes necessary to make the Java Access Bridge work with the Arduino IDE.

But the good news is that you can do it if you want, and Ken Perry is the hero of the day for finally telling us how. Thanks!

Arduino Setup and Accessibility Tips for Windows




In a world of talking iPhones, sexy accessibility announcements, and high-quality open-source screen readers, the naive sighted person could be forgiven for assuming that we have finally reached the point where a blind person could simply download and use the same exact software tools as everyone else for any given problem. Blind people know that it is rarely that simple. There are a wide variety of issues that still stand as barriers to equal access in all sorts of situations, and Arduino development on Windows is no exception. While it is definitely possible to set up an accessible development environment for Arduino on Windows, many of the steps may not be immediately obvious, especially to the beginner. 

This post is intended to streamline the Arduino setup process, flagging accessibility work-arounds, and providing a step-by-step guide to setting up the tools you’ll need for Arduino development as a blind maker. 

More advanced readers may want to check out the detailed source materials. Much of the information in this post comes from a more complete discussion of the Arduino command line on github.

If you are just getting started and don’t know anything about Windows, software development, accessibility, or Arduino, this blog is probably not the best place to start. This article assumes you’re already comfortable with Windows and your screen reader, and that you know what Arduino is and have some motivation to make things with it. 

About the Arduino IDE

Arduino sketches and many other kinds of software are usually developed in an application called an Integrated Development Environment (IDE). An IDE allows you to edit your code and turn it into a program that actually runs and does something. In the case of Arduino, the IDE compiles the source code and uploads it to the Arduino board. It also helps you find errors in your code and has many other helpful features. 

The Arduino IDE is written in Java which makes it easier to deploy on a bunch of different platforms (i.e., Windows, Mac, and Linux). Unfortunately, Java applications on Windows present some issues for screen readers, and most, like the Arduino IDE graphical user interface, are not readily accessible. It is possible to configure the Java Access Bridge for the Arduino IDE to let NVDA work quite well, but that's the topic of another post.
Luckily, the Arduino IDE also includes a command line interface that lets a blind user access most of its critical features from a Windows DOS Prompt -- that's what this article is all about.
Installing the Arduino IDE
Follow these steps to install the IDE on your Windows machine:

Make sure you have Administrator privileges. If you don’t, you will need to get the owner of the machine to install the software.
Download the Arduino IDE. It needs to be later than version 1.5 in order to support the command line interface.
After downloading, run the Arduino IDE installer and follow the prompts. This should be relatively straight-forward using NVDA or JAWS.
During installation, note the directory in which Arduino is being installed. This will be helpful when configuring the IDE, discussed in the next section.

Configuring the Arduino IDE from the DOS Prompt

The DOS Prompt is a command line interface to your Windows computer. It is a terminal window where you can type commands and the responses to those commands appear as text which scrolls up from the bottom of the window. This post assumes you have used it before and that you have some familiarity with it. If not, do a little reading starting with this nice little 

The DOS Prompt can be started by opening the Run Dialog (Windows+R) and typing cmd, followed by Return or pressing OK. 

When using the DOS Prompt with a screen reader, you should turn on the screen reader’s terminal mode. This will automatically read new text as it appears. NVDA users can toggle this mode using NVDA+5, while JAWS users should use JAWS+S. For more information on Terminal Mode, check your screen reader’s documentation.

After starting the DOS Prompt, change the current directory to the Arduino installation directory which you noted during installation. This will save you a lot of extra typing. 

Change the current directory by typing:
Cd ArduinoDirectory

Where ArduinoDirectory is the path where you installed the IDE. If you installed to the default location, you will need to put the path in double quotes since it contains spaces. For example, I used the default location:
C:\Program Files (x86)\Arduino

Since the path contains spaces, use double quotes around the directory name to let the DOS interpreter know that it all goes together like this:
cd "c:\Program Files (x86)\Arduino"

If you don’t type the directory name correctly, or if you don’t use quotes properly, the DOS interpreter will complain. If you type it correctly, the new prompt (the line of text which lets you know that it’s ready for the next command) will be the name of the Arduino directory – in my case, c:\Program Files (x86)\Arduino.

To configure the Arduino IDE from the command line, use Arduino_debug.exe, not Arduino.exe itself. This means that all of the commands we will be typing for things like Arduino configuration or uploading will begin with Arduino_debug, followed by additional command elements called command line arguments. No, we’re not being obstreperous, that’s just what they’re called.

The following sections describe how to set two important Arduino preferences: the com port to which your Arduino board is connected, and the particular Arduino board you are using. 

A Few Notes on Error Messages and GUI Pitfalls


Unless you have correctly installed the Java Access Bridge, the Graphical User Interface (GUI) of the Arduino IDE is mostly inaccessible using a screen reader, but a few bits of information do get through. Window names are about the only thing readable by NVDA or JAWS. When working with the command line interface, you may get error messages, warnings, and other unreadable GUI windows that pop up. In the case of error messages and warnings, you can simply dismiss them by hitting Return or Escape. 

You may also accidentally launch the Arduino editor if typos make it seem like you are trying to start a new sketch. In this case, a warning will be followed by an editor window which opens with the name of a new sketch based on the current date. You can dismiss this window with Alt+F4, provided that you haven’t accidentally typed anything into the editore window. Otherwise, the IDE will put up an additional unreadable dialog asking if you want to save your changes to the sketch. You don’t, so simply hit Alt+N to select the No button and return to the DOS Prompt window.

Setting the Arduino IDE COM Port Preference


First let’s set the port to which the Arduino is connected (e.g., COM 5). The Arduino connects by USB, but Windows sets up a serial connection through that USB port which emulates a COM port. The COM port is used for uploading code, sending commands, and monitoring output with a terminal program. 

After connecting your board to your computer with a USB cable, you can check your Windows Device Manager under USB ports to find out what COM port the Arduino is using. If the Arduino is connected, it should appear in the Device Manager Device list under USB along with the name of the COM port to which it is connected.

Note: The COM port number may change from session to session depending on the physical USB port you use and what other serial communications are active over USB or Bluetooth at any given time. When in doubt, check the device manager again to verify the current COM port being used by your Arduino board. In practice, it’s a good idea to always use the same physical USB port when connecting your Arduino to your development computer.

You can set the COM port used by the Arduino IDE with the following command at the DOS prompt:
arduino_debug --port comPort --save-prefs

Where comPort is actually the COM port you found in the Device Manager. The --save-prefs switch specifies that you want that port value to be saved in the preferences file and used by default. For example, my Arduino board usually ends up on COM5. To set my default Arduino port to COM5, I type:
arduino_debug --port com5 --save-prefs

If the setting change is successful, you will get a few lines of output in the DOS Prompt window saying things like, “Loading configuration…” and “Loading packages…”. If the command is unsuccessful, You will get unreadable error boxes from the Java-based IDE. In this case, just hit Return to dismiss them. If you mistype the command, or if something is wrong, you may also get error messages in your DOS Prompt window. These messages may be helpful in figuring out what is wrong. Use your screen reader review commands to see if you can infer anything useful. Often, typos are the culprit…

Setting the Arduino Board Preference


After successfully setting the communication port, it is time to tell the IDE what Arduino board you are using. This is done with the --board argument followed by a special package:arch:board parameter. Check the documentation associated with your particular board for the right parameter to use here. For example, to configure your IDE for an Arduino Uno board, you would type:
arduino_debug --board arduino:avr:uno --save-prefs

If this parameter change is accepted you’ll get a few lines of innocuous output in the DOS Prompt window without any dialog or error boxes. If something is wrong, the IDE will throw up unreadable error boxes which need to be dismissed with Enter or Escape. Check the DOS Prompt window for meaningful text error messages before trying again.

Verifying Preferences


You can verify your settings by asking the IDE to tell you what the current port and board are using the --get-pref argument. To get the port preference, type:
arduino_debug --get-pref serial.port

To get the board preference, type:
arduino_debug --get-pref board

As usual, typos or incorrect commands will yield unreadable error boxes, while well-formed and correct commands will report the current COM port or board in the DOS Prompt window.

Note: you can get a massive list of all of the current parameters of the Arduino IDE by typing:
arduino_debug --get-pref


That's it -- you're done with the setup. Now you’re ready to start uploading sketches to your board, but that’s another article!

Acknowledgements

I’d like to give a special shout out to John Schimmel of 
who helped me find a good deal of the information in this and subsequent posts. He also held my hand and shouted encouragement for my first few Arduino baby steps. Thanks also to all the blind and sighted Makers who support this project – Chancey Fleet, Ben Einstein, Laura Eberly, Jim Barbour, Brian Buhrow, Erin Lauredsen, Will Butler, Bryan Bashin, and others. Blind Arduino is a project of 



How to Compile and Upload Arduino Sketches with Notepad++ -- Simple, Convenient, Accessible

Introduction Blind people living in the real world are constantly working out hacks and alternatives to “normal” work flows that are...