msobral commited on
Commit
e5bc67d
·
1 Parent(s): cd94660

added recent code

Browse files
.ipynb_checkpoints/tests-checkpoint.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
__pycache__/frontend.cpython-37.pyc ADDED
Binary file (4.63 kB). View file
 
__pycache__/leds.cpython-37.pyc ADDED
Binary file (2.73 kB). View file
 
capture.py ADDED
File without changes
frontend.py CHANGED
@@ -17,7 +17,7 @@ WREG = 0x40
17
 
18
  class Reading:
19
  def __init__(self, values: [int]):
20
- print(values)
21
  assert values[0] & 0xF0 == 0xC0, "Invalid readback"
22
  self.loff_statp = (values[0] & 0x0F) << 4 | (values[1] & 0xF0) >> 4
23
  self.loff_statn = (values[1] & 0x0F) << 4 | (values[2] & 0xF0) >> 4
@@ -28,7 +28,7 @@ class Reading:
28
  for i in range(8)
29
  ]
30
 
31
- print(self.loff_statp, self.loff_statn, self.gpios, self._channels)
32
 
33
  def channels(self):
34
  return self._channels
@@ -49,13 +49,14 @@ class Frontend:
49
  def __init__(self):
50
  self.nrst = GPIO("/dev/gpiochip2", 9, "out")
51
  self.pwdn = GPIO("/dev/gpiochip2", 12, "out")
52
- self.start = GPIO("/dev/gpiochip3", 29, "out")
 
53
  self.dev = SpiDev()
54
  self.dev.open(0, 0)
55
- self.dev.max_speed_hz = 8000000
56
  self.dev.mode = 0b01
57
 
58
- self.start.write(False)
59
  self.powerup()
60
  self.reset()
61
  self.stop_continuous()
@@ -94,5 +95,16 @@ class Frontend:
94
  values = self.dev.xfer([0x00] * 27)
95
  return Reading(values)
96
 
 
 
 
 
 
 
 
 
 
 
 
97
  def close(self):
98
  self.dev.close()
 
17
 
18
  class Reading:
19
  def __init__(self, values: [int]):
20
+ # print(values)
21
  assert values[0] & 0xF0 == 0xC0, "Invalid readback"
22
  self.loff_statp = (values[0] & 0x0F) << 4 | (values[1] & 0xF0) >> 4
23
  self.loff_statn = (values[1] & 0x0F) << 4 | (values[2] & 0xF0) >> 4
 
28
  for i in range(8)
29
  ]
30
 
31
+ # print(self.loff_statp, self.loff_statn, self.gpios, self._channels)
32
 
33
  def channels(self):
34
  return self._channels
 
49
  def __init__(self):
50
  self.nrst = GPIO("/dev/gpiochip2", 9, "out")
51
  self.pwdn = GPIO("/dev/gpiochip2", 12, "out")
52
+ self._start = GPIO("/dev/gpiochip3", 29, "out")
53
+ self.drdy = GPIO("/dev/gpiochip3", 28, "in")
54
  self.dev = SpiDev()
55
  self.dev.open(0, 0)
56
+ self.dev.max_speed_hz = 1000000
57
  self.dev.mode = 0b01
58
 
59
+ self._start.write(False)
60
  self.powerup()
61
  self.reset()
62
  self.stop_continuous()
 
95
  values = self.dev.xfer([0x00] * 27)
96
  return Reading(values)
97
 
98
+ def start(self):
99
+ self._start.write(True)
100
+ self.dev.xfer([START])
101
+
102
+ def stop(self):
103
+ self._start.write(False)
104
+ self.dev.xfer([STOP])
105
+
106
+ def is_ready(self):
107
+ return not self.drdy.read()
108
+
109
  def close(self):
110
  self.dev.close()
prog.py CHANGED
@@ -1,34 +1,62 @@
1
  from time import sleep
2
  from playsound import playsound
3
  import numpy as np
