forked from jonnay/mindwave-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmindwave-display.el
167 lines (147 loc) · 6.27 KB
/
mindwave-display.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;;; mindwave-display.el --- A simplified mindwave display file
;; Copyright (C) 2012 Jonathan Arkell
;; Author: Jonathan Arkell <[email protected]>
;; Created: 16 June 2012
;; Keywords: comint mindwave
;; This file is not part of GNU Emacs.
;; Released under the GPL
(require 'mindwave-emacs)
(defvar mw-display/timer nil
"Timer responsible for updating the output buffer")
(defcustom mw-display/colors
'((delta . ("RoyalBlue2" . "RoyalBlue4"))
(theta . ("DeepSkyBlue2" . "DeepSkyBlue4"))
(lowAlpha . ("cyan2" . "cyan4"))
(highAlpha . ("aquamarine2" . "aquamarine4"))
(lowBeta . ("yellow2" . "yellow4"))
(highBeta . ("gold2" . "gold4"))
(lowGamma . ("tan2" . "tan4"))
(highGamma . ("firebrick2" . "firebrick4"))
(attention . ("MistyRose2" . "MistyRose4"))
(meditation . ("seashell2" . "seashell4")))
"The colors to use when displaying the graph."
:safe t
:group 'mindwave)
(defun mw-display/show ()
"Shows the output of the mindwave device in a nicely formatted buffer."
(interactive)
(mindwave-get-buffer)
(let ((mwbuffer (get-buffer-create "*mindwave-status*")))
(when (not (timerp mw-display/timer))
(setq mw-display/timer (run-at-time t 1 'mw-display/write-values)))
(save-excursion
(buffer-disable-undo (set-buffer mwbuffer))
(add-hook 'kill-buffer-hook 'mw-display/kill-timer nil t))
(mw-display/write-values)
(pop-to-buffer mwbuffer)))
(defun mw-display/kill-timer ()
"Removes the timer"
(when (timerp mw-display/timer)
(cancel-timer mw-display/timer)
(setq mw-display/timer nil)))
(defun mw-display/write-values ()
"Actually write the values in the eeg buffer"
(let ((inhibit-read-only t))
(with-current-buffer "*mindwave-status*"
(erase-buffer)
(insert (propertize " Mindwave Status \n"
'face '(:background "white" :foreground "black")))
(insert (format "%3d Signal Serial (%d/%d)\n\n"
(cdr (assoc 'poorSignalLevel mindwave/current))
mindwave-serial--bad-packets
mindwave-serial--total-packets))
(mw-display/insert-eeg 'delta 'eegPower)
(mw-display/insert-eeg 'theta 'eegPower)
(mw-display/insert-eeg 'lowAlpha 'eegPower)
(mw-display/insert-eeg 'highAlpha 'eegPower)
(mw-display/insert-eeg 'lowBeta 'eegPower)
(mw-display/insert-eeg 'highBeta 'eegPower)
(mw-display/insert-eeg 'lowGamma 'eegPower)
(mw-display/insert-eeg 'highGamma 'eegPower)
(insert "\n")
(mw-display/insert-eeg 'meditation 'eSense)
(mw-display/insert-eeg 'attention 'eSense)
(insert "\n")
(let ((current-pos (point)))
(insert (pp-to-string mindwave/current))
(goto-char current-pos)
(mw-display/write-hooks current-pos)
(vertical-motion 1)
(mw-display/insert-raw-eeg)
(mw-display/insert-signature)))))
(defconst mw-display/2nd-column 30)
(defun mw-display/write-hooks (top)
(let ((mw-display/hdp top))
(mw-display/show-hook 'mindwave-hook)
(mw-display/show-hook 'mindwave-blink-hook)
(mw-display/show-hook 'mindwave-e-sense-hook)
(mw-display/show-hook 'mindwave-eeg-power-hook)
(mw-display/show-hook 'mindwave/brain-ring-full-hook)))
(defun mw-display/show-hook (hook-name)
(move-to-column mw-display/2nd-column t)
(if (null (symbol-value hook-name))
(progn
(move-to-column mw-display/2nd-column t)
(insert (format "Hook: %s - Empty" hook-name))
(vertical-motion 1))
(progn
(move-to-column mw-display/2nd-column t)
(insert (format "Hook: %s" hook-name))
(vertical-motion 1)
(dolist (hook (symbol-value hook-name))
(move-to-column mw-display/2nd-column t)
(insert (format " * %s" (symbol-name hook)))
(vertical-motion 1)))))
(defvar mw-display/last-packet-count 0
"Total of packets received in the previous second.
This is to keep track of the sample rate.")
(defun mw-display/insert-raw-eeg ()
(move-to-column mw-display/2nd-column t)
(insert (format "Sample Rate (Hz): %d/512" (- mindwave-serial--total-packets
mw-display/last-packet-count)))
(setq mw-display/last-packet-count mindwave-serial--total-packets)
(vertical-motion 2))
(defun mw-display/insert-signature ()
(move-to-column mw-display/2nd-column t)
(insert (if (featurep 'mw-info-catcher)
(format "Signature: %s"
(mapcar #'(lambda (v)
(case v
(-2 "⇓")
(-1 "↓")
(0 "-")
(1 "↑")
(2 "⇑")))
mw-info-catcher/current-signature))
"mw-info-catcher not loaded")))
(defun mw-display/insert-eeg (band type)
"Insert an eeg string.
If TYPE is eeg, the bargraph displayed will be out of 1 000 000"
(let ((val (cdr (assoc band (cdr (assoc type mindwave/current))))))
(insert (format "%-10s - %7d " band val)
(if (equal type 'eegPower)
(mw-display/graph val
100000
band)
(mw-display/graph val
100
band))
"\n")))
(defun mw-display/graph (val total band)
"Return a simple string bar graph from VAL and TOTAL"
(let* ((gsize (round (min (* (/ (float val) total)
50)
50)))
(esize (- 50 gsize)))
(concat (propertize (make-string esize ?\ )
'face `(:background ,(cdr (cdr (assoc band mw-display/colors)))
:foreground "grey1"))
(propertize (make-string gsize ?\ )
'face `(:background ,(car (cdr (assoc band mw-display/colors)))
:foreground "grey1"))
(propertize (format " | %8s %12s "
val
band)
'face `(:background ,(car (cdr (assoc band mw-display/colors)))
:foreground "grey1")))))
(provide 'mindwave-display)