J'ai pu obtenir deux Led RGB Grove chainables, histoire d'apporter un peu de couleur aux montages. Le problème, c'est qu'en cherchant sur le site de GHI Electronics, il y avait des drivers pour toutes sortes de LED, mais pas pour le modèle Grove qui utilise un protocole un peu spécial. Fort heureusement, il existe sur le site https://github.com/timmattison/timmattison-netduino-drivers/tree/master/drivers un driver pour ce composant. J'ai adapté assez simplement le code pour qu'il puisse fonctionner sur la FEZ Panda.
Voici le résultat.
Cette classe utilise un classe RGB (Red Green Blue) pour stocker la couleur.
Et voici un exemple d'utilisation.
Voici le résultat.
 using System;  
 using System.Threading;  
 using Microsoft.SPOT;  
 using Microsoft.SPOT.Hardware;  
 namespace SeeedStudio  
 {  
   public class SeeedChainableRGBLed  
   {  
     OutputPort cin;  
     OutputPort din;  
     public SeeedChainableRGBLed(OutputPort cin, OutputPort din)  
     {  
       this.cin = cin;  
       this.din = din;  
     }  
     public void setColors(RGB[] colors)  
     {  
       bool first = true;  
       bool last = false;  
       // Loop through each color  
       foreach(RGB rgb in colors) {  
         // Set it  
         setColor(rgb, first, last);  
         // Make sure first is no longer true  
         first = false;  
       }  
       // We're done, send the end frame  
       sendEndFrame();  
     }  
     private void setColor(RGB rgb, bool first, bool last)  
     {  
       setColor(rgb.red, rgb.green, rgb.blue, first, last);  
     }  
     private void setColor(byte red, byte green, byte blue, bool first, bool last)  
     {  
       // Is this the first color?  
       if (first)  
       {  
         // Yes, send the start frame  
         sendStartFrame();  
       }  
       else  
       {  
         // No, do nothing  
       }  
       // Send the flag bits  
       sendFlagBits();  
       // Send the colors  
       sendColorData(red, green, blue);  
       // Is this the last color?  
       if (last)  
       {  
         // Yes, send the end frame  
         sendEndFrame();  
       }  
       else  
       {  
         // No, do nothing  
       }  
     }  
     private void sendBit(bool bit)  
     {  
       // Get DIN into the proper state  
       din.Write(bit);  
       // Set the clock high  
       cin.Write(true);  
       // Set the clock low  
       cin.Write(false);  
     }  
     private void sendByte(byte data)  
     {  
       // Send the bits MSB first  
       sendBit((data & 0x80) == 0x80);  
       sendBit((data & 0x40) == 0x40);  
       sendBit((data & 0x20) == 0x20);  
       sendBit((data & 0x10) == 0x10);  
       sendBit((data & 0x08) == 0x08);  
       sendBit((data & 0x04) == 0x04);  
       sendBit((data & 0x02) == 0x02);  
       sendBit((data & 0x01) == 0x01);  
     }  
     private void sendStartFrame()  
     {  
       // The start frame is 32 bits of zeroes  
       sendByte(0);  
       sendByte(0);  
       sendByte(0);  
       sendByte(0);  
     }  
     private void sendEndFrame()  
     {  
       // The end frame is the same as the start frame  
       sendStartFrame();  
     }  
     private void sendFlagBits()  
     {  
       // The flag bits are two 1s  
       sendBit(true);  
       sendBit(true);  
     }  
     private void sendColorData(byte red, byte green, byte blue)  
     {  
       // Send the inverse bits of the B7, B6, G7, G6, R7, R6  
       sendBit((blue & 0x80) != 0x80);  
       sendBit((blue & 0x40) != 0x40);  
       sendBit((green & 0x80) != 0x80);  
       sendBit((green & 0x40) != 0x40);  
       sendBit((red & 0x80) != 0x80);  
       sendBit((red & 0x40) != 0x40);  
       // Send the actual colors  
       sendByte((byte)blue);  
       sendByte((byte)green);  
       sendByte((byte)red);  
     }  
   }  
 }  
Cette classe utilise un classe RGB (Red Green Blue) pour stocker la couleur.
 using System;  
 using Microsoft.SPOT;  
 namespace SeeedStudio  
 {  
   public class RGB  
   {  
     public byte red;  
     public byte green;  
     public byte blue;  
     public RGB(byte red, byte green, byte blue)  
     {  
       this.red = red;  
       this.green = green;  
       this.blue = blue;  
     }  
   }  
 }  
Et voici un exemple d'utilisation.
 using System;  
 using System.Threading;  
 using Microsoft.SPOT;  
 using Microsoft.SPOT.Hardware;  
 using GHIElectronics.NETMF.FEZ;  
 using GHIElectronics.NETMF.Hardware;  
 using GHIElectronics.NETMF.Native;  
 using SeeedStudio;  
 ...  
 static SeeedChainableRGBLed leds;   
 ...   
  OutputPort cin = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di6, false);   
  OutputPort din = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di7, false);   
  // Create our chainable RGB led object using the ports from above   
  leds = new SeeedChainableRGBLed(cin, din);   
  // Change the color when a button is pressed   
  static void button_ButtonPressEvent(FEZ_Pin.Interrupt pin, FEZ_Components.Button.ButtonState state)   
  {   
     if (state == FEZ_Components.Button.ButtonState.Pressed)   
     {   
      Debug.Print("My Button is pressed");   
      RGB first = new RGB(255, 0, 0);   
      RGB[] colors = { first };   
      leds.setColors(colors);   
     }   
     else   
     {   
      Debug.Print("My Button is not pressed");   
      RGB first = new RGB(0, 255, 0);   
      RGB[] colors = { first };   
      leds.setColors(colors);   
     }   
  }  

Commentaires
Enregistrer un commentaire