Server/Display.cs
author StephaneLenclud
Wed, 04 Feb 2015 17:44:25 +0100
changeset 104 189aac7dd3d6
parent 57 544132d07c3b
child 105 4196b0ca97d9
permissions -rw-r--r--
Adding events for display closed/opened.
Display types now loaded dynamically from our MiniDisplay library.
Adding editor config.
Tested against Futaba GP1212A02.
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
sl@3
    76
            MiniDisplayClose(iDevice);
sl@3
    77
            iDevice = IntPtr.Zero;
StephaneLenclud@104
    78
			//Broadcast closed event
StephaneLenclud@104
    79
			OnClosed(this);
sl@3
    80
        }
sl@3
    81
sl@3
    82
        public bool IsOpen()
sl@3
    83
        {
sl@3
    84
            return iDevice != IntPtr.Zero;
sl@3
    85
        }
sl@3
    86
sl@3
    87
        public void Clear()
sl@3
    88
        {
sl@3
    89
            MiniDisplayClear(iDevice);
sl@3
    90
        }
sl@3
    91
sl@3
    92
        public void Fill()
sl@3
    93
        {
sl@3
    94
            MiniDisplayFill(iDevice);
sl@3
    95
        }
sl@3
    96
sl@3
    97
        public void SwapBuffers()
sl@3
    98
        {
sl@3
    99
            MiniDisplaySwapBuffers(iDevice);
sl@3
   100
        }
sl@3
   101
sl@3
   102
        public int MaxBrightness()
sl@3
   103
        {
sl@3
   104
            return MiniDisplayMaxBrightness(iDevice);
sl@3
   105
        }
sl@3
   106
sl@3
   107
        public int MinBrightness()
sl@3
   108
        {
sl@3
   109
            return MiniDisplayMinBrightness(iDevice);
sl@3
   110
        }
sl@3
   111
sl@3
   112
        public void SetBrightness(int aBrightness)
sl@3
   113
        {
sl@3
   114
            if (!IsOpen()) return;
sl@3
   115
sl@3
   116
            MiniDisplaySetBrightness(iDevice, aBrightness);
sl@3
   117
        }
sl@3
   118
sl@4
   119
        public int WidthInPixels()
sl@4
   120
        {
sl@4
   121
            return MiniDisplayWidthInPixels(iDevice);
sl@4
   122
        }
sl@4
   123
sl@4
   124
        public int HeightInPixels()
sl@4
   125
        {
sl@4
   126
            return MiniDisplayHeightInPixels(iDevice);
sl@4
   127
        }
sl@4
   128
sl@57
   129
        public void SetPixel(int aX, int aY, uint aValue)
sl@4
   130
        {
sl@4
   131
            MiniDisplaySetPixel(iDevice,aX,aY,aValue);
sl@4
   132
        }
sl@4
   133
sl@12
   134
        public void RequestPowerSupplyStatus()
sl@12
   135
        {
sl@44
   136
            MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus);
sl@12
   137
        }
sl@12
   138
sl@12
   139
        public void RequestDeviceId()
sl@12
   140
        {
sl@44
   141
            MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestDeviceId);
sl@12
   142
        }
sl@12
   143
sl@12
   144
        public void RequestFirmwareRevision()
sl@12
   145
        {
sl@44
   146
            MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision);
sl@12
   147
        }
sl@12
   148
sl@52
   149
        public void PowerOn()
sl@52
   150
        {
sl@52
   151
            MiniDisplayPowerOn(iDevice);
sl@52
   152
        }
sl@52
   153
sl@52
   154
        public void PowerOff()
sl@52
   155
        {
sl@52
   156
            MiniDisplayPowerOff(iDevice);
sl@52
   157
        }
sl@52
   158
sl@52
   159
        public bool SupportPowerOnOff()
sl@52
   160
        {
sl@52
   161
            return MiniDisplaySupportPowerOnOff(iDevice);
sl@52
   162
        }
sl@52
   163
sl@53
   164
        public void ShowClock()
sl@53
   165
        {
sl@53
   166
            MiniDisplayShowClock(iDevice);
sl@53
   167
        }
sl@53
   168
