Spaces:
Build error
Build error
Xavier L'Heureux
commited on
Add wrapper for frontend reading
Browse files- frontend.py +36 -5
- prog.py +13 -18
frontend.py
CHANGED
|
@@ -15,6 +15,36 @@ RDATA = 0x12
|
|
| 15 |
RREG = 0x20
|
| 16 |
WREG = 0x40
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
class Frontend:
|
| 19 |
def __init__(self):
|
| 20 |
self.nrst = GPIO("/dev/gpiochip2", 9, "out")
|
|
@@ -50,9 +80,9 @@ class Frontend:
|
|
| 50 |
def write_regs(self, start, values):
|
| 51 |
self.dev.xfer([WREG | (start & 0x1F), (len(values) - 1) & 0x1F] + values)
|
| 52 |
|
| 53 |
-
def read(self
|
| 54 |
-
values = self.dev.xfer([RDATA] + [0x00] *
|
| 55 |
-
return values[1:]
|
| 56 |
|
| 57 |
def start_continuous(self):
|
| 58 |
self.dev.xfer([RDATAC])
|
|
@@ -60,8 +90,9 @@ class Frontend:
|
|
| 60 |
def stop_continuous(self):
|
| 61 |
self.dev.xfer([SDATAC])
|
| 62 |
|
| 63 |
-
def read_continuous(self
|
| 64 |
-
values = self.dev.xfer([0x00] *
|
|
|
|
| 65 |
|
| 66 |
def close(self):
|
| 67 |
self.dev.close()
|
|
|
|
| 15 |
RREG = 0x20
|
| 16 |
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
|
| 24 |
+
self.gpios = values[2] & 0x0F
|
| 25 |
+
|
| 26 |
+
self._channels = [
|
| 27 |
+
(values[3 + i * 3] << 16) | (values[4 + i * 3] << 8) | values[5 + i * 3]
|
| 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
|
| 35 |
+
|
| 36 |
+
def gpio(idx: int):
|
| 37 |
+
assert 0 <= idx <= 3, "Invalid gpio index"
|
| 38 |
+
return (self.gpios >> idx) & 0x01 == 0x01
|
| 39 |
+
|
| 40 |
+
def loff_p(idx: int):
|
| 41 |
+
assert 0 <= idx <= 7, "Invalid loff index"
|
| 42 |
+
return (self.loff_statp >> idx) & 0x01 == 0x01
|
| 43 |
+
|
| 44 |
+
def loff_n(idx: int):
|
| 45 |
+
assert 0 <= idx <= 7, "Invalid loff index"
|
| 46 |
+
return (self.loff_statn >> idx) & 0x01 == 0x01
|
| 47 |
+
|
| 48 |
class Frontend:
|
| 49 |
def __init__(self):
|
| 50 |
self.nrst = GPIO("/dev/gpiochip2", 9, "out")
|
|
|
|
| 80 |
def write_regs(self, start, values):
|
| 81 |
self.dev.xfer([WREG | (start & 0x1F), (len(values) - 1) & 0x1F] + values)
|
| 82 |
|
| 83 |
+
def read(self):
|
| 84 |
+
values = self.dev.xfer([RDATA] + [0x00] * 27)
|
| 85 |
+
return Reading(values[1:])
|
| 86 |
|
| 87 |
def start_continuous(self):
|
| 88 |
self.dev.xfer([RDATAC])
|
|
|
|
| 90 |
def stop_continuous(self):
|
| 91 |
self.dev.xfer([SDATAC])
|
| 92 |
|
| 93 |
+
def read_continuous(self):
|
| 94 |
+
values = self.dev.xfer([0x00] * 27)
|
| 95 |
+
return Reading(values)
|
| 96 |
|
| 97 |
def close(self):
|
| 98 |
self.dev.close()
|
prog.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
from time import sleep
|
| 2 |
from playsound import playsound
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from frontend import Frontend
|
| 5 |
-
from leds import LEDs
|
| 6 |
|
| 7 |
FRONTEND_CONFIG = [
|
| 8 |
0x3E, # Overwrite ID?
|
|
@@ -32,16 +35,16 @@ FRONTEND_CONFIG = [
|
|
| 32 |
frontend = Frontend()
|
| 33 |
leds = LEDs()
|
| 34 |
|
| 35 |
-
playsound('sample.mp3')
|
| 36 |
-
|
| 37 |
try:
|
| 38 |
-
data = frontend.
|
| 39 |
assert data == [0x3E], "Wrong output"
|
| 40 |
print("EEG Frontend responsive")
|
|
|
|
| 41 |
|
| 42 |
print("Configuring EEG Frontend")
|
| 43 |
-
data = frontend.
|
| 44 |
print("EEG Frontend configured")
|
|
|
|
| 45 |
|
| 46 |
leds.aquisition(True)
|
| 47 |
sleep(0.5)
|
|
@@ -49,19 +52,11 @@ try:
|
|
| 49 |
sleep(0.5)
|
| 50 |
leds.aquisition(True)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
for state in [RED, BLUE, PURPLE, CLOSED] * 3:
|
| 59 |
-
leds.led2(state)
|
| 60 |
-
sleep(0.2)
|
| 61 |
-
|
| 62 |
-
for state in [RED, CLOSED] * 3:
|
| 63 |
-
leds.led3(state)
|
| 64 |
-
sleep(0.2)
|
| 65 |
|
| 66 |
finally:
|
| 67 |
frontend.close()
|
|
|
|
| 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?
|
|
|
|
| 35 |
frontend = Frontend()
|
| 36 |
leds = LEDs()
|
| 37 |
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
+
data = frontend.read_regs(0x00, 1)
|
| 40 |
assert data == [0x3E], "Wrong output"
|
| 41 |
print("EEG Frontend responsive")
|
| 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)
|
|
|
|
| 52 |
sleep(0.5)
|
| 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()
|