hemokit 0.6.2 → 0.6.3
raw patch · 3 files changed
+49/−23 lines, 3 files
Files
- apps/Dump.hs +24/−11
- apps/DumpConduit.hs +24/−11
- hemokit.cabal +1/−1
apps/Dump.hs view
@@ -47,7 +47,7 @@ -- | In what format to print the output. -- `Default` is raw bytes to stdout for `Raw` mode and `show` for everything else.-data OutputFormat = Default | Json | Spaced deriving (Eq, Ord, Show)+data OutputFormat = Default | Json | Spaced | SensorBytes deriving (Eq, Ord, Show) -- | Whether to serve via plain TCP or Websockets (hostname and port). data ServeMethod@@ -92,10 +92,11 @@ -- | `OutputFormat` command line parser. parseOutputFormat :: Monad m => String -> m OutputFormat parseOutputFormat s = case s of- "default"-> return Default- "json" -> return Json- "spaced" -> return Spaced- _ -> fail "Format is not valid. Must be 'default', 'json', or 'spaced'."+ "default" -> return Default+ "json" -> return Json+ "spaced" -> return Spaced+ "sensorbytes" -> return SensorBytes+ _ -> fail "Format is not valid. Must be 'default', 'json', 'spaced' or 'sensorbytes'." -- | Parses host and port from a string like "0.0.0.0:1234".@@ -115,6 +116,9 @@ splitLast sep s = let sp = splitOn sep s -- splitOn never returns [] in (intercalate sep (init sp), last sp) ++-- | Space-separates all values in the order+-- `[counter] [battery] [gyroX] [gyroY] [sensors..] [qualities..]`. whitespaceFormat :: EmotivState -> BSL.ByteString whitespaceFormat EmotivState{ counter, battery, gyroX, gyroY, sensors, qualities } = Builder.toLazyByteString . mconcat@@ -123,6 +127,13 @@ ints = [ counter, battery, gyroX, gyroY ] ++ V.toList sensors ++ V.toList qualities +-- | Formats all sensor values as 16 bit ints (big endian) into a `ByteString`.+sensorBytesFormat :: EmotivState -> BSL.ByteString+sensorBytesFormat EmotivState{ sensors }+ = Builder.toLazyByteString . V.foldl1' (<>)+ . V.map (Builder.word16BE . fromIntegral) $ sensors++ main :: IO () main = do DumpArgs{ emotivArgs@@ -135,7 +146,7 @@ -- Catch invalid mode/format combinations immediately -- (so that we don't block first and error afterwards, see `formatOutput`).- when (format == Spaced && mode /= State) $+ when (format `elem` [Spaced, SensorBytes] && mode /= State) $ error $ "cannot space-format in " ++ show mode ++ " mode" if listDevices -- Only list devices@@ -150,9 +161,10 @@ Right device -> do let formatOutput x = case format of- Default -> BSL8.pack (show x)- Json -> encode x- Spaced -> error "hemokit-dump BUG: formatOutput/spaced not caught early"+ Default -> BSL8.pack (show x)+ Json -> encode x+ Spaced -> error "hemokit-dump BUG: formatOutput/spaced not caught early"+ SensorBytes -> error "hemokit-dump BUG: formatOutput/sensorbytes not caught early" -- Print to stdout or serve via websockets? Show the datatype or format via JSON? -- `output` accepts anything that's JSON-formattable and showable (wrapped in JsonShowable).@@ -181,8 +193,9 @@ State -> readEmotiv device `withJustM` \(state, _) -> case format of- Spaced -> output $ whitespaceFormat state- _ -> output $ formatOutput state+ Spaced -> output $ whitespaceFormat state+ SensorBytes -> output $ sensorBytesFormat state+ _ -> output $ formatOutput state Raw -> readEmotivRaw device `withJustM` \rawBytes -> do case format of
apps/DumpConduit.hs view
@@ -48,7 +48,7 @@ -- | In what format to print the output. -- `Default` is raw bytes to stdout for `Raw` mode and `show` for everything else.-data OutputFormat = Default | Json | Spaced deriving (Eq, Ord, Show)+data OutputFormat = Default | Json | Spaced | SensorBytes deriving (Eq, Ord, Show) -- | Whether to serve via plain TCP or Websockets (hostname and port). data ServeMethod@@ -93,10 +93,11 @@ -- | `OutputFormat` command line parser. parseOutputFormat :: Monad m => String -> m OutputFormat parseOutputFormat s = case s of- "default"-> return Default- "json" -> return Json- "spaced" -> return Spaced- _ -> fail "Format is not valid. Must be 'default', 'json', or 'spaced'."+ "default" -> return Default+ "json" -> return Json+ "spaced" -> return Spaced+ "sensorbytes" -> return SensorBytes+ _ -> fail "Format is not valid. Must be 'default', 'json', 'spaced' or 'sensorbytes'." -- | Parses host and port from a string like "0.0.0.0:1234".@@ -116,6 +117,9 @@ splitLast sep s = let sp = splitOn sep s -- splitOn never returns [] in (intercalate sep (init sp), last sp) ++-- | Space-separates all values in the order+-- `[counter] [battery] [gyroX] [gyroY] [sensors..] [qualities..]`. whitespaceFormat :: EmotivState -> BSL.ByteString whitespaceFormat EmotivState{ counter, battery, gyroX, gyroY, sensors, qualities } = Builder.toLazyByteString . mconcat@@ -124,6 +128,13 @@ ints = [ counter, battery, gyroX, gyroY ] ++ V.toList sensors ++ V.toList qualities +-- | Formats all sensor values as 16 bit ints (big endian) into a `ByteString`.+sensorBytesFormat :: EmotivState -> BSL.ByteString+sensorBytesFormat EmotivState{ sensors }+ = Builder.toLazyByteString . V.foldl1' (<>)+ . V.map (Builder.word16BE . fromIntegral) $ sensors++ main :: IO () main = do DumpArgs{ emotivArgs@@ -136,7 +147,7 @@ -- Catch invalid mode/format combinations immediately -- (so that we don't block first and error afterwards, see `formatOutput`).- when (format == Spaced && mode /= State) $+ when (format `elem` [Spaced, SensorBytes] && mode /= State) $ error $ "cannot space-format in " ++ show mode ++ " mode" if listDevices -- Only list devices@@ -153,9 +164,10 @@ let -- Show the datatype or format via JSON? formatConduit :: (ToJSON i, Show i) => Conduit i IO BSL.ByteString formatConduit = case format of- Default -> CL.map (BSL8.pack . show)- Json -> CL.map encode- Spaced -> error "hemokit-dump BUG: formatOutput/spaced not caught early"+ Default -> CL.map (BSL8.pack . show)+ Json -> CL.map encode+ Spaced -> error "hemokit-dump BUG: formatOutput/spaced not caught early"+ SensorBytes -> error "hemokit-dump BUG: formatOutput/sensorbytes not caught early" -- Print to stdout or serve via websockets? outputSink = case serve of@@ -173,8 +185,9 @@ Packets -> throttled (emotivPackets device) $$ formatConduit =$ outputSink State -> throttled (emotivStates device) $$ case format of- Spaced -> CL.map whitespaceFormat =$ outputSink- _ -> formatConduit =$ outputSink+ Spaced -> CL.map whitespaceFormat =$ outputSink+ SensorBytes -> CL.map sensorBytesFormat =$ outputSink+ _ -> formatConduit =$ outputSink Raw -> throttled (rawSource device) $$ case format of Default -> rawBytesSink
hemokit.cabal view
@@ -1,5 +1,5 @@ name: hemokit-version: 0.6.2+version: 0.6.3 license: MIT copyright: 2013 Niklas Hambüchen <mail@nh2.me>, Patrick Chilton <chpatrick@gmail.com> author: Niklas Hambüchen <mail@nh2.me>, Patrick Chilton <chpatrick@gmail.com>