-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ceca6e
commit f9c74d1
Showing
9 changed files
with
355 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Copyright (c) 2019 The nanoFramework project contributors | ||
// See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using nanoFramework.Runtime.Events; | ||
|
||
namespace Windows.Storage | ||
{ | ||
/// <summary> | ||
/// Contains argument values for Removable Devices events. | ||
/// </summary> | ||
public class RemovableDeviceEventArgs : EventArgs | ||
{ | ||
private readonly string _path; | ||
private readonly RemovableDeviceEvent _event; | ||
|
||
internal RemovableDeviceEventArgs(string path, RemovableDeviceEvent deviceEvent) | ||
{ | ||
_path = path; | ||
_event = deviceEvent; | ||
} | ||
|
||
/// <summary> | ||
/// The path of the Removable Device. | ||
/// </summary> | ||
public string Path | ||
{ | ||
get | ||
{ | ||
return _path; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The <see cref="RemovableDeviceEvent"/> occurred. | ||
/// </summary> | ||
public RemovableDeviceEvent Event | ||
{ | ||
get | ||
{ | ||
return _event; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Specifies the type of event occurred with the Removable Device specified. | ||
/// </summary> | ||
/// <remarks> | ||
/// This enum is specific to nanoFramework. There is no equivalent in the UWP API. | ||
/// </remarks> | ||
public enum RemovableDeviceEvent | ||
{ | ||
/// <summary> | ||
/// A Removable Device has been inserted. | ||
/// </summary> | ||
Inserted, | ||
|
||
/// <summary> | ||
/// A Removable Device has been removed. | ||
/// </summary> | ||
Removed, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// | ||
// Copyright (c) 2019 The nanoFramework project contributors | ||
// See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using nanoFramework.Runtime.Events; | ||
using System; | ||
using static Windows.Storage.RemovableDeviceEventArgs; | ||
|
||
namespace Windows.Storage | ||
{ | ||
/// <summary> | ||
/// Provides an event handler that is called when a Removable Device event occurs. | ||
/// </summary> | ||
/// <param name="sender">Specifies the object that sent the Removable Device event. </param> | ||
/// <param name="e">Contains the Removable Device event arguments. </param> | ||
public delegate void RemovableDeviceEventHandler(Object sender, RemovableDeviceEventArgs e); | ||
|
||
/// <summary> | ||
/// Event manager for Storage events. | ||
/// </summary> | ||
public static class StorageEventManager | ||
{ | ||
[Flags] | ||
internal enum StorageEventType : byte | ||
{ | ||
Invalid = 0, | ||
RemovableDeviceInsertion = 1, | ||
RemovableDeviceRemoval = 2, | ||
} | ||
|
||
internal class StorageEvent : BaseEvent | ||
{ | ||
public StorageEventType EventType; | ||
public byte DriveIndex; | ||
public DateTime Time; | ||
} | ||
|
||
internal class StorageEventListener : IEventListener, IEventProcessor | ||
{ | ||
public void InitializeForEventSource() | ||
{ | ||
} | ||
|
||
public BaseEvent ProcessEvent(uint data1, uint data2, DateTime time) | ||
{ | ||
StorageEvent storageEvent = new StorageEvent | ||
{ | ||
EventType = (StorageEventType)((data1 >> 16) & 0xFF), | ||
DriveIndex = (byte)(data1 & 0xFF), | ||
Time = time | ||
}; | ||
|
||
return storageEvent; | ||
} | ||
|
||
public bool OnEvent(BaseEvent ev) | ||
{ | ||
if (ev is StorageEvent) | ||
{ | ||
OnStorageEventCallback((StorageEvent)ev); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Event that occurs when a Removable Device is inserted. | ||
/// </summary> | ||
/// <remarks> | ||
/// The <see cref="StorageEventManager"/> class raises <see cref="RemovableDeviceEventArgs"/> events when Removable Devices (typically SD Cards and USB mass storage device) are inserted and removed. | ||
/// | ||
/// To have a <see cref="StorageEventManager"/> object call an event-handling method when a <see cref="RemovableDeviceInserted"/> event occurs, | ||
/// you must associate the method with a <see cref="RemovableDeviceEventHandler"/> delegate, and add this delegate to this event. | ||
/// </remarks> | ||
public static event RemovableDeviceEventHandler RemovableDeviceInserted; | ||
|
||
/// <summary> | ||
/// Event that occurs when a Removable Device is removed. | ||
/// </summary> | ||
/// <remarks> | ||
/// The <see cref="StorageEventManager"/> class raises <see cref="RemovableDeviceEventArgs"/> events when Removable Devices (typically SD Cards and USB mass storage device) are inserted and removed. | ||
/// | ||
/// To have a <see cref="StorageEventManager"/> object call an event-handling method when a <see cref="RemovableDeviceRemoved"/> event occurs, | ||
/// you must associate the method with a <see cref="RemovableDeviceEventHandler"/> delegate, and add this delegate to this event. | ||
/// </remarks> | ||
public static event RemovableDeviceEventHandler RemovableDeviceRemoved; | ||
|
||
static StorageEventManager() | ||
{ | ||
StorageEventListener storageEventListener = new StorageEventListener(); | ||
|
||
EventSink.AddEventProcessor(EventCategory.Storage, storageEventListener); | ||
EventSink.AddEventListener(EventCategory.Storage, storageEventListener); | ||
} | ||
|
||
internal static void OnStorageEventCallback(StorageEvent storageEvent) | ||
{ | ||
switch (storageEvent.EventType) | ||
{ | ||
case StorageEventType.RemovableDeviceInsertion: | ||
{ | ||
if (RemovableDeviceInserted != null) | ||
{ | ||
RemovableDeviceEventArgs args = new RemovableDeviceEventArgs(DriveIndexToPath(storageEvent.DriveIndex), RemovableDeviceEvent.Inserted); | ||
|
||
RemovableDeviceInserted(null, args); | ||
} | ||
break; | ||
} | ||
case StorageEventType.RemovableDeviceRemoval: | ||
{ | ||
if (RemovableDeviceRemoved != null) | ||
{ | ||
RemovableDeviceEventArgs args = new RemovableDeviceEventArgs(DriveIndexToPath(storageEvent.DriveIndex), RemovableDeviceEvent.Removed); | ||
|
||
RemovableDeviceRemoved(null, args); | ||
} | ||
|
||
break; | ||
} | ||
default: | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
internal static string DriveIndexToPath(byte driveIndex) | ||
{ | ||
///////////////////////////////////////////////////////////////////////////////////// | ||
// Drive indexes have a fixed mapping with a driver letter | ||
// Keep the various INDEX0_DRIVE_LETTER in sync with nanoHAL_Windows_Storage.h in native code | ||
///////////////////////////////////////////////////////////////////////////////////// | ||
|
||
switch (driveIndex) | ||
{ | ||
// INDEX0_DRIVE_LETTER | ||
case 0: | ||
return "D:"; | ||
|
||
// INDEX1_DRIVE_LETTER | ||
case 1: | ||
return "E:"; | ||
|
||
// INDEX2_DRIVE_LETTER | ||
case 2: | ||
return "F:"; | ||
|
||
default: | ||
throw new IndexOutOfRangeException(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="nanoFramework.CoreLibrary" version="1.1.1" targetFramework="netnanoframework10" /> | ||
<package id="nanoFramework.Runtime.Events" version="1.0.5-preview-010" targetFramework="netnanoframework10" /> | ||
<package id="nanoFramework.Windows.Storage.Streams" version="1.0.5-preview-009" targetFramework="netnanoframework10" /> | ||
<package id="Nerdbank.GitVersioning" version="3.0.6-beta" developmentDependency="true" targetFramework="netnanoframework10" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Copyright (c) 2019 The nanoFramework project contributors | ||
// See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
using System.Threading; | ||
using Windows.Storage; | ||
|
||
namespace RemovableDeviceEvent | ||
{ | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
// add event handlers for Removable Device insertion and removal | ||
StorageEventManager.RemovableDeviceInserted += StorageEventManager_RemovableDeviceInserted; | ||
StorageEventManager.RemovableDeviceRemoved += StorageEventManager_RemovableDeviceRemoved; | ||
|
||
Thread.Sleep(Timeout.Infinite); | ||
} | ||
|
||
private static void StorageEventManager_RemovableDeviceRemoved(object sender, RemovableDeviceEventArgs e) | ||
{ | ||
Console.WriteLine($"Removable Device @ \"{e.Path}\" removed."); | ||
} | ||
|
||
private static void StorageEventManager_RemovableDeviceInserted(object sender, RemovableDeviceEventArgs e) | ||
{ | ||
Console.WriteLine($"Removable Device @ \"{e.Path}\" inserted."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("CSharp.BlankApplication")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("CSharp.BlankApplication")] | ||
[assembly: AssemblyCopyright("Copyright © ")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
Oops, something went wrong.