sl@53
   169
        public void HideClock()
sl@53
   170
        {
sl@53
   171
            MiniDisplayHideClock(iDevice);
sl@53
   172
        }
sl@53
   173
sl@53
   174
        public bool SupportClock()
sl@53
   175
        {
sl@53
   176
            return MiniDisplaySupportClock(iDevice);
sl@53
   177
        }
sl@53
   178
sl@12
   179
        public bool PowerSupplyStatus()
sl@12
   180
        {
sl@12
   181
            bool res = MiniDisplayPowerSupplyStatus(iDevice);
sl@12
   182
            return res;
sl@12
   183
        }
sl@12
   184
sl@12
   185
        public TMiniDisplayRequest AttemptRequestCompletion()
sl@12
   186
        {
sl@12
   187
            return MiniDisplayAttemptRequestCompletion(iDevice);
sl@12
   188
        }
sl@12
   189
sl@12
   190
        public TMiniDisplayRequest CurrentRequest()
sl@12
   191
        {
sl@12
   192
            return MiniDisplayCurrentRequest(iDevice);
sl@12
   193
        }
sl@12
   194
sl@12
   195
        public bool IsRequestPending()
sl@12
   196
        {
sl@12
   197
            return CurrentRequest() != TMiniDisplayRequest.EMiniDisplayRequestNone;
sl@12
   198
        }
sl@12
   199
sl@12
   200
sl@10
   201
        public string Vendor()
sl@10
   202
        {
sl@10
   203
            IntPtr ptr = MiniDisplayVendor(iDevice);
sl@10
   204
            string str = Marshal.PtrToStringUni(ptr);
sl@10
   205
            return str;
sl@10
   206
        }
sl@4
   207
sl@10
   208
        public string Product()
sl@10
   209
        {
sl@10
   210
            IntPtr ptr = MiniDisplayProduct(iDevice);
sl@10
   211
            string str = Marshal.PtrToStringUni(ptr);
sl@10
   212
            return str;
sl@10
   213
        }
sl@10
   214
sl@10
   215
        public string SerialNumber()
sl@10
   216
        {
sl@10
   217
            IntPtr ptr = MiniDisplaySerialNumber(iDevice);
sl@10
   218
            string str = Marshal.PtrToStringUni(ptr);
sl@10
   219
            return str;
sl@10
   220
        }
sl@4
   221
sl@12
   222
        public string DeviceId()
sl@12
   223
        {
sl@12
   224
            IntPtr ptr = MiniDisplayDeviceId(iDevice);
sl@12
   225
            string str = Marshal.PtrToStringAnsi(ptr);
sl@12
   226
            return str;
sl@12
   227
        }
sl@12
   228
sl@12
   229
        public string FirmwareRevision()
sl@12
   230
        {
sl@12
   231
            IntPtr ptr = MiniDisplayFirmwareRevision(iDevice);
sl@12
   232
            string str = Marshal.PtrToStringAnsi(ptr);
sl@12
   233
            return str;
sl@12
   234
        }
sl@12
   235
sl@46
   236
        //[Serializable]
sl@39
   237
        public enum TMiniDisplayType
sl@39
   238
        {
sl@39
   239
            EMiniDisplayAutoDetect, /*Not yet implemented*/
sl@46
   240
            //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
sl@39
   241
            EMiniDisplayFutabaGP1212A01,
sl@46
   242
            //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
sl@39
   243
            EMiniDisplayFutabaGP1212A02
sl@39
   244
        };
sl@39
   245
sl@39
   246
sl@10
   247
        public enum TMiniDisplayRequest
sl@10
   248
        {
sl@10
   249
            EMiniDisplayRequestNone,
sl@10
   250
            EMiniDisplayRequestDeviceId,
sl@10
   251
            EMiniDisplayRequestFirmwareRevision,
sl@10
   252
            EMiniDisplayRequestPowerSupplyStatus
sl@39
   253
        };
sl@10
   254
sl@3
   255
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@39
   256
        public static extern IntPtr MiniDisplayOpen(TMiniDisplayType aType);
sl@3
   257
sl@3
   258
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   259
        public static extern void MiniDisplayClose(IntPtr aDevice);
