Remove (require 'org) to fix recursive require.
[arduino-mode.git] / ob-arduino.el
blob6b5e42dc402b821f1f0b75fb1f567a0d2c67bdff
1 ;;; ob-arduino.el --- Org-mode Babel support for Arduino. -*- lexical-binding: t; -*-
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 <https://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 'ob)
35 (require 'arduino-mode nil t)
37 (defgroup ob-arduino nil
38 "org-mode blocks for Arduino."
39 :group 'org)
41 (defcustom ob-arduino:program "arduino"
42 "Default Arduino program name."
43 :type 'string)
45 (defcustom ob-arduino:port "/dev/ttyACM0"
46 "Default Arduino port."
47 :type 'string)
49 (defcustom ob-arduino:board "arduino:avr:uno"
50 "Default Arduino board."
51 :type 'string)
54 (defvar org-babel-default-header-args:sclang nil) ;;FIXME: What's this for??
55 (defvar org-babel-temporary-directory)
57 ;;;###autoload
58 (defun org-babel-execute:arduino (body params)
59 "org-babel arduino hook."
60 (let* ((port (cdr (assoc :port params)))
61 (board (cdr (assoc :board params)))
62 ;; (cmd (mapconcat #'identity (list
63 ;; ob-arduino:program "--upload"
64 ;; (if port (concat "--port " port))
65 ;; (if board (concat "--board " board))
66 ;; )
67 ;; " "))
68 (code (org-babel-expand-body:generic body params))
69 (src-file (org-babel-temp-file "ob-arduino-" ".ino")))
70 ;; delete all `ob-arduino' temp files, otherwise arduino will compile all
71 ;; ob-arduino temp files, and report error.
72 (mapc
73 (lambda (f)
74 (unless (file-directory-p f)
75 (delete-file (expand-file-name f org-babel-temporary-directory))))
76 (directory-files
77 (file-name-directory (org-babel-temp-file "ob-arduino-" ".ino"))
78 nil ".ino"))
79 ;; specify file for arduino command.
80 (with-temp-file src-file
81 (insert code))
82 (org-babel-eval
83 (concat ob-arduino:program
84 " " "--upload"
85 " " (if port (concat "--port " port))
86 " " (if board (concat "--board " board))
87 " " src-file)
88 "" ; pass empty string "" as `BODY' to `org-babel--shell-command-on-region'
89 ;; to fix command `arduino' don't accept string issue.
94 ;;;###autoload
95 (with-eval-after-load 'org
96 (add-to-list 'org-src-lang-modes '("arduino" . arduino))
97 (add-to-list 'org-babel-tangle-lang-exts '("arduino" . "ino")))
101 (provide 'ob-arduino)
103 ;;; ob-arduino.el ends here