From 9147363a5440c94fe9bf379a5ef7529c9440a386 Mon Sep 17 00:00:00 2001 From: Robert Beekman Date: Wed, 8 Feb 2017 00:09:28 +0100 Subject: [PATCH] Created Adding a non-keyboard device yourself (markdown) --- Adding-a-non-keyboard-device-yourself.md | 92 ++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Adding-a-non-keyboard-device-yourself.md diff --git a/Adding-a-non-keyboard-device-yourself.md b/Adding-a-non-keyboard-device-yourself.md new file mode 100644 index 0000000..ae0a002 --- /dev/null +++ b/Adding-a-non-keyboard-device-yourself.md @@ -0,0 +1,92 @@ +Adding a new RGB device to Artemis is relatively simple. This guide assumes you already have some C# knowledge and got Artemis to compile in Visual Studio. + +### Step 1 +Create a new folder in the DeviceProviders folder, in this case called MyDevice +![](http://i.imgur.com/KsEj0YM.png) + +### Step 2 +Inside the folder create a new class called MyDeviceGeneric, if you're adding a mouse name it MyDeviceMouse etc. + +### Step 3 +Make this new class public and implement DeviceProvider so you end up with something like this: +```cs +using System; +using System.Drawing; + +namespace Artemis.DeviceProviders.MyDevice +{ + public class MyDeviceGeneric : DeviceProvider + { + public override void UpdateDevice(Bitmap bitmap) + { + throw new NotImplementedException(); + } + + public override bool TryEnable() + { + throw new NotImplementedException(); + } + + public override void Disable() + { + // Disable doesn't need to be implemented since this isn't a keyboard (this may change) + throw new NotSupportedException("Can only disable a keyboard"); + } + } +} +``` + +### Step 4 +Implement the methods. You can do this however you like, just keep in mind that UpdateDevice is called 25 times a second, or ever 40 ms, so make sure it's quick. + +A super simple untested example using a serial port to talk to an Arduino: +```cs +using System; +using System.Drawing; +using System.IO.Ports; + +namespace Artemis.DeviceProviders.MyDevice +{ + public class MyDeviceGeneric : DeviceProvider + { + private SerialPort _port; + + public override void UpdateDevice(Bitmap bitmap) + { + if (bitmap == null) + return; + + // If the device that's being implemented only has one LED, simply take the color + // of the pixel in the middle of the bitmap + var color = bitmap.GetPixel(bitmap.Width / 2, bitmap.Height / 2); + + // Write the color to the device + _port.WriteLine(color.ToString()); + } + + public override bool TryEnable() + { + // Do a check here to see if your device is available. Make sure you set CanUse so + // Artemis knows it's available. + CanUse = true; + + // Lets open a COM1 connection, this could be an Arduino or something similar + if (CanUse) + { + _port = new SerialPort("COM1", 9600); + _port.Open(); + } + + return CanUse; + } + + public override void Disable() + { + // Disable doesn't need to be implemented since this isn't a keyboard (this may change) + throw new NotSupportedException("Can only disable a keyboard"); + } + } +} +``` + +If you have something that works well and is useful for others, please consider making a pull request! :) \ No newline at end of file