Skip to content

Commit

Permalink
Slave to the linter...
Browse files Browse the repository at this point in the history
  • Loading branch information
dangirsh committed Apr 24, 2014
1 parent cb317e9 commit cc10c5c
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 59 deletions.
5 changes: 1 addition & 4 deletions Auto.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Auto where

import Control.Monad.Trans.Reader (ReaderT, runReaderT, ask)
import Control.Applicative ((<$>))
import qualified Data.Map as M
import Control.Monad.IO.Class (liftIO)

type Env a = (M.Map String a)

Expand All @@ -22,4 +19,4 @@ type Auto a = ReaderT (Env a) IO


runAuto :: Auto a b -> Env a -> IO b
runAuto auto env = runReaderT auto $ env
runAuto = runReaderT
1 change: 0 additions & 1 deletion CCSDS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

module CCSDS where

import Data.Word
import Data.BitVector
import Control.Exception.Base (assert)
import Control.Applicative ((<$>))
Expand Down
4 changes: 2 additions & 2 deletions Command.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE DeriveGeneric #-}

module Command (
Command (..)
Command (Command)
) where

import Numeric (showHex)
Expand All @@ -24,5 +24,5 @@ instance FromJSON Command

instance Show Command where

show (Command cc ps) = "CMD: " ++ " cc:" ++ showHex cc " " ++ show ps
show (Command c ps) = "CMD: " ++ " cc:" ++ showHex c " " ++ show ps

8 changes: 1 addition & 7 deletions Common.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
module Common where

import Data.BitVector hiding (showHex)
import Data.Binary (encode, Binary)
import Data.Binary.Put
import Data.Binary.Get
import Data.Word
import Numeric (showHex)
import Data.ByteString.Lazy.Builder (Builder, toLazyByteString)
import qualified Data.ByteString.Lazy as B


type Byte = Word8
Expand Down Expand Up @@ -37,5 +31,5 @@ bits2bytes bits | length bits `mod` 8 == 0 = map fromInteger (pack bits)

swapBytes :: [Byte] -> [Byte]
swapBytes [] = []
swapBytes (b1:b2:bs) = (b2:b1:swapBytes bs)
swapBytes (b1:b2:bs) = b2:b1:swapBytes bs
swapBytes _ = error "Must be an even number of bytes to swap."
11 changes: 3 additions & 8 deletions Main.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import System.Environment (getArgs)
--import System.Environment (getArgs)
import Control.Monad
import Control.Applicative
import Data.BitVector hiding (showHex)
import Numeric (showHex)
import qualified Data.ByteString.Lazy as B
import Control.Concurrent
import Control.Concurrent.MVar
import Controller
import Send
import Parse
Expand All @@ -29,8 +24,8 @@ myForkIOs actions = mapM myForkIO actions >>= mapM_ takeMVar

run :: Controller -> IO ()
run (Controller {meta=cm, sequenced=s, parallel=p}) = do
let actions = [mapM_ go s] ++ map go p
let actions = mapM_ go s : map go p
--myForkIOs actions
sequence_ actions
where
go mm = send cm (frequency mm) (file mm) >> return ()
go mm = void $ send cm (frequency mm) (file mm)
19 changes: 13 additions & 6 deletions Message.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Message (
,MessageDef (..)
) where

import Data.Aeson (FromJSON, parseJSON, (.:), (.:?), Value(Object))
import Data.Aeson (FromJSON, parseJSON, (.:), Value(Object))
import GHC.Generics (Generic)
import Control.Applicative ((<$>), (<*>))
import Data.BitVector (fromBool)
Expand Down Expand Up @@ -39,15 +39,22 @@ instance (FromJSON a) => FromJSON (MessageDef a)

instance CCSDS (Message Telemetry) where

--packetType (Message _ (Command _ _)) = fromBool True
packetType (Message _ (Telemetry _)) = fromBool False

applicationProcessId (Message mid _) = safeBitVec 11 mid

--secondaryHeader (Message _ (Command cc _)) = [cc, 0]
secondaryHeader (Message _ (Telemetry _)) = [0, 0, 0, 0, 0, 0] -- timestamp
--secondaryHeader _ = undefined

--payload (Message _ (Command _ ps)) = concat <$> mapM packParam ps
payload (Message _ (Telemetry ps)) = concat <$> mapM packParam ps
--payload _ = undefined



instance CCSDS (Message Command) where

packetType (Message _ (Command _ _)) = fromBool True

applicationProcessId (Message mid _) = safeBitVec 11 mid

secondaryHeader (Message _ (Command cc _)) = [cc, 0]

