1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Added full Arduino sample

Robert Beekman 2018-01-08 12:18:30 +01:00
parent c625855515
commit 455b00c417

@ -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. 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 ### Step 1
Create a new folder in the DeviceProviders folder, in this case called MyDevice Create a new folder in the DeviceProviders folder, in this case called MyDevice
![](http://i.imgur.com/KsEj0YM.png) ![](http://i.imgur.com/KsEj0YM.png)
@ -45,60 +47,43 @@ namespace Artemis.DeviceProviders.MyDevice
### Step 4 ### 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. 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: 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:
```cs https://github.com/rakosi2/Artemis/blob/master/Artemis/Artemis/DeviceProviders/Arduino/ArduinoStrip.cs
using System;
using System.Drawing;
using System.IO.Ports;
namespace Artemis.DeviceProviders.MyDevice ### Step 5
{ In this case we're talking to an Arduino so lets look at the code running on the Arduino:
public class MyDeviceGeneric : DeviceProvider ```arduino
{ #include "FastLED.h"//https://github.com/FastLED/FastLED
private SerialPort _port; 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) #define DATA_PIN 3 //change to your pin #
{ //#define CLK_PIN 4 //some leds need a clock
Logger = logger; #define LED_TYPE WS2812B //change to your type of led, check link for supported leds https://github.com/FastLED/FastLED/wiki/Chipset-reference
Type = DeviceType.Generic; #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];
void setup() {
Serial.begin(2000000);//use buad rate of choose, native USB device will just use max baud rate
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(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
} }
public override void UpdateDevice(Bitmap bitmap) void loop() {
{ FastLED.show();
if (bitmap == null) for (int i = 0; i < NUM_LEDS; i++) {
return; 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 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! :) If you have something that works well and is useful for others, please consider making a pull request! :)