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