4
- import matplotlib as plt
5
  import os
 
6
 
7
  from frontend import Frontend
8
  from leds import LEDs, Color
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  FRONTEND_CONFIG = [
11
- 0x3E, # Overwrite ID?
12
  0x95, # Datarate = 500 SPS
13
  0xC0, # No tests
14
- 0xE1, # Power-down reference buffer, no bias
15
  0x00, # No lead-off
16
- 0x60, # Channel 1 active, 24 gain, no SRB2 & normal input
17
- 0x60, # Channel 2 active, 24 gain, no SRB2 & normal input
18
- 0x60, # Channel 3 active, 24 gain, no SRB2 & normal input
19
- 0x60, # Channel 4 active, 24 gain, no SRB2 & normal input
20
- 0x60, # Channel 5 active, 24 gain, no SRB2 & normal input
21
- 0x60, # Channel 6 active, 24 gain, no SRB2 & normal input
22
- 0x60, # Channel 7 active, 24 gain, no SRB2 & normal input
23
  0xE0, # Channel 8 disabled, 24 gain, no SRB2 & normal input
24
  0x00, # No bias
25
  0x00, # No bias
26
  0xFF, # Lead-off on all positive pins?
27
  0xFF, # Lead-off on all negative pins?
28
  0x00, # Normal lead-off
29
- 0x00, # Lead-off positive status (RO) ?
30
- 0x01, # Lead-off negative status (RO) ?
31
- 0x00, # All GPIOs as output ???
32
  0x20, # Disable SRB1
33
  ]
34
 
@@ -42,9 +70,21 @@ try:
42
  leds.led2(Color.BLUE)
43
 
44
  print("Configuring EEG Frontend")
45
- data = frontend.write_regs(0x00, FRONTEND_CONFIG)
 
 
 
 
 
 
 
 
 
46
  print("EEG Frontend configured")
47
  leds.led2(Color.PURPLE)
 
 
 
48
 
49
  leds.aquisition(True)
50
  sleep(0.5)
@@ -53,10 +93,29 @@ try:
53
  leds.aquisition(True)
54
 
55
  points = []
56
- for x in range(2000):
57
- sleep(0.001)
 
 
 
 
 
58
  values = frontend.read()
59
  print(values.channels())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  finally:
62
  frontend.close()
 
1
  from time import sleep
2
  from playsound import playsound
3
  import numpy as np
4
+ import matplotlib.pyplot as plt
5
  import os
6
+ from datetime import datetime, timedelta
7
 
8
  from frontend import Frontend
9
  from leds import LEDs, Color
10
 
