Safe Haskell | None |
---|
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 {
- handleDevice :: Handle
- data EmotivDevice = EmotivDevice {
- rawDevice :: EmotivRawDevice
- serial :: SerialNumber
- emotivModel :: EmotivModel
- stateRef :: IORef (Maybe EmotivState)
- 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 {
- emotivRawDataBytes :: ByteString
- 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
The USB vendor ID of the Emotiv EPOC.
_EMOTIV_PRODUCT_ID :: ProductID
The USB product ID of the Emotiv EPOC.
data EmotivDeviceInfo
Identifies an Emotiv device.
EmotivDeviceInfo | |
|
Show EmotivDeviceInfo | |
Generic EmotivDeviceInfo | |
NFData EmotivDeviceInfo |
data EmotivRawDevice
An open data source to read bytes from.
HidapiDevice | |
| |
HandleDevice | |
|
Generic EmotivRawDevice |
data EmotivDevice
Identifies an open Emotiv device.
Also contains the cumulative EmotivState
of the EEG.
EmotivDevice | |
|
Generic EmotivDevice |
getEmotivDevices :: IO [EmotivDeviceInfo]
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
Opens a given Emotiv device.
Returns an EmotivDevice
to read from with readEmotiv
.
openEmotivDeviceFile :: EmotivModel -> SerialNumber -> String -> IO EmotivDevice
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
Creates an EmotivDevice
device from an open file handle.
readEmotiv :: EmotivDevice -> IO (Maybe (EmotivState, EmotivPacket))
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
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 EmotivException | |
Show EmotivException | |
Typeable EmotivException | |
Generic EmotivException | |
NFData EmotivException | |
Exception EmotivException |
data SerialNumber
A valid Emotiv serial number. 16 bytes.
Eq SerialNumber | |
Ord SerialNumber | |
Show SerialNumber | |
Generic SerialNumber |
makeSerialNumber :: ByteString -> Maybe SerialNumber
Checks an Emotiv serial, returning a SerialNumber
if it's valid.
makeSerialNumberFromString :: String -> Maybe SerialNumber
Like makeSerialNumber
, using a String
.
deviceInfoSerial :: EmotivDeviceInfo -> Maybe SerialNumber
Conveniently expose the serial number of a device.
data EmotivModel
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.
Eq EmotivModel | |
Ord EmotivModel | |
Show EmotivModel | |
Generic EmotivModel |
EEG data
data EmotivPacket
Contains the data of a single packet sent from the device.
Accumulated data (the current state) is available in EmotivState
.
EmotivPacket | |
|
Eq EmotivPacket | |
Ord EmotivPacket | |
Show EmotivPacket | |
Generic EmotivPacket | |
NFData EmotivPacket |
data EmotivState
Contains the current state of the EEG, cumulateively updated by
incoming EmotivPacket
s.
Eq EmotivState | |
Ord EmotivState | |
Show EmotivState | |
Generic EmotivState | |
NFData EmotivState |
data Sensor
The sensors of an Emotiv EPOC. Uses the names from the International 10-20 system.
allSensors :: [Sensor]
Contains all Sensor
s.
Dealing with (decrypted) raw data
newtype EmotivRawData
Wraps (unencrypted) Emotiv raw data. Ensures that it is 32 bytes.
EmotivRawData | |
|
Eq EmotivRawData | |
Ord EmotivRawData | |
Show EmotivRawData | |
Generic EmotivRawData | |
NFData EmotivRawData |
readEmotivRaw :: EmotivDevice -> IO (Maybe EmotivRawData)
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
Treat a ByteString
as Emotiv raw data.
Errors if the input is non 32 bytes.
parsePacket :: EmotivRawData -> EmotivPacket
Parses an EmotivPacket
from raw bytes.
updateEmotivState :: EmotivDevice -> EmotivRawData -> IO (EmotivState, EmotivPacket)
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
Takes a 32 bytes encrypted EEG data, returns 32 bytes decrypted EEG data.
Internals
getSensorMask :: Sensor -> BitMask
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
Extracts the sensor value for the given sensor from Emotiv raw data.
batteryValue :: Word8 -> Int
Parses a battery percentage value from a byte.
qualitySensorFromByte0 :: Word8 -> Maybe Sensor
Which sensor's quality is transmitted in the packet (depends on first byte, the packet counter).
Interactive use
withDataFromLastEEG :: EmotivModel -> ((EmotivState, EmotivPacket) -> IO ()) -> IO ()
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)