Safe Haskell | None |
---|---|
Language | Haskell2010 |
A library for reading from an Emotic EPOC EEG.
- Use
getEmotivDevices
to list available EEGs. - Use
openEmotivDevice
to open a device for reading. - Use
readEmotiv
read from an open device. - You will obtain
EmotivPacket
s andEmotivState
s.
- _EMOTIV_VENDOR_ID :: VendorID
- _EMOTIV_PRODUCT_ID :: ProductID
- data EmotivDeviceInfo = EmotivDeviceInfo {}
- data EmotivRawDevice
- = HidapiDevice { }
- | HandleDevice { }
- data EmotivDevice = EmotivDevice {}
- getEmotivDevices :: IO [EmotivDeviceInfo]
- openEmotivDevice :: EmotivModel -> EmotivDeviceInfo -> IO EmotivDevice
- openEmotivDeviceFile :: EmotivModel -> SerialNumber -> String -> IO EmotivDevice
- openEmotivDeviceHandle :: EmotivModel -> SerialNumber -> Handle -> IO EmotivDevice
- readEmotiv :: EmotivDevice -> IO (Maybe (EmotivState, EmotivPacket))
- data EmotivException
- data SerialNumber
- makeSerialNumber :: ByteString -> Maybe SerialNumber
- makeSerialNumberFromString :: String -> Maybe SerialNumber
- deviceInfoSerial :: EmotivDeviceInfo -> Maybe SerialNumber
- data EmotivModel
- data EmotivPacket = EmotivPacket {
- packetCounter :: Int
- packetBattery :: Maybe Int
- packetGyroX :: Int
- packetGyroY :: Int
- packetSensors :: Vector Int
- packetQuality :: Maybe (Sensor, Int)
- data EmotivState = EmotivState {}
- data Sensor
- allSensors :: [Sensor]
- newtype EmotivRawData = EmotivRawData {}
- readEmotivRaw :: EmotivDevice -> IO (Maybe EmotivRawData)
- makeEmotivRawData :: ByteString -> EmotivRawData
- parsePacket :: EmotivRawData -> EmotivPacket
- updateEmotivState :: EmotivDevice -> EmotivRawData -> IO (EmotivState, EmotivPacket)
- decrypt :: SerialNumber -> EmotivModel -> ByteString -> EmotivRawData
- newtype BitMask = BitMask [Word8]
- getSensorMask :: Sensor -> BitMask
- qualityMask :: BitMask
- getLevel :: EmotivRawData -> BitMask -> Int
- batteryValue :: Word8 -> Int
- qualitySensorFromByte0 :: Word8 -> Maybe Sensor
- withDataFromLastEEG :: EmotivModel -> ((EmotivState, EmotivPacket) -> IO ()) -> IO ()
Opening and reading from EEGs
_EMOTIV_VENDOR_ID :: VendorID Source
The USB vendor ID of the Emotiv EPOC.
_EMOTIV_PRODUCT_ID :: ProductID Source
The USB product ID of the Emotiv EPOC.
data EmotivDeviceInfo Source
Identifies an Emotiv device.
EmotivDeviceInfo | |
|
data EmotivRawDevice Source
An "open" data source to read bytes from.
HidapiDevice | |
| |
HandleDevice | |
|
data EmotivDevice Source
Identifies an open Emotiv device.
Also contains the cumulative EmotivState
of the EEG.
EmotivDevice | |
|
getEmotivDevices :: IO [EmotivDeviceInfo] Source
Lists all EPOC devices, ordered by interface number. If you do not actively choose amongst them, the last one is usually the one you want (especially if only 1 EEG is connected).
openEmotivDevice :: EmotivModel -> EmotivDeviceInfo -> IO EmotivDevice Source
Opens a given Emotiv device.
Returns an EmotivDevice
to read from with readEmotiv
.
openEmotivDeviceFile :: EmotivModel -> SerialNumber -> String -> IO EmotivDevice Source
Creates an EmotivDevice
device from a path, e.g. a device like
devhidraw1
or a normal file containing dumped binary data.
openEmotivDeviceHandle :: EmotivModel -> SerialNumber -> Handle -> IO EmotivDevice Source
Creates an EmotivDevice
device from an open file handle.
readEmotiv :: EmotivDevice -> IO (Maybe (EmotivState, EmotivPacket)) Source
Reads one 32 byte packet from the device, parses the raw bytes into an
EmotivPacket
and updates the cumulative EmotivState
that we maintain
for that device.
Returns both the packet read from the device and the updated state.
Returns Nothing on end of input (or if there are < 32 bytes before it).
Note that if the EEG is (turned) off, this function block until it is turned on again.
data EmotivException Source
Emotiv related errors.
InvalidSerialNumber SerialNumber | Serial does not have right format. |
CouldNotReadSerial DevicePath | We could not read the serial from the device. |
OtherEmotivException String |
data SerialNumber Source
A valid Emotiv serial number. 16 bytes.
makeSerialNumber :: ByteString -> Maybe SerialNumber Source
Checks an Emotiv serial, returning a SerialNumber
if it's valid.
makeSerialNumberFromString :: String -> Maybe SerialNumber Source
Like makeSerialNumber
, using a String
.
deviceInfoSerial :: EmotivDeviceInfo -> Maybe SerialNumber Source
Conveniently expose the serial number of a device.
data EmotivModel Source
Whether the EPOC is a consumer or developer model.
This affects how the EEG data is to be decrypted.
You can check if you are using the correct model by seeing if the packet
counter
increases from 0 until 128 on subsequent packets.
EEG data
data EmotivPacket Source
Contains the data of a single packet sent from the device.
Accumulated data (the current state) is available in EmotivState
.
EmotivPacket | |
|
data EmotivState Source
Contains the "current state" of the EEG, cumulateively updated by
incoming EmotivPacket
s.
The sensors of an Emotiv EPOC. Uses the names from the International 10-20 system.
allSensors :: [Sensor] Source
Contains all Sensor
s.
Dealing with (decrypted) raw data
newtype EmotivRawData Source
Wraps (unencrypted) Emotiv raw data. Ensures that it is 32 bytes.
readEmotivRaw :: EmotivDevice -> IO (Maybe EmotivRawData) Source
Reads one 32 byte packet from the device and decrypts it to raw data.
Returns Nothing on end of input (or if there are < 32 bytes before it).
Note that if the EEG is (turned) off, this function block until it is turned on again.
makeEmotivRawData :: ByteString -> EmotivRawData Source
Treat a ByteString
as Emotiv raw data.
Errors if the input is non 32 bytes.
parsePacket :: EmotivRawData -> EmotivPacket Source
Parses an EmotivPacket
from raw bytes.
updateEmotivState :: EmotivDevice -> EmotivRawData -> IO (EmotivState, EmotivPacket) Source
Given a device and a Emotiv raw data, parses the raw data into an
EmotivPacket
and updates the cumulative EmotivState
that we maintain
for that device.
Care should be taken that raw data is fed into this function in the right order (e.g. respecting the EEG's increasing sequence numbers and quality updates).
This function is only neededif you want to obtain both raw data and
parsed packages.
If you are not interested in raw data, use readEmotiv
instead.
Returns both the packet read from the device and the updated state.
Encrypted raw data
decrypt :: SerialNumber -> EmotivModel -> ByteString -> EmotivRawData Source
Takes a 32 bytes encrypted EEG data, returns 32 bytes decrypted EEG data.
Internals
Describes the indices of bits to make up a certain value.
getSensorMask :: Sensor -> BitMask Source
Describes which bits in a raw data packet make up the given sensor.
Describes which bits in a raw data packat make up a sensor quality value.
getLevel :: EmotivRawData -> BitMask -> Int Source
Extracts the sensor value for the given sensor from Emotiv raw data.
batteryValue :: Word8 -> Int Source
Parses a battery percentage value from a byte.
qualitySensorFromByte0 :: Word8 -> Maybe Sensor Source
Which sensor's quality is transmitted in the packet (depends on first byte, the packet counter).
Interactive use
withDataFromLastEEG :: EmotivModel -> ((EmotivState, EmotivPacket) -> IO ()) -> IO () Source
Opens and reads from the last available device, giving all data from it to the given function. Stops if end of input is reached.
Intended for use with ghci.
Examples:
withDataFromLastEEG Consumer print withDataFromLastEEG Consumer (print . packetQuality . snd) withDataFromLastEEG Consumer (putStrLn . unwords . map show . V.toList . qualities . fst)