11
+ DEFAULT_FRONTEND_CONFIG = [
12
+ 0x3E, # ID (RO)
13
+ 0x96, # Datarate = 250 SPS
14
+ 0xC0, # No tests
15
+ 0x60, # Power-down reference buffer, no bias
16
+ 0x00, # No lead-off
17
+ 0x61, # Channel 1 active, 24 gain, no SRB2 & input shorted
18
+ 0x61, # Channel 2 active, 24 gain, no SRB2 & input shorted
19
+ 0x61, # Channel 3 active, 24 gain, no SRB2 & input shorted
20
+ 0x61, # Channel 4 active, 24 gain, no SRB2 & input shorted
21
+ 0x61, # Channel 5 active, 24 gain, no SRB2 & input shorted
22
+ 0x61, # Channel 6 active, 24 gain, no SRB2 & input shorted
23
+ 0x61, # Channel 7 active, 24 gain, no SRB2 & input shorted
24
+ 0x61, # Channel 8 active, 24 gain, no SRB2 & input shorted
25
+ 0x00, # No bias
26
+ 0x00, # No bias
27
+ 0x00, # No lead-off
28
+ 0x00, # No lead-off
29
+ 0x00, # No lead-off flip
30
+ 0x00, # Lead-off positive status (RO)
31
+ 0x00, # Laed-off negative status (RO)
32
+ 0x0F, # All GPIOs as inputs
33
+ 0x00, # Disable SRB1
34
+ 0x00, # Unused
35
+ 0x00, # Single-shot, lead-off comparator disabled
36
+ ]
37
+
38
  FRONTEND_CONFIG = [
39
+ 0x3E, # ID (RO)
40
  0x95, # Datarate = 500 SPS
41
  0xC0, # No tests
42
+ 0xE0, # Power-down reference buffer, no bias
43
  0x00, # No lead-off
44
+ 0x68, # Channel 1 active, 24 gain, no SRB2 & normal input
45
+ 0x68, # Channel 2 active, 24 gain, no SRB2 & normal input
46
+ 0x68, # Channel 3 active, 24 gain, no SRB2 & normal input
47
+ 0x68, # Channel 4 active, 24 gain, no SRB2 & normal input
48
+ 0x68, # Channel 5 active, 24 gain, no SRB2 & normal input
49
+ 0x68, # Channel 6 active, 24 gain, no SRB2 & normal input
50
+ 0x68, # Channel 7 active, 24 gain, no SRB2 & normal input
51
  0xE0, # Channel 8 disabled, 24 gain, no SRB2 & normal input
52
  0x00, # No bias
53
  0x00, # No bias
54
  0xFF, # Lead-off on all positive pins?
55
  0xFF, # Lead-off on all negative pins?
56
  0x00, # Normal lead-off
57
+ 0x00, # Lead-off positive status (RO)
58
+ 0x00, # Lead-off negative status (RO)
59
+ 0x00, # All GPIOs as output ?
60
  0x20, # Disable SRB1
61
  ]
62
 
 
70
  leds.led2(Color.BLUE)
71
 
72
  print("Configuring EEG Frontend")
73
+ frontend.write_regs(0x00, FRONTEND_CONFIG)
74
+ #config = DEFAULT_FRONTEND_CONFIG[:]
75
+ #config[0x02] = 0xD0 # Activate test signals
76
+ #config[0x03] = 0xE0 # Power-up reference buffer
77
+ #for i in range(0x05, 0x0D):
78
+ # config[i] = 0x05 # Channel active, 1 gain, no SRB2 & Test signal
79
+ #frontend.write_regs(0x00, config)
80
+ data = frontend.read_regs(0x00, len(FRONTEND_CONFIG))
81
+ assert data == FRONTEND_CONFIG, f"Wrong config: {data} vs {FRONTEND_CONFIG}"
82
+ frontend.start()
83
  print("EEG Frontend configured")
84
  leds.led2(Color.PURPLE)
85
+ while not frontend.is_ready():
86
+ pass
87
+ print("Ready for data")
88
 
89
  leds.aquisition(True)
90
  sleep(0.5)
 
93
  leds.aquisition(True)
94
 
95
  points = []
96
+ START = datetime.now()
97
+ NUM_STEPS = 2000
98
+ #times = [timedelta(milliseconds=i) for i in range(NUM_STEPS)]
99
+ times = [i / 250 for i in range(NUM_STEPS)]
100
+ for x in range(NUM_STEPS):
101
+ while not frontend.is_ready():
102
+ pass
103
  values = frontend.read()
104
  print(values.channels())
105
+ points.append(values.channels())
106
+ while frontend.is_ready():
107
+ pass
108
+ leds.aquisition(False)
109
+
110
+ points = np.transpose(np.array(points))
111
+ fig, ax = plt.subplots()
112
+ for i in [0]:#range(8):
113
+ ax.plot(times, points[i] * 4.5 / 2**24, label='channel #' + str(i))
114
+ ax.set_xlabel('Time since start (s)')
115
+ ax.set_ylabel('Value')
116
+ ax.set_title('Test readings')
117
+ ax.legend()
118
+ plt.savefig('readings.png')
119
 
120
  finally:
121
  frontend.close()
readings.png ADDED
tests.ipynb ADDED
The diff for this file is too large to render. See raw diff