Server/Display.cs
author StephaneLenclud
Thu, 05 Feb 2015 17:04:59 +0100
changeset 106 32270ff62819
parent 104 189aac7dd3d6
child 108 7dd1d881c142
permissions -rw-r--r--
Server: Adding scrolling speed setting.
Fixing issue with alignment of newly created text field not being set properly.
Client: Now starting-up first client automatically in debug mode.
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
sl@12
   205
sl@10
   206
        public string Vendor()
sl@10
   207
        {
sl@10
   208
            IntPtr ptr = MiniDisplayVendor(iDevice);
sl@10
   209
            string str = Marshal.PtrToStringUni(ptr);
sl@10
   210
            return str;
sl@10
   211
        }
sl@4
   212
sl@10
   213
        public string Product()
sl@10
   214
        {
sl@10
   215
            IntPtr ptr = MiniDisplayProduct(iDevice);
sl@10
   216
            string str = Marshal.PtrToStringUni(ptr);
sl@10
   217
            return str;
sl@10
   218
        }
sl@10
   219
sl@10
   220
        public string SerialNumber()
sl@10
   221
        {
sl@10
   222
            IntPtr ptr = MiniDisplaySerialNumber(iDevice);
sl@10
   223
            string str = Marshal.PtrToStringUni(ptr);
sl@10
   224
            return str;
sl@10
   225
        }
sl@4
   226
sl@12
   227
        public string DeviceId()
sl@12
   228
        {
sl@12
   229
            IntPtr ptr = MiniDisplayDeviceId(iDevice);
sl@12
   230
            string str = Marshal.PtrToStringAnsi(ptr);
sl@12
   231
            return str;
sl@12
   232
        }
sl@12
   233
sl@12
   234
        public string FirmwareRevision()
sl@12
   235
        {
sl@12
   236
            IntPtr ptr = MiniDisplayFirmwareRevision(iDevice);
sl@12
   237
            string str = Marshal.PtrToStringAnsi(ptr);
sl@12
   238
            return str;
sl@12
   239
        }
sl@12
   240
sl@46
   241
        //[Serializable]
sl@39
   242
        public enum TMiniDisplayType
sl@39
   243
        {
sl@39
   244
            EMiniDisplayAutoDetect, /*Not yet implemented*/
sl@46
   245
            //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
sl@39
   246
            EMiniDisplayFutabaGP1212A01,
sl@46
   247
            //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
sl@39
   248
            EMiniDisplayFutabaGP1212A02
sl@39
   249
        };
sl@39
   250
sl@39
   251
sl@10
   252
        public enum TMiniDisplayRequest
sl@10
   253
        {
sl@10
   254
            EMiniDisplayRequestNone,
sl@10
   255
            EMiniDisplayRequestDeviceId,
sl@10
   256
            EMiniDisplayRequestFirmwareRevision,
sl@10
   257
            EMiniDisplayRequestPowerSupplyStatus
sl@39
   258
        };
sl@10
   259
sl@3
   260
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@39
   261
        public static extern IntPtr MiniDisplayOpen(TMiniDisplayType aType);
sl@3
   262
sl@3
   263
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   264
        public static extern void MiniDisplayClose(IntPtr aDevice);
sl@3
   265
StephaneLenclud@104
   266
		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
StephaneLenclud@104
   267
		public static extern int MiniDisplayTypeCount();
StephaneLenclud@104
   268
StephaneLenclud@104
   269
		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
StephaneLenclud@104
   270
		public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType);
StephaneLenclud@104
   271
sl@3
   272
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   273
        public static extern void MiniDisplayClear(IntPtr aDevice);
sl@3
   274
sl@3
   275
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   276
        public static extern void MiniDisplayFill(IntPtr aDevice);
sl@3
   277
sl@3
   278
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   279
        public static extern void MiniDisplaySwapBuffers(IntPtr aDevice);
sl@3
   280
sl@3
   281
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   282
        public static extern void MiniDisplaySetBrightness(IntPtr aDevice, int aBrightness);
sl@3
   283
sl@3
   284
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   285
        public static extern int MiniDisplayMinBrightness(IntPtr aDevice);
sl@3
   286
sl@3
   287
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@3
   288
        public static extern int MiniDisplayMaxBrightness(IntPtr aDevice);
sl@3
   289
sl@4
   290
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@4
   291
        public static extern int MiniDisplayWidthInPixels(IntPtr aDevice);
sl@4
   292
sl@4
   293
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@4
   294
        public static extern int MiniDisplayHeightInPixels(IntPtr aDevice);
sl@4
   295
sl@4
   296
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@57
   297
        public static extern int MiniDisplaySetPixel(IntPtr aDevice, int aX, int aY, uint aValue);
sl@4
   298
sl@10
   299
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   300
        public static extern IntPtr MiniDisplayVendor(IntPtr aDevice);
sl@10
   301
sl@10
   302
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   303
        public static extern IntPtr MiniDisplayProduct(IntPtr aDevice);
sl@10
   304
sl@10
   305
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   306
        public static extern IntPtr MiniDisplaySerialNumber(IntPtr aDevice);
sl@10
   307
sl@10
   308
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   309
        public static extern IntPtr MiniDisplayDeviceId(IntPtr aDevice);
sl@10
   310
sl@10
   311
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   312
        public static extern IntPtr MiniDisplayFirmwareRevision(IntPtr aDevice);
sl@10
   313
sl@10
   314
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@12
   315
        [return: MarshalAs(UnmanagedType.I1)]
sl@10
   316
        public static extern bool MiniDisplayPowerSupplyStatus(IntPtr aDevice);
sl@10
   317
sl@10
   318
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@44
   319
        public static extern void MiniDisplayRequest(IntPtr aDevice, TMiniDisplayRequest aRequest);
sl@10
   320
sl@10
   321
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   322
        public static extern TMiniDisplayRequest MiniDisplayAttemptRequestCompletion(IntPtr aDevice);
sl@10
   323
sl@10
   324
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   325
        public static extern TMiniDisplayRequest MiniDisplayCurrentRequest(IntPtr aDevice);
sl@10
   326
sl@10
   327
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@10
   328
        public static extern void MiniDisplayCancelRequest(IntPtr aDevice);
sl@10
   329
sl@52
   330
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@52
   331
        public static extern void MiniDisplayPowerOn(IntPtr aDevice);
sl@52
   332
sl@52
   333
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@52
   334
        public static extern void MiniDisplayPowerOff(IntPtr aDevice);
sl@52
   335
sl@52
   336
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@52
   337
        [return: MarshalAs(UnmanagedType.I1)]
sl@52
   338
        public static extern bool MiniDisplaySupportPowerOnOff(IntPtr aDevice);
sl@10
   339
sl@53
   340
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@53
   341
        public static extern void MiniDisplayShowClock(IntPtr aDevice);
sl@53
   342
sl@53
   343
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@53
   344
        public static extern void MiniDisplayHideClock(IntPtr aDevice);
sl@53
   345
sl@53
   346
        [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
sl@53
   347
        [return: MarshalAs(UnmanagedType.I1)]
sl@53
   348
        public static extern bool MiniDisplaySupportClock(IntPtr aDevice);
sl@53
   349
sl@53
   350
sl@3
   351
    }
sl@3
   352
}