/** MID2253 API. API functions for the 2253 board.
 * Copyright 2009-2025 Sensoray Company Inc.
 * Provided as is under the standard Sensoray license.
 * This functions encapsulate the DirectShow functionality of the 2253 AVStream driver
 * These functions are not required for developing an application. If the user
 * wants to use a different DirectShow program(AMCap) or write their own DirectShow program, they may do so.
 * Note: No support will be given for compiling or modifying the source code of this DLL API.
 */

#ifndef _MID2253FUNC_H
#define _MID2253FUNC_H

// Please note that due to USB bandwidth and other considerations,
// the practical limit of 2253s in one system may be much lower than 32.
#define MAX_DEVICES 32  // maximum number of 2253s in system

#include "mid2253types.h"

typedef int (__stdcall *cbfunc_t) ( BYTE *data, long size, int devid, int strmidx);

// Please refer to Manual for detailed description of all functions.

/** Opens the SDK/API.  All boards in the system will be accessible.
  * use brd_idx = -1 to access all boards
  * @return 0 on success
  */
MID2253_API int __stdcall S2253_Open(int brd_idx);


/** Cleans up and shuts down the API gracefully
 *  use brd_idx = -1 to access all board
 *  @return 0 on success
 */
MID2253_API int __stdcall S2253_Close(int brd_idx); 

// query number of devices in system. (only works after S2253_Open or S2253_OpenBoard)
MID2253_API int __stdcall S2253_GetNumDevices(int *num_devices);

/** set the current video system.  If board is currently streaming 
 *  board will be stopped and restarted.  If board recording, recording will
 *  be stopped.
 *  @param vidsys is the video system type
 *  @return 0 on success
 */
MID2253_API int __stdcall S2253_SetVidSys(MID2253_VIDSYS vidsys, int devid);
MID2253_API int __stdcall S2253_GetVidSys(MID2253_VIDSYS *vidsys, int devid);

/** Set the value of the frame counter
 *  @param count is value to be loaded into the frame counter
 *  @return 0 on success
 */
MID2253_API int __stdcall S2253_SetFrameCount(int count, int devid);
MID2253_API int __stdcall S2253_GetFrameCount(int *count, int devid);

/** retrieves current SDK and file information
 *  file information in ascii and unicode
 *  @return 0 on success, negative otherwise 
 */
MID2253_API int __stdcall S2253_GetStatus(MID2253STATUS *status, int devid, int strmidx);

// unicode only version for .NET only
// record mode is for H264 and MPEG4 streams.  It is also for transport streams to turn audio on/off
// within the transport stream.
MID2253_API int __stdcall S2253_SetRecordMode(MID2253_RECMODE recmode, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetRecordMode(MID2253_RECMODE *recmode, int devid, int strmidx);
// retrieves decode status (0 - idle, 1-decoding, otherwise error code)
MID2253_API int __stdcall S2253_GetDecodeStatus(int *status, int devid);
// MP4 mode.  For MP4 files (used for H264 and MPEG4).  See manual
MID2253_API int __stdcall S2253_SetMp4Mode(MID2253_MP4MODE mp4mode, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetMp4Mode(MID2253_MP4MODE *mp4mode, int devid, int strmidx);


// Set Audio encoding.  For AVI streams.
MID2253_API int __stdcall S2253_SetAudioEncoding(MID2253_AUDENC aenc, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetAudioEncoding(MID2253_AUDENC *aenc, int devid, int strmidx);

// audio bitrate.  For AAC encoding only (MPEG4 and H264 muxed files only)
MID2253_API int __stdcall S2253_SetAudioBitrate(MID2253_AUDIO_BITRATE audbr, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetAudioBitrate(MID2253_AUDIO_BITRATE *audbr, int devid, int strmidx);

// set audio channels. For AAC encoding only (MPEG4 and H264 muxed files only)
MID2253_API int __stdcall S2253_SetAudioChannels(MID2253_AUDIO_CHANNELS audch, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetAudioChannels(MID2253_AUDIO_CHANNELS *audch, int devid, int strmidx);


/** 
 * Starts recording to file using filter graph previously set up.
 * @param filename is the video/mux filename to record to.  Use the full path and extension
 * @param bUnicode specifies if filename is unicode or not
 * @return 0 on success
 */
MID2253_API int __stdcall S2253_StartRecord(const void *filename, BOOL bUnicode, int devid, int strmidx);

/////////////////////////////////////////////////
// General configuration options

/** set the brightness, contrast and saturation
*   @param param is the parameter to change(MID2253_LEVEL_CONTRAST, MID2253_LEVEL_BRIGHTNESS, MID2253_LEVEL_SATURATION, MID2253_LEVEL_HUE, MID2253_LEVEL_HCENTER, MID2253_LEVEL_VCENTER)
*   @param value is the value of the parameter
*    ( BRIGHTNESS: 0 to 255 default 128)
*    ( SATURATION: 0 to 255 default 128 recommended value)
*    ( CONTRAST: 0 to 255 default 128)
*    ( HUE: -127 to 127, default 0 (recommended to stay unchanged))
*    ( HCENTER: -128 to 128 default 0)
*    ( VCENTER: -128 to 128 default 0)
*    ( FIELDMODE: 0 to 1 default 0)
*    ( AUTOGAIN: 0 to 1 default 1)
*    ( AUTOBRIGHTNESS: 0 to 1 default 1)
*   @return 0 on success
*/
MID2253_API int __stdcall S2253_SetLevel(int param, unsigned char value, int devid);

/** get the brightness, contrast or saturation
  * @param param is the parameter to get(MID2253_LEVEL_CONTRAST, MID2253_LEVEL_BRIGHTNESS, MID2253_LEVEL_SATURATION, MID2253_LEVEL_HUE, MID2253_LEVEL_HCENTER, MID2253_LEVEL_VCENTER)
  * @param value is the returned value of the parameter
  * @return 0 on success
  */
MID2253_API int __stdcall S2253_GetLevel(int param, unsigned char *value, int devid);

// Auto gain control and auto brightness.  default is on
// deprecated.  kept for backward compatibility.  Please use SetLevel and GetLevel
// with MID2253_LEVEL_AUTOGAIN and MID2253_LEVEL_AUTOBRIGHTNESS
//MID2253_API int __stdcall S2253_SetAutoGain(int bAuto, int devid);
//MID2253_API int __stdcall S2253_SetAutoBrightness(int bAuto, int devid);
//MID2253_API int __stdcall S2253_GetAutoGain(int *bAuto, int devid);
//MID2253_API int __stdcall S2253_GetAutoBrightness(int *bAuto, int devid);



// Preview is by default a pop-up Window.
// Use S2253_SetStreamWindow to change preview to render to a Window handle
// Please see the comments and documentation for S2253_SetStreamWindow and associated functions
MID2253_API int __stdcall S2253_StartPreview(int devid, int strmidx); 

// Note: Audio preview is completely independent from video preview.
//       Unlike recording, it is not synchronized with the video streams.
MID2253_API int __stdcall S2253_StartAudioPreview(int devid); 
MID2253_API int __stdcall S2253_StopAudioPreview(int devid);

// Note: Like audio preview, audio callback is not synchronized with the video streams
MID2253_API int __stdcall S2253_StartAudioCallback(int devid); 
MID2253_API int __stdcall S2253_StopAudioCallback(int devid);

MID2253_API int __stdcall S2253_StartAudioPreviewWithCallback(int devid); 

MID2253_API int __stdcall S2253_RegisterAudioCallback( cbfunc_t callback, int devid);


// Stops any streaming operation( started with S2253_StartPreview, S2253_StartRecord or S2253_StartCallback)
MID2253_API int __stdcall S2253_StopStream(int devid, int strmidx); 

typedef enum {
	S2253_PAUSE_MODE_DISABLE = 0,
	S2253_PAUSE_MODE_RISING_EDGE = 1,
	S2253_PAUSE_MODE_FALLING_EDGE = 2,
	S2253_PAUSE_MODE_LEVEL_HIGH = 3,
	S2253_PAUSE_MODE_LEVEL_LOW = 4,
} S2253_PAUSE_MODE;

// Please note: Pause/Resume not supported for AVI file saving.
MID2253_API int __stdcall S2253_PauseStream(int devid, int strmidx);
MID2253_API int __stdcall S2253_ResumeStream(int devid, int strmidx);
MID2253_API int __stdcall S2253_PauseTrigger(int devid, int strmidx, S2253_PAUSE_MODE mode);
MID2253_API int __stdcall S2253_FreezeInput(int devid, bool enable);
MID2253_API int __stdcall S2253_FreezeTrigger(int devid, S2253_PAUSE_MODE mode);

// see manual
MID2253_API int __stdcall S2253_SetImageSize(int width, int height, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetImageSize(int *width, int *height, int devid, int strmidx);


// see manual
// Please note that RGB565 and RGB24 are NOT recommended for preview.
// Additionally, RGB24 is not supported above the resolution 640x480 and
// is only supported for ONE channel at a time.
MID2253_API int __stdcall S2253_SetStreamType(MID2253_STREAMTYPE stype, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetStreamType(MID2253_STREAMTYPE *stype, int devid, int strmidx);

MID2253_API int __stdcall S2253_SetH264Profile(MID2253_H264_PROFILE profile, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetH264Profile(MID2253_H264_PROFILE *profile, int devid, int strmidx);
MID2253_API int __stdcall S2253_SetH264Level(MID2253_H264_LEVEL level, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetH264Level(MID2253_H264_LEVEL *level, int devid, int strmidx);

// see manual
MID2253_API int __stdcall S2253_SetOsd(MID2253_OSD_TYPE osdtype, MID2253_OSD_DATA *osd, int devid, int strmidx);

// updates the clock on the board(used for OSD)
MID2253_API int __stdcall S2253_SetClock(MID2253_USER_CLOCK *clk, int devid);

// see manual
MID2253_API int __stdcall S2253_SetBitrate(unsigned int bitrate, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetBitrate(unsigned int *bitrate, int devid, int strmidx);

// q from 10 to 90 for JPEG encoding only
MID2253_API int __stdcall S2253_SetJpegQ(int q, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetJpegQ(int *q, int devid, int strmidx);

// show a frame every q frames
MID2253_API int __stdcall S2253_SetDecimation(int q, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetDecimation(int *q, int devid, int strmidx);

// fixed qp from -1 to 42 for H.264 encoding only
MID2253_API int __stdcall S2253_SetFixedQP(int intraframeqp, int interpframeqp, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetFixedQP(int *intraframeqp, int *interpframeqp, int devid, int strmidx);

// see manual
MID2253_API int __stdcall S2253_SetInterpolateMode(int val, int devid);
MID2253_API int __stdcall S2253_GetInterpolateMode(int *val, int devid);
MID2253_API int __stdcall S2253_SetDeinterlaceMode(int val, int devid);
MID2253_API int __stdcall S2253_GetDeinterlaceMode(int *val, int devid);
MID2253_API int __stdcall S2253_SetInputCrop(int left, int top, int width, int height, int devid);
MID2253_API int __stdcall S2253_GetInputCrop(int *left, int *top, int *width, int *height, int devid);

// StartSnapshot is independent from StartPreview and StartRecord.  Only one streaming function
// can be running at a time per strmidx.  
// Start snapshot creates a graph with the stream connected to the sample grabber filter
// Internally in the MID2253 DLL, the sample is buffered via callback.
// a call to S2253_GrabSample will get the sample if available.
// Stop the snapshot stream by calling S2253_StopStream
// Example use case: occasional snapshots required while stream recorded
//                -Call S2253_StartSnapshot with strmidx = 0
//                -Call S2253_StartRecord with strmidx = 1
// when snapshot required, call S2553_GetSample to get the latest sample in the sample grabber
MID2253_API int __stdcall S2253_StartSnapshot(int devid, int strmidx); 
// see manual
MID2253_API int __stdcall S2253_GetSample(unsigned char *data, unsigned int inlen, unsigned int *outlen, int timeout, int devid, int strmidx);
// see manual 
MID2253_API int __stdcall S2253_EnableSnapshot(BOOL bOn, int devid, int strmidx);

// see manual
MID2253_API int __stdcall S2253_GetSerialNumber(unsigned int *serial_number, int devid);
// see manual
MID2253_API int __stdcall S2253_GetFullDevicePath(int nWC, WCHAR *path, int devid);
MID2253_API int __stdcall S2253_GetFullDeviceInstance(int nWC, WCHAR *dev_instance, int devid);

// if hwnd == NULL, pop-up window is used.
// If hwnd is not NULL, video window in question should override WM_ERASE to prevent flicker
// (Please see the manual and S2253_RepaintWindow)
MID2253_API int __stdcall S2253_SetStreamWindow(HWND hwnd, int devid, int strmidx);

MID2253_API int __stdcall S2253_SetStreamWindowPosition(RECT rcDst, int devid, int strmidx);

// Only used if S2253_SetStreamWindow called with non-NULL hwnd.  Please see manual.
MID2253_API int __stdcall S2253_RepaintWindow(HDC hdc, int devid, int strmidx);


// GPIO
MID2253_API int __stdcall S2253_GetGpioInput(int *value, int devid);
// change GPIO output
MID2253_API int __stdcall S2253_SetGpioOutput(int value, int devid);
// get current state of GPIO output
MID2253_API int __stdcall S2253_GetGpioOutput(int *value, int devid);
//
MID2253_API int __stdcall S2253_WaitGpioInput(MID2253_GPIO_SIGNAL signal, int timeout, int devid);

MID2253_API int __stdcall S2253_SetAudioInput(MID2253_AUDIO_INPUT input, int devid);
MID2253_API int __stdcall S2253_GetAudioInput(MID2253_AUDIO_INPUT *input, int devid);

// if AGC is ON, then gain setting has no effect
// When AGC is off, gain controls input volume (0-119, steps of 0.5dB)
MID2253_API int __stdcall S2253_SetAudioGain(BOOL bAGC, int gain, int devid);
MID2253_API int __stdcall S2253_GetAudioGain(BOOL *bAGC, int *gain, int devid);

// flip vertically and/or horizontally.  (set to FALSE for default)
MID2253_API int __stdcall S2253_FlipImage(BOOL bFlipV, BOOL bFlipH, int devid, int strmidx);

// For H264 stream only
// val = 0 (setting one IDR at the start of the stream)
// val = 1 (IDR for every GOP, new default setting 9/4/2013)
// val = 2 (IDR for every 2nd GOP), etc...
// Requires a driver/firmware update.  Make sure to update BOTH drivers 
// s2253loader.inf and s2253av.inf
// (run DPInst in directory with these files for easier updating)
MID2253_API int __stdcall S2253_SetIDR(int val, int devid, int strmidx);

// Get/Set the GOP (Group of Pictures) size for H264/MPEG4 
// This controls the number of P-frames per I-frame (keyframe)
// To force every frame to be an I-frame, use val=1.
// A value of zero means to use encoder default.
MID2253_API int __stdcall S2253_GetGOPSize(int *pval, int devid, int strmidx);
MID2253_API int __stdcall S2253_SetGOPSize(int val, int devid, int strmidx);

// Low Latency mode runs the direct show graph with sync clock = -1
// The video will go through the graph at the maximum speed without pacing
// The tradeoff is video may not be completely smooth.
// Call this function before starting the stream if desired
// For preview only.
MID2253_API int __stdcall S2253_LowLatencyPreview(BOOL bON, int devid, int strmidx);


// decode functions
// decode from file
MID2253_API int __stdcall S2253_StartDecode(const void *filename, BOOL bUnicode, int devid);
// decode from memory
MID2253_API int __stdcall S2253_StartDecodeMem(int devid);
// to be used in conjunction with S2253_StartDecodeMem (for compressed data only)
MID2253_API int __stdcall S2253_DecodeData(unsigned char *data, int len, int devid);
// to be used in conjunction with S2253_StartDecodeMem (for RAW DATA aka YUV, Y8 only)
MID2253_API int __stdcall S2253_DecodeDataRaw(unsigned char *data, int len, int devid);
// stop decode
MID2253_API int __stdcall S2253_StopDecode(int devid);
// Low latency display mode for decoding
// When on, frames will be dropped if a newer frame is available, for use with live video.
// When off, every frame will be played, use with recorded video.
MID2253_API int __stdcall S2253_LowLatencyDecode(BOOL bON, int devid);


// for the decode type.  to call after S2253_StartDecodeMem
// primarily for RAW decode (where format type can not be auto detected)
MID2253_API int __stdcall S2253_ForceDecodeFormat(MID2253_DECODETYPE format, int w, int h, int devid);

// see demo program for example usage
// 2253 device messages (unplug and and stream done) related to Windows DirectShow
MID2253_API int __stdcall S2253_SetNotify(HWND hNotifyApp, UINT mNotifyMessage, int devid);
// If message received, tests if device was removed(unplug)
MID2253_API int __stdcall S2253_TestDeviceRemoval(int devid);

// If message received, tests if decode was finished in the driver.
// Note: Decode may not be finished right away.
// Poll S2253_GetDecodeStatus after decode done event to find out exactly when decode is finished on the device.
MID2253_API int __stdcall S2253_TestDecodeDone(int devid);


// video output functions
MID2253_API int __stdcall S2253_SetOverlay(MID2253_OVL_STRUCT *ovl, unsigned char *data, int devid);
MID2253_API int __stdcall S2253_StreamOverlay(MID2253_OVL_STRUCT *ovl, unsigned char *data, int devid, int strmidx);
// do not change mode until output is completely stopped.  
MID2253_API int __stdcall S2253_SetOutputMode(MID2253_OUTPUT_MODE mode, int devid);
MID2253_API int __stdcall S2253_SetOutputVGA(BOOL enable, int devid);


// Gets supplemental 2253 parameter (from MID2253_PARAM list of params)
MID2253_API int __stdcall S2253_GetParam(MID2253_PARAM type, int *val, int devid, int strmidx);


// inserts User Data into the stream.  Applies to H264 and MPEG4 only
// user data can be a string or binary data.  specify the length
// interval 0: insert once per ioctl, otherwise every Nth frame
MID2253_API int __stdcall S2253_SetUserData(char *data, int len, int interval, int devid, int strmidx);

// Enable capturing of closed captions from VBI.  Applies to H264 only.
// Note: this will override User Data insertion
MID2253_API int __stdcall S2253_EnableClosedCaptions(int enabled, int devid, int strmidx);

// See manual:
// must have registered a callback first with S2253_RegisterCB
MID2253_API int __stdcall S2253_StartCallback(int devid, int strmidx);

MID2253_API int __stdcall S2253_RegisterCallback( cbfunc_t callback, int devid, int strmidx);
// starts preview with callback registered by previous function, stop with StopStream
MID2253_API int __stdcall S2253_StartPreviewCallback(int devid, int strmidx);
// this returns the timestamp for the frame capture 
MID2253_API int __stdcall S2253_GetCallbackTimestamp(REFERENCE_TIME *tstamp, REFERENCE_TIME *now, int devid, int strmidx);


// sets preview type.  By default uses the more advanced VMR-9 video renderer. 
// (Renderers are described in the Microsoft MSDN documentation)
// The user may want to switch renderers as some older and even newer video card
// do not have fully compliant DirectX 9 hardware or drivers
// In this case, the older renderers can be used.
// Usually this is only an issue on laptops. 
MID2253_API int __stdcall S2253_SetPreviewType(MID2253_PREVIEWTYPE type, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetPreviewType(MID2253_PREVIEWTYPE *type, int devid, int strmidx);

// for preview type  MID2253_PREVIEWTYPE_VMR9, MID2253_PREVIEWTYPE_VMR7 only
// If letterbox set, aspect ratio will be maintained no matter what dimensions of video window are
// by adding a letterbox around the video
MID2253_API int __stdcall S2253_SetVMRLetterBox(BOOL bOn, int devid, int strmidx);
MID2253_API int __stdcall S2253_GetVMRLetterBox(BOOL *bOn, int devid, int strmidx);

// lock = 1 (HV locked), lock = 0 (unlocked)
MID2253_API int __stdcall S2253_GetHVLock(int *lock, int devid);


MID2253_API int __stdcall S2253_GetVersions(MID2253_VERSIONS *ver, int devid);

struct s2253_features {
	int feature1;
	int feature2;
	int reserved[2]; // future use
};

MID2253_API int __stdcall S2253_GetFeatures(struct s2253_features *features, int devid);



// 0 - 1:1 (square), 1 - 4:3, 2 - 16:9
// sets the aspect ratio for the recorded MPEG stream (affects playback)
MID2253_API int __stdcall S2253_SetMpegAspectRatio(int val, int devid, int strmidx);

// set the audio delay for stream, range: -500 to 500 ms
MID2253_API int __stdcall S2253_SetAudioDelay(int val, int devid, int strmidx);

// force VCR mode ( default is auto-detect, which works very reliably)
MID2253_API int __stdcall S2253_SetVcrMode(int bVCR, int devid);

// internal use only.
MID2253_API int __stdcall S2253_GetDebug(int idx, int *val, int devid, int strmidx);
MID2253_API int __stdcall S2253_RegisterMp4Message(HWND hwnd, UINT msg, int devid, int strmidx);


#define S2253_FW_VERSION  9170  // current firmware version
#define S2253_FW_COMB_FILTER 8937 //minimum firmware for Comb,Chroma, Luma Filters
#define S2253_FW_AUTOBRIGHNESS 8812 // minimum firmware for auto brightness and gain

#define S2253_DLL_VER3     1  // MSB
#define S2253_DLL_VER2     2  //
#define S2253_DLL_VER1     54 //
#define S2253_DLL_VER0     1  // LSB

#define S2253_DLL_VERSION ((S2253_DLL_VER3 << 24) | (S2253_DLL_VER2 << 16) | (S2253_DLL_VER1 << 8) | S2253_DLL_VER0) // current DLL version
#define S2253_DLL_VERTEXT "1.2.54.1"

// Retrieves last DirectShow error.  For extended debug.  Value will
// not always correlate with S2253_ERR_xyz error code.
MID2253_API int __stdcall S2253_GetLastError(int devid, int strmidx);

// if video is preview, will draw (alpha blend) bitmap with VMR.
// Set hdcBMP to NULL to remove bitmap
// Please NOTE: If running a 64 bit app, you must select VMR-9.  VMR-7 does not support bitmap mixing in 64 bit mode.
MID2253_API int __stdcall S2253_DrawBitmap(HDC hdcBMP, RECT *src, NORMALIZEDRECT *dst, float alpha, int devid, int strmidx);

// if using a clrSrcKey for color transparency. (see drawpane and demo app for example)
MID2253_API int __stdcall S2253_DrawBitmapColorRef(HDC hdcBMP, RECT *src, NORMALIZEDRECT *dst, float alpha, 
                                                   COLORREF clrSrcKey, int devid, int strmidx);


//---------------------------------------------------------------------------
// The following functions are for the S2253P only (2253 TB with 2253 Rev H)
//---------------------------------------------------------------------------

// Read the online status of the S2253P
// online: set to 1 if the S2253P is reported online, 0 otherwise
MID2253_API int __stdcall S2253P_ReadOnline(int devid, int *online);
// Read the PIC software version reported by the S2253TB
MID2253_API int __stdcall S2253P_ReadVersion(int devid, int *version);
// Read the com stats reported by the S2253TB 
MID2253_API int __stdcall S2253P_ReadComstat(int devid, void *comstat);
// Set whether the TB should be held in reset during USB Suspend (default disabled)
MID2253_API int __stdcall S2253P_SetSuspend(int devid, int enable);


// Reset encoder to zero
//  encoderId: 0..1
MID2253_API int __stdcall S2253P_EncoderReset(int devid, int encoderId);

// Load a specific value into the encoder count
//  encoderId: 0..1
MID2253_API int __stdcall S2253P_EncoderLoad(int devid, int encoderId, int value);

// Set the asynchronous mode for the encoder updates 
// (when asynchronous mode is enabled, encoder counts are periodically updated for OSD or READ without UPDATE)
//  encoderId: 0..1
//  enable: 0..1
MID2253_API int __stdcall S2253P_EnableEncoderAsync(int devid, int encoderId, int enable);

typedef enum {
	S2253P_MODE_UPDATE = 1,
	S2253P_MODE_READ = 2,
	S2253P_MODE_UPDATE_READ = 3,
} S2253P_MODE;

// Read the encoder count
//  encoderId: 0..1
//  mode: 1..3 (use S2253P_MODE_*)  (if EncoderAsync is enabled, UPDATE is not needed)
// returns MID2253_SUCCESS or MID2253_ERR_TIMEOUT on failure and application should retry
MID2253_API int __stdcall S2253P_EncoderRead(int devid, int encoderId, S2253P_MODE mode, int *value);
MID2253_API int __stdcall S2253P_EncoderReadScaled(int devid, int encoderId, S2253P_MODE mode, double *value);

// Calculate and set the scale factor, using the current encoder count
//  encoderId: 0..1
MID2253_API int __stdcall S2253P_EncoderLoadScaled(int devid, int encoderId, double value);

// Set or get the scale factor for encoder count
//  encoderId: 0..1
MID2253_API int __stdcall S2253P_EncoderSetScale(int devid, int encoderId, double scale);
MID2253_API int __stdcall S2253P_EncoderGetScale(int devid, int encoderId, double *scale);

typedef enum {
	S2253P_GPIO_DIR_IN = 0,
	S2253P_GPIO_DIR_OUT = 1,
} S2253P_GPIO_DIRECTION;

// Configure the gpio direction
//  gpioId: 0..1
//  direction: 0=In 1=Out
MID2253_API int __stdcall S2253P_GpioConfig(int devid, int gpioId, S2253P_GPIO_DIRECTION direction);
// Set the gpio value for output mode
//  gpioId: 0..1
//  value: 0..1  (active low)
MID2253_API int __stdcall S2253P_GpioWrite(int devid, int gpioId, int value);
// Read the gpio value for input mode
//  gpioId: 0..1
//  value: 0..1  (active low)
// returns MID2253_SUCCESS or MID2253_ERR_TIMEOUT on failure and application should retry
MID2253_API int __stdcall S2253P_GpioRead(int devid, int gpioId, int *value, S2253P_MODE mode);

typedef enum {
	S2253P_XIO_PAUSE_MODE_DISABLE = 0,
	S2253P_XIO_PAUSE_MODE_RISING_EDGE = 1,
	S2253P_XIO_PAUSE_MODE_FALLING_EDGE = 2,
	S2253P_XIO_PAUSE_MODE_LEVEL_HIGH = 3,
	S2253P_XIO_PAUSE_MODE_LEVEL_LOW = 4,
} S2253P_XIO_PAUSE_MODE;

// Enable or disable asynchronous updates for xio port 
//  xioId: 0..3
//  enable: 0..1
MID2253_API int __stdcall S2253P_EnableXIOAsync(int devid, int xioId, int enable);
// Read the value from xio port 0..3, (requires UPDATE mode when port async is not enabled)
//  xioId: 0..3
//  value: returns 0..1 (active low)
// returns MID2253_SUCCESS or MID2253_ERR_TIMEOUT on failure and application should retry
MID2253_API int __stdcall S2253P_ReadXIO(int devid, int xioId, int *value, S2253P_MODE mode);
// Configure a stream for pausing based on xio event (requires async mode enabled on xio port)
//  streamId: 0=StreamA 1=StreamB 2=Output
//  xioId: 0..3
//  mode: 0=disable, 1=rising edge, 2=falling edge, 3=level high, 4=level low
MID2253_API int __stdcall S2253P_PauseConfigXIO(int devid, int streamId, int xioId, S2253P_XIO_PAUSE_MODE mode);

// Enable the GPS updates (for OSD and other functions below)
MID2253_API int __stdcall S2253P_EnableGPS(int devid, int enable);
// Read GPS status
MID2253_API int __stdcall S2253P_ReadGPSStatus(int devid, int *status);
// Read Latitude (16 char buffer) ddmm.mmmmn  (returns empty if no location is available)
MID2253_API int __stdcall S2253P_ReadLatitude(int devid, char *value, int size);
// Read Longitude (16 char buffer) dddmm.mmmme  (returns empty if no location is available)
MID2253_API int __stdcall S2253P_ReadLongitude(int devid, char *value, int size);
// Read Altitude (16 char buffer) nnn.n meters (returns empty if no location is available)
MID2253_API int __stdcall S2253P_ReadAltitude(int devid, char *value, int size);
// Read Speed (16 char buffer) d.dd knots
MID2253_API int __stdcall S2253P_ReadSpeed(int devid, char *value, int size);
// Read Course (16 char buffer) ddddd.dd degrees
MID2253_API int __stdcall S2253P_ReadCourse(int devid, char *value, int size);
// Read UTC Time (16 char buffer) hhmmss.sss
MID2253_API int __stdcall S2253P_ReadUTCTime(int devid, char *value, int size);
// Read UTC Date (16 char buffer) ddmmyy
MID2253_API int __stdcall S2253P_ReadUTCDate(int devid, char *value, int size);

// Read GPS number of satellites used (0..12)
MID2253_API int __stdcall S2253P_ReadSatellites(int devid, int *value);
// Read GPS lock status (0=no lock 1=locked)
MID2253_API int __stdcall S2253P_ReadLock(int devid, int *value);

// Read last GPGGA message (80 char buffer)
MID2253_API int __stdcall S2253P_ReadGPS_GGA(int devid, char *value, int size);
// Read last GPGSA message (80 char buffer)
MID2253_API int __stdcall S2253P_ReadGPS_GSA(int devid, char *value, int size);
// Read last GPGSV message (80 char buffer)
MID2253_API int __stdcall S2253P_ReadGPS_GSV(int devid, char *value, int size);
// Read last GPRMC message (80 char buffer)
MID2253_API int __stdcall S2253P_ReadGPS_RMC(int devid, char *value, int size);

MID2253_API int __stdcall S2253_ResetBoard(int devid);

MID2253_API int __stdcall S2253_GetFPS(double *pfps, int devid, int strmidx);

// get gpio DIP switch position (firmware 8939 required)
MID2253_API int __stdcall S2253_GetDIPSwitch(int *dipswitch, int devid);

#endif
