Advertisement
Guest User

Wor

a guest
Oct 28th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.22 KB | None | 0 0
  1. (in-package :theremin-pc)
  2.  
  3. (defparameter *com-port-name* "/dev/cuau0")
  4. (defparameter *baud-rate* sb-posix:B9600)
  5. (defparameter *value-end* #\Return) ;; Character at the end of value
  6.  
  7. (defconstant FIONREAD #x541B)
  8. (defconstant IXANY #o4000)
  9. (defconstant CRTSCTS #o20000000000)
  10. (defconstant IXON #x00000200)
  11. (defconstant IXOFF #x00000400)
  12.  
  13. (defun calc-flags (flag on-list off-list)
  14.   (let ((off-reduced (logand flag
  15.                  (lognot (apply #'logior off-list)))))
  16.  
  17.     (apply #'logior (cons off-reduced on-list))))
  18.  
  19. (defun open-theremin (&optional (name *com-port-name*))
  20.   (let* ((fd (sb-posix:open name (logior sb-posix:O-RDWR
  21.                      sb-posix:O-NOCTTY)))
  22.      (attrib (sb-posix:tcgetattr fd)))
  23.  
  24.     (sb-posix:cfsetispeed *baud-rate* attrib)
  25.     (sb-posix:cfsetospeed *baud-rate* attrib)
  26.  
  27.     (with-accessors ((cflag sb-posix:termios-cflag)
  28.              (iflag sb-posix:termios-iflag)) attrib
  29.  
  30.             (setf cflag
  31.               ;; Set CREAD and CLOCAL flags
  32.               ;; Set 8N1 mode
  33.               (calc-flags
  34.                cflag
  35.                (list sb-posix:CREAD sb-posix:CLOCAL sb-posix:CS8)
  36.                (list #+nil sb-posix:CNEW_RTSCTS sb-posix:PARENB
  37.                  sb-posix:CSTOPB sb-posix:CSIZE))
  38.  
  39.              
  40.              
  41.               iflag
  42.               ;; No flow control
  43.               (calc-flags iflag
  44.                       nil
  45.                       (list IXON IXOFF IXANY))))
  46.  
  47.     (sb-posix:tcsetattr fd sb-posix:TCSANOW attrib)
  48.     (values
  49.      (sb-sys:make-fd-stream fd
  50.                 :input t
  51.                 :buffering :full)
  52.      fd)))
  53.  
  54. (defun close-theremin (fd)
  55.   (sb-posix:close fd))
  56.  
  57. (defun restore-sync (stream)
  58.   (loop for char = (read-char stream)
  59.     until (char= *value-end* char)))
  60.  
  61. (defun make-measurement (stream &optional (data-length 6))
  62.   (let ((buffer (make-array (list data-length)
  63.                 :element-type 'character)))
  64.     (read-sequence buffer stream)
  65.     (parse-integer
  66.      (coerce buffer 'string))))
  67.  
  68. (defun calc-statistics (stream &optional (n 150))
  69.   (let* ((measurements (loop for i below n collect
  70.                  (progn
  71.                    (print i)
  72.                    (let ((foo (make-measurement stream)))
  73.                  (print foo)
  74.                  foo))))
  75.      (average (/ (apply #'+ measurements) n))
  76.      (s (expt (/ (apply #'+
  77.                 (mapcar #'(lambda (x) (expt (- average x) 2))
  78.                     measurements))
  79.              (1- n)) 0.5)))
  80.     (values average s)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement