Advertisement
Guest User

AdaFruitPWMDriver

a guest
Dec 31st, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.24 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Hardware;
  5. using Math = System.Math;
  6.  
  7. namespace ReefinDotNet.i2cDevices
  8. {
  9.     class Adafruit_PWMServoDriver
  10.     {
  11.         private MultiI2C _Device;
  12.  
  13.         private const byte PCS9685_SUBADR1 = 0x2;
  14.         private const byte PCA9685_SUBADR2 = 0x3;
  15.         private const byte PCA9685_SUBADR3 = 0x4;
  16.  
  17.         private const byte PCA9685_MODE1 = 0x0;
  18.         private const byte PCA9685_PRESCALE = 0xFE;
  19.  
  20.         private const byte LED0_ON_L = 0x6;
  21.         private const byte LED0_ON_H = 0x7;
  22.         private const byte LED0_OFF_L = 0x8;
  23.         private const byte LED0_OFF_H = 0x9;
  24.  
  25.         private const byte ALLLED_ON_L = 0xFA;
  26.         private const byte ALLLED_ON_H = 0xFB;
  27.         private const byte ALLLED_OFF_L = 0xFC;
  28.         private const byte ALLLED_OFF_H = 0xFD;
  29.  
  30.         private int[] on = new int[15];
  31.         private int[] off = new int[15];
  32.  
  33.         private byte _i2caddr;
  34.  
  35.         public bool Override;
  36.         public int Step;
  37.  
  38.         public Adafruit_PWMServoDriver(byte addr = 0x40, int ClockRateKhz = 200, int step=0)
  39.         {
  40.             _i2caddr = addr;
  41.             this._Device = new MultiI2C(new I2CDevice.Configuration(0x40, 200));
  42.             this.Step = step;
  43.             Override = false;
  44.         }
  45.  
  46.         public void Begin()
  47.         {
  48.             Reset();
  49.         }
  50.         public void Reset()
  51.         {
  52.             write8(PCA9685_MODE1, 0x0);
  53.         }
  54.         public void setPWMFreq(float freq)
  55.         {
  56.             float prescaleval = 25000000;
  57.             prescaleval /= 4096;
  58.             prescaleval /= freq;
  59.             prescaleval -= 1;
  60.             // Debug.Print("Estimated pre-scale: " + prescaleval);
  61.             byte prescale = (byte)System.Math.Floor(prescaleval + 0.5);
  62.             // Debug.Print("Final Pre-scale: " + prescale);
  63.             byte oldmode = read8(PCA9685_MODE1);
  64.             byte newmode = (byte)((oldmode & 0x7F) | 0x10); // sleep
  65.             write8(PCA9685_MODE1, newmode); // go to sleep
  66.             write8(PCA9685_PRESCALE, prescale); // set the prescaler
  67.             write8(PCA9685_MODE1, oldmode);
  68.             Thread.Sleep(5);
  69.             write8(PCA9685_MODE1, (byte)(oldmode | 0x80));
  70.  
  71.         }
  72.         public void setPWM(byte num, UInt16 on, UInt16 off)
  73.         {
  74.             write8((byte)(LED0_ON_L + 4 * num), (byte)on);
  75.             write8((byte)(LED0_ON_H + 4 * num), (byte)(on >> 8));
  76.             write8((byte)(LED0_OFF_L + 4 * num), (byte)off);
  77.             write8((byte)(LED0_OFF_H + 4 * num), (byte)(off >> 8));
  78.             this.on[num] = on;
  79.             this.off[num] = off;
  80.         }
  81.         public int getPower(byte num)
  82.         {
  83.             return this.off[num];
  84.         }
  85.         public void fadeTo(int ch1, int ch2, int ch3, int ch4, int ch5, int seconds, int offset=0)
  86.         {
  87.             seconds = seconds * 10;
  88.             int step1 = (int)Math.Ceiling((double)(Math.Abs(this.off[0 + offset] - ch1)) / (double)(seconds));
  89.             int step2 = (int)Math.Ceiling((double)(Math.Abs(this.off[1 + offset] - ch2)) / (double)(seconds));
  90.             int step3 = (int)Math.Ceiling((double)(Math.Abs(this.off[2 + offset] - ch3)) / (double)(seconds));
  91.             int step4 = (int)Math.Ceiling((double)(Math.Abs(this.off[3 + offset] - ch4)) / (double)(seconds));
  92.             int step5 = (int)Math.Ceiling((double)(Math.Abs(this.off[4 + offset] - ch5)) / (double)(seconds));
  93.             while (seconds > 1)
  94.             {
  95.                     if (this.off[0 + offset] > ch1)
  96.                     {
  97.                         this.setPWM((byte) (0 + offset), 0, (ushort)(this.off[0+offset] - step1));
  98.                     }
  99.                     else
  100.                     {
  101.                         setPWM((byte)(0 + offset), 0, (ushort)(this.off[0 + offset] + step1));
  102.                     }
  103.                     //ch2
  104.                     if (this.off[1 + offset] > ch2)
  105.                     {
  106.                         this.setPWM((byte)(1 + offset), 0, (ushort)(this.off[1 + offset] - step2));
  107.                     }
  108.                     else
  109.                     {
  110.                         this.setPWM((byte)(1 + offset), 0, (ushort)(this.off[1 + offset] + step2));
  111.                     }
  112.                     //ch3    
  113.                     if (this.off[2 + offset] > ch3)
  114.                     {
  115.                         this.setPWM((byte)(2 + offset), 0, (ushort)(this.off[2 + offset] - step3));
  116.                     }
  117.                     else
  118.                     {
  119.                         this.setPWM((byte)(2 + offset), 0, (ushort)(this.off[2 + offset] + step3));
  120.                     }
  121.                     //ch4
  122.                     if (this.off[3 + offset] > ch4)
  123.                     {
  124.                         this.setPWM((byte)(3 + offset), 0, (ushort)(this.off[3 + offset] - step4));
  125.                     }
  126.                     else
  127.                     {
  128.                         this.setPWM((byte)(3 + offset), 0, (ushort)(this.off[3 + offset] + step4));
  129.                     }
  130.                     //ch5
  131.                     if (this.off[4 + offset] > ch5)
  132.                     {
  133.                         this.setPWM((byte)(4 + offset), 0, (ushort)(this.off[4 + offset] - step5));
  134.                     }
  135.                     else
  136.                     {
  137.                         this.setPWM((byte)(4 + offset), 0, (ushort)(this.off[4 + offset] + step5));
  138.                     }
  139.                     Thread.Sleep(100);
  140.                     //Debug.Print(this.off[0 + offset] + " - " + this.off[1 + offset] + " - " + this.off[2 + offset]);
  141.                 seconds--;
  142.             }
  143.             setPWM((byte)(0 + offset), 0, (ushort)(ch1));
  144.             setPWM((byte)(1 + offset), 0, (ushort)(ch2));
  145.             setPWM((byte)(2 + offset), 0, (ushort)(ch3));
  146.             setPWM((byte)(3 + offset), 0, (ushort)(ch4));
  147.             setPWM((byte)(4 + offset), 0, (ushort)(ch5));
  148.         }
  149.    
  150.         private byte read8(byte addr)
  151.         {
  152.             byte[] ReadBuffer = new byte[2];
  153.             this._Device.ReadRegister(addr, ReadBuffer);
  154.             return ReadBuffer[0];
  155.         }
  156.         private void write8(byte addr, byte d)
  157.         {
  158.             this._Device.WriteRegister(addr, d);
  159.         }
  160.  
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement