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