-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Nintendo Switch Controller Library -- -- Please see the README on GitHub at -- https://github.com/typedbyte/switch#readme @package switch @version 0.1.0.0 -- | Types and functions for connecting to Nintendo Switch controllers, -- reading input (e.g., buttons, sensors) and sending commands (e.g., -- rumble). module Device.Nintendo.Switch -- | A handle which represents a virtual Nintendo Switch console. The -- handle is used to detect controllers and manage their connections. data Console -- | Initializes a Nintendo Switch console handle. In other words, it lets -- us pretend to be Nintendo Switch console in order to detect -- controllers and manage their connections. You must call this first -- before doing anything else. init :: IO Console -- | Destroys a virtual Nintendo Switch handle. You must call this when you -- are finished talking to the controllers. exit :: Console -> IO () -- | A convenient wrapper around init and exit. withConsole :: (Console -> IO a) -> IO a -- | The types of Nintendo Switch controllers that are currently supported -- by this library. -- -- Note that this type is mostly used on the type level (using -- DataKinds) in order to prevent programming mistakes at -- compile-time (e.g., to prevent sending a rumble command to a -- controller which has no rumble feature). -- -- Chances are very high that you don't need this type on the value -- level, but rather on the type level, for example via -- TypeApplications (see getControllerInfos). data ControllerType LeftJoyCon :: ControllerType RightJoyCon :: ControllerType ProController :: ControllerType -- | A handle which represents an unconnected Nintendo Switch controller. data ControllerInfo (t :: ControllerType) -- | A handle which represents a connected Nintendo Switch controller. data Controller (t :: ControllerType) -- | Detects all Nintendo Switch controllers of a specific -- ControllerType, usually connected via Bluetooth. -- -- You may want to use this function with TypeApplications if -- the controller type cannot be inferred, like: -- --
--   getControllerInfos @'LeftJoyCon console
--   
getControllerInfos :: forall t. IsController t => Console -> IO [ControllerInfo t] -- | Connects to a detected Nintendo Switch controller. -- -- Can throw a ConnectionException if something is very wrong with -- your internal controller memory (i.e., if you have tampered with it). connect :: forall t. HasCalibration t => ControllerInfo t -> IO (Controller t) -- | Disconnects a Nintendo Switch controller. You must not use the -- controller handle after disconnecting. disconnect :: Controller t -> IO () -- | A convenient wrapper around connect and disconnect. withController :: HasCalibration t => ControllerInfo t -> (Controller t -> IO a) -> IO a -- | The input mode of a Nintendo Switch controller determines the -- frequency and amount of information received by getInput. data InputMode -- | The default input mode. In this mode, controllers push Input -- packages in a 60Hz (Joy-Con) or 120Hz (Pro Controller) frequency, -- including battery information, Analog stick directions -- (stickLeft, stickRight) and Inertial sensor data -- (extras) if activated via setInertialMeasurement. Standard :: InputMode -- | A simple input mode where a controller only pushes its Input -- whenever a button is pressed or a CommandReply (extras) -- is sent. In this mode, controllers only send Discrete stick -- directions (stickLeft, stickRight) and no inertial -- sensor data. Furthermore, battery information is only sent in -- combination with command replies. Simple :: InputMode -- | Sets the input mode of a Nintendo Switch controller. -- -- Note: After sending a command like this to a controller, it is highly -- advised to check its corresponding CommandReply -- (SetInputMode, to be exact) or at least call getInput -- once before sending another command to that controller. The function -- withCommandReply is a convenient way to wait for a specific -- command reply from the controller. setInputMode :: HasInputMode t => InputMode -> Controller t -> IO () -- | Enables (True) or disables (False) the inertial -- measurement unit (i.e., accelerometer, gyroscope) of a Nintendo Switch -- controller. Inertial measurement is disabled by default. -- -- Note: After sending a command like this to a controller, it is highly -- advised to check its corresponding CommandReply -- (SetInertialMeasurement, to be exact) or at least call -- getInput once before sending another command to that -- controller. The function withCommandReply is a convenient way -- to wait for a specific command reply from the controller. setInertialMeasurement :: Bool -> Controller t -> IO () -- | Reads input from a Nintendo Switch controller. Blocks until controller -- input is available. getInput :: HasInput t => Controller t -> IO Input -- | Reads input from a Nintendo Switch controller. Blocks until controller -- input is available or a given time interval elapses. getTimeoutInput :: HasInput t => Int -> Controller t -> IO (Maybe Input) -- | The input provided by a Nintendo Switch controller. type Input = ControllerInput Float Float -- | The input provided by a Nintendo Switch controller, where s -- is the numeric type of the analog stick direction and e is -- the numeric type of the sensor readings (i.e., accelerometer and -- gyroscope). data ControllerInput s e Input :: Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> StickDirection s -> StickDirection s -> ExtraInput e -> Maybe BatteryInfo -> ControllerInput s e [btnL] :: ControllerInput s e -> Bool [btnZL] :: ControllerInput s e -> Bool [btnMinus] :: ControllerInput s e -> Bool [btnLeftStick] :: ControllerInput s e -> Bool [btnUp] :: ControllerInput s e -> Bool [btnLeft] :: ControllerInput s e -> Bool [btnRight] :: ControllerInput s e -> Bool [btnDown] :: ControllerInput s e -> Bool [btnCapture] :: ControllerInput s e -> Bool [btnLeftSL] :: ControllerInput s e -> Bool [btnLeftSR] :: ControllerInput s e -> Bool [btnR] :: ControllerInput s e -> Bool [btnZR] :: ControllerInput s e -> Bool [btnPlus] :: ControllerInput s e -> Bool [btnX] :: ControllerInput s e -> Bool [btnY] :: ControllerInput s e -> Bool [btnA] :: ControllerInput s e -> Bool [btnB] :: ControllerInput s e -> Bool [btnRightStick] :: ControllerInput s e -> Bool [btnHome] :: ControllerInput s e -> Bool [btnRightSL] :: ControllerInput s e -> Bool [btnRightSR] :: ControllerInput s e -> Bool [stickLeft] :: ControllerInput s e -> StickDirection s [stickRight] :: ControllerInput s e -> StickDirection s [extras] :: ControllerInput s e -> ExtraInput e [battery] :: ControllerInput s e -> Maybe BatteryInfo -- | The direction of the left (stickLeft) and right -- (stickRight) analog sticks. data StickDirection a -- | In Simple input mode, controllers send discrete stick -- directions. Discrete :: Direction -> StickDirection a -- | In Standard input mode, controllers send analog stick -- directions. The first value is left/right (interval [-1,1]), -- the second value is down/up (interval [-1,1]). Analog :: a -> a -> StickDirection a -- | The nine possible discrete positions of the analog stick in -- Simple input mode. data Direction None :: Direction Left :: Direction Up :: Direction Right :: Direction Down :: Direction LeftUp :: Direction LeftDown :: Direction RightUp :: Direction RightDown :: Direction -- | Information about the battery of a Nintendo Switch controller. It is -- only returned by getInput (see battery) if the -- controller sends a command reply or the input mode of the controller -- is Standard. data BatteryInfo BatteryInfo :: BatteryStatus -> Bool -> BatteryInfo [batteryStatus] :: BatteryInfo -> BatteryStatus [charging] :: BatteryInfo -> Bool -- | The battery status of a Nintendo Switch controller. data BatteryStatus Empty :: BatteryStatus Low :: BatteryStatus Medium :: BatteryStatus Good :: BatteryStatus Full :: BatteryStatus -- | Depending on the InputMode, Input can contain additional -- information: Replies to commands (e.g., an acknowledgement when -- sending a rumble command) and inertial sensor data (i.e., -- accelerometer and gyroscope). data ExtraInput a -- | After sending commands to the controller (e.g., setting the -- InputMode), a command reply is returned as extra data in the -- next input. CommandReply :: ReplyData -> ExtraInput a -- | A controller provides inertial sensor data (i.e., accelerometer and -- gyroscope) only if it is in Standard input mode and inertial -- measurement is activated via setInertialMeasurement. -- -- Regarding the x/y/z coordinate system, consider the left Joy-Con lying -- flat on a table, the analog stick pointing up. The x-axis then points -- towards the Z/ZL shoulder buttons (or alternatively: to where the up -- arrow button is pointing), the y-axis points to the opposite side of -- the SL/SR buttons (or alternatively: to where the left arrow button is -- pointing), and the z-axis points up in the air. The coordinate system -- is the same for all controller types. Inertial :: Accelerometer a -> Gyroscope a -> ExtraInput a -- | Indicates that there is no additional input data. Unavailable :: ExtraInput a -- | Accelerometer data consists of three measurements recorded in 15ms -- (i.e., the precision is 5ms). Each measurement is an x/y/z triple -- measured in Gs. type Accelerometer a = ((a, a, a), (a, a, a), (a, a, a)) -- | Gyroscope data consists of three measurements recorded in 15ms (i.e., -- the precision is 5ms). Each measurement is an x/y/z triple measured in -- radians per second. type Gyroscope a = ((a, a, a), (a, a, a), (a, a, a)) -- | Data type that combines the command type and its corresponding -- acknowledgement. data ReplyData SetHomeLight :: Acknowledgement () -> ReplyData SetInertialMeasurement :: Acknowledgement () -> ReplyData SetInputMode :: Acknowledgement () -> ReplyData SetPlayerLights :: Acknowledgement () -> ReplyData SetVibration :: Acknowledgement () -> ReplyData -- | Whenever a command is sent to a controller (e.g., setting the -- InputMode), the controller replies with an -- Acknowledgement. data Acknowledgement a -- | The command was executed successfully, possibly holding some response -- data (e.g., if the command was a query of the internal SPI flash -- memory). ACK :: a -> Acknowledgement a -- | The command was not executed successfully. NACK :: Acknowledgement a -- | A convenient constant that represents no input. This can be used to -- set specific buttons and stick directions in order to test functions -- without having a Nintendo Switch controller at hand, like: -- --
--   noInput { btnX = True, stickLeft = Discrete Up }
--   
noInput :: ControllerInput s e -- | Converts stick directions into x/y coordinates in the interval -- [-1,1]. Analog values are taken as is, while -- Discrete directions are converted to their analog counterpart. coordinates :: StickDirection Float -> (Float, Float) -- | Merges the inputs of two Nintendo Switch controllers. The resulting -- input contains the left button states and left analog stick direction -- from one input, and the right button states and right analog stick -- direction from the other input. This can be used to unify the inputs -- of two controllers that belong together (e.g., a pair of left and -- right Joy-Cons). -- -- Note that the extras and battery information of the -- original inputs are discarded in the merged input (they are set to -- Unavailable and Nothing, respectively). mergeInputs :: Input -> Input -> Input -- | Consumes inputs from a Nintendo Switch controller until a specific -- command reply is encountered. Throws a NoReplyException if the -- expected command reply is not encountered within a specified count of -- inputs. -- -- This function can be used to make sure that the controller is in an -- expected state after sending commands (e.g., to wait for an -- Acknowledgement after switching its InputMode). withCommandReply :: Int -> Int -> Controller t -> (ReplyData -> Maybe a) -> IO a -- | Sets the home light (i.e., the LED ring around the home button) of a -- Nintendo Switch controller. -- -- Note: After sending a command like this to a controller, it is highly -- advised to check its corresponding CommandReply -- (SetHomeLight, to be exact) or at least call getInput -- once before sending another command to that controller. The function -- withCommandReply is a convenient way to wait for a specific -- command reply from the controller. setHomeLight :: HasHomeLight t => HomeLightConfig -> Controller t -> IO () -- | The home light of a Nintendo Switch Controller can be controlled using -- repeatable configuration cycles. See endlessPulse for an -- example configuration. data HomeLightConfig -- | Turn off the home light. Off :: HomeLightConfig -- | Given a start intensity of the home light LED, fade to a target LED -- intensity in a given time, and then keep this LED intensity up for a -- given amount of time. -- -- The fade duration in milliseconds is calculated by multiplying the -- BaseDuration with the FadeFactor of the -- CycleConfig. The light upkeep duration in milliseconds is -- calculated by multiplying the BaseDuration with the -- LightFactor of the CycleConfig. -- -- Example - fade from a switched off LED (0) to a fully bright -- LED (100) in 500ms (50ms * 10), stay there -- for one second (50ms * 20), then turn it off: -- --
--   Once 50 0 (100, 10, 20)
--   
Once :: BaseDuration -> Intensity -> CycleConfig -> HomeLightConfig -- | Given a start intensity of the home light LED, repeatedly fade to a -- target LED intensity in a given time, and then keep this LED intensity -- up for a given amount of time. The fade durations and light upkeep -- durations are calculated per cycle configuration as described for -- Once. See endlessPulse for a cyclic configuration -- example. Cyclic :: BaseDuration -> Intensity -> [CycleConfig] -> RepeatBehaviour -> HomeLightConfig -- | A home light cycle consists of a target LED intensity, a fade factor -- which controls the time needed to reach that LED intensity, and a -- light factor which controls how long to keep the target LED intensity -- up. type CycleConfig = (Intensity, FadeFactor, LightFactor) -- | The base duration of a home light configuration in milliseconds. It -- will always be limited to an interval between 8ms and 175ms. It is -- called base duration because it will be multiplied with other factors -- in order to obtain the overall durations of fadings within home light -- configurations. type BaseDuration = Word8 -- | The LED intensity of the home light. It will always be limited to an -- interval between 0 and 100. type Intensity = Word8 -- | The fade duration factor of the home light. It will always be limited -- to an interval between 0 and 15 and is multiplied with the -- BaseDuration to obtain the overall fade duration in -- milliseconds. type FadeFactor = Word8 -- | The light duration factor of the home light. It will always be limited -- to an interval between 0 and 15 and is multiplied with the -- BaseDuration to obtain the overall light duration in -- milliseconds. type LightFactor = Word8 -- | Defines the repeat behaviour after all the home light configuration -- cycles have ended. data RepeatBehaviour -- | A convenient home light configuration which pulsates the home light -- LED: -- --
--   Cyclic
--     ( 100 )         -- Base duration factor is 100ms.
--     (   0 )         -- LED is turned off at the beginning (intensity 0).
--     [ (100, 5, 1)   -- Fade to LED intensity 100 in 500ms (100ms * 5) and stay there for 100ms (100ms * 1).
--     , (  0, 5, 1) ] -- Fade to LED intensity   0 in 500ms (100ms * 5) and stay there for 100ms (100ms * 1).
--     ( Forever )     -- Repeat these two cycles forever, thus generating a pulse-like LED.
--   
endlessPulse :: HomeLightConfig -- | Sets the player lights of a Nintendo Switch controller. -- -- Note: After sending a command like this to a controller, it is highly -- advised to check its corresponding CommandReply -- (SetPlayerLights, to be exact) or at least call getInput -- once before sending another command to that controller. The function -- withCommandReply is a convenient way to wait for a specific -- command reply from the controller. setPlayerLights :: HasPlayerLights t => PlayerLightsConfig -> Controller t -> IO () -- | Nintendo Switch controllers have four LEDs that can be used to -- indicate various things, for example the player number or the -- Bluetooth pairing status. The LEDs are numbered from left to right -- (i.e., led0 is the leftmost LED, led3 is the rightmost -- LED). data PlayerLightsConfig PlayerLightsConfig :: LightMode -> LightMode -> LightMode -> LightMode -> PlayerLightsConfig [led0] :: PlayerLightsConfig -> LightMode [led1] :: PlayerLightsConfig -> LightMode [led2] :: PlayerLightsConfig -> LightMode [led3] :: PlayerLightsConfig -> LightMode -- | Each player light LED can be individually turned on, turned off or -- used in a pulsating manner (i.e., flashing). data LightMode LightOn :: LightMode LightOff :: LightMode Flashing :: LightMode -- | A convenient player lights configuration where all LEDs are turned -- off. noPlayerLights :: PlayerLightsConfig -- | A convenient player lights configuration indicating player one (i.e., -- led0 is set). playerOne :: PlayerLightsConfig -- | A convenient player lights configuration indicating player two (i.e., -- led1 is set). playerTwo :: PlayerLightsConfig -- | A convenient player lights configuration indicating player three -- (i.e., led2 is set). playerThree :: PlayerLightsConfig -- | A convenient player lights configuration indicating player four (i.e., -- led3 is set). playerFour :: PlayerLightsConfig -- | A convenient player lights configuration where all LEDs are flashing. flashAll :: PlayerLightsConfig -- | Enables (True) or disables (False) the rumble feature of -- a Nintendo Switch controller. The rumble feature is disabled by -- default. -- -- Note: After sending a command like this to a controller, it is highly -- advised to check its corresponding CommandReply -- (SetVibration, to be exact) or at least call getInput -- once before sending another command to that controller. The function -- withCommandReply is a convenient way to wait for a specific -- command reply from the controller. setVibration :: Bool -> Controller t -> IO () -- | Sets the left rumble of a Nintendo Switch controller. setLeftRumble :: HasLeftRumble t => RumbleConfig -> Controller t -> IO () -- | Sets the right rumble of a Nintendo Switch controller. setRightRumble :: HasRightRumble t => RumbleConfig -> Controller t -> IO () -- | Sets both the left rumble and right rumble of a Nintendo Switch -- controller. Note that this is more efficient than setting the left -- rumble and right rumble separately via setLeftRumble and -- setRightRumble. setRumble :: (HasLeftRumble t, HasRightRumble t) => RumbleConfig -> RumbleConfig -> Controller t -> IO () -- | Nintendo Switch controllers have a HD rumble feature which allows -- fine-grained control of rumble strengths and directions. As a -- consequence, a rumble is not configured by a mere numeric value, but -- by two (high and low) pairs of frequencies and amplitudes. This -- library constrains the value ranges of frequencies and amplitudes in -- order to always obtain sane configurations. However, sending extreme -- values for these pairs over an extended period of time may still -- damage a controller, so experiment wisely with rather short rumbles. -- -- For technical discussions and the meaning of these values, one can -- read this, for example. A sample rumble configuration is -- provided by normalRumble. data RumbleConfig RumbleConfig :: Double -> Double -> Double -> Double -> RumbleConfig -- | The high frequency. It will always be limited to an interval between -- 81.75177 Hz and 1252.572266 Hz. [highFrequency] :: RumbleConfig -> Double -- | The high amplitude. It will always be limited to an interval between -- 0.0 and 1.0. [highAmplitude] :: RumbleConfig -> Double -- | The low frequency. It will always be limited to an interval between -- 40.875885 Hz and 626.286133 Hz. [lowFrequency] :: RumbleConfig -> Double -- | The low amplitude. It will always be limited to an interval between -- 0.0 and 1.0. [lowAmplitude] :: RumbleConfig -> Double -- | A convenient rumble configuration indicating a medium rumble strength. -- --
--   RumbleConfig
--     { highFrequency = 800
--     , highAmplitude = 0.5
--     , lowFrequency  = 330
--     , lowAmplitude  = 0.75
--     }
--   
normalRumble :: RumbleConfig -- | A convenient rumble configuration indicating no rumble. noRumble :: RumbleConfig -- | A ConnectionException is thrown if something goes wrong when -- reading the internal data of a Nintendo Switch controller when -- connecting to it. This should not occur if you have an unmodified -- controller (i.e., you have not tampered with its internal SPI flash -- memory). data ConnectionException -- | Indicates that a controller has no factory stick calibration. NoFactoryStickException :: ConnectionException -- | Indicates that a controller has no factory sensor calibration. NoFactoryAxisException :: ConnectionException -- | An InputException is thrown if something goes wrong with -- getInput. data InputException -- | Indicates that an expected reply wasn't received in a specific time -- interval. NoReplyException :: InputException -- | Indicates that the controller input has an unexpected format. It -- essentially means that a specific part of the protocol has not been -- implemented yet. This should not occur as long as you stick to the -- public API of this library. UnknownFormatException :: ByteString -> InputException -- | An OutputException is thrown if something goes wrong when -- sending commands to a Nintendo Switch controller. data OutputException WriteException :: OutputException -- | A constraint which indicates that a controller is a valid Nintendo -- Switch controller that can be detected and connected to. class IsController (t :: ControllerType) -- | A constraint which indicates that a Nintendo Switch controller is able -- to turn portions of its internal flash memory into valid calibration -- information. class HasCalibration (t :: ControllerType) -- | A constraint which indicates that a Nintendo Switch controller -- supports multiple input modes (see setInputMode). class HasInputMode t -- | A constraint which indicates that a Nintendo Switch controller can -- provide Input (see getInput). class HasInput t -- | A constraint which indicates that a Nintendo Switch controller has a -- home light (see setHomeLight). class HasHomeLight t -- | A constraint which indicates that a Nintendo Switch controller has -- player lights (i.e., the four LEDs which represent the player number; -- see setPlayerLights). class HasPlayerLights t -- | A constraint which indicates that a Nintendo Switch controller has a -- left-side rumble unit (see setLeftRumble). class HasLeftRumble t -- | A constraint which indicates that a Nintendo Switch controller has a -- right-side rumble unit (see setRightRumble). class HasRightRumble t