Server/Display.cs
author StephaneLenclud
Sun, 08 Feb 2015 18:56:43 +0100
changeset 114 debe115b3b22
parent 108 7dd1d881c142
child 115 5c61a13c4241
permissions -rw-r--r--
Audio: Taking icon statuc count into account.
Volume indicator on MDM166AA now using half brightness bar when rounding up.
     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 SetAllIconsStatus(int aStatus)
   222 		{
   223 			foreach (TMiniDisplayIconType icon in Enum.GetValues(typeof(TMiniDisplayIconType)))
   224 			{
   225 				int count=IconCount(icon);
   226 				for (int i = 0; i < count; i++)
   227 				{
   228 					SetIconStatus(icon,i,aStatus);
   229 				}
   230 			}
   231 
   232 		}
   233 
   234 
   235 
   236         public string Vendor()
   237         {
   238             IntPtr ptr = MiniDisplayVendor(iDevice);
   239             string str = Marshal.PtrToStringUni(ptr);
   240             return str;
   241         }
   242 
   243         public string Product()
   244         {
   245             IntPtr ptr = MiniDisplayProduct(iDevice);
   246             string str = Marshal.PtrToStringUni(ptr);
   247             return str;
   248         }
   249 
   250         public string SerialNumber()
   251         {
   252             IntPtr ptr = MiniDisplaySerialNumber(iDevice);
   253             string str = Marshal.PtrToStringUni(ptr);
   254             return str;
   255         }
   256 
   257         public string DeviceId()
   258         {
   259             IntPtr ptr = MiniDisplayDeviceId(iDevice);
   260             string str = Marshal.PtrToStringAnsi(ptr);
   261             return str;
   262         }
   263 
   264         public string FirmwareRevision()
   265         {
   266             IntPtr ptr = MiniDisplayFirmwareRevision(iDevice);
   267             string str = Marshal.PtrToStringAnsi(ptr);
   268             return str;
   269         }
   270 
   271         //[Serializable]
   272         public enum TMiniDisplayType
   273         {
   274             EMiniDisplayAutoDetect, /*Not yet implemented*/
   275             //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
   276             EMiniDisplayFutabaGP1212A01,
   277             //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
   278             EMiniDisplayFutabaGP1212A02
   279         };
   280 
   281 		/// <summary>
   282 		/// 
   283 		/// </summary>
   284         public enum TMiniDisplayRequest
   285         {
   286             EMiniDisplayRequestNone,
   287             EMiniDisplayRequestDeviceId,
   288             EMiniDisplayRequestFirmwareRevision,
   289             EMiniDisplayRequestPowerSupplyStatus
   290         };
   291 
   292 			
   293 		/// <summary>
   294 		/// Define the various type of icons we support.
   295 		/// For binary compatibility new entries must be added at the end.
   296 		/// </summary>
   297 		public enum TMiniDisplayIconType
   298 		{
   299 			EMiniDisplayIconNetwork=0,
   300 			EMiniDisplayIconEmail,
   301 			EMiniDisplayIconMute,
   302 			EMiniDisplayIconVolume,
   303 			EMiniDisplayIconVolumeLabel,
   304 			EMiniDisplayIconPlay,
   305 			EMiniDisplayIconPause,
   306 			EMiniDisplayIconRecording
   307 		};
   308 
   309         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   310         public static extern IntPtr MiniDisplayOpen(TMiniDisplayType aType);
   311 
   312         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   313         public static extern void MiniDisplayClose(IntPtr aDevice);
   314 
   315 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   316 		public static extern int MiniDisplayTypeCount();
   317 
   318 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   319 		public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType);
   320 
   321         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   322         public static extern void MiniDisplayClear(IntPtr aDevice);
   323 
   324         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   325         public static extern void MiniDisplayFill(IntPtr aDevice);
   326 
   327         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   328         public static extern void MiniDisplaySwapBuffers(IntPtr aDevice);
   329 
   330         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   331         public static extern void MiniDisplaySetBrightness(IntPtr aDevice, int aBrightness);
   332 
   333         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   334         public static extern int MiniDisplayMinBrightness(IntPtr aDevice);
   335 
   336         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   337         public static extern int MiniDisplayMaxBrightness(IntPtr aDevice);
   338 
   339         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   340         public static extern int MiniDisplayWidthInPixels(IntPtr aDevice);
   341 
   342         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   343         public static extern int MiniDisplayHeightInPixels(IntPtr aDevice);
   344 
   345         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   346         public static extern int MiniDisplaySetPixel(IntPtr aDevice, int aX, int aY, uint aValue);
   347 
   348         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   349         public static extern IntPtr MiniDisplayVendor(IntPtr aDevice);
   350 
   351         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   352         public static extern IntPtr MiniDisplayProduct(IntPtr aDevice);
   353 
   354         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   355         public static extern IntPtr MiniDisplaySerialNumber(IntPtr aDevice);
   356 
   357         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   358         public static extern IntPtr MiniDisplayDeviceId(IntPtr aDevice);
   359 
   360         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   361         public static extern IntPtr MiniDisplayFirmwareRevision(IntPtr aDevice);
   362 
   363         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   364         [return: MarshalAs(UnmanagedType.I1)]
   365         public static extern bool MiniDisplayPowerSupplyStatus(IntPtr aDevice);
   366 
   367         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   368         public static extern void MiniDisplayRequest(IntPtr aDevice, TMiniDisplayRequest aRequest);
   369 
   370         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   371         public static extern TMiniDisplayRequest MiniDisplayAttemptRequestCompletion(IntPtr aDevice);
   372 
   373         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   374         public static extern TMiniDisplayRequest MiniDisplayCurrentRequest(IntPtr aDevice);
   375 
   376         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   377         public static extern void MiniDisplayCancelRequest(IntPtr aDevice);
   378 
   379         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   380         public static extern void MiniDisplayPowerOn(IntPtr aDevice);
   381 
   382         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   383         public static extern void MiniDisplayPowerOff(IntPtr aDevice);
   384 
   385         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   386         [return: MarshalAs(UnmanagedType.I1)]
   387         public static extern bool MiniDisplaySupportPowerOnOff(IntPtr aDevice);
   388 
   389         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   390         public static extern void MiniDisplayShowClock(IntPtr aDevice);
   391 
   392         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   393         public static extern void MiniDisplayHideClock(IntPtr aDevice);
   394 
   395         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   396         [return: MarshalAs(UnmanagedType.I1)]
   397         public static extern bool MiniDisplaySupportClock(IntPtr aDevice);
   398 
   399 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   400 		public static extern int MiniDisplayIconCount(IntPtr aDevice, TMiniDisplayIconType aIcon);
   401 
   402 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   403 		public static extern int MiniDisplayIconStatusCount(IntPtr aDevice, TMiniDisplayIconType aIcon);
   404 		
   405 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   406 		public static extern void MiniDisplaySetIconStatus(IntPtr aDevice, TMiniDisplayIconType aIcon, int aIndex, int aStatus);
   407 
   408     }
   409 }