* ob-arduino.el (supporting Arduino in Org-mode Babel): Add.
[org-mode/org-tableheadings.git] / contrib / lisp / ob-arduino.el
blob57a15ffa8ecd7a48bd4fba7edfd7c0da6c145bff
1 ;;; ob-arduino.el --- Org-mode Babel support for Arduino.
2 ;;
3 ;; Authors: stardiviner <numbchild@gmail.com>
4 ;; Package-Requires: ((emacs "24.4") (org "24.1"))
5 ;; Package-Version: 1.0
6 ;; Keywords: arduino org babel
7 ;; homepage: https://github.com/stardiviner/arduino-mode
8 ;;
9 ;; You should have received a copy of the GNU General Public License
10 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
12 ;;; Commentary:
13 ;;
14 ;; Like the following src block, press =[C-c C-c]= to upload to Arduino board.
15 ;;
16 ;; #+begin_src arduino
17 ;; // the setup function runs once when you press reset or power the board
18 ;; void setup() {
19 ;; // initialize digital pin LED_BUILTIN as an output.
20 ;; pinMode(LED_BUILTIN, OUTPUT);
21 ;; }
22 ;;
23 ;; // the loop function runs over and over again forever
24 ;; void loop() {
25 ;; digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
26 ;; delay(100); // wait for 0.1 second
27 ;; digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
28 ;; delay(100); // wait for 0.1 second
29 ;; }
30 ;; #+end_src
32 ;;; Code:
34 (require 'org)
35 (require 'ob)
36 (require 'arduino-mode)
38 (defgroup ob-arduino nil
39 "org-mode blocks for Arduino."
40 :group 'org)
42 (defcustom ob-arduino:program "arduino"
43 "Default Arduino program name."
44 :group 'ob-arduino
45 :type 'string)
47 (defcustom ob-arduino:port "/dev/ttyACM0"
48 "Default Arduino port."
49 :group 'ob-arduino
50 :type 'string)
52 (defcustom ob-arduino:board "arduino:avr:uno"
53 "Default Arduino board."
54 :group 'ob-arduino
55 :type 'string)
58 (defvar org-babel-default-header-args:sclang nil)
60 ;;;###autoload
61 (defun org-babel-execute:arduino (body params)
62 "org-babel arduino hook."
63 (let* ((port (cdr (assoc :port params)))
64 (board (cdr (assoc :board params)))
65 (cmd (mapconcat 'identity (list
66 ob-arduino:program "--upload"
67 (if port (concat "--port " port))
68 (if board (concat "--board " board))
69 ) " "))
70 (code (org-babel-expand-body:generic body params))
71 (src-file (org-babel-temp-file "ob-arduino-" ".ino")))
72 ;; delete all `ob-arduino' temp files, otherwise arduino will compile all
73 ;; ob-arduino temp files, and report error.
74 (mapc
75 (lambda (f)
76 (unless (file-directory-p f)
77 (delete-file (expand-file-name f org-babel-temporary-directory))))
78 (directory-files
79 (file-name-directory (org-babel-temp-file "ob-arduino-" ".ino"))
80 nil ".ino"))
81 ;; specify file for arduino command.
82 (with-temp-file src-file
83 (insert code))
84 (org-babel-eval
85 (concat ob-arduino:program
86 " " "--upload"
87 " " (if port (concat "--port " port))
88 " " (if board (concat "--board " board))
89 " " src-file)
90 "" ; pass empty string "" as `BODY' to `org-babel--shell-command-on-region'
91 ;; to fix command `arduino' don't accept string issue.
96 ;;;###autoload
97 (eval-after-load 'org
98 '(add-to-list 'org-src-lang-modes '("arduino" . arduino)))
103 (provide 'ob-arduino)
105 ;;; ob-arduino.el ends here