[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

gEDA-user: [PATCH] gnetlist, (any)spice, and variable resistors on schematics



Hello

I created quite hackish way to convert-on-fly variable resistors placed on
schematic to two resistors on netlist for spice backend, to allow use variable
resistor with two attributes, one named value and with expected meaning, and
second wiper which is 0 < wiper < 1, used to calculate which side of pot goes
to which resistor.

It isn't best code, won't work with value=3k3, because code which extract unit suffix
will stop at 'k' character, thus selecting 3 as value, k3 as unit, thus values will be
produced using (wiper * 3) and then stringified and k3 appended, and (1-wiper) * 3 
and stringified then k3 appended for second one, but 3Meg should work as expected.

Feel free to fix it.

best regards
Maciej 

-- 
Maciej Pijanka
I don't fear computers, I fear lack of them -- Isaac Asimov
diff -Nur gnetlist.ori/scheme/gnet-spice-sdb.scm gnetlist.mod/scheme/gnet-spice-sdb.scm
--- gnetlist.ori/scheme/gnet-spice-sdb.scm	2010-05-28 23:57:48.000000000 +0200
+++ gnetlist.mod/scheme/gnet-spice-sdb.scm	2010-05-29 00:03:35.000000000 +0200
@@ -1310,6 +1310,127 @@
  ) ;; close of lambda
 ) ;; close of define
 
+;; ----
+;; Set of routines that allow to convert-on-fly variable resistors on 
+;; schematics to two resistor with apreciate values on netlist
+;; 2010-05-27 - Maciej 'agaran' Pijanka 
+;;--------------------------------------------------------------------
+;; Given a value it extract part of string which is a number in value
+;; but will fail on 2k2 or similar (sorry, no fix idea yet)
+;;--------------------------------------------------------------------
+(define spice-sdb:value-number-extract
+  (lambda (value)
+    (let* (
+          (value-length (string-length value))
+          (value-cut (substring value 0 (- value-length 1)))
+          (value-tail (substring value (- value-length 1)))
+          (value-number (string->number value-cut))
+         ) ;; End of local assignments
+
+;;    (debug-spew (string-append "At extract " (number->string value-length) "\n cuted = " value-cut "\n"))
+
+    (if (not value-number) 
+      (set! value-cut (spice-sdb:value-number-extract value-cut)))
+
+    (string-append value-cut)
+  ) ;; end of let
+ ) ;; close of lambda
+) ;; close of define
+
+
+(define spice-sdb:value-multiplier
+  (lambda (value coef)
+    (let* (
+          (value-number-string (spice-sdb:value-number-extract value))
+          (value-tail (substring value (string-length value-number-string)))
+          (value-number (string->number value-number-string))
+         ) ;; End of local assignments
+
+    (string-append (number->string (* value-number coef)) value-tail)
+  ) ;; end of let
+ ) ;; close of lambda
+) ;; close of define
+    
+;;--------------------------------------------------------------------
+;; Given a refdes and pin number, this writes out the net attached to the 
+;; component's pin. Used only in write variable resistor. Call it with
+;; component refdes and number of pin to print
+;; this is hacked from write-net-names-on-component, and probably it could
+;; use this procedure to do actual writing (no idea if that makes sense)
+;;--------------------------------------------------------------------
+(define spice-sdb:write-net-name-on-component
+  (lambda (refdes number-of-pin port)
+    (if (> number-of-pin 0)
+      (begin
+            ;; generate a pin-name e.g. pin1, pin2, pin3 ...
+        (let* ((pin-name (number->string number-of-pin))
+	       (pinnumber (gnetlist:get-attribute-by-pinseq refdes pin-name "pinnumber"))
+	       (pinseq (gnetlist:get-attribute-by-pinseq refdes pin-name "pinseq"))
+	       (netname (car (spice-sdb:get-net refdes pinnumber)) )
+	       )
+
+;; -------  Super debug stuff  --------
+	  (debug-spew "  In write-net-name-on-component. . . . \n")
+	  (debug-spew (string-append "     pin-name = " pin-name "\n"))
+	  (debug-spew (string-append "     pinnumber = " pinnumber "\n"))
+	  (debug-spew (string-append "     pinseq = " pinseq "\n"))
+	  (debug-spew (string-append "     netname = " netname "\n"))
+;; ------------------------------ 
+
+	  (if (not (string=? netname "ERROR_INVALID_PIN"))
+             (display (string-append netname " ") port)     ;; write out attached net if OK.
+             (debug-spew (string-append "For " refdes ", found pin with no pinseq attribute.  Ignoring. . . .\n"))
+          )
+        )  ;; let*
+      )    ;; begin
+    )
+  )
+)
+
+
+(define spice-sdb:write-variable-resistor
+  (lambda (package port)
+    ;; fetch attributes..
+    (let ((value (gnetlist:get-package-attribute package "value"))
+         (wiper (gnetlist:get-package-attribute package "wiper"))
+         ) ;; end of local assignments
+
+    (debug-spew (string-append "Found Variable resistor, refdes = " package "\n"))
+
+    (if (string=? wiper "unknown")
+      (set! wiper "0.5"))
+
+    (debug-spew (string-append "Value = " value "\nwiper = " wiper "\n" ))
+
+    (set! wiper (string->number wiper))
+    (if (not wiper)
+       (set! wiper 0.5)
+    )
+
+    ;; at first fixed refdes
+    (display (string-append "R" package "_1 ") port)
+    ;; next first and third net (so one end plus wiper) then value
+    (spice-sdb:write-net-name-on-component package 1 port)
+    (spice-sdb:write-net-name-on-component package 3 port)
+    (display (spice-sdb:value-multiplier value wiper) port)
+    (newline port) 
+    ;; then a second compound resistor with pins second and third.)
+    (display (string-append "R" package "_2 ") port)
+    (spice-sdb:write-net-name-on-component package 2 port)
+    (spice-sdb:write-net-name-on-component package 3 port)
+    (display (spice-sdb:value-multiplier value (- 1 wiper)) port)
+
+    ;; first write out refdes and attached nets
+    ;;(spice-sdb:write-component-no-value (string-append "R" package "_1") port)
+
+;    (debug-spew (string-append "Output " (spice-sdb:write-variable-resistor-val-multiplier package value wiper) "\n"))
+;    (string-append "Got Wiper " (string->number wiper) "\n")
+    
+    (newline port) 
+  ) ;; end of let
+ ) ;; close of lambda
+) ;; close of define
+
 ;;--------------------------------------------------------------------
 ;; Given a refdes and number of pins, this writes out the nets
 ;; attached to the component's pins.  This is used to write out
@@ -1673,6 +1794,8 @@
               (spice-sdb:write-include package port))
           ( (string=? device "TESTPOINT")
               (spice-sdb:write-probe package port))
+          ( (string=? device "VARIABLE_RESISTOR")
+              (spice-sdb:write-variable-resistor package port))
           ( else 
 	      (spice-sdb:write-default-component package file-info-list port))
         ) ;; end of cond

_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user