Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (in-package :theremin-pc)
- (defparameter *com-port-name* "/dev/cuau0")
- (defparameter *baud-rate* sb-posix:B9600)
- (defparameter *value-end* #\Return) ;; Character at the end of value
- (defconstant FIONREAD #x541B)
- (defconstant IXANY #o4000)
- (defconstant CRTSCTS #o20000000000)
- (defconstant IXON #x00000200)
- (defconstant IXOFF #x00000400)
- (defun calc-flags (flag on-list off-list)
- (let ((off-reduced (logand flag
- (lognot (apply #'logior off-list)))))
- (apply #'logior (cons off-reduced on-list))))
- (defun open-theremin (&optional (name *com-port-name*))
- (let* ((fd (sb-posix:open name (logior sb-posix:O-RDWR
- sb-posix:O-NOCTTY)))
- (attrib (sb-posix:tcgetattr fd)))
- (sb-posix:cfsetispeed *baud-rate* attrib)
- (sb-posix:cfsetospeed *baud-rate* attrib)
- (with-accessors ((cflag sb-posix:termios-cflag)
- (iflag sb-posix:termios-iflag)) attrib
- (setf cflag
- ;; Set CREAD and CLOCAL flags
- ;; Set 8N1 mode
- (calc-flags
- cflag
- (list sb-posix:CREAD sb-posix:CLOCAL sb-posix:CS8)
- (list #+nil sb-posix:CNEW_RTSCTS sb-posix:PARENB
- sb-posix:CSTOPB sb-posix:CSIZE))
- iflag
- ;; No flow control
- (calc-flags iflag
- nil
- (list IXON IXOFF IXANY))))
- (sb-posix:tcsetattr fd sb-posix:TCSANOW attrib)
- (values
- (sb-sys:make-fd-stream fd
- :input t
- :buffering :full)
- fd)))
- (defun close-theremin (fd)
- (sb-posix:close fd))
- (defun restore-sync (stream)
- (loop for char = (read-char stream)
- until (char= *value-end* char)))
- (defun make-measurement (stream &optional (data-length 6))
- (let ((buffer (make-array (list data-length)
- :element-type 'character)))
- (read-sequence buffer stream)
- (parse-integer
- (coerce buffer 'string))))
- (defun calc-statistics (stream &optional (n 150))
- (let* ((measurements (loop for i below n collect
- (progn
- (print i)
- (let ((foo (make-measurement stream)))
- (print foo)
- foo))))
- (average (/ (apply #'+ measurements) n))
- (s (expt (/ (apply #'+
- (mapcar #'(lambda (x) (expt (- average x) 2))
- measurements))
- (1- n)) 0.5)))
- (values average s)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement