using Emacs to develop

Processing & Arduino

by Shelandy Ting

powered by Emacs + reveal.js + github

Why Processing/Arduino

Why starting (teaching) from Processing/Arduino instead of other languages?

it is an easy and quick way for beginners to use Processing for learning drawing things,

and to use Arduino for making interaction in physical things

the learning experience of Processing(Java-based) or Arduino (C-based) can be reused in the journey of leaning Java/C and something else later

Built-in IDE?

Is built-in Processing/Arduino IDE powerful enough?

No!

Define "Powerful Enough"

1st level: show the relationship of an object (variable, class, file,...) to others in the coding context
  • Static relationship
  • (e.g., scope in the declaration hierarchy)
  • Dynamic relationship
  • (e.g., calling/using a variable/function)
  • Semantic relationship
  • (e.g., mapping the error occurrence to the line of source code during compilation and running)
2nd level:
  • context/content
  • dual view and switching between different levels of information abstraction
  • Support proper self-documentation (refactoring...)

Why Emacs

instead of other IDEs (Eclipse, IntelliJ...?)

Emacs is portable, light-weighted (e.g., fast), yet powerful

Emacs is a good platform (instead of just being an editor), so the learning experience can be applied to other tasks

e.g., using Emacs with reveal.js libraries to write HTML5 presentation slides like this one, where I do not need to learn JavaScript yet I can easily come up an nice interactive sides for presentation.

Requirement

for using Emacs to develop Processing/Arduino
  • software:
  • Emacs and the relevant packages
  • hardware (optional, only if you want):
  • Arduino board

installation procedure

  1. download the Emacs

  2. configure the init.el file for locating the package repositories
  3. install the required packages:(ECB, JDEE, multi-refactory...)

  4. configure the init.el file for each package

Install Emacs

  • Linux should be able to download using whatever package system (.rpm, .pkg...)
  • Windows
  • OSX

Emacs configuration file name

  • Linux
  • ~/.emacs.d/init.el
  • Windows
  • OSX
emacs.d/.init.el package repository setting sample

(when (>= emacs-major-version 24)
    (require 'package)
    (add-to-list 'package-archives
    '("marmalade" . "http://marmalade-repo.org/packages/"))
    (add-to-list 'package-archives
    '("melpa" . "https://melpa.org/packages/"))
    (package-initialize)
    )

Install Packages

  • packages using the package management system
    • ECB
    • JDEE
  • "Unofficial/external" packages by manual installation
    • multi-refactory or c-xref

Keys, keys

Some basic convention to describe key combination

  • C-
  • "control" key with other key. E.g., C-d means Control-d: press "d"-key while holding Control key

  • S-
  • "shift" with other key
  • M-
  • "meta" with other key, where
    • in windows/Linux: "Alt"
    • in OSX: <>

Running Package Management

  1. Emacs Main Menu ->
  2. Options ->
  3. Manage Emacs Packages ->

  • use arrow keys to navigate among package listing
  • C-s to search, for example "ecb"
    • key "i" to select the item under the cursor
    • key "u" to deselect
    • key "x" to install all selected packages

let it finish the download and compilation

then quit (Main Menu -> File -> Quit) the Emacs and relaunch

install multi-refactory

  1. download it from the green button on multi-refactory
  2. unzip the file
  3. follow the instruction to install
emacs.d/.init.el setting for multi-refactory mode

(defun my-xref-settings ()   
      (defvar c-xref-current-project nil) ;; can be also "my_project_name"
      (defvar c-xref-key-binding 'global) ;; can be also 'local or 'none
      (setq load-path (cons "/usr/local/c-xref/emacs" load-path))
      (setq exec-path (cons "/usr/local/c-xref" exec-path))
      (load "c-xrefactory")
      (provide 'xref-settings))
(add-hook 'processing-mode-hook 'processing-mode-init)
emacs.d/.init.el setting for Processing mode

(autoload 'processing-mode "processing-mode" "Processing mode" t)
(add-to-list 'auto-mode-alist '("\\.pde\\'" . processing-mode))
(defun processing-mode-init ()
      (setq processing-sketchbook-dir "~/yourProcessingDir")
      (setq processing-output-dir "/tmp")
      (my-xref-settings))
      ;;;;;;
((eq system-type 'gnu/linux) ;; os specific setting
  (progn 
    (setq processing-location "/usr/local/processing-3.2.1/processing-java")
    (setq processing-application-dir "/usr/local/processing-3.2.1/")
    ))
emacs.d/.init.el setting for JDEE mode

(custom-set-variables
      '(jdee-compiler (quote ("javac")))
      '(jdee-jdk-registry (quote (("1.8" . "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.101-1.b14.1.mga5.x86_64") ("" . "")))))
(defun my-java-mode-extra () 
      (my-jde-refactor-init)
      (my-xref-settings) ; have to use add-hook instead of eval-after-load
      )
(add-hook 'java-mode-hook 'my-java-mode-extra)
(eval-after-load "java-mode" 'my-java-mode-extra) ; only load snippets files after using yasnippet
  
use the icon on menu to save the changed file, or type C-x C-s
Need to restart Emacs after changing the .init.el

Run ECB

  1. execute Emacs
  2. load your .pde Processing file
  3. execute the Emacs command M-x ecb-activate
    • Emacs commands can be set up as a menu button (will be covered in a different slide)

Full-blown IDE

  • ECB provides code view, simple debugging and both text and graph result in run time.
  • .pde file will automatically load processing mode, where
    • the processing sub menu provides the major functions (compile, run...) as the Processing built-in IDE
    • maintain the compatibility to run under Processing's built-in IDE
    • The c-xref sub menu provides extra functions such as refactoring and other compile/run function, but requiring setting a project

switching between major modes

  • Run as Java program
    • switch to Emacs' jdee-mode by executing M-x jdee-mode
    • if adding enough java code, it provide hierarchical variable and method view, can also compile and run as java program, but needs extra configuration
    • lose the compatibility for running under Proceesing's built-in IDE
  • Run as Processing program
    • switch to Emacs' processing-mode by executing M-x processing-mode
    • I suggest save as .pde, but add few java header

Learning Cost

Is the effort of learning Emacs worth the investment?

yes!

Emacs is a flexible platform, not just a simple editor

Powerful IDE revisited

know the relationship of an object (variable, class, file,...) in the coding context

Static relationship (e.g., scope in declaration hierarchy)

(fully by JDEE mode package, partially by processing mode package)

mapping dynamic relationship e.g., all locations using this method/variable

(by highlight-symbol package)

dual Context/Content view and switching
  • by ECB hierarchy windows(Context) + code window (Content)
  • code folding/unfolding (by hide-region or ECB/JDEE mode)
refactoring (by multi-refactory)
Semantic relationship (e.g., compilation and running)

  • show line number of error
  • (requires compiler/symbol table integration) (supported by processing-mode)
  • clickable meant to jump back to the particular code location (supported by processing-mode)
  • show content of variables (w/ debugger)

Table Of Content

need a authoring tool to make up here as the end:

  1. title page
  2. the rest pages here...