24 Mayıs 2016 Salı

WS2811 controller (rgb led kontrolü)

Python Code :

import serial,time, pymouse

import thread
import gtk.gdk


mouse = pymouse.PyMouse()
ser = serial.Serial("/dev/ttyACM0",115200,bytesize=8,timeout=.1,parity="N",\
                    stopbits=1,)

class _get_pxc:
    def __init__(self):
        self.color = "000000"
        self.old_color = ""

    def pixel_at(self,x, y):
        rw = gtk.gdk.get_default_root_window()
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
        pixbuf = pixbuf.get_from_drawable(rw, rw.get_colormap(), x, y, 0, 0, 1, 1)
        return tuple(pixbuf.pixel_array[0, 0])
    def send_ardu(self,passarg):
       
        while True :

            s = mouse.position()
          
            self.color = self.pixel_at(s[0],s[1])
            if self.color != self.old_color :
                d = "".join("%0.2x"%f for f in self.color) + "\n"
                ser.write(d)
                time.sleep(0.01)
                while not ser.inWaiting():
                    time.sleep(.01)
                z = ser.read(ser.inWaiting())
                self.old_color = self.color         
                time.sleep(0.01)

get_pxc = _get_pxc()
thread.start_new_thread(get_pxc.send_ardu,(None,))

while True: time.sleep(0.1)


Arduino Code:

#include <FastLED.h>



#define LED_PIN     3
#define NUM_LEDS    3
#define BRIGHTNESS  255
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];


String inData;
String rs = "000000";
long r, g, b ;


void setup() {
  Serial.begin(115200);
 
  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );

}

void loop(){
    while (Serial.available() > 0)
    {
        char recieved = Serial.read();
        inData += recieved;

        // Process message when new line character is recieved
       if (recieved == '\n')
        {
            int boy = inData.length() - 1;
         
            if(boy == 6){
              rs = inData.substring(0,6);
              long number = strtol( &rs[0], NULL, 16);
              long r = number >> 16;
              long g = number >> 8 & 0xFF;
              long b = number & 0xFF;
              for(int id = 0; id < 3; id++){
                leds[id] = CRGB(g,r,b);               
                }
            FastLED.show();
           
           

           
            rs = "";

            }
            delay(10);
            Serial.print(inData+"\n");



            inData = ""; // Clear recieved buffer
        }
    }
}

Hiç yorum yok:

Yorum Gönder