sl@3
   260
StephaneLenclud@104
   261
		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
StephaneLenclud@104
   262
		public static extern int MiniDisplayTypeCount();
StephaneLenclud@104
   263
StephaneLenclud@104
   264
		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
StephaneLenclud@104
   265
		public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType);
StephaneLenclud@104
   266
sl@3
   267
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   268
        public static extern void MiniDisplayClear(IntPtr aDevice);
sl@3
   269
sl@3
   270
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   271
        public static extern void MiniDisplayFill(IntPtr aDevice);
sl@3
   272
sl@3
   273
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   274
        public static extern void MiniDisplaySwapBuffers(IntPtr aDevice);
sl@3
   275
sl@3
   276
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   277
        public static extern void MiniDisplaySetBrightness(IntPtr aDevice, int aBrightness);
sl@3
   278
sl@3
   279
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   280
        public static extern int MiniDisplayMinBrightness(IntPtr aDevice);
sl@3
   281
sl@3
   282
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   283
        public static extern int MiniDisplayMaxBrightness(IntPtr aDevice);
sl@3
   284
sl@4
   285
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@4
   286
        public static extern int MiniDisplayWidthInPixels(IntPtr aDevice);
sl@4
   287
sl@4
   288
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@4
   289
        public static extern int MiniDisplayHeightInPixels(IntPtr aDevice);
sl@4
   290
sl@4
   291
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@57
   292
        public static extern int MiniDisplaySetPixel(IntPtr aDevice, int aX, int aY, uint aValue);
sl@4
   293
sl@10
   294
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   295
        public static extern IntPtr MiniDisplayVendor(IntPtr aDevice);
sl@10
   296
sl@10
   297
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   298
        public static extern IntPtr MiniDisplayProduct(IntPtr aDevice);
sl@10
   299
sl@10
   300
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   301
        public static extern IntPtr MiniDisplaySerialNumber(IntPtr aDevice);
sl@10
   302
sl@10
   303
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   304
        public static extern IntPtr MiniDisplayDeviceId(IntPtr aDevice);
sl@10
   305
sl@10
   306
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   307
        public static extern IntPtr MiniDisplayFirmwareRevision(IntPtr aDevice);
sl@10
   308
sl@10
   309
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@12
   310
        [return: MarshalAs(UnmanagedType.I1)]
sl@10
   311
        public static extern bool MiniDisplayPowerSupplyStatus(IntPtr aDevice);
sl@10
   312
sl@10
   313
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@44
   314
        public static extern void MiniDisplayRequest(IntPtr aDevice, TMiniDisplayRequest aRequest);
sl@10
   315
sl@10
   316
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   317
        public static extern TMiniDisplayRequest MiniDisplayAttemptRequestCompletion(IntPtr aDevice);
sl@10
   318
sl@10
   319
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   320
        public static extern TMiniDisplayRequest MiniDisplayCurrentRequest(IntPtr aDevice);
sl@10
   321
sl@10
   322
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   323
        public static extern void MiniDisplayCancelRequest(IntPtr aDevice);
sl@10
   324
sl@52
   325
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@52
   326
        public static extern void MiniDisplayPowerOn(IntPtr aDevice);
sl@52
   327
sl@52
   328
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@52
   329
        public static extern void MiniDisplayPowerOff(IntPtr aDevice);
sl@52
   330
sl@52
   331
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@52
   332
        [return: MarshalAs(UnmanagedType.I1)]
sl@52
   333
        public static extern bool MiniDisplaySupportPowerOnOff(IntPtr aDevice);
sl@10
   334
sl@53
   335
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@53
   336
        public static extern void MiniDisplayShowClock(IntPtr aDevice);
sl@53
   337
sl@53
   338
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@53
   339
        public static extern void MiniDisplayHideClock(IntPtr aDevice);
sl@53
   340
sl@53
   341
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@53
   342
        [return: MarshalAs(UnmanagedType.I1)]
sl@53
   343
        public static extern bool MiniDisplaySupportClock(IntPtr aDevice);
sl@53
   344
sl@53
   345
sl@3
   346
    }
sl@3
   347
}