{-# LANGUAGE TupleSections #-}
module STLink.Detection
( pickBoard
)
where
import System.IO
import System.USB
import Data.Functor
import qualified Data.Vector as V
import Data.Word
import qualified Data.Text as T
import Text.PrettyPrint.Boxes
import Numeric ( showHex )
stVendorId :: Word16
stVendorId = 0x0483
getDeviceAndDesc :: Device -> IO (Device, DeviceDesc)
getDeviceAndDesc d = (d, ) <$> getDeviceDesc d
toHex :: (Show n, Integral n) => n -> String
toHex x = showHex x ""
getStrUSB :: (DeviceDesc -> Maybe StrIx) -> (Device, DeviceDesc) -> IO T.Text
getStrUSB f (d, dd) = withDeviceHandle d $ \h -> case f dd of
Just i -> getStrDescFirstLang h i 256
Nothing -> pure ""
printBoards :: [(Device, DeviceDesc)] -> IO ()
printBoards ds = do
names <- traverse (getStrUSB deviceProductStrIx) ds
serials <- traverse (getStrUSB deviceSerialNumberStrIx) ds
let numbox =
vcat right
$ "num"
: (text . (\t -> "[" ++ t ++ "]") . show <$> [1 .. length ds])
productbox =
vcat right $ "id" : (text . toHex . deviceProductId . snd <$> ds)
namebox = vcat left $ "name" : (text . T.unpack <$> names)
serialbox = vcat left $ "serial" : (text . T.unpack <$> serials)
printBox $ numbox <+> productbox <+> namebox <+> serialbox
findStMicros :: IO (V.Vector (Device, DeviceDesc))
findStMicros = do
ctx <- newCtx
devs <- getDevices ctx
descs <- traverse getDeviceAndDesc devs
pure $ V.filter ((== stVendorId) . deviceVendorId . snd) descs
pickBoard :: IO (Maybe Device)
pickBoard = do
micros <- findStMicros
printBoards (V.toList micros)
case length micros of
0 -> putStrLn "no boards detected!" $> Nothing
1 -> putStrLn "auto-selecting board 1" $> Just (fst . V.head $ micros)
_ -> do
putStr "select board: "
hFlush stdout
num <- read <$> getLine
pure $ fst <$> V.indexM micros (num - 1)