summaryrefslogtreecommitdiff
path: root/eeg.ino
blob: d8af88beaeeb676e3f94a05a167f92f8dd89ec60 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <MicroView.h>
#include <Brain.h>
 
#include <Wire.h>
 
template <class Eint Len=10class Queue {
 private:
  E queue[Len];
  byte qHead,qTail;
 public:
 
  Queue() : qHead(0), qTail(0{ }
  bool empty() {
    return length()==0;
  }
  bool full() {
    return length()==Len-1;
  }
  int length() {
    return (qTail+Len-qHead)%Len;
  }
  E* current() {
    return &queue[qHead];
  }
  void remove() {
    if (empty()) return;
    qHead++;
    qHead %= Len;
  }
  void add(const E &value) {
    if (full()) return;
 
    queue[qTail] = value;
 
    qTail++;
    qTail %= Len;
  }
};
 
class Keepon {
 public:
  enum State { WAITING=1, MASTERING, READY, QFULL };
  typedef void (*callback)(State,int);
 private:
  static const int maxAttempts = 50;
 
  // Set clock to 50kHz (actualy 49.6kHz seems to work better) 
  static const long MK_FREQ = 49600L;
 
  // Sound controller (device ID 82).  Write to 0xA4, read from 0xA5. 
  static const byte SOUND = 0x52;
  // Button controller (device ID 80). Write to 0xA0, read from 0xA1. 
  static const byte BUTTON = 0x50;
  // Motor controller (device ID 85).  Write to 0xAA, read from 0xAB. 
  static const byte MOTOR = 0x55;
 
  struct Command {
    byte device;
    byte command;
    byte arg;
  };
  Queue<Command,10> queue;
  byte attempts;
 
  callback display_callback;
 
  State current_state;
  int last_time;
 
  void set_state(State value,int n) {
    current_state = value;
    display_callback(value,n);
  }
  
  void check_state() {
    int r = analogRead(0);
    int t = millis() - last_time;
    if (r <= 512{
      digitalWrite(SDA, LOW);
      digitalWrite(SCL, LOW);
      set_state(WAITING,r);
    }
    else if (current_state == WAITING) {
      last_time = millis();
      set_state(MASTERING,0);
    }
    else if (current_state == MASTERING && t > 1000{
      Wire.begin();
      TWBR = ((F_CPU / MK_FREQ) - 16) / 2;
      set_state(READY,t/10);
    }
  }
 
 public:
 
  Keepon(callback x) : attempts(0), display_callback(x) { }
 
  void begin() {
    pinMode(SDA, OUTPUT); // Data wire on My Keepon 
    pinMode(SCL, OUTPUT); // Clock wire on My Keepon 
    check_state();
  }
 
  void update() {
    check_state();
 
    if (current_state != READY) return;
 
    if (attempts > maxAttempts) {
      attempts = 0;
      queue.remove();
    }
 
    if (queue.empty()) return;
 
    Command* current = queue.current();
 
    Wire.beginTransmission(current->device);
    Wire.write(current->command);
    Wire.write(current->arg);
    int result = (int)Wire.endTransmission();
 
    if (result == 0{
      queue.remove();
      display_callback(current_state,queue.length());
      attempts = 0;
    }
    else {
      attempts++;
    }
  }
 
  void add(const Command&c) {
    if (queue.full()) {
      display_callback(QFULL,queue.length());
      return;
    }
    queue.add(c);
    display_callback(current_state,queue.length());
  }
 
  void pan(byte pos) {
    add(Command{MOTOR,4,pos});
  }
  void tilt(byte pos) {
    add(Command{MOTOR,2,pos});
  }
};
 
Brain brain(Serial);
 
void kscan_callback(Keepon::State s, int n) {
  if (s == Keepon::WAITING) {
    uView.setCursor(25,0);uView.print("K?");
  }
  else if (s == Keepon::MASTERING){
    uView.setCursor(25,0);uView.print("K..");
  }
  else if (s == Keepon::READY){
    uView.setCursor(25,0);uView.print("K!");
  }
  else if (s == Keepon::QFULL){
    uView.setCursor(25,0);uView.print("KO");
  }
  uView.setCursor(40,0);uView.print(n);
}
 
Keepon keepon(&kscan_callback);
 
void setup() {
  Serial.begin(9600);
 
  uView.begin();
  uView.clear(ALL);
  uView.setFontType(0);
 
  keepon.begin();
}
 
const int hist_width=3;
const int hist_pad=1;
int samples_delay;
 
byte attention, meditation, quality;
int hist_height[8];
 
void loop() {
  uView.clear(PAGE);
 
  keepon.update();
  
  if (brain.update()) {
    quality=brain.readSignalQuality();
    attention=brain.readAttention();
    meditation=brain.readMeditation();
 
    const uint32_t* power = brain.readPowerArray();
    uint32_t max_value=0;
    for (int x=0;x<8;++x) {
      if (power[x] > max_value) {
        max_value = power[x];
      }
    }
    
    for (int x=0;x<8;++x) {
      // we have 30 vertical pixels 
      hist_height[x] = power[x] * 30 / max_value;
    }
 
    if (quality < 10 && ++samples_delay > 5{
      keepon.pan(attention);
      keepon.tilt(meditation);
      samples_delay=0;
 
      uView.setCursor(5,0);uView.print("sent");
    }
  }
 
 
  uView.setCursor(0,0); uView.print(quality=brain.readSignalQuality());
 
  uView.setCursor(0,9); uView.print(attention=brain.readAttention());
  uView.setCursor(18,9); uView.print(meditation=brain.readMeditation());
 
  for (int x=0;x<8;++x) {
    int x0 = (hist_width + hist_pad) * x;
    for (int o=0;o<hist_width;++o) {
      uView.lineV(x0+o,47-hist_height[x],hist_height[x]);
    }
  }
 
  uView.display();
}