From 9bda22f993b34291ced88562c4d714a63d79f8c3 Mon Sep 17 00:00:00 2001 From: fpi Date: Thu, 12 Apr 2018 03:26:33 +0200 Subject: Added support for output and value :results flags --- ob-spice.org | 113 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 58 insertions(+), 55 deletions(-) diff --git a/ob-spice.org b/ob-spice.org index e86c5c0..5a96fb2 100644 --- a/ob-spice.org +++ b/ob-spice.org @@ -197,9 +197,11 @@ Access with ~$x[0]~ or ~$x[2-len]~. "^ *\\*.*$" "" body)))) (defun org-babel-execute:spice-exp (body params) "Execute a block of Spice code with Babel. - This function is called by `org-babel-execute-src-block'." + This function is called by `org-babel-execute-src-block'." (let* ((body (org-babel-expand-body:spice-exp body params)) (gnuplot (cdr (assq :gnuplot params))) + (result-params (cdr (assq :result-params params))) + (result-type (cdr (assq :result-type params))) (session (org-babel-spice-initiate-session (cdr (assq :session params)))) (vars (org-babel--get-vars params)) @@ -220,14 +222,15 @@ Access with ~$x[0]~ or ~$x[2-len]~. (circuit-file (if circuit-body (org-babel-temp-file "spice-body-" ".cir"))) (result)) + (message (concat "circuit:\n" circuit-body)) (message (concat "\n-----\ncontrol:\n" control-body)) - (message (buffer-name session)) + ;; Source circuit-body (with-temp-file circuit-file (insert circuit-body)) (org-babel-spice-source session circuit-file) ;; Run control-body - (setq result (org-babel-spice-evaluate session full-control-body)) + (setq result (org-babel-spice-evaluate session full-control-body result-type result-params)) ;; TODO deal with temporary files @@ -249,35 +252,55 @@ Access with ~$x[0]~ or ~$x[2-len]~. (defun org-babel-spice-source (buffer file) "Source FILE in ngspice process running in BUFFER and return results." (let ((body (concat "source " file))) - (org-babel-spice-evaluate buffer body))) - (defun org-babel-spice-evaluate (buffer body) - "Pass BODY to ngspice process in BUFFER and return results." + (org-babel-spice-evaluate buffer body 'value))) + (defun org-babel-spice-evaluate (buffer body result-type &optional result-params) + "Use BUFFER running ngspice process to eval BODY and return results. + If RESULT-TYPE equals `output' return all outputs, if it equals + `value' return only value of last statement." (let ((eoe-string (format "echo \"%s\"" org-babel-spice-eoe-indicator))) - ;; Force session to be ready - ;;(org-babel-comint-with-output - ;; (buffer org-babel-spice-eoe-indicator t eoe-string) - ;; (insert eoe-string) (comint-send-input nil t)) - ;; Eval body - (replace-regexp-in-string - "^\\(ngspice [0-9]+ -> \\)*" "" - (mapconcat - #'identity - (butlast - (cdr - (split-string - (mapconcat - #'org-trim - (org-babel-comint-with-output (buffer org-babel-spice-eoe-indicator t body) - (mapcar (lambda (line) - (insert (org-babel-chomp line)) (comint-send-input nil t)) - (list body - eoe-string - "\n"))) - "\n") "[\r\n]")) 2) "\n")) - )) - - (provide 'ob-spice-exp) - ;;; ob-spice.el ends here + (pcase result-type + (`output + ;; Force session to be ready + ;;(org-babel-comint-with-output + ;; (buffer org-babel-spice-eoe-indicator t eoe-string) + ;; (insert eoe-string) (comint-send-input nil t)) + ;; Eval body + (replace-regexp-in-string + "^\\(ngspice [0-9]+ -> \\)*" "" + (mapconcat + #'identity + (butlast + (cdr + (split-string + (mapconcat + #'org-trim + (org-babel-comint-with-output (buffer org-babel-spice-eoe-indicator t body) + (mapcar (lambda (line) + (insert (org-babel-chomp line)) (comint-send-input nil t)) + (list body + eoe-string + "\n"))) + "\n") "[\r\n]")) 2) "\n")) + ) + (`value + (let ((tmp-file (org-babel-temp-file "spice-"))) + (org-babel-comint-with-output + (buffer org-babel-spice-eoe-indicator t body) + (mapcar + (lambda (line) + (insert (org-babel-chomp line)) (comint-send-input nil t)) + (append (list body) + (list (format "!! > %s" tmp-file) + (format "echo \"%s\"" org-babel-spice-eoe-indicator) + ))) + (comint-send-input nil t)) + (org-babel-eval-read-file tmp-file)) + ) + ;;todo: add "smart" result type to display measurements (or echos?) & plot filenames + ))) + + (provide 'ob-spice-exp) + ;;; ob-spice.el ends here #+END_SRC * Tests @@ -289,7 +312,7 @@ echo "Hello World" : Hello World -#+BEGIN_SRC spice-exp :var file="/tmp/spice_test" :session spicetest +#+BEGIN_SRC spice-exp :var file="/tmp/spice_test" :session spicetest :results value ,*Time Constant Measurement r1 1 0 10k c1 1 0 1p @@ -307,34 +330,14 @@ echo "Hello World" meas tran value_at_five_tau find V(1) at=5e-8 echo value_at_tau = "$&value_at_tau" > $file.txt ,* Any better way to write one value of vector to a file?? - echo value_at_five_tau = "$&value_at_five_tau" >> $file.txt + echo value_at_five_tau = "$&value_at_five_tau" .endc #+END_SRC #+RESULTS: -#+begin_example -Doing analysis at TEMP = 27.000000 and TNOM = 27.000000 - - -Initial Transient Solution --------------------------- - -Node Voltage ----- ------- -1 1 - - - -No. of Data Rows : 108 -value_at_tau = 3.679797e-01 -value_at_five_tau = 6.717322e-03 -Error: file.txt: no such variable. -Error: missing name for output. -Error: file.txt: no such variable. -Error: missing name for output. -#+end_example +: value_at_five_tau = 0.00671732 -#+BEGIN_SRC spice-exp :var file="/tmp/spice_test2" :session spicetest +#+BEGIN_SRC spice-exp :var file="/tmp/spice_test2" :session spicetest :results output ,*Virtual Ground Test: opamp gain = 1000 vin in 0 dc 0V sin(0 .1 100Hz) r1 in inn 10k -- cgit v1.2.3