Server/Display.cs
author StephaneLenclud
Tue, 10 Feb 2015 15:41:34 +0100
changeset 122 300f3d3114a8
parent 118 606c22398045
child 123 0df386e37e29
permissions -rw-r--r--
More consistent clock and clear support.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 //
     7 using System.Runtime.InteropServices;
     8 //using System.Runtime.Serialization;
     9 
    10 namespace SharpDisplayManager
    11 {
    12 
    13 
    14     /// <summary>
    15     /// Provide access to our display hardware through MiniDisplay API.
    16     /// </summary>
    17     public class Display
    18     {
    19 		public delegate void OnOpenedHandler(Display aDisplay);
    20 		public event OnOpenedHandler OnOpened;
    21 
    22 		public delegate void OnClosedHandler(Display aDisplay);
    23 		public event OnClosedHandler OnClosed;
    24 
    25 		//Our display device handle
    26 		IntPtr iDevice;
    27 
    28 		//static functions
    29 		public static int TypeCount()
    30 		{
    31 			return MiniDisplayTypeCount();
    32 		}
    33 
    34 		public static string TypeName(TMiniDisplayType aType)
    35 		{
    36 			IntPtr ptr = MiniDisplayTypeName(aType);
    37 			string str = Marshal.PtrToStringUni(ptr);
    38 			return str;
    39 		}
    40 
    41         //Constructor
    42         public Display()
    43         {
    44             iDevice = IntPtr.Zero;
    45         }
    46 
    47         //
    48         public bool Open(TMiniDisplayType aType)
    49         {
    50 			if (IsOpen())
    51 			{
    52 				//Already open return an error
    53 				return false;
    54 			}
    55 
    56             iDevice = MiniDisplayOpen(aType);
    57 
    58             bool success = iDevice != IntPtr.Zero;
    59 			if (success)
    60 			{
    61 				//Broadcast opened event
    62 				OnOpened(this);
    63 			}
    64 
    65 			return success;
    66         }
    67 
    68         public void Close()
    69         {
    70 			if (!IsOpen())
    71 			{
    72 				//Pointless
    73 				return;
    74 			}
    75 
    76 			//
    77             MiniDisplayClose(iDevice);
    78             iDevice = IntPtr.Zero;
    79 			//Broadcast closed event
    80 			OnClosed(this);
    81         }
    82 
    83         public bool IsOpen()
    84         {
    85             return iDevice != IntPtr.Zero;
    86         }
    87 
    88         public void Clear()
    89         {
    90             MiniDisplayClear(iDevice);
    91         }
    92 
    93         public void Fill()
    94         {
    95             MiniDisplayFill(iDevice);
    96         }
    97 
    98         public void SwapBuffers()
    99         {
   100             MiniDisplaySwapBuffers(iDevice);
   101         }
   102 
   103         public int MaxBrightness()
   104         {
   105             return MiniDisplayMaxBrightness(iDevice);
   106         }
   107 
   108         public int MinBrightness()
   109         {
   110             return MiniDisplayMinBrightness(iDevice);
   111         }
   112 
   113         public void SetBrightness(int aBrightness)
   114         {
   115             if (!IsOpen()) return;
   116 
   117             MiniDisplaySetBrightness(iDevice, aBrightness);
   118         }
   119 
   120         public int WidthInPixels()
   121         {
   122             return MiniDisplayWidthInPixels(iDevice);
   123         }
   124 
   125         public int HeightInPixels()
   126         {
   127             return MiniDisplayHeightInPixels(iDevice);
   128         }
   129 
   130         public void SetPixel(int aX, int aY, uint aValue)
   131         {
   132             MiniDisplaySetPixel(iDevice,aX,aY,aValue);
   133         }
   134 
   135         public void RequestPowerSupplyStatus()
   136         {
   137             MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus);
   138         }
   139 
   140         public void RequestDeviceId()
   141         {
   142             MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestDeviceId);
   143         }
   144 
   145         public void RequestFirmwareRevision()
   146         {
   147             MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision);
   148         }
   149 
   150         public void PowerOn()
   151         {
   152             MiniDisplayPowerOn(iDevice);
   153         }
   154 
   155         public void PowerOff()
   156         {
   157             MiniDisplayPowerOff(iDevice);
   158         }
   159 
   160         public bool SupportPowerOnOff()
   161         {
   162             return MiniDisplaySupportPowerOnOff(iDevice);
   163         }
   164 
   165         public void ShowClock()
   166         {
   167             MiniDisplayShowClock(iDevice);
   168         }
   169 
   170         public void HideClock()
   171         {
   172             MiniDisplayHideClock(iDevice);
   173         }
   174 
   175         public bool SupportClock()
   176         {
   177             return MiniDisplaySupportClock(iDevice);
   178         }
   179 
   180         public bool PowerSupplyStatus()
   181         {
   182             bool res = MiniDisplayPowerSupplyStatus(iDevice);
   183             return res;
   184         }
   185 
   186         public TMiniDisplayRequest AttemptRequestCompletion()
   187         {
   188             return MiniDisplayAttemptRequestCompletion(iDevice);
   189         }
   190 
   191         public TMiniDisplayRequest CurrentRequest()
   192         {
   193             return MiniDisplayCurrentRequest(iDevice);
   194         }
   195 
   196         public bool IsRequestPending()
   197         {
   198             return CurrentRequest() != TMiniDisplayRequest.EMiniDisplayRequestNone;
   199         }
   200 
   201 		//
   202 		public int IconCount(TMiniDisplayIconType aIcon)
   203 		{
   204 			return MiniDisplayIconCount(iDevice,aIcon);
   205 		}
   206 
   207 		public int IconStatusCount(TMiniDisplayIconType aIcon)
   208 		{
   209 			return MiniDisplayIconStatusCount(iDevice, aIcon);
   210 		}
   211 
   212 		public void SetIconStatus(TMiniDisplayIconType aIcon, int aIndex, int aStatus)
   213 		{
   214 			MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, aStatus);
   215 		}
   216 
   217 		public void SetIconOn(TMiniDisplayIconType aIcon, int aIndex)
   218 		{
   219 			MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, IconStatusCount(aIcon) - 1);
   220 		}
   221 
   222 		public void SetIconOff(TMiniDisplayIconType aIcon, int aIndex)
   223 		{
   224 			MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, 0);
   225 		}
   226 
   227 
   228 		public void SetAllIconsStatus(int aStatus)
   229 		{
   230 			foreach (TMiniDisplayIconType icon in Enum.GetValues(typeof(TMiniDisplayIconType)))
   231 			{
   232 				int count=IconCount(icon);
   233 				for (int i = 0; i < count; i++)
   234 				{
   235 					SetIconStatus(icon,i,aStatus);
   236 				}
   237 			}
   238 		}
   239 
   240 		/// <summary>
   241 		/// Set all elements of an icon to the given status.
   242 		/// </summary>
   243 		/// <param name="aIcon"></param>
   244 		/// <param name="aStatus"></param>
   245 		public void SetIconStatus(TMiniDisplayIconType aIcon, int aStatus)
   246 		{
   247 			int iconCount = IconCount(aIcon);
   248 			for (int i = 0; i < iconCount; i++)
   249 			{
   250 				SetIconStatus(aIcon, i, aStatus);
   251 			}
   252 		}
   253 
   254 		/// <summary>
   255 		/// Set all elements of an icon to be either on or off.
   256 		/// </summary>
   257 		/// <param name="aIcon"></param>
   258 		/// <param name="aOn"></param>		
   259 		public void SetIconOnOff(TMiniDisplayIconType aIcon, bool aOn)
   260 		{
   261 			if (aOn)
   262 			{
   263 				SetIconOn(aIcon);
   264 			}
   265 			else
   266 			{
   267 				SetIconOff(aIcon);
   268 			}
   269 		}
   270 
   271 		/// <summary>
   272 		/// Set all elements of an icon to there maximum status.
   273 		/// </summary>
   274 		/// <param name="aIcon"></param>
   275 		public void SetIconOn(TMiniDisplayIconType aIcon)
   276 		{
   277 			int iconCount = IconCount(aIcon);
   278 			for (int i = 0; i < iconCount; i++)
   279 			{
   280 				SetIconStatus(aIcon, i, IconStatusCount(aIcon) - 1);
   281 			}
   282 		}
   283 
   284 		/// <summary>
   285 		/// Turn off all elements of an icon.
   286 		/// </summary>
   287 		/// <param name="aIcon"></param>
   288 		public void SetIconOff(TMiniDisplayIconType aIcon)
   289 		{
   290 			int iconCount = IconCount(aIcon);
   291 			for (int i = 0; i < iconCount; i++)
   292 			{
   293 				SetIconStatus(aIcon, i, 0);
   294 			}
   295 		}
   296 
   297 
   298 
   299         public string Vendor()
   300         {
   301             IntPtr ptr = MiniDisplayVendor(iDevice);
   302             string str = Marshal.PtrToStringUni(ptr);
   303             return str;
   304         }
   305 
   306         public string Product()
   307         {
   308             IntPtr ptr = MiniDisplayProduct(iDevice);
   309             string str = Marshal.PtrToStringUni(ptr);
   310             return str;
   311         }
   312 
   313         public string SerialNumber()
   314         {
   315             IntPtr ptr = MiniDisplaySerialNumber(iDevice);
   316             string str = Marshal.PtrToStringUni(ptr);
   317             return str;
   318         }
   319 
   320         public string DeviceId()
   321         {
   322             IntPtr ptr = MiniDisplayDeviceId(iDevice);
   323             string str = Marshal.PtrToStringAnsi(ptr);
   324             return str;
   325         }
   326 
   327         public string FirmwareRevision()
   328         {
   329             IntPtr ptr = MiniDisplayFirmwareRevision(iDevice);
   330             string str = Marshal.PtrToStringAnsi(ptr);
   331             return str;
   332         }
   333 
   334         //[Serializable]
   335         public enum TMiniDisplayType
   336         {
   337             EMiniDisplayAutoDetect, /*Not yet implemented*/
   338             //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
   339             EMiniDisplayFutabaGP1212A01,
   340             //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
   341             EMiniDisplayFutabaGP1212A02
   342         };
   343 
   344 		/// <summary>
   345 		/// 
   346 		/// </summary>
   347         public enum TMiniDisplayRequest
   348         {
   349             EMiniDisplayRequestNone,
   350             EMiniDisplayRequestDeviceId,
   351             EMiniDisplayRequestFirmwareRevision,
   352             EMiniDisplayRequestPowerSupplyStatus
   353         };
   354 
   355 			
   356 		/// <summary>
   357 		/// Define the various type of icons we support.
   358 		/// For binary compatibility new entries must be added at the end.
   359 		/// </summary>
   360 		public enum TMiniDisplayIconType
   361 		{
   362 			EMiniDisplayIconNetworkSignal=0,
   363 			EMiniDisplayIconInternet,
   364 			EMiniDisplayIconEmail,
   365 			EMiniDisplayIconMute,
   366 			EMiniDisplayIconVolume,
   367 			EMiniDisplayIconVolumeLabel,
   368 			EMiniDisplayIconPlay,
   369 			EMiniDisplayIconPause,
   370 			EMiniDisplayIconRecording
   371 		};
   372 
   373         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   374         public static extern IntPtr MiniDisplayOpen(TMiniDisplayType aType);
   375 
   376         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   377         public static extern void MiniDisplayClose(IntPtr aDevice);
   378 
   379 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   380 		public static extern int MiniDisplayTypeCount();
   381 
   382 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   383 		public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType);
   384 
   385         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   386         public static extern void MiniDisplayClear(IntPtr aDevice);
   387 
   388         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   389         public static extern void MiniDisplayFill(IntPtr aDevice);
   390 
   391         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   392         public static extern void MiniDisplaySwapBuffers(IntPtr aDevice);
   393 
   394         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   395         public static extern void MiniDisplaySetBrightness(IntPtr aDevice, int aBrightness);
   396 
   397         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   398         public static extern int MiniDisplayMinBrightness(IntPtr aDevice);
   399 
   400         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   401         public static extern int MiniDisplayMaxBrightness(IntPtr aDevice);
   402 
   403         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   404         public static extern int MiniDisplayWidthInPixels(IntPtr aDevice);
   405 
   406         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   407         public static extern int MiniDisplayHeightInPixels(IntPtr aDevice);
   408 
   409         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   410         public static extern int MiniDisplaySetPixel(IntPtr aDevice, int aX, int aY, uint aValue);
   411 
   412         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   413         public static extern IntPtr MiniDisplayVendor(IntPtr aDevice);
   414 
   415         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   416         public static extern IntPtr MiniDisplayProduct(IntPtr aDevice);
   417 
   418         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   419         public static extern IntPtr MiniDisplaySerialNumber(IntPtr aDevice);
   420 
   421         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   422         public static extern IntPtr MiniDisplayDeviceId(IntPtr aDevice);
   423 
   424         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   425         public static extern IntPtr MiniDisplayFirmwareRevision(IntPtr aDevice);
   426 
   427         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   428         [return: MarshalAs(UnmanagedType.I1)]
   429         public static extern bool MiniDisplayPowerSupplyStatus(IntPtr aDevice);
   430 
   431         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   432         public static extern void MiniDisplayRequest(IntPtr aDevice, TMiniDisplayRequest aRequest);
   433 
   434         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   435         public static extern TMiniDisplayRequest MiniDisplayAttemptRequestCompletion(IntPtr aDevice);
   436 
   437         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   438         public static extern TMiniDisplayRequest MiniDisplayCurrentRequest(IntPtr aDevice);
   439 
   440         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   441         public static extern void MiniDisplayCancelRequest(IntPtr aDevice);
   442 
   443         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   444         public static extern void MiniDisplayPowerOn(IntPtr aDevice);
   445 
   446         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   447         public static extern void MiniDisplayPowerOff(IntPtr aDevice);
   448 
   449         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   450         [return: MarshalAs(UnmanagedType.I1)]
   451         public static extern bool MiniDisplaySupportPowerOnOff(IntPtr aDevice);
   452 
   453         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   454         public static extern void MiniDisplayShowClock(IntPtr aDevice);
   455 
   456         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   457         public static extern void MiniDisplayHideClock(IntPtr aDevice);
   458 
   459         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   460         [return: MarshalAs(UnmanagedType.I1)]
   461         public static extern bool MiniDisplaySupportClock(IntPtr aDevice);
   462 
   463 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   464 		public static extern int MiniDisplayIconCount(IntPtr aDevice, TMiniDisplayIconType aIcon);
   465 
   466 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   467 		public static extern int MiniDisplayIconStatusCount(IntPtr aDevice, TMiniDisplayIconType aIcon);
   468 		
   469 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   470 		public static extern void MiniDisplaySetIconStatus(IntPtr aDevice, TMiniDisplayIconType aIcon, int aIndex, int aStatus);
   471 
   472     }
   473 }