payload (Message _ (Command _ ps)) = concat <$> mapM packParam ps
4 changes: 1 addition & 3 deletions Parameter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ module Parameter (
import Control.Applicative ((<$>), (<*>))
import Data.Aeson
import Data.Aeson.Types
import Data.Aeson.Types
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word8, Word16, Word32, Word64)
import Data.Vector (toList)
import Numeric (showHex)
import qualified Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Builder
import Foreign.Marshal.Utils (fromBool)
import qualified Data.Map as M
import qualified Data.Text as T
import Common
import Auto
Expand All @@ -39,7 +37,7 @@ data Parameter = S String String Int


packParam :: Parameter -> Auto Parameter [Byte]
packParam (S _ s n) = return $ b2w (string7 s) ++ replicate (n-(length s)) 0
packParam (S _ s n) = return $ b2w (string7 s) ++ replicate (n - length s) 0
packParam (B _ b) = return $ b2w . word8 . fromBool $ b
packParam (I8 _ i) = return $ b2w . int8 $ i
packParam (W8 _ w) = return $ b2w . word8 $ w
Expand Down
9 changes: 1 addition & 8 deletions Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

module Parse where

import Control.Monad
import Control.Applicative
import Data.Aeson (FromJSON, parseJSON, (.:), (.:?), Value(Object), eitherDecode)
import Data.Aeson.Types (Parser)
import Data.Aeson (FromJSON, eitherDecode)
import qualified Data.ByteString.Lazy as B
import Data.ByteString.Search (replace)
import qualified Data.ByteString.Lazy.Char8 as C
import Controller
import Message
import Command
import Telemetry


parseFile :: (FromJSON a) => FilePath -> IO a
Expand Down
6 changes: 3 additions & 3 deletions Send.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Network.Socket.ByteString (sendTo)
import qualified Data.ByteString.Lazy as B
import System.FilePath.Posix (takeExtensions)
import Control.Concurrent (threadDelay)
import Text.Show.Pretty
import Text.Show.Pretty (ppShow)
import System.IO
import qualified Data.Map as M
import Data.List (transpose)
Expand All @@ -33,8 +33,8 @@ import Auto
send :: ControllerMeta -> Frequency -> FilePath -> IO ()
send meta freq file =
case takeExtensions file of
".tlm" -> (sendFile meta freq file :: IO (Message Telemetry)) >> return ()
--".cmd" -> (sendFile meta freq file :: IO Command) >> return ()
".tlm" -> void (sendFile meta freq file :: IO (Message Telemetry))
".cmd" -> void (sendFile meta freq file :: IO (Message Command))


sendFile :: (FromJSON a, CCSDS (Message a)) => ControllerMeta -> Frequency -> FilePath -> IO (Message a)
Expand Down
5 changes: 1 addition & 4 deletions Telemetry.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
{-# LANGUAGE DeriveGeneric #-}

module Telemetry (
Telemetry (..)
Telemetry(Telemetry)
) where

import Common
import CCSDS
import Numeric (showHex)
import GHC.Generics (Generic)
import Data.Aeson (FromJSON)
import Parameter
Expand Down
30 changes: 17 additions & 13 deletions Variable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Variable (
Variable (..)
) where

import Data.Int (Int8, Int16, Int32, Int64)
--import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word8, Word16, Word32, Word64)
import Data.Aeson
import Data.Aeson.Types
Expand All @@ -18,20 +18,24 @@ data Variable = F String [Float]

instance FromJSON Variable where
parseJSON (Object o) = do
typ <- (o .: "type" :: Parser String)
typ <- o .: "type" :: Parser String
id_ <- o .: "id"
element_type <- (o .: "element_type" :: Parser String)
element_type <- o .: "element_type" :: Parser String
case typ of
"range" -> makeRange element_type id_ o
s -> undefined
"range" -> makeRange element_type id_
_ -> undefined
where
makeRange "float" id_ o = F id_ <$> (makeRangeList o)
makeRange "uint8" id_ o = W8 id_ <$> (makeRangeList o)

makeRangeList o = do
start <- o .: "start"
end <- o .: "end"
spacing <- o .: "spacing"
makeRange "float" id_ = F id_ <$> rangeList o
makeRange "uint8" id_ = W8 id_ <$> rangeList o
makeRange _ id_ = undefined

rangeList obj = do
start <- obj .: "start"
end <- obj .: "end"
spacing <- obj .: "spacing"
return $ takeWhile (<= end) $ f start spacing

f a b = a : f (a + b) b
f a b = a : f (a + b) b


parseJSON _ = error "Invalid variables definition."

0 comments on commit cc10c5c

Please sign in to comment.