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