ob-arduino.el 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/>.
  11. ;;
  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
  31. ;;
  32. ;;; Code:
  33. (require 'org)
  34. (require 'ob)
  35. (require 'arduino-mode nil t)
  36. (defgroup ob-arduino nil
  37. "org-mode blocks for Arduino."
  38. :group 'org)
  39. (defcustom ob-arduino:program "arduino"
  40. "Default Arduino program name."
  41. :group 'ob-arduino
  42. :type 'string)
  43. (defcustom ob-arduino:port "/dev/ttyACM0"
  44. "Default Arduino port."
  45. :group 'ob-arduino
  46. :type 'string)
  47. (defcustom ob-arduino:board "arduino:avr:uno"
  48. "Default Arduino board."
  49. :group 'ob-arduino
  50. :type 'string)
  51. (defvar org-babel-default-header-args:sclang nil)
  52. ;;;###autoload
  53. (defun org-babel-execute:arduino (body params)
  54. "org-babel arduino hook."
  55. (let* ((port (cdr (assoc :port params)))
  56. (board (cdr (assoc :board params)))
  57. (cmd (mapconcat 'identity (list
  58. ob-arduino:program "--upload"
  59. (if port (concat "--port " port))
  60. (if board (concat "--board " board))
  61. ) " "))
  62. (code (org-babel-expand-body:generic body params))
  63. (src-file (org-babel-temp-file "ob-arduino-" ".ino")))
  64. ;; delete all `ob-arduino' temp files, otherwise arduino will compile all
  65. ;; ob-arduino temp files, and report error.
  66. (mapc
  67. (lambda (f)
  68. (unless (file-directory-p f)
  69. (delete-file (expand-file-name f org-babel-temporary-directory))))
  70. (directory-files
  71. (file-name-directory (org-babel-temp-file "ob-arduino-" ".ino"))
  72. nil ".ino"))
  73. ;; specify file for arduino command.
  74. (with-temp-file src-file
  75. (insert code))
  76. (org-babel-eval
  77. (concat ob-arduino:program
  78. " " "--upload"
  79. " " (if port (concat "--port " port))
  80. " " (if board (concat "--board " board))
  81. " " src-file)
  82. "" ; pass empty string "" as `BODY' to `org-babel--shell-command-on-region'
  83. ;; to fix command `arduino' don't accept string issue.
  84. )
  85. ))
  86. ;;;###autoload
  87. (eval-after-load 'org
  88. '(add-to-list 'org-src-lang-modes '("arduino" . arduino)))
  89. (provide 'ob-arduino)
  90. ;;; ob-arduino.el ends here