From 455b00c417c67d4a22a660d47813d9dabcbcd350 Mon Sep 17 00:00:00 2001 From: Robert Beekman Date: Mon, 8 Jan 2018 12:18:30 +0100 Subject: [PATCH] Added full Arduino sample --- Adding-a-non-keyboard-device-yourself.md | 85 ++++++++++-------------- 1 file changed, 35 insertions(+), 50 deletions(-) diff --git a/Adding-a-non-keyboard-device-yourself.md b/Adding-a-non-keyboard-device-yourself.md index b313d0c..78c65cd 100644 --- a/Adding-a-non-keyboard-device-yourself.md +++ b/Adding-a-non-keyboard-device-yourself.md @@ -1,5 +1,7 @@ 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. +In this example we'll implement an Arduino-based RGB led strip as shown by [rakosi2](https://github.com/rakosi2) (thanks!). + ### Step 1 Create a new folder in the DeviceProviders folder, in this case called MyDevice ![](http://i.imgur.com/KsEj0YM.png) @@ -45,60 +47,43 @@ namespace Artemis.DeviceProviders.MyDevice ### 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 every 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; +In the sample provided by rakosi2 to talk to an Arduino LED strip is a bit long to completely include here but it's done like so: +https://github.com/rakosi2/Artemis/blob/master/Artemis/Artemis/DeviceProviders/Arduino/ArduinoStrip.cs -namespace Artemis.DeviceProviders.MyDevice -{ - public class MyDeviceGeneric : DeviceProvider - { - private SerialPort _port; +### Step 5 +In this case we're talking to an Arduino so lets look at the code running on the Arduino: +```arduino +#include "FastLED.h"//https://github.com/FastLED/FastLED +FASTLED_USING_NAMESPACE +#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000) +#warning "Requires FastLED 3.1 or later; check github for latest code." +#endif - public MyDeviceGeneric(ILogger logger) - { - Logger = logger; - Type = DeviceType.Generic; - } +#define DATA_PIN 3 //change to your pin # +//#define CLK_PIN 4 //some leds need a clock +#define LED_TYPE WS2812B //change to your type of led, check link for supported leds https://github.com/FastLED/FastLED/wiki/Chipset-reference +#define COLOR_ORDER GRB //leds RGB order, WS2812B is GRB +#define NUM_LEDS 288 //change to you number of leds +#define SERIAL_BUFFER_SIZE 2048 //this is really huge but it works so... +CRGB leds[NUM_LEDS]; +#define BRIGHTNESS 20 //0-255 at 5V each WS2812B chip draws 50mA so my 288 strip will draw 14.4A thats why I'm limiting to 8% or 5.6W +byte buff[3]; - public override void UpdateDevice(Bitmap bitmap) - { - if (bitmap == null) - return; +void setup() { + Serial.begin(2000000);//use buad rate of choose, native USB device will just use max baud rate + FastLED.addLeds(leds, NUM_LEDS); + FastLED.setBrightness(BRIGHTNESS); + pinMode(LED_BUILTIN_TX, INPUT);//disable TX LED for Arduino pro micro + pinMode(LED_BUILTIN_RX, INPUT);//disable RX LED for Arduino pro micro + while (!Serial); // wait for serial port to connect. Needed for native USB port only +} - // 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"); - } - } +void loop() { + FastLED.show(); + for (int i = 0; i < NUM_LEDS; i++) { + Serial.readBytes(buff, 3); + leds[(i)] = CRGB(buff[1], buff[0], buff[2]);//Artemis sends colors as RGB and WS2812B is GRB so I need to change order + } } ``` - If you have something that works well and is useful for others, please consider making a pull request! :) \ No newline at end of file