我们将数据从 extEEPROM 导出来,温度为 uint16_t 的类型,扩大了 100 倍,
我们使用 ByteString,我们将其 unpack Word8 序列,然后将其转换为 Word16,如下:
toTempList :: [Word8] -> [Word16]
toTempList [] = []
toTempList [x] = [fromIntegral x]
toTempList (x:y:xs) = (fromIntegral x * 256 + fromIntegral y) : toTempList xs
得益于模式匹配,我们很方便的就处理出来了。
我们经解析出来的数据 Word16 转成 Float 类型,如下:
toRealTemp :: Word16 -> Float
toRealTemp w = fromIntegral w / 100
通过两个函数我们 ByteString 转成温度的数组,如下:
import Data.ByteString (unpack)
let tempList = map toRealTemp . toTempList $ unpack tempListString
当然我们需要温度的系列,我们可以方便的使用 zip 进行整合
let tempPairList = zip [1..] tempList
nupic.visualizations
可视化工具需要用的是 csv 的格式,所以我们需要将数据转出成为 csv 文件,操作如下:
import Data.List (intercalate)
toString :: (Int, Float) -> String
toString (t, v) = show t ++ ", " ++ show v
putStrLn "timestamp, temperature"
putStrLn $ intercalate "\n" $ map toString tempPairList
最终可视化后温度曲线如下: