sl@3: using System; sl@3: using System.Collections.Generic; sl@3: using System.Linq; sl@3: using System.Text; sl@3: using System.Threading.Tasks; sl@3: // sl@3: using System.Runtime.InteropServices; sl@46: //using System.Runtime.Serialization; sl@3: sl@3: namespace SharpDisplayManager sl@3: { sl@46: StephaneLenclud@104: sl@44: /// sl@44: /// Provide access to our display hardware through MiniDisplay API. sl@44: /// sl@46: public class Display sl@3: { StephaneLenclud@104: public delegate void OnOpenedHandler(Display aDisplay); StephaneLenclud@104: public event OnOpenedHandler OnOpened; StephaneLenclud@104: StephaneLenclud@104: public delegate void OnClosedHandler(Display aDisplay); StephaneLenclud@104: public event OnClosedHandler OnClosed; StephaneLenclud@104: StephaneLenclud@104: //Our display device handle StephaneLenclud@104: IntPtr iDevice; StephaneLenclud@104: StephaneLenclud@104: //static functions StephaneLenclud@104: public static int TypeCount() StephaneLenclud@104: { StephaneLenclud@104: return MiniDisplayTypeCount(); StephaneLenclud@104: } StephaneLenclud@104: StephaneLenclud@104: public static string TypeName(TMiniDisplayType aType) StephaneLenclud@104: { StephaneLenclud@104: IntPtr ptr = MiniDisplayTypeName(aType); StephaneLenclud@104: string str = Marshal.PtrToStringUni(ptr); StephaneLenclud@104: return str; StephaneLenclud@104: } sl@3: sl@3: //Constructor sl@3: public Display() sl@3: { sl@3: iDevice = IntPtr.Zero; sl@3: } sl@3: sl@3: // sl@39: public bool Open(TMiniDisplayType aType) sl@3: { StephaneLenclud@104: if (IsOpen()) StephaneLenclud@104: { StephaneLenclud@104: //Already open return an error StephaneLenclud@104: return false; StephaneLenclud@104: } StephaneLenclud@104: StephaneLenclud@104: iDevice = MiniDisplayOpen(aType); StephaneLenclud@104: StephaneLenclud@104: bool success = iDevice != IntPtr.Zero; StephaneLenclud@104: if (success) StephaneLenclud@104: { StephaneLenclud@104: //Broadcast opened event StephaneLenclud@104: OnOpened(this); StephaneLenclud@104: } StephaneLenclud@104: StephaneLenclud@104: return success; sl@3: } sl@3: sl@3: public void Close() sl@3: { StephaneLenclud@104: if (!IsOpen()) StephaneLenclud@104: { StephaneLenclud@104: //Pointless StephaneLenclud@104: return; StephaneLenclud@104: } StephaneLenclud@104: StephaneLenclud@105: //Controversially clearing our screen before closing StephaneLenclud@105: //Consider moving this up into the UI layer StephaneLenclud@105: Clear(); StephaneLenclud@105: SwapBuffers(); StephaneLenclud@105: // sl@3: MiniDisplayClose(iDevice); sl@3: iDevice = IntPtr.Zero; StephaneLenclud@104: //Broadcast closed event StephaneLenclud@104: OnClosed(this); sl@3: } sl@3: sl@3: public bool IsOpen() sl@3: { sl@3: return iDevice != IntPtr.Zero; sl@3: } sl@3: sl@3: public void Clear() sl@3: { sl@3: MiniDisplayClear(iDevice); sl@3: } sl@3: sl@3: public void Fill() sl@3: { sl@3: MiniDisplayFill(iDevice); sl@3: } sl@3: sl@3: public void SwapBuffers() sl@3: { sl@3: MiniDisplaySwapBuffers(iDevice); sl@3: } sl@3: sl@3: public int MaxBrightness() sl@3: { sl@3: return MiniDisplayMaxBrightness(iDevice); sl@3: } sl@3: sl@3: public int MinBrightness() sl@3: { sl@3: return MiniDisplayMinBrightness(iDevice); sl@3: } sl@3: sl@3: public void SetBrightness(int aBrightness) sl@3: { sl@3: if (!IsOpen()) return; sl@3: sl@3: MiniDisplaySetBrightness(iDevice, aBrightness); sl@3: } sl@3: sl@4: public int WidthInPixels() sl@4: { sl@4: return MiniDisplayWidthInPixels(iDevice); sl@4: } sl@4: sl@4: public int HeightInPixels() sl@4: { sl@4: return MiniDisplayHeightInPixels(iDevice); sl@4: } sl@4: sl@57: public void SetPixel(int aX, int aY, uint aValue) sl@4: { sl@4: MiniDisplaySetPixel(iDevice,aX,aY,aValue); sl@4: } sl@4: sl@12: public void RequestPowerSupplyStatus() sl@12: { sl@44: MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus); sl@12: } sl@12: sl@12: public void RequestDeviceId() sl@12: { sl@44: MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestDeviceId); sl@12: } sl@12: sl@12: public void RequestFirmwareRevision() sl@12: { sl@44: MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision); sl@12: } sl@12: sl@52: public void PowerOn() sl@52: { sl@52: MiniDisplayPowerOn(iDevice); sl@52: } sl@52: sl@52: public void PowerOff() sl@52: { sl@52: MiniDisplayPowerOff(iDevice); sl@52: } sl@52: sl@52: public bool SupportPowerOnOff() sl@52: { sl@52: return MiniDisplaySupportPowerOnOff(iDevice); sl@52: } sl@52: sl@53: public void ShowClock() sl@53: { sl@53: MiniDisplayShowClock(iDevice); sl@53: } sl@53: sl@53: public void HideClock() sl@53: { sl@53: MiniDisplayHideClock(iDevice); sl@53: } sl@53: sl@53: public bool SupportClock() sl@53: { sl@53: return MiniDisplaySupportClock(iDevice); sl@53: } sl@53: sl@12: public bool PowerSupplyStatus() sl@12: { sl@12: bool res = MiniDisplayPowerSupplyStatus(iDevice); sl@12: return res; sl@12: } sl@12: sl@12: public TMiniDisplayRequest AttemptRequestCompletion() sl@12: { sl@12: return MiniDisplayAttemptRequestCompletion(iDevice); sl@12: } sl@12: sl@12: public TMiniDisplayRequest CurrentRequest() sl@12: { sl@12: return MiniDisplayCurrentRequest(iDevice); sl@12: } sl@12: sl@12: public bool IsRequestPending() sl@12: { sl@12: return CurrentRequest() != TMiniDisplayRequest.EMiniDisplayRequestNone; sl@12: } sl@12: StephaneLenclud@108: // StephaneLenclud@109: public int IconCount(TMiniDisplayIconType aIcon) StephaneLenclud@108: { StephaneLenclud@109: return MiniDisplayIconCount(iDevice,aIcon); StephaneLenclud@108: } StephaneLenclud@108: StephaneLenclud@109: public int IconStatusCount(TMiniDisplayIconType aIcon) StephaneLenclud@108: { StephaneLenclud@109: return MiniDisplayIconStatusCount(iDevice, aIcon); StephaneLenclud@108: } StephaneLenclud@108: StephaneLenclud@109: public void SetIconStatus(TMiniDisplayIconType aIcon, int aIndex, int aStatus) StephaneLenclud@108: { StephaneLenclud@109: MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, aStatus); StephaneLenclud@108: } StephaneLenclud@108: StephaneLenclud@118: public void SetIconOn(TMiniDisplayIconType aIcon, int aIndex) StephaneLenclud@118: { StephaneLenclud@118: MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, IconStatusCount(aIcon) - 1); StephaneLenclud@118: } StephaneLenclud@118: StephaneLenclud@118: public void SetIconOff(TMiniDisplayIconType aIcon, int aIndex) StephaneLenclud@118: { StephaneLenclud@118: MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, 0); StephaneLenclud@118: } StephaneLenclud@118: StephaneLenclud@118: StephaneLenclud@109: public void SetAllIconsStatus(int aStatus) StephaneLenclud@108: { StephaneLenclud@109: foreach (TMiniDisplayIconType icon in Enum.GetValues(typeof(TMiniDisplayIconType))) StephaneLenclud@109: { StephaneLenclud@109: int count=IconCount(icon); StephaneLenclud@109: for (int i = 0; i < count; i++) StephaneLenclud@109: { StephaneLenclud@109: SetIconStatus(icon,i,aStatus); StephaneLenclud@109: } StephaneLenclud@109: } StephaneLenclud@109: StephaneLenclud@108: } StephaneLenclud@108: StephaneLenclud@115: /// StephaneLenclud@116: /// Set all elements of an icon to the given status. StephaneLenclud@115: /// StephaneLenclud@115: /// StephaneLenclud@115: /// StephaneLenclud@115: public void SetIconStatus(TMiniDisplayIconType aIcon, int aStatus) StephaneLenclud@115: { StephaneLenclud@115: int iconCount = IconCount(aIcon); StephaneLenclud@115: for (int i = 0; i < iconCount; i++) StephaneLenclud@115: { StephaneLenclud@115: SetIconStatus(aIcon, i, aStatus); StephaneLenclud@115: } StephaneLenclud@115: } StephaneLenclud@115: StephaneLenclud@115: /// StephaneLenclud@116: /// Set all elements of an icon to be either on or off. StephaneLenclud@116: /// StephaneLenclud@116: /// StephaneLenclud@116: /// StephaneLenclud@116: public void SetIconOnOff(TMiniDisplayIconType aIcon, bool aOn) StephaneLenclud@116: { StephaneLenclud@116: if (aOn) StephaneLenclud@116: { StephaneLenclud@116: SetIconOn(aIcon); StephaneLenclud@116: } StephaneLenclud@116: else StephaneLenclud@116: { StephaneLenclud@116: SetIconOff(aIcon); StephaneLenclud@116: } StephaneLenclud@116: } StephaneLenclud@116: StephaneLenclud@116: /// StephaneLenclud@116: /// Set all elements of an icon to there maximum status. StephaneLenclud@115: /// StephaneLenclud@115: /// StephaneLenclud@115: public void SetIconOn(TMiniDisplayIconType aIcon) StephaneLenclud@115: { StephaneLenclud@115: int iconCount = IconCount(aIcon); StephaneLenclud@115: for (int i = 0; i < iconCount; i++) StephaneLenclud@115: { StephaneLenclud@115: SetIconStatus(aIcon, i, IconStatusCount(aIcon) - 1); StephaneLenclud@115: } StephaneLenclud@115: } StephaneLenclud@115: StephaneLenclud@115: /// StephaneLenclud@116: /// Turn off all elements of an icon. StephaneLenclud@115: /// StephaneLenclud@115: /// StephaneLenclud@115: public void SetIconOff(TMiniDisplayIconType aIcon) StephaneLenclud@115: { StephaneLenclud@115: int iconCount = IconCount(aIcon); StephaneLenclud@115: for (int i = 0; i < iconCount; i++) StephaneLenclud@115: { StephaneLenclud@115: SetIconStatus(aIcon, i, 0); StephaneLenclud@115: } StephaneLenclud@115: } StephaneLenclud@115: StephaneLenclud@108: sl@12: sl@10: public string Vendor() sl@10: { sl@10: IntPtr ptr = MiniDisplayVendor(iDevice); sl@10: string str = Marshal.PtrToStringUni(ptr); sl@10: return str; sl@10: } sl@4: sl@10: public string Product() sl@10: { sl@10: IntPtr ptr = MiniDisplayProduct(iDevice); sl@10: string str = Marshal.PtrToStringUni(ptr); sl@10: return str; sl@10: } sl@10: sl@10: public string SerialNumber() sl@10: { sl@10: IntPtr ptr = MiniDisplaySerialNumber(iDevice); sl@10: string str = Marshal.PtrToStringUni(ptr); sl@10: return str; sl@10: } sl@4: sl@12: public string DeviceId() sl@12: { sl@12: IntPtr ptr = MiniDisplayDeviceId(iDevice); sl@12: string str = Marshal.PtrToStringAnsi(ptr); sl@12: return str; sl@12: } sl@12: sl@12: public string FirmwareRevision() sl@12: { sl@12: IntPtr ptr = MiniDisplayFirmwareRevision(iDevice); sl@12: string str = Marshal.PtrToStringAnsi(ptr); sl@12: return str; sl@12: } sl@12: sl@46: //[Serializable] sl@39: public enum TMiniDisplayType sl@39: { sl@39: EMiniDisplayAutoDetect, /*Not yet implemented*/ sl@46: //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")] sl@39: EMiniDisplayFutabaGP1212A01, sl@46: //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")] sl@39: EMiniDisplayFutabaGP1212A02 sl@39: }; sl@39: StephaneLenclud@109: /// StephaneLenclud@109: /// StephaneLenclud@109: /// sl@10: public enum TMiniDisplayRequest sl@10: { sl@10: EMiniDisplayRequestNone, sl@10: EMiniDisplayRequestDeviceId, sl@10: EMiniDisplayRequestFirmwareRevision, sl@10: EMiniDisplayRequestPowerSupplyStatus sl@39: }; sl@10: StephaneLenclud@109: StephaneLenclud@109: /// StephaneLenclud@109: /// Define the various type of icons we support. StephaneLenclud@109: /// For binary compatibility new entries must be added at the end. StephaneLenclud@109: /// StephaneLenclud@109: public enum TMiniDisplayIconType StephaneLenclud@109: { StephaneLenclud@118: EMiniDisplayIconNetworkSignal=0, StephaneLenclud@118: EMiniDisplayIconInternet, StephaneLenclud@109: EMiniDisplayIconEmail, StephaneLenclud@109: EMiniDisplayIconMute, StephaneLenclud@109: EMiniDisplayIconVolume, StephaneLenclud@109: EMiniDisplayIconVolumeLabel, StephaneLenclud@109: EMiniDisplayIconPlay, StephaneLenclud@109: EMiniDisplayIconPause, StephaneLenclud@109: EMiniDisplayIconRecording StephaneLenclud@109: }; StephaneLenclud@109: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@39: public static extern IntPtr MiniDisplayOpen(TMiniDisplayType aType); sl@3: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@3: public static extern void MiniDisplayClose(IntPtr aDevice); sl@3: StephaneLenclud@104: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] StephaneLenclud@104: public static extern int MiniDisplayTypeCount(); StephaneLenclud@104: StephaneLenclud@104: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] StephaneLenclud@104: public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType); StephaneLenclud@104: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@3: public static extern void MiniDisplayClear(IntPtr aDevice); sl@3: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@3: public static extern void MiniDisplayFill(IntPtr aDevice); sl@3: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@3: public static extern void MiniDisplaySwapBuffers(IntPtr aDevice); sl@3: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@3: public static extern void MiniDisplaySetBrightness(IntPtr aDevice, int aBrightness); sl@3: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@3: public static extern int MiniDisplayMinBrightness(IntPtr aDevice); sl@3: sl@3: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@3: public static extern int MiniDisplayMaxBrightness(IntPtr aDevice); sl@3: sl@4: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@4: public static extern int MiniDisplayWidthInPixels(IntPtr aDevice); sl@4: sl@4: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@4: public static extern int MiniDisplayHeightInPixels(IntPtr aDevice); sl@4: sl@4: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@57: public static extern int MiniDisplaySetPixel(IntPtr aDevice, int aX, int aY, uint aValue); sl@4: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern IntPtr MiniDisplayVendor(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern IntPtr MiniDisplayProduct(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern IntPtr MiniDisplaySerialNumber(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern IntPtr MiniDisplayDeviceId(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern IntPtr MiniDisplayFirmwareRevision(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@12: [return: MarshalAs(UnmanagedType.I1)] sl@10: public static extern bool MiniDisplayPowerSupplyStatus(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@44: public static extern void MiniDisplayRequest(IntPtr aDevice, TMiniDisplayRequest aRequest); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern TMiniDisplayRequest MiniDisplayAttemptRequestCompletion(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern TMiniDisplayRequest MiniDisplayCurrentRequest(IntPtr aDevice); sl@10: sl@10: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@10: public static extern void MiniDisplayCancelRequest(IntPtr aDevice); sl@10: sl@52: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@52: public static extern void MiniDisplayPowerOn(IntPtr aDevice); sl@52: sl@52: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@52: public static extern void MiniDisplayPowerOff(IntPtr aDevice); sl@52: sl@52: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@52: [return: MarshalAs(UnmanagedType.I1)] sl@52: public static extern bool MiniDisplaySupportPowerOnOff(IntPtr aDevice); sl@10: sl@53: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@53: public static extern void MiniDisplayShowClock(IntPtr aDevice); sl@53: sl@53: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@53: public static extern void MiniDisplayHideClock(IntPtr aDevice); sl@53: sl@53: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] sl@53: [return: MarshalAs(UnmanagedType.I1)] sl@53: public static extern bool MiniDisplaySupportClock(IntPtr aDevice); sl@53: StephaneLenclud@108: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] StephaneLenclud@109: public static extern int MiniDisplayIconCount(IntPtr aDevice, TMiniDisplayIconType aIcon); StephaneLenclud@108: StephaneLenclud@108: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] StephaneLenclud@109: public static extern int MiniDisplayIconStatusCount(IntPtr aDevice, TMiniDisplayIconType aIcon); StephaneLenclud@108: StephaneLenclud@108: [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] StephaneLenclud@109: public static extern void MiniDisplaySetIconStatus(IntPtr aDevice, TMiniDisplayIconType aIcon, int aIndex, int aStatus); sl@53: sl@3: } sl@3: }